Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: support co-authoring for auto-merged PRs #176

Merged
merged 3 commits into from
Sep 15, 2022

Conversation

smoya
Copy link
Member

@smoya smoya commented Sep 13, 2022

Description

Something I realized we might need to think about is the fact merging release branches (i.e. next-major or next-spec) with squash merging strategy ends up obviously with only one commit being merged, being the author of it the person who created the PR.

This leads to:

  • Losing history of changes introduced. It might not be a real issue (if it hasn’t yet) since we write version notes. However, it’s hard to see where exactly a feature or bug was introduced.
  • Losing authority of commits. All changes to such version will appear to be introduced by the person who created the PR.

For the record, the merge is performed automatically by @asyncapi-bot thanks to the automerge-for-humans-merging.yml Github workflow.

This PR fixes the issue with authority, by adding co-authors to the final commit (the one that ends up being merged). See https://docs.github.com/en/pull-requests/committing-changes-to-your-project/creating-and-editing-commits/creating-a-commit-with-multiple-authors#creating-co-authored-commits-on-the-command-line.

You can see the workflow running on this test repository I created:

Examples:

The same will happen with the 2.0.0 version of the parser-js asyncapi/parser-js#598. And same with version 3 of the spec (this time @jonaslagoni will be the author of the whole v3 spec!): asyncapi/spec#759

Related issue(s)
Slack 🧵 https://asyncapi.slack.com/archives/CQVJXFNQL/p1662972475762699

cc @derberg @fmvilas @jonaslagoni @magicmatatjahu

id: authors
with:
cmd: |
curl -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.GH_TOKEN }}" "${{github.event.pull_request._links.commits.href}}?per_page=100" | jq -r '[.[] | {name: .commit.author.name, email: .commit.author.email, login: .author.login}] | map(select(.login != "${{github.event.pull_request.user.login}}")) | unique | map("Co-authored-by: " + .name + " <" + .email + ">") | join("\n")'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀
👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀
👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀
👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀 👀
I'm not sure I can survive it.....need to pick up my brain from the neighborhood and see again...

Copy link
Member Author

@smoya smoya Sep 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Life is hard sometimes :( But I can do multiline if you prefer to read like:

👀👀 👀 👀
👀👀 👀 👀
👀👀 👀 👀
👀👀 👀 👀
👀👀 👀 👀
👀👀 👀 👀
👀👀 👀 👀
👀👀 👀 👀
👀👀 👀 👀

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, I updated the PR description adding an execution of this change in a new test repository I created. Pasting the addition here for reference:

You can see the workflow running on this test repository I created:

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@derberg I've updated the cmd to become multiline. I hope this helps reviewers to follow what it does. I'm gonna add extra comments on this PR to explain almost line by what it does as well.

id: authors
with:
cmd: |
curl -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.GH_TOKEN }}" "${{github.event.pull_request._links.commits.href}}?per_page=100"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is querying the list of commits of the current PR via GH API. Why? Because the current event payload does not carry info about the commits.

with:
cmd: |
curl -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.GH_TOKEN }}" "${{github.event.pull_request._links.commits.href}}?per_page=100"
| jq -r '[.[]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iterating over the payload GH gives us, creating an array with the filtered results (below). An example of payload can be found in https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#webhook-payload-example-34

cmd: |
curl -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.GH_TOKEN }}" "${{github.event.pull_request._links.commits.href}}?per_page=100"
| jq -r '[.[]
| {name: .commit.author.name, email: .commit.author.email, login: .author.login}]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line grabs the data we need for adding the Co-authored-by: ... lines later and puts it into objects to be used later on.

curl -H "Accept: application/vnd.github+json" -H "Authorization: Bearer ${{ secrets.GH_TOKEN }}" "${{github.event.pull_request._links.commits.href}}?per_page=100"
| jq -r '[.[]
| {name: .commit.author.name, email: .commit.author.email, login: .author.login}]
| map(select(.login != "${{github.event.pull_request.user.login}}"))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line filters results by excluding the current PR sender. We don't need to add it as co-author since is the PR creator and it will become by default the main author.

| jq -r '[.[]
| {name: .commit.author.name, email: .commit.author.email, login: .author.login}]
| map(select(.login != "${{github.event.pull_request.user.login}}"))
| unique
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We remove repeated authors

| {name: .commit.author.name, email: .commit.author.email, login: .author.login}]
| map(select(.login != "${{github.event.pull_request.user.login}}"))
| unique
| map("Co-authored-by: " + .name + " <" + .email + ">")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We finally write the lines we want to later on print to stdout

| map(select(.login != "${{github.event.pull_request.user.login}}"))
| unique
| map("Co-authored-by: " + .name + " <" + .email + ">")
| join("\n")'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Transforming the array into a plain text so we can directly copy this stdout to the next Workflow step (wich is basically the automerge)

- name: Automerge PR
uses: pascalgn/automerge-action@v0.14.3
env:
GITHUB_TOKEN: "${{ secrets.GH_TOKEN }}"
MERGE_LABELS: "!do-not-merge,ready-to-merge"
MERGE_METHOD: "squash"
MERGE_COMMIT_MESSAGE: "{pullRequest.title} (#{pullRequest.number})"
# Important to keep 2 empty lines as https://docs.github.com/en/pull-requests/committing-changes-to-your-project/creating-and-editing-commits/creating-a-commit-with-multiple-authors#creating-co-authored-commits-on-the-command-line mentions
MERGE_COMMIT_MESSAGE: "{pullRequest.title} (#{pullRequest.number})\n\n\n${{ steps.authors.outputs.value }}"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use the output of the previous step and put it into the description.

@derberg
Copy link
Member

derberg commented Sep 15, 2022

@smoya thanks so much for the comments. Can you turn them into a comment inside the yaml file? I guess they cannot be inline, so just as a big comment for the step?

@smoya
Copy link
Member Author

smoya commented Sep 15, 2022

@smoya thanks so much for the comments. Can you turn them into a comment inside the yaml file? I guess they cannot be inline, so just as a big comment for the step?

Good point! Done. Would you mind checking again?

@smoya smoya requested a review from derberg September 15, 2022 09:47
Copy link
Member

@derberg derberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smoya LGTM, thanks a 💯 for that one 🙇🏼

@derberg
Copy link
Member

derberg commented Sep 15, 2022

/rtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants