@@ -5,47 +5,48 @@ description: Get the PR number without relying on the pull_request* event trigge
5
5
runs :
6
6
using : composite
7
7
steps :
8
- # The steps below are executed only when testing in a PR.
9
- - name : Get PR info
8
+ - name : Get PR info (non-main branch)
10
9
if : ${{ github.ref_name != 'main' }}
11
10
uses : nv-gha-runners/get-pr-info@main
12
11
id : get-pr-info
13
-
14
- - name : Extract PR number from info
12
+
13
+ - name : Extract PR number (non-main branch)
15
14
if : ${{ github.ref_name != 'main' }}
16
15
shell : bash --noprofile --norc -xeuo pipefail {0}
17
16
run : |
17
+ trap 'echo "Error at line $LINENO"; exit 1' ERR
18
18
PR_NUMBER="${{ fromJSON(steps.get-pr-info.outputs.pr-info).number }}"
19
- if [[ "$PR_NUMBER" == " " ]]; then
20
- echo "cannot extract PR number"
19
+ if [[ -z "$PR_NUMBER" ]]; then
20
+ echo "Cannot extract PR number for ref: ${{ github.ref_name }} "
21
21
exit 1
22
- else
23
- echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
24
22
fi
25
-
26
- # The steps below are executed only when building on main.
27
- - name : Get PR data
23
+ echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
24
+
25
+ - name : Get PR data (main branch)
28
26
if : ${{ github.ref_name == 'main' }}
29
27
uses : actions/github-script@v7
30
28
id : get-pr-data
31
29
with :
32
30
script : |
33
- return (
34
- await github.rest.repos.listPullRequestsAssociatedWithCommit({
35
- commit_sha: context.sha,
36
- owner: context.repo.owner,
37
- repo: context.repo.repo,
38
- })
39
- ).data[0];
40
-
41
- - name : Extract PR number from data
31
+ const prs = await github.rest.repos.listPullRequestsAssociatedWithCommit({
32
+ commit_sha: context.sha,
33
+ owner: context.repo.owner,
34
+ repo: context.repo.repo,
35
+ });
36
+ if (!prs.data.length) {
37
+ core.setFailed("No PR associated with this commit on 'main'.");
38
+ } else {
39
+ return prs.data[0];
40
+ }
41
+
42
+ - name : Extract PR number (main branch)
42
43
if : ${{ github.ref_name == 'main' }}
43
44
shell : bash --noprofile --norc -xeuo pipefail {0}
44
45
run : |
46
+ trap 'echo "Error at line $LINENO"; exit 1' ERR
45
47
PR_NUMBER="${{ fromJSON(steps.get-pr-data.outputs.result).number }}"
46
- if [[ "$PR_NUMBER" == " " ]]; then
47
- echo "cannot extract PR number "
48
+ if [[ -z "$PR_NUMBER" ]]; then
49
+ echo "No associated PR found for the commit in 'main'. "
48
50
exit 1
49
- else
50
- echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
51
51
fi
52
+ echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
0 commit comments