Skip to content

Commit 2b95f42

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 2b95f42

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

.github/workflows/ci.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,20 @@ jobs:
246246
if: github.ref == 'refs/heads/main'
247247
runs-on: "ubuntu-latest"
248248
steps:
249+
# Checkout code in order to determine PR number
250+
- uses: actions/[email protected]
251+
with:
252+
fetch-depth: 0
253+
254+
- name: Get PR number
255+
run: |
256+
commit_sha=$(git rev-parse HEAD)
257+
pr_number=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
258+
"https://api.github.com/repos/jorisroovers/gitlint/commits/$commit_sha/pulls" | jq -r '.[0].number')
259+
echo "PR_NUMBER=$pr_number" >> $GITHUB_ENV
260+
env:
261+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
262+
249263
- uses: actions/github-script@v6
250264
with:
251265
script: |
@@ -259,7 +273,7 @@ jobs:
259273
\`\`\`
260274
`;
261275
github.rest.issues.createComment({
262-
issue_number: context.issue.number,
276+
issue_number: process.env.PR_NUMBER,
263277
owner: context.repo.owner,
264278
repo: context.repo.repo,
265279
body: body

0 commit comments

Comments
 (0)