Skip to content

Commit 9363f9d

Browse files
authored
ci: resolve PR number from head repo/branch for fork PRs (#1041)
* ci: replace third-party comment action with local one * fix: update * ci: update * ci: update
1 parent bf0ba56 commit 9363f9d

1 file changed

Lines changed: 42 additions & 3 deletions

File tree

.github/workflows/leave-comment.yml

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,18 @@ on:
88
permissions:
99
contents: read
1010
actions: read
11-
pull-requests: write
11+
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.event.workflow_run.id }}
14+
cancel-in-progress: true
1215

1316
jobs:
1417
aggregate:
1518
name: Aggregate Comparison Results
1619
runs-on: ubuntu-latest
20+
permissions:
21+
pull-requests: write
22+
1723
steps:
1824
- name: Harden Runner
1925
uses: step-security/harden-runner@bf7454d06d71f1098171f2acdf0cd4708d7b5920 # v2.20.0
@@ -40,10 +46,43 @@ jobs:
4046
echo "EOF"
4147
} >> "$GITHUB_OUTPUT"
4248
43-
- name: Add Comment to PR
49+
- name: Resolve Pull Request Number
50+
id: pr
4451
if: steps.combine.outputs.combined
52+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
53+
with:
54+
script: |
55+
const run = context.payload.workflow_run;
56+
57+
// 1. For same-repo Pull Requests the run is already linked to its PR(s).
58+
if (run.pull_requests && run.pull_requests.length) {
59+
core.setOutput('number', run.pull_requests[0].number);
60+
return;
61+
}
62+
63+
// 2. For forks that list is empty, so find the open Pull Request who has the
64+
// correct branch information
65+
const match = await github.rest.pulls.list({
66+
owner: context.repo.owner,
67+
repo: context.repo.repo,
68+
state: 'open',
69+
head: `${run.head_repository.owner.login}:${run.head_branch}`,
70+
sort: 'updated',
71+
direction: 'desc',
72+
per_page: 1,
73+
}).then(r => r.data[0]);
74+
75+
if (!match) {
76+
core.info(`No open pull request found for HEAD ${run.head_sha}`);
77+
return;
78+
}
79+
80+
core.setOutput('number', match.number);
81+
82+
- name: Add Comment to PR
83+
if: steps.combine.outputs.combined && steps.pr.outputs.number != ''
4584
uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1
4685
with:
4786
comment-tag: compared
4887
message: ${{ steps.combine.outputs.combined }}
49-
pr-number: ${{ github.event.workflow_run.pull_requests[0].number }}
88+
pr-number: ${{ steps.pr.outputs.number }}

0 commit comments

Comments
 (0)