Skip to content

Commit dcfe6dc

Browse files
committed
update template init
1 parent 8ccc195 commit dcfe6dc

1 file changed

Lines changed: 44 additions & 4 deletions

File tree

.github/workflows/template-init.yml

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
# .github/workflows/template-init.yml
2-
31
name: Template initialisation
42

53
on:
64
push:
75
branches:
86
- main # runs after the very first push that creates the repo
7+
98
jobs:
109
init:
1110
# Do not run in the original template repository itself
@@ -14,6 +13,7 @@ jobs:
1413
permissions:
1514
contents: write
1615
pull-requests: write
16+
issues: write
1717

1818
steps:
1919
- name: Checkout repository
@@ -87,11 +87,51 @@ jobs:
8787
run: git push -f origin template-init
8888

8989
- name: Create pull request
90+
id: create_pr
91+
continue-on-error: true
9092
env:
9193
GH_TOKEN: ${{ github.token }}
9294
run: |
95+
issue_title="Grant GitHub Actions permission to create pull requests"
96+
# look for an open issue with that exact title
97+
issue_num=$(gh issue list --state open --search "in:title \"$issue_title\"" --json number --jq '.[0].number')
98+
99+
# Base PR body text
100+
pr_body=$'Automated placeholder replacement, path renames, and self‑cleanup.'
101+
102+
# Append closing reference if the issue exists
103+
if [ -n "$issue_num" ]; then
104+
pr_body+=$' Fixes #'"$issue_num"
105+
fi
106+
107+
# Create the PR
93108
gh pr create \
94109
--title "Initialize project from template" \
95-
--body "Automated placeholder replacement, path renames, and self‑cleanup." \
110+
--body "$pr_body" \
96111
--base ${{ github.ref }} \
97-
--head template-init \
112+
--head template-init
113+
114+
- name: Create issue to grant PR permissions (if missing)
115+
if: steps.create_pr.outcome == 'failure'
116+
env:
117+
GH_TOKEN: ${{ github.token }}
118+
run: |
119+
set -e
120+
issue_title="Grant GitHub Actions permission to create pull requests"
121+
# Does an open issue with this exact title already exist?
122+
existing=$(gh issue list --state open --search "in:title \"$issue_title\"" --json number --jq '.[0].number')
123+
if [ -z "$existing" ]; then
124+
echo "Creating new issue: $issue_title"
125+
gh issue create \
126+
--title "$issue_title" \
127+
--body $'The workflow attempted to create a pull request but failed with the following error:\n\n```\npull request create failed: GraphQL: GitHub Actions is not permitted to create or approve pull requests (createPullRequest)\n```\n\nPlease grant the required permission by visiting the following link:\n\nhttps://github.com/${{ github.repository }}/settings/actions\n\nThen, in **Actions → General → Workflow permissions**, choose **Read and write permissions** (and optionally **Allow GitHub Actions to create and approve pull requests**).\n![Screenshot of checkbox](https://github.com/user-attachments/assets/cac2626e-bace-4f97-99b4-529331218ee6)\nAfter updating the setting, re‑run the workflow to complete the project initialisation.' \
128+
--label "actions-feedback, automation"
129+
else
130+
echo "Issue #$existing already exists – skipping creation."
131+
fi
132+
133+
- name: Fail workflow because PR could not be created
134+
if: steps.create_pr.outcome == 'failure'
135+
run: |
136+
echo "Failing workflow due to failed PR creation. Please grant the required permissions and re-run." >&2
137+
exit 1

0 commit comments

Comments
 (0)