Skip to content

Commit 95962c9

Browse files
committed
Migrate pull_request_target workflows
1 parent 08547cb commit 95962c9

3 files changed

Lines changed: 98 additions & 8 deletions

File tree

.github/workflows/check-change-note.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
name: Check change note
22

33
permissions:
4+
contents: read
45
pull-requests: read
56

67
on:
7-
pull_request_target:
8+
pull_request:
89
types: [labeled, unlabeled, opened, synchronize, reopened, ready_for_review]
910
paths:
1011
- "*/ql/src/**/*.ql"
@@ -23,7 +24,7 @@ jobs:
2324
env:
2425
REPO: ${{ github.repository }}
2526
PULL_REQUEST_NUMBER: ${{ github.event.number }}
26-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27+
GH_TOKEN: ${{ github.token }}
2728
runs-on: ubuntu-latest
2829
steps:
2930

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
name: "Pull Request Labeler Apply"
2+
3+
on:
4+
workflow_run:
5+
workflows: ["Pull Request Labeler"]
6+
types: [completed]
7+
8+
permissions: {}
9+
10+
jobs:
11+
triage:
12+
if: >
13+
github.event.workflow_run.event == 'pull_request' &&
14+
github.event.workflow_run.conclusion == 'success'
15+
runs-on: ubuntu-latest
16+
permissions:
17+
actions: read
18+
contents: read
19+
pull-requests: write
20+
steps:
21+
- name: Validate pull request from workflow_run
22+
id: validate
23+
env:
24+
GH_TOKEN: ${{ github.token }}
25+
REPO: ${{ github.repository }}
26+
RUN_ID: ${{ github.event.workflow_run.id }}
27+
run: |
28+
set -euo pipefail
29+
30+
run_json=$(gh api "repos/$REPO/actions/runs/$RUN_ID")
31+
event=$(jq -r '.event' <<<"$run_json")
32+
conclusion=$(jq -r '.conclusion' <<<"$run_json")
33+
head_sha=$(jq -r '.head_sha' <<<"$run_json")
34+
head_repo=$(jq -r '.head_repository.full_name // empty' <<<"$run_json")
35+
36+
if [ "$event" != "pull_request" ] || [ "$conclusion" != "success" ]; then
37+
echo "apply=false" >> "$GITHUB_OUTPUT"
38+
exit 0
39+
fi
40+
41+
pr_number=$(jq -r 'if (.pull_requests | length) == 1 then .pull_requests[0].number else empty end' <<<"$run_json")
42+
43+
if [ -z "$pr_number" ]; then
44+
prs_json=$(gh api -H 'Accept: application/vnd.github+json' "repos/$REPO/commits/$head_sha/pulls")
45+
pr_number=$(jq -r 'map(select(.state == "open")) | if length == 1 then .[0].number else empty end' <<<"$prs_json")
46+
fi
47+
48+
if [ -z "$pr_number" ]; then
49+
head_owner=$(jq -r '.head_repository.owner.login // empty' <<<"$run_json")
50+
head_branch=$(jq -r '.head_branch // empty' <<<"$run_json")
51+
if [ -n "$head_owner" ] && [ -n "$head_branch" ]; then
52+
prs_json=$(gh api --method GET "repos/$REPO/pulls" -f state=open -f head="$head_owner:$head_branch")
53+
pr_number=$(jq -r 'if length == 1 then .[0].number else empty end' <<<"$prs_json")
54+
fi
55+
fi
56+
57+
if [ -z "$pr_number" ]; then
58+
echo "Could not identify a unique open pull request for workflow run $RUN_ID; skipping."
59+
echo "apply=false" >> "$GITHUB_OUTPUT"
60+
exit 0
61+
fi
62+
63+
pr_json=$(gh api "repos/$REPO/pulls/$pr_number")
64+
state=$(jq -r '.state' <<<"$pr_json")
65+
base_repo=$(jq -r '.base.repo.full_name' <<<"$pr_json")
66+
pr_head_sha=$(jq -r '.head.sha' <<<"$pr_json")
67+
pr_head_repo=$(jq -r '.head.repo.full_name // empty' <<<"$pr_json")
68+
69+
if [ "$state" != "open" ] || [ "$base_repo" != "$REPO" ]; then
70+
echo "Pull request #$pr_number is no longer an open pull request against $REPO; skipping."
71+
echo "apply=false" >> "$GITHUB_OUTPUT"
72+
exit 0
73+
fi
74+
75+
if [ "$pr_head_sha" != "$head_sha" ] || [ "$pr_head_repo" != "$head_repo" ]; then
76+
echo "Pull request #$pr_number changed since workflow run $RUN_ID; skipping stale labeling."
77+
echo "apply=false" >> "$GITHUB_OUTPUT"
78+
exit 0
79+
fi
80+
81+
echo "number=$pr_number" >> "$GITHUB_OUTPUT"
82+
echo "apply=true" >> "$GITHUB_OUTPUT"
83+
84+
- uses: actions/labeler@v4
85+
if: steps.validate.outputs.apply == 'true'
86+
with:
87+
repo-token: "${{ github.token }}"
88+
pr-number: ${{ steps.validate.outputs.number }}

.github/workflows/labeler.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
name: "Pull Request Labeler"
2+
23
on:
3-
- pull_request_target
4+
pull_request:
5+
types: [opened, synchronize, reopened]
46

57
permissions:
68
contents: read
7-
pull-requests: write
9+
pull-requests: read
810

911
jobs:
10-
triage:
12+
request-labels:
1113
runs-on: ubuntu-latest
1214
steps:
13-
- uses: actions/labeler@v4
14-
with:
15-
repo-token: "${{ secrets.GITHUB_TOKEN }}"
15+
- name: Request privileged labeling
16+
run: echo "Labels are applied by the workflow_run writer after re-validating the pull request."

0 commit comments

Comments
 (0)