Skip to content

Commit 81a4f4d

Browse files
committed
handle-pr-comment/handle-pr-push: create a Check Run in the PR
One of the really nice things about the Azure Pipelines was that they would automatically show PR Checks when they were running. This gave some sort of pleasing, visual feedback when `/submit`-ing patch series. Let's have the same even in the new world, where the PR comment is handled by a GitHub workflow running in a completely separate GitHub org, and not connected with the PR in any official capacity. Note: If we do not specify `output[text]` in the final update, the text that was specified when creating the Check Run will be _forgotten_. This is arguably a bug in Actions, and given the recent history of that product it probably won't get fixed, so we work around it here by simply specifying the entire `output` _again_. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 4a96b2e commit 81a4f4d

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

.github/workflows/handle-pr-comment.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,43 @@ jobs:
3939
private-key: ${{ secrets.GITGITGADGET_GIT_GITHUB_APP_PRIVATE_KEY }}
4040
owner: dscho
4141
repositories: git
42+
- name: create a check run
43+
id: create-check-run
44+
run: |
45+
title="Handle PR comment"
46+
summary="Handling PR comment $PR_COMMENT_URL"
47+
details_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
48+
text="This handles $PR_COMMENT_URL, see $details_url for details."
49+
echo "title=$title" >> $GITHUB_OUTPUT
50+
echo "summary=$summary" >> $GITHUB_OUTPUT
51+
echo "text=$text" >> $GITHUB_OUTPUT
52+
53+
PR_NUMBER="${PR_COMMENT_URL%#*}" # skip the suffix with the comment ID
54+
PR_NUMBER="${PR_NUMBER##*/}" # skip the prefix before the PR number
55+
56+
export GH_TOKEN="${{ secrets.GITHUB_TOKEN }}"
57+
eval "$(gh api repos/gitgitgadget/git/pulls/$PR_NUMBER \
58+
--jq '"head_sha=\(.head.sha) && repo=\(.base.repo.full_name)"')"
59+
echo "head_sha=$head_sha" >> $GITHUB_OUTPUT
60+
echo "repo=$repo" >> $GITHUB_OUTPUT
61+
62+
export GH_TOKEN="$(case "$repo" in
63+
gitgitgadget/git) echo "${{ steps.gitgitgadget-git-token.outputs.token }}";;
64+
git/git) echo "${{ steps.git-git-token.outputs.token }}";;
65+
dscho/git) echo "${{ steps.dscho-git-token.outputs.token }}";;
66+
*) echo "${{ secrets.GITHUB_TOKEN }}";;
67+
esac
68+
)"
69+
eval "$(gh api repos/$repo/check-runs -X POST \
70+
-f name='handle_pr_comment' \
71+
-f head_sha="$head_sha" \
72+
-f status='in_progress' \
73+
-f details_url="$details_url" \
74+
-f "output[title]=$title" \
75+
-f "output[summary]=$summary" \
76+
-f "output[text]=$text" \
77+
--jq '"check_run_id=\(.id)"')"
78+
echo "check_run_id=$check_run_id" >> $GITHUB_OUTPUT
4279
- uses: gitgitgadget/gitgitgadget/handle-pr-comment@v1
4380
with:
4481
pr-repo-token: ${{ steps.gitgitgadget-git-token.outputs.token }}
@@ -48,3 +85,20 @@ jobs:
4885
smtp-user: [email protected]
4986
smtp-pass: "${{ secrets.GITGITGADGET_SMTP_PASS }}"
5087
pr-comment-url: ${{ env.PR_COMMENT_URL }}
88+
- name: update the check run
89+
if: always() && steps.create-check-run.outputs.check_run_id != ''
90+
run: |
91+
export GH_TOKEN="$(case "${{ steps.create-check-run.outputs.repo }}" in
92+
gitgitgadget/git) echo "${{ steps.gitgitgadget-git-token.outputs.token }}";;
93+
git/git) echo "${{ steps.git-git-token.outputs.token }}";;
94+
dscho/git) echo "${{ steps.dscho-git-token.outputs.token }}";;
95+
*) echo "${{ secrets.GITHUB_TOKEN }}";;
96+
esac
97+
)"
98+
gh api repos/${{ steps.create-check-run.outputs.repo }}/check-runs/${{ steps.create-check-run.outputs.check_run_id }} \
99+
-X PATCH \
100+
-f status='completed' \
101+
-f conclusion='${{ job.status }}' \
102+
-f 'output[title]=${{ steps.create-check-run.outputs.title }}' \
103+
-f 'output[summary]=${{ steps.create-check-run.outputs.summary }}' \
104+
-f 'output[text]=${{ steps.create-check-run.outputs.text }}'

