Skip to content

Commit 10fd584

Browse files
authored
Safe template updating (#98)
GitHub intentionally prevents self-modifying CI pipelines, template repos silently injecting workflows, and supply-chain attacks via Actions. What this means is that any changes in the Az-RBSI ``.github/workflows/`` directory cannot be updated via the Template Sync action. Previously, the action would simply crash and not produce the desired PR in the target repository. This update removes any commit from Az-RBSI that was to be cherry-picked that contans updates to the ``.github/workflows/`` directory from the squash-commit PR. Thusly removed commits are noted in the PR for manual review. The upshot of all of this is that any PR or commit to ``main`` in Az-RBSI that contains updates to the ``.github/workflows/`` directory should contain ONLY updates to the ``.github/workflows/`` directory so as to not cause important Java code updates from being missed by the Template Sync action.
1 parent 9aaf72d commit 10fd584

File tree

1 file changed

+43
-26
lines changed

1 file changed

+43
-26
lines changed

.github/workflows/sync-template-updates.yaml

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -140,41 +140,56 @@ jobs:
140140
git fetch origin "$target_branch"
141141
git reset --hard origin/"$target_branch"
142142
143-
# Initialize commit summary
143+
# Initialize summaries
144144
commit_summary=""
145+
skipped_commits=""
145146
newest_commit=""
147+
applied_count=0
148+
146149
commit_list=$(echo "${{ steps.commits.outputs.new_commits }}" | tr -d '\r')
147150
148-
# Cherry-pick each commit safely
151+
# Cherry-pick each commit, skipping workflow changes
149152
while IFS= read -r commit_sha; do
150153
[ -z "$commit_sha" ] && continue
151154
152155
short_sha=$(git rev-parse --short=7 "$commit_sha")
153156
commit_msg=$(git log -1 --pretty=%B "$commit_sha")
154157
first_line=${commit_msg%%$'\n'*}
155158
156-
# Add only once to the summary
157-
commit_summary+="- $short_sha: $first_line"$'\n'
159+
# Skip commits that modify workflows
160+
if git diff-tree --no-commit-id --name-only -r "$commit_sha" | grep -q '^.github/workflows/'; then
161+
echo "Skipping workflow-modifying commit $short_sha"
162+
skipped_commits+="- $short_sha: $first_line"$'\n'
163+
continue
164+
fi
158165
159-
# Cherry-pick without committing yet
166+
# Apply commit
167+
commit_summary+="- $short_sha: $first_line"$'\n'
160168
git cherry-pick --no-commit "$commit_sha"
161169
newest_commit="$commit_sha"
170+
applied_count=$((applied_count + 1))
171+
162172
done <<< "$commit_list"
163173
164-
# Squash all cherry-picked commits into a single commit
174+
# If nothing was applied, exit cleanly
175+
if [ "$applied_count" -eq 0 ]; then
176+
echo "No applicable commits after filtering. Exiting."
177+
exit 0
178+
fi
179+
180+
# Squash all applied commits into one
165181
git commit -m "Template Sync Updates"$'\n\n'"$commit_summary"
166182
167-
# Update TEMPLATE_ORIGIN.txt (with new Recorded At)
168-
if [ -n "$newest_commit" ]; then
169-
{
170-
echo "Template: ${{ steps.template.outputs.template_repo }}"
171-
echo "Template Branch: ${{ steps.template.outputs.template_branch }}"
172-
echo "Template Commit: $newest_commit"
173-
echo "Recorded At (UTC): $timestamp"
174-
} > TEMPLATE_ORIGIN.txt
175-
git add TEMPLATE_ORIGIN.txt
176-
git commit --amend --no-edit
177-
fi
183+
# Update TEMPLATE_ORIGIN.txt
184+
{
185+
echo "Template: ${{ steps.template.outputs.template_repo }}"
186+
echo "Template Branch: ${{ steps.template.outputs.template_branch }}"
187+
echo "Template Commit: $newest_commit"
188+
echo "Recorded At (UTC): $timestamp"
189+
} > TEMPLATE_ORIGIN.txt
190+
191+
git add TEMPLATE_ORIGIN.txt
192+
git commit --amend --no-edit
178193
179194
# Push changes
180195
git push origin "$branch_name" -f
@@ -184,17 +199,19 @@ jobs:
184199
gh label create template-sync --color BC8F8F --description "Updates synced from template repository"
185200
fi
186201
187-
# Build PR title with date and non-merge commit count
188-
pr_count="${{ steps.commits.outputs.commit_count }}"
189-
[ -z "$pr_count" ] && pr_count=0
202+
# Build PR title
190203
plural="s"
191-
[ "$pr_count" -eq 1 ] && plural=""
192-
pr_title="Sync Template Updates ($pr_count commit$plural, $sync_date)"
204+
[ "$applied_count" -eq 1 ] && plural=""
205+
pr_title="Sync Template Updates ($applied_count commit$plural, $sync_date)"
193206
194-
# Build PR body with summary + source info
195-
pr_body=$(printf "Template Sync Commit Summary:\n\n%s\n_Synced from:_ [%s](https://github.com/%s/tree/%s) at commit \`%s\`\n\n_Last recorded at (UTC): %s_" \
196-
"$commit_summary" \
197-
"${{ steps.template.outputs.template_repo }}" \
207+
# Build PR body
208+
pr_body=$(printf "### Template Sync Commit Summary:\n\n%s\n" "$commit_summary")
209+
210+
if [ -n "$skipped_commits" ]; then
211+
pr_body+=$(printf "\n\n### ⚠️ Skipped workflow-related commits (require manual review):\n\n%s\n" "$skipped_commits")
212+
fi
213+
214+
pr_body+=$(printf "\n\n\n_Synced from:_ https://github.com/%s/tree/%s at commit \`%s\`\n\n_Last recorded at (UTC): %s_" \
198215
"${{ steps.template.outputs.template_repo }}" \
199216
"${{ steps.template.outputs.template_branch }}" \
200217
"$newest_commit" \

0 commit comments

Comments
 (0)