Skip to content

Commit 6d1e632

Browse files
committed
docs: handle PR permission restrictions in sync workflow
1 parent 62e0c67 commit 6d1e632

2 files changed

Lines changed: 26 additions & 6 deletions

File tree

.github/workflows/sdk-reference-sync.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ jobs:
134134
if: steps.changes.outputs.changes == 'true'
135135
id: pr
136136
env:
137-
GH_TOKEN: ${{ github.token }}
137+
GH_TOKEN: ${{ secrets.SDK_REFERENCE_SYNC_PR_TOKEN != '' && secrets.SDK_REFERENCE_SYNC_PR_TOKEN || github.token }}
138+
TOKEN_SOURCE: ${{ secrets.SDK_REFERENCE_SYNC_PR_TOKEN != '' && 'SDK_REFERENCE_SYNC_PR_TOKEN' || 'github.token' }}
138139
BASE_BRANCH: ${{ github.ref_name }}
139140
BRANCH_NAME: automation/sdk-reference-sync
140141
TRIGGER: ${{ steps.params.outputs.trigger }}
@@ -179,6 +180,12 @@ jobs:
179180
fi
180181
echo "" >> $GITHUB_STEP_SUMMARY
181182
183+
if [[ "$PR_OPERATION" == "manual" ]]; then
184+
echo "Automatic PR creation is blocked for \`github.token\` in this repository." >> $GITHUB_STEP_SUMMARY
185+
echo "Enable the repository setting 'Allow GitHub Actions to create and approve pull requests' or add the \`SDK_REFERENCE_SYNC_PR_TOKEN\` secret for this workflow." >> $GITHUB_STEP_SUMMARY
186+
echo "" >> $GITHUB_STEP_SUMMARY
187+
fi
188+
182189
if [[ "$CHANGES" == "true" ]]; then
183190
echo "### Generated Files" >> $GITHUB_STEP_SUMMARY
184191
echo '```' >> $GITHUB_STEP_SUMMARY

scripts/create-sdk-reference-sync-pr.sh

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ force="${FORCE:-false}"
2222
changed_files="${CHANGED_FILES:-0}"
2323
total_mdx_files="${TOTAL_MDX_FILES:-0}"
2424
workflow_name="${WORKFLOW_NAME:-}"
25-
repository="${REPOSITORY:-}"
26-
run_id="${RUN_ID:-}"
25+
repository="${REPOSITORY:?REPOSITORY is required}"
26+
run_id="${RUN_ID:?RUN_ID is required}"
27+
token_source="${TOKEN_SOURCE:-github.token}"
2728

2829
if [[ "$base_branch" == "$branch_name" ]]; then
2930
echo "Base branch and PR branch cannot be the same" >&2
@@ -45,6 +46,7 @@ if git diff --staged --quiet; then
4546
fi
4647

4748
title="docs: sync SDK reference for ${sdk_name} ${sdk_version}"
49+
compare_url="https://github.com/${repository}/compare/${base_branch}...${branch_name}?expand=1"
4850

4951
git commit -m "$title"
5052
git push --force-with-lease origin "HEAD:${branch_name}"
@@ -88,13 +90,24 @@ if [[ -n "$pr_info" ]]; then
8890
gh pr edit "$pr_number" --repo "$repository" --title "$title" --body-file "$body_file" >/dev/null
8991
operation="updated"
9092
else
91-
pr_url="$(gh pr create \
93+
if ! pr_url="$(gh pr create \
9294
--repo "$repository" \
9395
--base "$base_branch" \
9496
--head "$branch_name" \
9597
--title "$title" \
96-
--body-file "$body_file")"
97-
operation="created"
98+
--body-file "$body_file" 2>&1)"; then
99+
if [[ "$token_source" == "github.token" ]] && [[ "$pr_url" == *"GitHub Actions is not permitted to create or approve pull requests"* ]]; then
100+
echo "Automatic PR creation is blocked for github.token in this repository." >&2
101+
echo "Enable the repository setting 'Allow GitHub Actions to create and approve pull requests' or configure the SDK_REFERENCE_SYNC_PR_TOKEN secret." >&2
102+
pr_url="$compare_url"
103+
operation="manual"
104+
else
105+
echo "$pr_url" >&2
106+
exit 1
107+
fi
108+
else
109+
operation="created"
110+
fi
98111
fi
99112

100113
{

0 commit comments

Comments
 (0)