From 1a4447f6757337ebaac2523f472a5e4032c279d2 Mon Sep 17 00:00:00 2001 From: marksverdhei Date: Thu, 5 Jun 2025 12:23:55 +0200 Subject: [PATCH 01/11] Add template init --- .github/workflows/template-init.yml | 89 +++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 .github/workflows/template-init.yml diff --git a/.github/workflows/template-init.yml b/.github/workflows/template-init.yml new file mode 100644 index 0000000..71c09af --- /dev/null +++ b/.github/workflows/template-init.yml @@ -0,0 +1,89 @@ +# .github/workflows/template-init.yml + +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 + + 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" + + - name: Create working branch + run: git switch -c template-init + + - 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 .github/workflows/template-init.yml + + - name: Commit changes + run: | + git add -u + git commit -m "chore: initialize project from template" || echo "Nothing to commit" + + - name: Push branch + run: git push -u origin template-init + + - name: Create pull request + env: + GH_TOKEN: ${{ github.token }} + run: | + gh pr create \ + --title "Initialize project from template" \ + --body "Automated placeholder replacement, path renames, and self‑cleanup." \ + --base main \ + --head template-init \ + --label automation From 60eb5c1b7072b22126bd37fc9b40f185fd71e283 Mon Sep 17 00:00:00 2001 From: marksverdhei Date: Thu, 5 Jun 2025 12:25:25 +0200 Subject: [PATCH 02/11] Add current branch trigger --- .github/workflows/template-init.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/template-init.yml b/.github/workflows/template-init.yml index 71c09af..e1f41ae 100644 --- a/.github/workflows/template-init.yml +++ b/.github/workflows/template-init.yml @@ -6,7 +6,7 @@ on: push: branches: - main # runs after the very first push that creates the repo - + - add-init-workflow jobs: init: # Do not run in the original template repository itself From fe0afd8ed9ec2ce2d1abd0bf72e84b486951fe7f Mon Sep 17 00:00:00 2001 From: marksverdhei Date: Thu, 12 Jun 2025 18:57:33 +0200 Subject: [PATCH 03/11] add test suffix for pr creation --- .github/workflows/template-init.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/template-init.yml b/.github/workflows/template-init.yml index e1f41ae..31f65d0 100644 --- a/.github/workflows/template-init.yml +++ b/.github/workflows/template-init.yml @@ -25,7 +25,7 @@ jobs: - name: Derive project‑specific names id: names run: | - echo "repo_slug=${{ github.event.repository.name }}" >> "$GITHUB_OUTPUT" + echo "repo_slug=${{ github.event.repository.name }}-test" >> "$GITHUB_OUTPUT" snake=$(echo "${{ github.event.repository.name }}" | tr '-' '_') echo "repo_snake=$snake" >> "$GITHUB_OUTPUT" From 7df4e6eb88a27db67b2f3d2ebb1041f657e3b5e4 Mon Sep 17 00:00:00 2001 From: marksverdhei Date: Thu, 12 Jun 2025 18:59:05 +0200 Subject: [PATCH 04/11] add test suffix to snake as well for pr creation --- .github/workflows/template-init.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/template-init.yml b/.github/workflows/template-init.yml index 31f65d0..914a3ec 100644 --- a/.github/workflows/template-init.yml +++ b/.github/workflows/template-init.yml @@ -26,7 +26,7 @@ jobs: id: names run: | echo "repo_slug=${{ github.event.repository.name }}-test" >> "$GITHUB_OUTPUT" - snake=$(echo "${{ github.event.repository.name }}" | tr '-' '_') + snake=$(echo "${{ github.event.repository.name }}-test" | tr '-' '_') echo "repo_snake=$snake" >> "$GITHUB_OUTPUT" - name: Create working branch From e351b17172bc42a327b40292db1531536492a69c Mon Sep 17 00:00:00 2001 From: marksverdhei Date: Thu, 12 Jun 2025 19:00:33 +0200 Subject: [PATCH 05/11] Remove by force --- .github/workflows/template-init.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/template-init.yml b/.github/workflows/template-init.yml index 914a3ec..5eac3fb 100644 --- a/.github/workflows/template-init.yml +++ b/.github/workflows/template-init.yml @@ -67,7 +67,7 @@ jobs: done - name: Remove this workflow so it never runs again - run: git rm .github/workflows/template-init.yml + run: git rm -f .github/workflows/template-init.yml - name: Commit changes run: | From 47beecc8955332dd2a8b02051e25efd635759e63 Mon Sep 17 00:00:00 2001 From: marksverdhei Date: Thu, 12 Jun 2025 19:01:51 +0200 Subject: [PATCH 06/11] Dont add nonexistent label --- .github/workflows/template-init.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/template-init.yml b/.github/workflows/template-init.yml index 5eac3fb..21a4df2 100644 --- a/.github/workflows/template-init.yml +++ b/.github/workflows/template-init.yml @@ -86,4 +86,3 @@ jobs: --body "Automated placeholder replacement, path renames, and self‑cleanup." \ --base main \ --head template-init \ - --label automation From 29f78ac3c0f5ce5982253e5541576ff54565d1e3 Mon Sep 17 00:00:00 2001 From: marksverdhei Date: Thu, 12 Jun 2025 19:08:32 +0200 Subject: [PATCH 07/11] Use ref instead of main --- .github/workflows/template-init.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/template-init.yml b/.github/workflows/template-init.yml index 21a4df2..4a4729d 100644 --- a/.github/workflows/template-init.yml +++ b/.github/workflows/template-init.yml @@ -84,5 +84,5 @@ jobs: gh pr create \ --title "Initialize project from template" \ --body "Automated placeholder replacement, path renames, and self‑cleanup." \ - --base main \ + --base ${{ github.ref }} \ --head template-init \ From 2b194f1b86e9805851b26d31134f7e2d0cc75368 Mon Sep 17 00:00:00 2001 From: marksverdhei Date: Thu, 12 Jun 2025 19:09:59 +0200 Subject: [PATCH 08/11] add git status for debugging --- .github/workflows/template-init.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/template-init.yml b/.github/workflows/template-init.yml index 4a4729d..f97b5cf 100644 --- a/.github/workflows/template-init.yml +++ b/.github/workflows/template-init.yml @@ -65,6 +65,7 @@ jobs: find . -depth -name '*python_template*' -print0 | while IFS= read -r -d '' f; do rename_path "$f" "${f//python_template/$repo_snake}"; done + git status - name: Remove this workflow so it never runs again run: git rm -f .github/workflows/template-init.yml From 5a325ed5c8856f4ed98ba6be298a0e355ee94a34 Mon Sep 17 00:00:00 2001 From: marksverdhei Date: Thu, 12 Jun 2025 19:17:33 +0200 Subject: [PATCH 09/11] identify gh actions --- .github/workflows/template-init.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/template-init.yml b/.github/workflows/template-init.yml index f97b5cf..3114ab4 100644 --- a/.github/workflows/template-init.yml +++ b/.github/workflows/template-init.yml @@ -65,13 +65,14 @@ jobs: find . -depth -name '*python_template*' -print0 | while IFS= read -r -d '' f; do rename_path "$f" "${f//python_template/$repo_snake}"; done - git status - 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" From 7c9bbb24bfa088b967c8048bf5b4d5538439749e Mon Sep 17 00:00:00 2001 From: marksverdhei Date: Thu, 12 Jun 2025 19:24:43 +0200 Subject: [PATCH 10/11] fix suffix and enable current repo check --- .github/workflows/template-init.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/template-init.yml b/.github/workflows/template-init.yml index 3114ab4..b459455 100644 --- a/.github/workflows/template-init.yml +++ b/.github/workflows/template-init.yml @@ -10,7 +10,7 @@ on: jobs: init: # Do not run in the original template repository itself - # if: github.event.repository.name != 'python-template' + if: github.event.repository.name != 'python-template' runs-on: ubuntu-latest permissions: contents: write @@ -25,8 +25,8 @@ jobs: - name: Derive project‑specific names id: names run: | - echo "repo_slug=${{ github.event.repository.name }}-test" >> "$GITHUB_OUTPUT" - snake=$(echo "${{ github.event.repository.name }}-test" | tr '-' '_') + echo "repo_slug=${{ github.event.repository.name }}" >> "$GITHUB_OUTPUT" + snake=$(echo "${{ github.event.repository.name }}" | tr '-' '_') echo "repo_snake=$snake" >> "$GITHUB_OUTPUT" - name: Create working branch From 1bcdae02d8e113dbee7abe68394c8d8085639b89 Mon Sep 17 00:00:00 2001 From: marksverdhei Date: Thu, 12 Jun 2025 19:50:57 +0200 Subject: [PATCH 11/11] remove branch trigger --- .github/workflows/template-init.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/template-init.yml b/.github/workflows/template-init.yml index b459455..ecd96ad 100644 --- a/.github/workflows/template-init.yml +++ b/.github/workflows/template-init.yml @@ -6,7 +6,6 @@ on: push: branches: - main # runs after the very first push that creates the repo - - add-init-workflow jobs: init: # Do not run in the original template repository itself