|
1 | | -# .github/workflows/template-init.yml |
2 | | - |
3 | 1 | name: Template initialisation |
4 | 2 |
|
5 | 3 | on: |
6 | 4 | push: |
7 | 5 | branches: |
8 | 6 | - main # runs after the very first push that creates the repo |
| 7 | + |
9 | 8 | jobs: |
10 | 9 | init: |
11 | 10 | # Do not run in the original template repository itself |
|
14 | 13 | permissions: |
15 | 14 | contents: write |
16 | 15 | pull-requests: write |
| 16 | + issues: write |
17 | 17 |
|
18 | 18 | steps: |
19 | 19 | - name: Checkout repository |
@@ -87,11 +87,51 @@ jobs: |
87 | 87 | run: git push -f origin template-init |
88 | 88 |
|
89 | 89 | - name: Create pull request |
| 90 | + id: create_pr |
| 91 | + continue-on-error: true |
90 | 92 | env: |
91 | 93 | GH_TOKEN: ${{ github.token }} |
92 | 94 | 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 |
93 | 108 | gh pr create \ |
94 | 109 | --title "Initialize project from template" \ |
95 | | - --body "Automated placeholder replacement, path renames, and self‑cleanup." \ |
| 110 | + --body "$pr_body" \ |
96 | 111 | --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\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