.github/workflows/handle-pr-push.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,62 @@ jobs:
3939
private-key: ${{ secrets.GITGITGADGET_GIT_GITHUB_APP_PRIVATE_KEY }}
4040
owner: dscho
4141
repositories: git
42+
- name: create a check run
43+
id: create-check-run
44+
run: |
45+
title="Handle PR push"
46+
summary="Handling new commits in $PR_URL"
47+
details_url="${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
48+
text="This handles $PR_URL, see $details_url for details."
49+
echo "title=$title" >> $GITHUB_OUTPUT
50+
echo "summary=$summary" >> $GITHUB_OUTPUT
51+
echo "text=$text" >> $GITHUB_OUTPUT
52+
53+
PR_NUMBER="${PR_URL##*/}" # skip the prefix before the PR number
54+
55+
export GH_TOKEN="${{ secrets.GITHUB_TOKEN }}"
56+
eval "$(gh api repos/gitgitgadget/git/pulls/$PR_NUMBER \
57+
--jq '"head_sha=\(.head.sha) && repo=\(.base.repo.full_name)"')"
58+
echo "head_sha=$head_sha" >> $GITHUB_OUTPUT
59+
echo "repo=$repo" >> $GITHUB_OUTPUT
60+
61+
export GH_TOKEN="$(case "$repo" in
62+
gitgitgadget/git) echo "${{ steps.gitgitgadget-git-token.outputs.token }}";;
63+
git/git) echo "${{ steps.git-git-token.outputs.token }}";;
64+
dscho/git) echo "${{ steps.dscho-git-token.outputs.token }}";;
65+
*) echo "${{ secrets.GITHUB_TOKEN }}";;
66+
esac
67+
)"
68+
eval "$(gh api repos/$repo/check-runs -X POST \
69+
-f name='handle_pr_push' \
70+
-f head_sha="$head_sha" \
71+
-f status='in_progress' \
72+
-f details_url="$details_url" \
73+
-f "output[title]=$title" \
74+
-f "output[summary]=$summary" \
75+
-f "output[text]=$text" \
76+
--jq '"check_run_id=\(.id)"')"
77+
echo "check_run_id=$check_run_id" >> $GITHUB_OUTPUT
4278
- uses: gitgitgadget/gitgitgadget/handle-pr-push@v1
4379
with:
4480
pr-repo-token: ${{ steps.gitgitgadget-git-token.outputs.token }}
4581
upstream-repo-token: ${{ steps.git-git-token.outputs.token }}
4682
test-repo-token: ${{ steps.dscho-git-token.outputs.token }}
4783
pr-url: ${{ env.PR_URL }}
84+
- name: update the check run
85+
if: always() && steps.create-check-run.outputs.check_run_id != ''
86+
run: |
87+
export GH_TOKEN="$(case "${{ steps.create-check-run.outputs.repo }}" in
88+
gitgitgadget/git) echo "${{ steps.gitgitgadget-git-token.outputs.token }}";;
89+
git/git) echo "${{ steps.git-git-token.outputs.token }}";;
90+
dscho/git) echo "${{ steps.dscho-git-token.outputs.token }}";;
91+
*) echo "${{ secrets.GITHUB_TOKEN }}";;
92+
esac
93+
)"
94+
gh api repos/${{ steps.create-check-run.outputs.repo }}/check-runs/${{ steps.create-check-run.outputs.check_run_id }} \
95+
-X PATCH \
96+
-f status='completed' \
97+
-f conclusion='${{ job.status }}' \
98+
-f 'output[title]=${{ steps.create-check-run.outputs.title }}' \
99+
-f 'output[summary]=${{ steps.create-check-run.outputs.summary }}' \
100+
-f 'output[text]=${{ steps.create-check-run.outputs.text }}'

0 commit comments

Comments
 (0)