Skip to content

Commit 12a2a92

Browse files
committed
fix(ci): implement unique branch strategy for pre-commit hooks workflow
Replace complex branch reuse logic with timestamped unique branches. Remove stash/pop operations that caused 'nothing to commit' issues. Simplify workflow by always creating fresh PRs instead of updating existing ones. Add timestamp suffix to branch names for guaranteed uniqueness. Eliminate Git conflicts from attempting to checkout existing branches with local changes. This resolves the workflow failures where existing branches already contained the same updates, resulting in empty commits and merge conflicts.
1 parent 0363242 commit 12a2a92

1 file changed

Lines changed: 33 additions & 94 deletions

File tree

.github/workflows/update-pre-commit-hooks.yml

Lines changed: 33 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,13 @@ jobs:
128128
exit 1
129129
fi
130130
131+
# Create unique branch name with timestamp to avoid conflicts
132+
TIMESTAMP=$(date -u +%Y%m%d-%H%M)
133+
UNIQUE_BRANCH_NAME="${BRANCH_NAME}-${TIMESTAMP}"
134+
131135
# Set as environment variables for all subsequent steps
132-
echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
136+
echo "BRANCH_NAME=$UNIQUE_BRANCH_NAME" >> $GITHUB_ENV
137+
echo "BASE_BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV
133138
echo "CONFIG_FILE=$CONFIG_FILE" >> $GITHUB_ENV
134139
echo "PR_LABELS=$PR_LABELS" >> $GITHUB_ENV
135140
echo "PR_ASSIGNEE=$PR_ASSIGNEE" >> $GITHUB_ENV
@@ -151,7 +156,8 @@ jobs:
151156
# Log configuration
152157
echo "🔍 Configuration loaded:"
153158
echo " 📁 Config file: $CONFIG_FILE"
154-
echo " 🌿 Update branch: $BRANCH_NAME"
159+
echo " 🌿 Base branch pattern: $BRANCH_NAME"
160+
echo " 🌿 Unique branch name: $UNIQUE_BRANCH_NAME"
155161
echo " 📁 Pip directory: $PIP_DIR"
156162
echo " 🔀 Create PR: ${{ env.CREATE_PR }}"
157163
echo " 🧪 Test hooks: ${{ env.TEST_HOOKS }}"
@@ -317,33 +323,26 @@ jobs:
317323
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
318324
319325
# ————————————————————————————————————————————————————————————————
320-
# Check if PR branch already exists
326+
# Check for existing PRs with base branch pattern
321327
# ————————————————————————————————————————————————————————————————
322-
- name: 🔍 Check for existing PR branch
328+
- name: 🔍 Check for existing PRs
323329
id: check_branch
324330
if: steps.check_changes.outputs.has_changes == 'true' && env.CREATE_PR == 'true'
325331
run: |
326-
echo "🔍 Checking for existing branch and PR..."
327-
328-
# Check if branch exists remotely
329-
if git ls-remote --exit-code --heads origin "${{ env.BRANCH_NAME }}" >/dev/null 2>&1; then
330-
echo "🌿 Branch ${{ env.BRANCH_NAME }} already exists remotely"
331-
echo "branch_exists=true" >> $GITHUB_OUTPUT
332-
333-
# Check if there's already an open PR
334-
pr_number=$(gh pr list --head "${{ env.BRANCH_NAME }}" --json number --jq '.[0].number // empty')
335-
if [ -n "$pr_number" ]; then
336-
echo "📋 Open PR already exists: #$pr_number"
337-
echo "pr_exists=true" >> $GITHUB_OUTPUT
338-
echo "pr_number=$pr_number" >> $GITHUB_OUTPUT
339-
else
340-
echo "🌿 Branch exists but no open PR found"
341-
echo "pr_exists=false" >> $GITHUB_OUTPUT
342-
fi
332+
echo "🔍 Checking for existing PRs with similar branch pattern..."
333+
334+
# Since we use unique timestamped branches, this branch is always new
335+
echo "🆕 Using unique branch: ${{ env.BRANCH_NAME }}"
336+
echo "branch_exists=false" >> $GITHUB_OUTPUT
337+
echo "pr_exists=false" >> $GITHUB_OUTPUT
338+
339+
# Check if there are other open PRs for pre-commit updates (for info only)
340+
existing_prs=$(gh pr list --head "${{ env.BASE_BRANCH_NAME }}*" --json number,title,headRefName --jq '.[] | select(.headRefName | startswith("${{ env.BASE_BRANCH_NAME }}")) | .number' || echo "")
341+
if [ -n "$existing_prs" ]; then
342+
echo "ℹ️ Found existing pre-commit update PRs: $existing_prs"
343+
echo "💡 These will remain open - consider closing them if this update supersedes them"
343344
else
344-
echo "🆕 Branch ${{ env.BRANCH_NAME }} does not exist"
345-
echo "branch_exists=false" >> $GITHUB_OUTPUT
346-
echo "pr_exists=false" >> $GITHUB_OUTPUT
345+
echo "✅ No existing pre-commit update PRs found"
347346
fi
348347
env:
349348
GH_TOKEN: ${{ secrets.GH_PAT_TOKEN || secrets.GITHUB_TOKEN }}
@@ -356,34 +355,9 @@ jobs:
356355
run: |
357356
echo "🌿 Preparing to commit changes..."
358357
359-
# Create or switch to the branch
360-
if [ "${{ steps.check_branch.outputs.branch_exists }}" = "true" ]; then
361-
echo "🔄 Switching to existing branch..."
362-
# Stash any local changes to avoid checkout conflicts
363-
if ! git diff --quiet || ! git diff --cached --quiet; then
364-
echo "💾 Stashing local changes..."
365-
git stash push -m "Pre-commit workflow: temporary stash for branch checkout"
366-
STASH_CREATED=true
367-
else
368-
STASH_CREATED=false
369-
fi
370-
371-
# Fetch and checkout the existing branch
372-
git fetch origin "${{ env.BRANCH_NAME }}"
373-
git checkout "${{ env.BRANCH_NAME }}"
374-
375-
# Apply stashed changes if we created a stash
376-
if [ "$STASH_CREATED" = "true" ]; then
377-
echo "📤 Applying stashed changes..."
378-
git stash pop || echo "⚠️ Stash pop failed - changes may need manual resolution"
379-
fi
380-
381-
# Merge with main to get latest changes
382-
git merge origin/main --no-edit || echo "⚠️ Merge conflicts may need manual resolution"
383-
else
384-
echo "🆕 Creating new branch..."
385-
git checkout -b "${{ env.BRANCH_NAME }}"
386-
fi
358+
# Create unique timestamped branch (always new)
359+
echo "🆕 Creating unique timestamped branch: ${{ env.BRANCH_NAME }}"
360+
git checkout -b "${{ env.BRANCH_NAME }}"
387361
388362
# Stage the changes
389363
git add "${{ env.CONFIG_FILE }}"
@@ -443,8 +417,8 @@ jobs:
443417
# ————————————————————————————————————————————————————————————————
444418
# Create a new pull request using GitHub CLI
445419
# ————————————————————————————————————————————————————————————————
446-
- name: 🔀 Create new pull request
447-
if: steps.check_changes.outputs.has_changes == 'true' && env.CREATE_PR == 'true' && steps.check_branch.outputs.pr_exists != 'true'
420+
- name: 🔀 Create pull request
421+
if: steps.check_changes.outputs.has_changes == 'true' && env.CREATE_PR == 'true'
448422
run: |
449423
echo "🔀 Creating new pull request..."
450424
@@ -481,6 +455,7 @@ jobs:
481455
2. **Check CI status** - automated tests will verify compatibility
482456
3. **Test locally** with \`pre-commit run --all-files\` if desired
483457
4. **Merge when ready** - no manual action needed unless issues arise
458+
5. **Close older pre-commit PRs** if this update supersedes them
484459
485460
### 🔐 Security Notes
486461
- All Python packages installed use hash verification (\`--require-hashes\`)
@@ -490,6 +465,7 @@ jobs:
490465
### 🤖 Automation Details
491466
- **Workflow:** \`${{ github.workflow }}\`
492467
- **Trigger:** ${{ github.event_name }}
468+
- **Branch:** \`${{ env.BRANCH_NAME }}\` (timestamped for uniqueness)
493469
- **Run ID:** [${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
494470
495471
---
@@ -537,34 +513,6 @@ jobs:
537513
env:
538514
GH_TOKEN: ${{ secrets.GH_PAT_TOKEN || secrets.GITHUB_TOKEN }}
539515

540-
# ————————————————————————————————————————————————————————————————
541-
# Update existing PR if changes detected
542-
# ————————————————————————————————————————————————————————————————
543-
- name: 🔄 Update existing PR
544-
if: steps.check_changes.outputs.has_changes == 'true' && env.CREATE_PR == 'true' && steps.check_branch.outputs.pr_exists == 'true'
545-
run: |
546-
pr_number="${{ steps.check_branch.outputs.pr_number }}"
547-
548-
echo "🔄 Updating existing PR #$pr_number with new changes..."
549-
550-
# Add comment about the update
551-
gh pr comment "$pr_number" --body "### 🔄 Pre-commit Hooks Updated Again
552-
553-
New changes detected and committed to this PR:
554-
- Updated on $(date -u '+%Y-%m-%d %H:%M:%S UTC')
555-
- Number of hooks updated: **${{ steps.analyze_changes.outputs.hooks_updated }}**
556-
- Run ID: [${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
557-
558-
### Latest Changes:
559-
\`\`\`diff
560-
$(git diff HEAD~1 -- "${{ env.CONFIG_FILE }}" | head -50)
561-
\`\`\`
562-
563-
The PR now contains the most recent hook versions available."
564-
565-
echo "✅ Existing PR #$pr_number updated with new changes"
566-
env:
567-
GH_TOKEN: ${{ secrets.GH_PAT_TOKEN || secrets.GITHUB_TOKEN }}
568516

569517
# ----------------------------------------------------------------------------------
570518
# Generate Workflow Summary Report
@@ -602,13 +550,8 @@ jobs:
602550
BRANCH_NAME=$(echo "$ENV_JSON" | jq -r '.UPDATE_PRE_COMMIT_HOOKS_BRANCH')
603551
604552
echo "| **Config File** | \`$CONFIG_FILE\` |" >> $GITHUB_STEP_SUMMARY
605-
echo "| **Branch** | \`$BRANCH_NAME\` |" >> $GITHUB_STEP_SUMMARY
606-
607-
if [ "${{ needs.update-hooks.outputs.pr_exists }}" = "true" ]; then
608-
echo "| **Action** | Updated existing PR #${{ needs.update-hooks.outputs.pr_number }} |" >> $GITHUB_STEP_SUMMARY
609-
else
610-
echo "| **Action** | Created new pull request |" >> $GITHUB_STEP_SUMMARY
611-
fi
553+
echo "| **Branch Pattern** | \`$BRANCH_NAME\` |" >> $GITHUB_STEP_SUMMARY
554+
echo "| **Action** | Created new timestamped pull request |" >> $GITHUB_STEP_SUMMARY
612555
else
613556
echo "## ℹ️ No Updates Available" >> $GITHUB_STEP_SUMMARY
614557
echo "" >> $GITHUB_STEP_SUMMARY
@@ -634,11 +577,7 @@ jobs:
634577
echo "=== 🪝 Pre-commit Hooks Update Summary ==="
635578
if [ "${{ needs.update-hooks.result }}" = "success" ]; then
636579
if [ "${{ needs.update-hooks.outputs.has_changes }}" = "true" ]; then
637-
if [ "${{ needs.update-hooks.outputs.pr_exists }}" = "true" ]; then
638-
echo "✅ Status: Updated existing PR #${{ needs.update-hooks.outputs.pr_number }}"
639-
else
640-
echo "✅ Status: Created new pull request"
641-
fi
580+
echo "✅ Status: Created new timestamped pull request"
642581
echo "📊 Hooks updated: ${{ needs.update-hooks.outputs.hooks_updated }}"
643582
else
644583
echo "ℹ️ Status: No changes detected - hooks are up to date"

0 commit comments

Comments
 (0)