Skip to content

Commit

Permalink
GHA: Notify - extract PR number when merging on main (#511)
Browse files Browse the repository at this point in the history
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
jorisroovers committed Jun 23, 2023
1 parent de7f8e2 commit ee64803
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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: |
Expand All @@ -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
Expand Down

0 comments on commit ee64803

Please sign in to comment.