-
Notifications
You must be signed in to change notification settings - Fork 66
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
Conversation
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")' |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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:
👀👀 👀 👀
👀👀 👀 👀
👀👀 👀 👀
👀👀 👀 👀
👀👀 👀 👀
👀👀 👀 👀
👀👀 👀 👀
👀👀 👀 👀
👀👀 👀 👀
There was a problem hiding this comment.
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:
- PR: docs: remove line smoya/automerge-coauthoring#1
- The PR has two commits, one by a fictitious user
jciment
, another is made by me;smoya
.
The merged commit: smoya/automerge-coauthoring@7071981 - The commit contains
Co-authored-by: Sergio Moya <[email protected]>
. - GH UI shows the co-author as expected:
- The PR has two commits, one by a fictitious user
There was a problem hiding this comment.
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" |
There was a problem hiding this comment.
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 '[.[] |
There was a problem hiding this comment.
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}] |
There was a problem hiding this comment.
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}}")) |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 + ">") |
There was a problem hiding this comment.
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")' |
There was a problem hiding this comment.
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/[email protected] | ||
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 }}" |
There was a problem hiding this comment.
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.
@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? |
There was a problem hiding this 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 🙇🏼
/rtm |
Description
Something I realized we might need to think about is the fact merging release branches (i.e.
next-major
ornext-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:
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:
jciment
, another is made by me;smoya
.The merged commit: smoya/automerge-coauthoring@7071981
Co-authored-by: Sergio Moya <[email protected]>
.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