fix missing labels #17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Template initialisation | |
| on: | |
| push: | |
| branches: | |
| - main # runs after the very first push that creates the repo | |
| jobs: | |
| init: | |
| # Do not run in the original template repository itself | |
| if: github.event.repository.name != 'python-template' | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| issues: write | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Derive project‑specific names | |
| id: names | |
| run: | | |
| echo "repo_slug=${{ github.event.repository.name }}" >> "$GITHUB_OUTPUT" | |
| snake=$(echo "${{ github.event.repository.name }}" | tr '-' '_') | |
| echo "repo_snake=$snake" >> "$GITHUB_OUTPUT" | |
| echo "repo_owner=${{ github.event.repository.owner.login }}" >> "$GITHUB_OUTPUT" | |
| - name: Create working branch | |
| run: git switch -c template-init | |
| - name: Replace repo owner | |
| if: ${{ github.event.repository.owner.login }} != marksverdhei | |
| run: | | |
| repo_owner="${{ steps.names.outputs.repo_owner }}" | |
| # replace repo owner placeholder | |
| grep -lr --exclude-dir=.git 'marksverdhei' . | xargs -r sed -i "s/marksverdhei/${repo_owner}/g" | |
| - name: Replace placeholders inside files | |
| run: | | |
| repo_slug="${{ steps.names.outputs.repo_slug }}" | |
| repo_snake="${{ steps.names.outputs.repo_snake }}" | |
| # replace dash-style placeholder | |
| grep -lr --exclude-dir=.git 'python-template' . | xargs -r sed -i "s/python-template/${repo_slug}/g" | |
| # replace snake_case placeholder | |
| grep -lr --exclude-dir=.git 'python_template' . | xargs -r sed -i "s/python_template/${repo_snake}/g" | |
| - name: Rename files and directories | |
| run: | | |
| repo_slug="${{ steps.names.outputs.repo_slug }}" | |
| repo_snake="${{ steps.names.outputs.repo_snake }}" | |
| # function to rename safely preserving path depth | |
| rename_path() { | |
| old_path="$1"; new_path="$2"; | |
| mkdir -p "$(dirname "$new_path")" | |
| git mv "$old_path" "$new_path" | |
| } | |
| export -f rename_path | |
| # dash‑style paths | |
| find . -depth -name '*python-template*' -print0 | while IFS= read -r -d '' f; do | |
| rename_path "$f" "${f//python-template/$repo_slug}"; | |
| done | |
| # snake_case paths | |
| find . -depth -name '*python_template*' -print0 | while IFS= read -r -d '' f; do | |
| rename_path "$f" "${f//python_template/$repo_snake}"; | |
| done | |
| - name: Remove this workflow so it never runs again | |
| run: git rm -f .github/workflows/template-init.yml | |
| - name: Commit changes | |
| run: | | |
| git config --global user.name 'github-actions[bot]' | |
| git config --global user.email 'github-actions[bot]@users.noreply.github.com' | |
| git add -u | |
| git commit -m "chore: initialize project from template" || echo "Nothing to commit" | |
| - name: Push branch | |
| run: git push -f origin template-init | |
| - name: Create pull request | |
| id: create_pr | |
| continue-on-error: true | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: | | |
| issue_title="Grant GitHub Actions permission to create pull requests" | |
| # look for an open issue with that exact title | |
| issue_num=$(gh issue list --state open --search "in:title \"$issue_title\"" --json number --jq '.[0].number') | |
| # Base PR body text | |
| pr_body=$'Automated placeholder replacement, path renames, and self‑cleanup.' | |
| # Append closing reference if the issue exists | |
| if [ -n "$issue_num" ]; then | |
| pr_body+=$' Fixes #'"$issue_num" | |
| fi | |
| # Create the PR | |
| gh pr create \ | |
| --title "Initialize project from template" \ | |
| --body "$pr_body" \ | |
| --base ${{ github.ref }} \ | |
| --head template-init | |
| - name: Create issue to grant PR permissions (if missing) | |
| if: steps.create_pr.outcome == 'failure' | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| run: | | |
| set -e | |
| issue_title="Grant GitHub Actions permission to create pull requests" | |
| # Does an open issue with this exact title already exist? | |
| existing=$(gh issue list --state open --search "in:title \"$issue_title\"" --json number --jq '.[0].number') | |
| if [ -z "$existing" ]; then | |
| echo "Creating new issue: $issue_title" | |
| gh issue create \ | |
| --title "$issue_title" \ | |
| --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.' | |
| else | |
| echo "Issue #$existing already exists – skipping creation." | |
| fi | |
| - name: Fail workflow because PR could not be created | |
| if: steps.create_pr.outcome == 'failure' | |
| run: | | |
| echo "Failing workflow due to failed PR creation. Please grant the required permissions and re-run." >&2 | |
| exit 1 |