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: implemment pagination #266

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 74 additions & 21 deletions .github/workflows/automerge-for-humans-merging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Don't make changes to this file in this repo as they will be overwritten with changes made to the same file in above mentioned repo

# Purpose of this workflow is to allow people to merge PR without a need of maintainer doing it. If all checks are in place (including maintainers approval) - JUST MERGE IT!

name: Automerge For Humans

on:
Expand All @@ -18,30 +19,82 @@ on:

jobs:
automerge-for-humans:
if: github.event.pull_request.draft == false && (github.event.pull_request.user.login != 'asyncapi-bot' || github.event.pull_request.user.login != 'dependabot[bot]' || github.event.pull_request.user.login != 'dependabot-preview[bot]') #it runs only if PR actor is not a bot, at least not a bot that we know
if:
github.event.pull_request.draft == false &&
(github.event.pull_request.user.login != 'asyncapi-bot' &&
github.event.pull_request.user.login != 'dependabot[bot]' &&
github.event.pull_request.user.login != 'dependabot-preview[bot]')

runs-on: ubuntu-latest

steps:
- name: Get list of authors
uses: sergeysova/jq-action@v2
- name: Checkout code
uses: actions/checkout@v2
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved

- name: Setup Node.js
uses: actions/setup-node@v2
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
with:
node-version: '16'
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved

- name: Install dependencies
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
run: npm install @octokit/core @octokit/plugin-paginate-rest @octokit/rest cross-fetch
derberg marked this conversation as resolved.
Show resolved Hide resolved

- name: Get List of authors
id: authors
uses: actions/github-script@v5
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
with:
# This cmd does following (line by line):
# 1. CURL 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.
# 2. Iterates over the previous returned payload, and creates an array with the filtered results (see below) so we can work wit it later. 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.
# 3. Grabs the data we need for adding the `Co-authored-by: ...` lines later and puts it into objects to be used later on.
# 4. Filters the 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.
# 5. Removes repeated authors (authors can have more than one commit in the PR).
# 6. Builds the `Co-authored-by: ...` lines with actual info.
# 7. Transforms the array into plain text. Thanks to this, the actual stdout of this step can be used by the next Workflow step (wich is basically the automerge).
cmd: |

Choose a reason for hiding this comment

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

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")'
multiline: true
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
Gmin2 marked this conversation as resolved.
Show resolved Hide resolved
const { Octokit } = require('@octokit/rest');
const { paginateRest } = require('@octokit/plugin-paginate-rest');

const fetch = require('cross-fetch');

const token = process.env.GITHUB_TOKEN;
const prNumber = process.env.PR_NUMBER;
const repository = process.env.GITHUB_REPOSITORY;

async function getCoAuthors() {
try {
const octokit = new Octokit({
auth: token,
request: {
fetch,
},
});

const commitsResponse = await octokit.paginate("GET /repos/{owner}/{repo}/pulls/{pull_number}/commits", {
owner: repository.split('/')[0],
repo: repository.split('/')[1],
pull_number: ${{ github.event.number }},
per_page: 100,
});

const authors = commitsResponse
.map(data => ({
name: data.commit.author.name,
email: data.commit.author.email,
login: data.commit.author.login,
}))
.filter(author => author.login !== 'PR_sender_login')
.reduce((uniqueAuthors, author) => {
if (!uniqueAuthors.some(a => a.email === author.email)) {
uniqueAuthors.push(author);
}
return uniqueAuthors;
}, [])
.map(author => `Co-authored-by: ${author.name} <${author.email}>`)
.join('\n');
console.log(authors);
return authors;
} catch (error) {
console.error('Error fetching commits:', error);
return null;
}
}

await getCoAuthors();
Comment on lines +75 to +76
Copy link

Choose a reason for hiding this comment

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

Correct way to set the output in the Get List of authors step:

Suggested change
await getCoAuthors();
const coAuthors = await getCoAuthors();
core.setOutput("value", coAuthors);


- name: Automerge PR
uses: pascalgn/automerge-action@22948e0bc22f0aa673800da838595a3e7347e584 #v0.15.6 https://github.com/pascalgn/automerge-action/releases/tag/v0.15.6
env:
Expand All @@ -50,6 +103,6 @@ jobs:
MERGE_METHOD: "squash"
# Using the output of the previous step (`Co-authored-by: ...` lines) as commit description.
KhudaDad414 marked this conversation as resolved.
Show resolved Hide resolved
# 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 }}"
MERGE_COMMIT_MESSAGE: "{pullRequest.title} (#{pullRequest.number})\n\n\n${{ steps.authors.outputs.value }}"
MERGE_RETRIES: "20"
MERGE_RETRY_SLEEP: "30000"
Loading