Skip to content

Commit 2fd48a6

Browse files
authored
Improve PR Details job to use github-script and output labels (#2397)
* Improve PR Details job to use github-script and output labels * Fix wrongly using github.ref in workflow_run actions which always refer to develop * Update pr-details to be far more generic
1 parent 2f540e3 commit 2fd48a6

File tree

2 files changed

+21
-31
lines changed

2 files changed

+21
-31
lines changed

.github/workflows/pr_details.yml

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
# Find details about the PR associated with this ref
2-
# Outputs:
3-
# prnumber: the ID number of the associated PR
4-
# headref: the name of the head branch of the PR
5-
# baseref: the name of the base branch of the PR
62
name: PR Details
73
on:
84
workflow_call:
@@ -18,13 +14,16 @@ on:
1814
outputs:
1915
pr_id:
2016
description: The ID of the PR found
21-
value: ${{ jobs.prdetails.outputs.pr_id }}
17+
value: ${{ fromJSON(jobs.prdetails.outputs.result).number }}
2218
head_branch:
2319
description: The head branch of the PR found
24-
value: ${{ jobs.prdetails.outputs.head_branch }}
20+
value: ${{ fromJSON(jobs.prdetails.outputs.result).head.ref }}
2521
base_branch:
2622
description: The base branch of the PR found
27-
value: ${{ jobs.prdetails.outputs.base_branch }}
23+
value: ${{ fromJSON(jobs.prdetails.outputs.result).base.ref }}
24+
data:
25+
description: The JSON data of the pull request API object
26+
value: ${{ jobs.prdetails.outputs.result }})
2827

2928
jobs:
3029
prdetails:
@@ -33,27 +32,18 @@ jobs:
3332
steps:
3433
- name: "🔍 Read PR details"
3534
id: details
36-
# We need to find the PR number that corresponds to the branch, which we do by searching the GH API
37-
# The workflow_run event includes a list of pull requests, but it doesn't get populated for
38-
# forked PRs: https://docs.github.com/en/rest/reference/checks#create-a-check-run
39-
run: |
40-
head_branch='${{ inputs.owner }}:${{ inputs.branch }}'
41-
echo "Head branch: $head_branch"
42-
pulls_uri="https://api.github.com/repos/${{ github.repository }}/pulls?head=$(jq -Rr '@uri' <<<$head_branch)"
43-
pr_data=$(curl -s -H 'Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' "$pulls_uri")
44-
45-
pr_number=$(jq -r '.[] | .number' <<< "$pr_data")
46-
echo "PR number: $pr_number"
47-
echo "::set-output name=prnumber::$pr_number"
48-
49-
head_ref=$(jq -r '.[] | .head.ref' <<< "$pr_data")
50-
echo "Head ref: $head_ref"
51-
echo "::set-output name=headref::$head_ref"
52-
53-
base_ref=$(jq -r '.[] | .base.ref' <<< "$pr_data")
54-
echo "Base ref: $base_ref"
55-
echo "::set-output name=baseref::$base_ref"
35+
uses: actions/github-script@v5
36+
with:
37+
# We need to find the PR number that corresponds to the branch, which we do by searching the GH API
38+
# The workflow_run event includes a list of pull requests, but it doesn't get populated for
39+
# forked PRs: https://docs.github.com/en/rest/reference/checks#create-a-check-run
40+
script: |
41+
const [owner, repo] = "${{ github.repository }}".split("/");
42+
const response = await github.rest.pulls.list({
43+
head: "${{ inputs.owner }}:${{ inputs.branch }}",
44+
owner,
45+
repo,
46+
});
47+
return response.data[0];
5648
outputs:
57-
pr_id: ${{ steps.details.outputs.prnumber }}
58-
head_branch: ${{ steps.details.outputs.headref }}
59-
base_branch: ${{ steps.details.outputs.baseref }}
49+
result: ${{ steps.details.outputs.result }}

.github/workflows/sonarqube.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
types:
66
- completed
77
concurrency:
8-
group: ${{ github.workflow }}-${{ github.ref }}
8+
group: ${{ github.workflow }}-${{ github.event.workflow_run.head_branch }}
99
cancel-in-progress: true
1010
jobs:
1111
prdetails:

0 commit comments

Comments
 (0)