Skip to content

Commit 897743b

Browse files
committed
Add Chat-GPT created template-sync files
deleted: .github/workflows/init.yaml new file: .github/workflows/record_template_origin.yaml new file: .github/workflows/sync-template-updates.yaml modified: .github/workflows/template-sync.yml
1 parent f953a6d commit 897743b

4 files changed

Lines changed: 278 additions & 29 deletions

File tree

.github/workflows/init.yaml

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: Record Template Origin
2+
3+
on:
4+
push:
5+
# Run only on the default branch after creation (auto-detected)
6+
# No need to hardcode 'main' or 'master'
7+
branches:
8+
- '**'
9+
10+
permissions:
11+
contents: write
12+
13+
jobs:
14+
record-origin:
15+
runs-on: ubuntu-latest
16+
if: github.run_number == 1 # Run only on first push (template creation event)
17+
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 0
23+
24+
- name: Install GitHub CLI
25+
run: sudo apt-get install -y gh
26+
27+
- name: Authenticate GitHub CLI
28+
env:
29+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
30+
run: gh auth login --with-token <<< "$GITHUB_TOKEN"
31+
32+
- name: Detect this repository's default branch
33+
id: branch
34+
env:
35+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
36+
run: |
37+
default_branch=$(gh repo view "${{ github.repository }}" --json defaultBranchRef --jq '.defaultBranchRef.name')
38+
echo "Default branch detected: $default_branch"
39+
echo "default_branch=$default_branch" >> $GITHUB_OUTPUT
40+
41+
- name: Get template repository info
42+
id: template
43+
env:
44+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
45+
run: |
46+
template_repo="${{ github.event.repository.template_repository.full_name }}"
47+
48+
if [ -z "$template_repo" ]; then
49+
echo "⚠️ No template repository information available — this repo may not have been created from a template."
50+
exit 0
51+
fi
52+
53+
echo "Template repository: $template_repo"
54+
55+
# Get the template's default branch
56+
template_default_branch=$(gh repo view "$template_repo" --json defaultBranchRef --jq '.defaultBranchRef.name')
57+
echo "Template default branch: $template_default_branch"
58+
59+
# Get latest commit SHA from that branch
60+
latest_commit=$(gh api repos/$template_repo/commits/$template_default_branch --jq '.sha')
61+
echo "Latest template commit: $latest_commit"
62+
63+
echo "template_repo=$template_repo" >> $GITHUB_OUTPUT
64+
echo "template_branch=$template_default_branch" >> $GITHUB_OUTPUT
65+
echo "template_commit=$latest_commit" >> $GITHUB_OUTPUT
66+
67+
- name: Record template origin file
68+
run: |
69+
echo "Template: ${{ steps.template.outputs.template_repo }}" > TEMPLATE_ORIGIN.txt
70+
echo "Template Branch: ${{ steps.template.outputs.template_branch }}" >> TEMPLATE_ORIGIN.txt
71+
echo "Template Commit: ${{ steps.template.outputs.template_commit }}" >> TEMPLATE_ORIGIN.txt
72+
echo "Recorded template origin:"
73+
cat TEMPLATE_ORIGIN.txt
74+
75+
- name: Commit and push template origin record
76+
env:
77+
BRANCH_NAME: ${{ steps.branch.outputs.default_branch }}
78+
run: |
79+
git config user.name "github-actions[bot]"
80+
git config user.email "github-actions[bot]@users.noreply.github.com"
81+
git add TEMPLATE_ORIGIN.txt
82+
git commit -m "Record template origin commit" || echo "No changes to commit"
83+
git push origin "HEAD:$BRANCH_NAME"
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
name: Sync Template Updates
2+
3+
on:
4+
# cronjob trigger
5+
# schedule:
6+
- cron: "30 04 * * 1"
7+
# manual trigger
8+
workflow_dispatch:
9+
10+
permissions:
11+
contents: write
12+
pull-requests: write
13+
14+
jobs:
15+
sync-template:
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Checkout target repository
20+
uses: actions/checkout@v4
21+
with:
22+
fetch-depth: 0
23+
24+
- name: Parse TEMPLATE_ORIGIN.txt
25+
id: origin
26+
run: |
27+
if [ ! -f TEMPLATE_ORIGIN.txt ]; then
28+
echo "❌ TEMPLATE_ORIGIN.txt not found. Cannot determine template origin."
29+
exit 1
30+
fi
31+
32+
template_repo=$(grep '^Template:' TEMPLATE_ORIGIN.txt | awk '{print $2}')
33+
template_branch=$(grep '^Template Branch:' TEMPLATE_ORIGIN.txt | awk '{print $3}')
34+
template_commit=$(grep '^Template Commit:' TEMPLATE_ORIGIN.txt | awk '{print $3}')
35+
36+
echo "Template repo: $template_repo"
37+
echo "Template branch: $template_branch"
38+
echo "Template commit: $template_commit"
39+
40+
echo "template_repo=$template_repo" >> $GITHUB_OUTPUT
41+
echo "template_branch=$template_branch" >> $GITHUB_OUTPUT
42+
echo "template_commit=$template_commit" >> $GITHUB_OUTPUT
43+
44+
- name: Fetch template repository
45+
run: |
46+
git remote add template https://github.com/${{ steps.origin.outputs.template_repo }}.git
47+
git fetch template ${{ steps.origin.outputs.template_branch }}
48+
49+
- name: Determine new commits
50+
id: commits
51+
run: |
52+
set -e
53+
git log template/${{ steps.origin.outputs.template_branch }} --pretty=format:"%H" > all_commits.txt
54+
commits_to_apply=$(awk -v last="${{ steps.origin.outputs.template_commit }}" 'BEGIN{found=0}{if($1==last){found=1;next} if(found==1) print $1}' all_commits.txt | tac)
55+
if [ -z "$commits_to_apply" ]; then
56+
echo "No new commits to apply."
57+
echo "has_updates=false" >> $GITHUB_OUTPUT
58+
else
59+
echo "$commits_to_apply" > commits_to_apply.txt
60+
echo "has_updates=true" >> $GITHUB_OUTPUT
61+
fi
62+
63+
- name: Stop if no updates
64+
if: steps.commits.outputs.has_updates == 'false'
65+
run: echo "No new commits to cherry-pick."
66+
67+
- name: Find existing template-sync PR
68+
if: steps.commits.outputs.has_updates == 'true'
69+
id: existing_pr
70+
env:
71+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
72+
REPO: ${{ github.repository }}
73+
run: |
74+
gh auth login --with-token <<< "$GITHUB_TOKEN"
75+
pr_number=$(gh pr list --repo "$REPO" --label "template-sync" --state open --json number --jq '.[0].number')
76+
if [ -n "$pr_number" ]; then
77+
echo "Found existing PR: #$pr_number"
78+
branch=$(gh pr view "$pr_number" --repo "$REPO" --json headRefName --jq '.headRefName')
79+
echo "branch_name=$branch" >> $GITHUB_OUTPUT
80+
echo "existing_pr_number=$pr_number" >> $GITHUB_OUTPUT
81+
echo "has_existing_pr=true" >> $GITHUB_OUTPUT
82+
else
83+
branch="sync-template-$(date +%Y%m%d-%H%M%S)"
84+
echo "branch_name=$branch" >> $GITHUB_OUTPUT
85+
echo "has_existing_pr=false" >> $GITHUB_OUTPUT
86+
fi
87+
88+
- name: Create or checkout sync branch
89+
if: steps.commits.outputs.has_updates == 'true'
90+
run: |
91+
branch="${{ steps.existing_pr.outputs.branch_name }}"
92+
git fetch origin "$branch" || true
93+
git checkout -B "$branch" origin/"$branch" || git checkout -b "$branch"
94+
95+
- name: Cherry-pick new commits
96+
if: steps.commits.outputs.has_updates == 'true'
97+
run: |
98+
set -e
99+
while read c; do
100+
echo "Cherry-picking $c"
101+
git cherry-pick $c || (git cherry-pick --abort && echo "Cherry-pick conflict. Please resolve manually." && exit 1)
102+
done < commits_to_apply.txt
103+
104+
- name: Update Template Commit in TEMPLATE_ORIGIN.txt
105+
if: steps.commits.outputs.has_updates == 'true'
106+
run: |
107+
latest=$(git rev-parse template/${{ steps.origin.outputs.template_branch }})
108+
echo "Updating Template Commit to $latest"
109+
sed -i "s|^Template Commit: .*|Template Commit: $latest|" TEMPLATE_ORIGIN.txt
110+
111+
git config user.name "github-actions[bot]"
112+
git config user.email "github-actions[bot]@users.noreply.github.com"
113+
git add TEMPLATE_ORIGIN.txt
114+
git commit -m "Update Template Commit to $latest" || echo "No changes to commit"
115+
116+
- name: Push updated branch
117+
if: steps.commits.outputs.has_updates == 'true'
118+
run: git push origin HEAD:${{ steps.existing_pr.outputs.branch_name }}
119+
120+
- name: Ensure template-sync label exists
121+
if: steps.commits.outputs.has_updates == 'true'
122+
env:
123+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
124+
REPO: ${{ github.repository }}
125+
run: |
126+
gh auth login --with-token <<< "$GITHUB_TOKEN"
127+
if ! gh label list --repo "$REPO" | grep -q '^template-sync'; then
128+
echo "Creating 'template-sync' label..."
129+
gh label create template-sync \
130+
--repo "$REPO" \
131+
--description "Pull requests that synchronize changes from the template repository" \
132+
--color "0E8A16" || true
133+
else
134+
echo "'template-sync' label already exists."
135+
fi
136+
137+
- name: Generate commit summary
138+
if: steps.commits.outputs.has_updates == 'true'
139+
id: summary
140+
run: |
141+
echo "### Template commits since last sync" > commit_summary.md
142+
echo "" >> commit_summary.md
143+
git log ${{ steps.origin.outputs.template_commit }}..template/${{ steps.origin.outputs.template_branch }} --pretty=format:"- %h %s (%an)" >> commit_summary.md
144+
echo "" >> commit_summary.md
145+
echo "commit_summary<<EOF" >> $GITHUB_OUTPUT
146+
cat commit_summary.md >> $GITHUB_OUTPUT
147+
echo "EOF" >> $GITHUB_OUTPUT
148+
149+
- name: Create new PR (if none exists)
150+
if: steps.commits.outputs.has_updates == 'true' && steps.existing_pr.outputs.has_existing_pr == 'false'
151+
uses: peter-evans/create-pull-request@v6
152+
with:
153+
token: ${{ secrets.GITHUB_TOKEN }}
154+
branch: ${{ steps.existing_pr.outputs.branch_name }}
155+
title: "Sync template updates"
156+
body: |
157+
This pull request applies new commits from the template repository.
158+
159+
- **Template:** `${{ steps.origin.outputs.template_repo }}`
160+
- **Branch:** `${{ steps.origin.outputs.template_branch }}`
161+
- **Base commit:** `${{ steps.origin.outputs.template_commit }}`
162+
163+
---
164+
${{ steps.summary.outputs.commit_summary }}
165+
---
166+
167+
⚠️ Resolve any conflicts manually before merging.
168+
commit-message: "Sync template updates"
169+
labels: template-sync
170+
171+
- name: Update existing PR body (if already open)
172+
if: steps.commits.outputs.has_updates == 'true' && steps.existing_pr.outputs.has_existing_pr == 'true'
173+
env:
174+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
175+
REPO: ${{ github.repository }}
176+
run: |
177+
pr_number=${{ steps.existing_pr.outputs.existing_pr_number }}
178+
echo "Updating PR #$pr_number..."
179+
bodyfile="updated_body.md"
180+
cat <<EOF > "$bodyfile"
181+
This pull request applies new commits from the template repository.
182+
183+
- **Template:** \`${{ steps.origin.outputs.template_repo }}\`
184+
- **Branch:** \`${{ steps.origin.outputs.template_branch }}\`
185+
- **Base commit:** \`${{ steps.origin.outputs.template_commit }}\`
186+
187+
---
188+
${{ steps.summary.outputs.commit_summary }}
189+
---
190+
191+
⚠️ Resolve any conflicts manually before merging.
192+
EOF
193+
gh pr edit "$pr_number" --repo "$REPO" --body-file "$bodyfile"

.github/workflows/template-sync.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ name: "Sync with Az-RBSI Template"
33

44
on:
55
# cronjob trigger
6-
schedule:
7-
- cron: "30 04 * * 1"
6+
# schedule:
7+
# - cron: "30 04 * * 1"
88
# manual trigger
99
workflow_dispatch:
1010
jobs:

0 commit comments

Comments
 (0)