-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
de7f8e2
commit ee64803
Showing
1 changed file
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,51 @@ concurrency: | |
cancel-in-progress: true | ||
|
||
jobs: | ||
test-pr-nr: | ||
if: github.ref == 'refs/heads/main' | ||
runs-on: "ubuntu-latest" | ||
steps: | ||
# Checkout code in order to determine PR number | ||
- uses: actions/[email protected] | ||
with: | ||
fetch-depth: 0 | ||
|
||
# https://stackoverflow.com/a/70102700 | ||
- name: Get Pull Request Number | ||
id: pr | ||
run: echo "::set-output name=pull_request_number::$(gh pr view --json number -q .number || echo "")" | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Get PR number | ||
id: pr2 | ||
run: | | ||
commit_sha=$(git rev-parse HEAD) | ||
pr_number=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | ||
"https://api.github.com/repos/jorisroovers/gitlint/commits/$commit_sha/pulls" | jq -r '.[0].number') | ||
echo "PR_NUMBER=$pr_number" >> $GITHUB_ENV | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- run: echo ${{ env.PR_NUMBER }} | ||
|
||
- run: jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH" | ||
|
||
- run: echo "${{ github.event.pull_request.number }}" | ||
|
||
- run: | | ||
echo "PR number: ${{ steps.pr.outputs.pull_request_number }}" | ||
- run: gh pr view --json number -q .number | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- run: gh pr view --json | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
tests: | ||
needs: test-pr-nr | ||
runs-on: "ubuntu-latest" | ||
strategy: | ||
matrix: | ||
|
@@ -246,6 +290,18 @@ jobs: | |
if: github.ref == 'refs/heads/main' | ||
runs-on: "ubuntu-latest" | ||
steps: | ||
# Checkout code in order to determine PR number | ||
- uses: actions/[email protected] | ||
with: | ||
fetch-depth: 0 | ||
|
||
# https://stackoverflow.com/a/70102700 | ||
- name: Get Pull Request Number | ||
id: pr | ||
run: echo "::set-output name=pull_request_number::$(gh pr view --json number -q .number || echo "")" | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
|
@@ -259,7 +315,7 @@ jobs: | |
\`\`\` | ||
`; | ||
github.rest.issues.createComment({ | ||
issue_number: context.issue.number, | ||
issue_number: ${{ steps.pr.outputs.pull_request_number }}, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: body | ||
|