Skip to content

Commit 011ae9a

Browse files
committed
ci: probe GITHUB_TOKEN PR permission
1 parent a5f4ce0 commit 011ae9a

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
name: Probe GITHUB_TOKEN Pull Request Permission
2+
3+
on:
4+
push:
5+
branches:
6+
- qinezh/sync-foundry-canvas-probe
7+
8+
permissions:
9+
contents: write
10+
pull-requests: write
11+
12+
jobs:
13+
probe:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout main
18+
uses: actions/checkout@v4
19+
with:
20+
ref: main
21+
fetch-depth: 0
22+
23+
- name: Push temporary probe branch
24+
id: branch
25+
shell: bash
26+
env:
27+
PROBE_BRANCH: bot/github-token-pr-probe-${{ github.run_id }}-${{ github.run_attempt }}
28+
run: |
29+
set -euo pipefail
30+
31+
echo "name=$PROBE_BRANCH" >> "$GITHUB_OUTPUT"
32+
git switch -c "$PROBE_BRANCH"
33+
34+
mkdir -p .github/token-probes
35+
printf 'Workflow run: %s\n' "$GITHUB_RUN_ID" \
36+
> ".github/token-probes/$GITHUB_RUN_ID.txt"
37+
38+
git config user.name "github-actions[bot]"
39+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
40+
git add ".github/token-probes/$GITHUB_RUN_ID.txt"
41+
git commit -m "ci: probe GITHUB_TOKEN pull request permission"
42+
git push origin "HEAD:refs/heads/$PROBE_BRANCH"
43+
44+
- name: Create temporary draft pull request
45+
id: pr
46+
shell: bash
47+
env:
48+
GH_TOKEN: ${{ github.token }}
49+
PROBE_BRANCH: ${{ steps.branch.outputs.name }}
50+
run: |
51+
set -euo pipefail
52+
53+
response=$(gh api --method POST \
54+
"repos/$GITHUB_REPOSITORY/pulls" \
55+
-f title="GITHUB_TOKEN PR permission probe" \
56+
-f head="$PROBE_BRANCH" \
57+
-f base="main" \
58+
-F draft=true \
59+
-f body="Temporary automated probe from workflow run $GITHUB_RUN_ID.")
60+
61+
pr_number=$(jq -r '.number' <<< "$response")
62+
pr_url=$(jq -r '.html_url' <<< "$response")
63+
echo "number=$pr_number" >> "$GITHUB_OUTPUT"
64+
65+
{
66+
echo "## GITHUB_TOKEN PR Permission"
67+
echo
68+
echo "Successfully created temporary draft PR [#$pr_number]($pr_url)."
69+
} >> "$GITHUB_STEP_SUMMARY"
70+
71+
- name: Clean up temporary pull request and branch
72+
if: always()
73+
shell: bash
74+
env:
75+
GH_TOKEN: ${{ github.token }}
76+
PROBE_BRANCH: ${{ steps.branch.outputs.name }}
77+
PR_NUMBER: ${{ steps.pr.outputs.number }}
78+
run: |
79+
set -u
80+
81+
if [[ -n "${PR_NUMBER:-}" ]]; then
82+
gh api --method PATCH \
83+
"repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER" \
84+
-f state="closed"
85+
fi
86+
87+
if [[ -n "${PROBE_BRANCH:-}" ]]; then
88+
gh api --method DELETE \
89+
"repos/$GITHUB_REPOSITORY/git/refs/heads/$PROBE_BRANCH" \
90+
|| true
91+
fi

0 commit comments

Comments
 (0)