Skip to content

Commit ee64803

Browse files
committed
GHA: Notify - extract PR number when merging on main (#511)
When using github.rest.issues.createComment, context.issue.number does not work when the notify job is triggered by a push to the 'main' branch event. Instead, we manually extract the PR number using the gh CLI.
1 parent de7f8e2 commit ee64803

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

.github/workflows/ci.yml

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,51 @@ concurrency:
1515
cancel-in-progress: true
1616

1717
jobs:
18+
test-pr-nr:
19+
if: github.ref == 'refs/heads/main'
20+
runs-on: "ubuntu-latest"
21+
steps:
22+
# Checkout code in order to determine PR number
23+
- uses: actions/[email protected]
24+
with:
25+
fetch-depth: 0
26+
27+
# https://stackoverflow.com/a/70102700
28+
- name: Get Pull Request Number
29+
id: pr
30+
run: echo "::set-output name=pull_request_number::$(gh pr view --json number -q .number || echo "")"
31+
env:
32+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
33+
34+
- name: Get PR number
35+
id: pr2
36+
run: |
37+
commit_sha=$(git rev-parse HEAD)
38+
pr_number=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
39+
"https://api.github.com/repos/jorisroovers/gitlint/commits/$commit_sha/pulls" | jq -r '.[0].number')
40+
echo "PR_NUMBER=$pr_number" >> $GITHUB_ENV
41+
env:
42+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43+
44+
- run: echo ${{ env.PR_NUMBER }}
45+
46+
- run: jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH"
47+
48+
- run: echo "${{ github.event.pull_request.number }}"
49+
50+
- run: |
51+
echo "PR number: ${{ steps.pr.outputs.pull_request_number }}"
52+
53+
- run: gh pr view --json number -q .number
54+
env:
55+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
56+
57+
- run: gh pr view --json
58+
env:
59+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
60+
1861
tests:
62+
needs: test-pr-nr
1963
runs-on: "ubuntu-latest"
2064
strategy:
2165
matrix:
@@ -246,6 +290,18 @@ jobs:
246290
if: github.ref == 'refs/heads/main'
247291
runs-on: "ubuntu-latest"
248292
steps:
293+
# Checkout code in order to determine PR number
294+
- uses: actions/[email protected]
295+
with:
296+
fetch-depth: 0
297+
298+
# https://stackoverflow.com/a/70102700
299+
- name: Get Pull Request Number
300+
id: pr
301+
run: echo "::set-output name=pull_request_number::$(gh pr view --json number -q .number || echo "")"
302+
env:
303+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
304+
249305
- uses: actions/github-script@v6
250306
with:
251307
script: |
@@ -259,7 +315,7 @@ jobs:
259315
\`\`\`
260316
`;
261317
github.rest.issues.createComment({
262-
issue_number: context.issue.number,
318+
issue_number: ${{ steps.pr.outputs.pull_request_number }},
263319
owner: context.repo.owner,
264320
repo: context.repo.repo,
265321
body: body

0 commit comments

Comments
 (0)