From 1a85dbc26a4adfffc4afd46f9f87d72e0f77e190 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Sun, 7 Feb 2021 17:12:51 +0100 Subject: [PATCH 1/2] Action to auto-comment on pull requests with a link to the artifact Whenever a CI build for a pull request succeeds, `github-actions[bot]` will comment (or edit the comment) with a link to directly download the artifact (i.e. the APK) from it. The bot's comment will tend to be near the start of the thread. The service https://nightly.link/ is used for the actual link because it allows downloads without logging in. It is possible to make an equivalent link directly to GitHub, but users who happen to not be logged into GitHub will just get a 404 error. Why this can't be done more simply: * Running the comment API directly from the pull request's action can't be done due to permission boundaries. * [workflow_run](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_run) is the intended place for commenting actions, but GitHub really just doesn't provide a way to find the originating pull request from this event, so we have search for it through the SHA (and that basically takes up the top half of this). You can see what other hacks people have devised for this [here](https://github.com/nyurik/auto_pr_comments_from_forks) and [here](https://github.com/search?l=YAML&q=workflow_run+createComment&type=Code) --- .github/workflows/pr-comment.yml | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 .github/workflows/pr-comment.yml diff --git a/.github/workflows/pr-comment.yml b/.github/workflows/pr-comment.yml new file mode 100644 index 00000000000..830844c9094 --- /dev/null +++ b/.github/workflows/pr-comment.yml @@ -0,0 +1,52 @@ +name: Comment on pull request +on: + workflow_run: + workflows: ['CI'] + types: [completed] +jobs: + pr_comment: + if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success' + runs-on: ubuntu-latest + steps: + - uses: actions/github-script@v3 + with: + script: | + const {owner, repo} = context.repo; + const run_id = ${{github.event.workflow_run.id}}; + const pull_head_sha = '${{github.event.workflow_run.head_sha}}'; + const pull_user_id = ${{github.event.sender.id}}; + + const issue_number = await (async () => { + const pulls = await github.pulls.list({owner, repo}); + for await (const {data} of github.paginate.iterator(pulls)) { + for (const pull of data) { + if (pull.head.sha === pull_head_sha && pull.user.id === pull_user_id) { + return pull.number; + } + } + } + })(); + if (issue_number) { + core.info(`Using pull request ${issue_number}`); + } else { + return core.error(`No matching pull request found`); + } + + const {data: {artifacts}} = await github.actions.listWorkflowRunArtifacts({owner, repo, run_id}); + if (!artifacts.length) { + return core.error(`No artifacts found`); + } + let body = `Download the artifact for this pull request:`; + for (const art of artifacts) { + body += ` [**${art.name}.zip**](https://nightly.link/${owner}/${repo}/actions/artifacts/${art.id}.zip)`; + } + + const {data: comments} = await github.issues.listComments({repo, owner, issue_number}); + const existing_comment = comments.find((c) => c.user.login === 'github-actions[bot]'); + if (existing_comment) { + core.info(`Updating comment ${existing_comment.id}`); + await github.issues.updateComment({repo, owner, comment_id: existing_comment.id, body}); + } else { + core.info(`Creating a comment`); + await github.issues.createComment({repo, owner, issue_number, body}); + } From 9b7d99d61ec140de9d6c6feae5c332868fc854d0 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Mon, 8 Feb 2021 20:49:25 +0100 Subject: [PATCH 2/2] Discern which workflow's comment to update --- .github/workflows/pr-comment.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/pr-comment.yml b/.github/workflows/pr-comment.yml index 830844c9094..0c962651927 100644 --- a/.github/workflows/pr-comment.yml +++ b/.github/workflows/pr-comment.yml @@ -36,13 +36,16 @@ jobs: if (!artifacts.length) { return core.error(`No artifacts found`); } - let body = `Download the artifact for this pull request:`; + const prefix = ''; + let body = `${prefix}\n\nDownload the artifact for this pull request:`; for (const art of artifacts) { body += ` [**${art.name}.zip**](https://nightly.link/${owner}/${repo}/actions/artifacts/${art.id}.zip)`; } const {data: comments} = await github.issues.listComments({repo, owner, issue_number}); - const existing_comment = comments.find((c) => c.user.login === 'github-actions[bot]'); + const existing_comment = comments.find((c) => { + return c.user.login === 'github-actions[bot]' && c.body.startsWith(prefix); + }); if (existing_comment) { core.info(`Updating comment ${existing_comment.id}`); await github.issues.updateComment({repo, owner, comment_id: existing_comment.id, body});