Skip to content

Commit 0837127

Browse files
committed
fix: pre-commit workflow
1 parent ac09320 commit 0837127

File tree

3 files changed

+51
-84
lines changed

3 files changed

+51
-84
lines changed

.github/.env.shared

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ PIP_DIRECTORY=.github/pip # Directory
8484
UPDATE_PYTHON_DEPENDENCIES_SCHEDULE_UPDATE_MAIN=true # Default: Update main requirements on scheduled runs
8585
UPDATE_PYTHON_DEPENDENCIES_SCHEDULE_UPDATE_PIP_TOOLS=true # Default: Update pip-tools requirements on scheduled runs
8686
UPDATE_PYTHON_DEPENDENCIES_SCHEDULE_CREATE_PR=true # Default: Create PR on scheduled runs
87-
UPDATE_PYTHON_DEPENDENCIES_PR_LABELS=dependencies,chore,automated,python # Labels to apply to PRs (comma-separated)
87+
UPDATE_PYTHON_DEPENDENCIES_PR_LABELS=dependencies,chore # Labels to apply to PRs (comma-separated)
8888
UPDATE_PYTHON_DEPENDENCIES_PR_ASSIGNEE=mrz1836 # Default assignee for PRs
8989

9090
# ───────────────────────────────────────────────────────────────────────────────
@@ -93,7 +93,7 @@ UPDATE_PYTHON_DEPENDENCIES_PR_ASSIGNEE=mrz1836 # Default a
9393
UPDATE_PRE_COMMIT_HOOKS_BRANCH=chore/update-pre-commit-hooks # Branch name for pre-commit update PRs
9494
PRE_COMMIT_CONFIG_FILE=.pre-commit-config.yaml # Path to the pre-commit config file
9595
UPDATE_PRE_COMMIT_HOOKS_SCHEDULE_CREATE_PR=true # Default: Create PR on scheduled runs
96-
UPDATE_PRE_COMMIT_HOOKS_PR_LABELS=dependencies,chore,automated,pre-commit # Labels to apply to PRs (comma-separated)
96+
UPDATE_PRE_COMMIT_HOOKS_PR_LABELS=dependencies,chore # Labels to apply to PRs (comma-separated)
9797
UPDATE_PRE_COMMIT_HOOKS_PR_ASSIGNEE=mrz1836 # Default assignee for PRs
9898
UPDATE_PRE_COMMIT_HOOKS_TEST_ON_UPDATE=true # Default: Test hooks after update
9999

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

Lines changed: 46 additions & 79 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 }}"
@@ -254,6 +260,10 @@ jobs:
254260
echo "📦 Running pre-commit autoupdate --freeze..."
255261
pre-commit autoupdate --freeze
256262
263+
# Fix YAML formatting - ensure single space before frozen comments
264+
echo "🔧 Fixing YAML formatting for prettier compliance..."
265+
sed -i 's/ # frozen:/ # frozen:/g' "${{ env.CONFIG_FILE }}"
266+
257267
# Migrate config if needed
258268
echo "🔧 Running config migration..."
259269
pre-commit migrate-config
@@ -317,33 +327,26 @@ jobs:
317327
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
318328
319329
# ————————————————————————————————————————————————————————————————
320-
# Check if PR branch already exists
330+
# Check for existing PRs with base branch pattern
321331
# ————————————————————————————————————————————————————————————————
322-
- name: 🔍 Check for existing PR branch
332+
- name: 🔍 Check for existing PRs
323333
id: check_branch
324334
if: steps.check_changes.outputs.has_changes == 'true' && env.CREATE_PR == 'true'
325335
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
336+
echo "🔍 Checking for existing PRs with similar branch pattern..."
337+
338+
# Since we use unique timestamped branches, this branch is always new
339+
echo "🆕 Using unique branch: ${{ env.BRANCH_NAME }}"
340+
echo "branch_exists=false" >> $GITHUB_OUTPUT
341+
echo "pr_exists=false" >> $GITHUB_OUTPUT
342+
343+
# Check if there are other open PRs for pre-commit updates (for info only)
344+
existing_prs=$(gh pr list --head "${{ env.BASE_BRANCH_NAME }}*" --json number,title,headRefName --jq '.[] | select(.headRefName | startswith("${{ env.BASE_BRANCH_NAME }}")) | .number' || echo "")
345+
if [ -n "$existing_prs" ]; then
346+
echo "ℹ️ Found existing pre-commit update PRs: $existing_prs"
347+
echo "💡 These will remain open - consider closing them if this update supersedes them"
343348
else
344-
echo "🆕 Branch ${{ env.BRANCH_NAME }} does not exist"
345-
echo "branch_exists=false" >> $GITHUB_OUTPUT
346-
echo "pr_exists=false" >> $GITHUB_OUTPUT
349+
echo "✅ No existing pre-commit update PRs found"
347350
fi
348351
env:
349352
GH_TOKEN: ${{ secrets.GH_PAT_TOKEN || secrets.GITHUB_TOKEN }}
@@ -356,16 +359,9 @@ jobs:
356359
run: |
357360
echo "🌿 Preparing to commit changes..."
358361
359-
# Create or switch to the branch
360-
if [ "${{ steps.check_branch.outputs.branch_exists }}" = "true" ]; then
361-
echo "🔄 Switching to existing branch..."
362-
git fetch origin "${{ env.BRANCH_NAME }}"
363-
git checkout "${{ env.BRANCH_NAME }}"
364-
git merge origin/main --no-edit || echo "⚠️ Merge conflicts may need manual resolution"
365-
else
366-
echo "🆕 Creating new branch..."
367-
git checkout -b "${{ env.BRANCH_NAME }}"
368-
fi
362+
# Create unique timestamped branch (always new)
363+
echo "🆕 Creating unique timestamped branch: ${{ env.BRANCH_NAME }}"
364+
git checkout -b "${{ env.BRANCH_NAME }}"
369365
370366
# Stage the changes
371367
git add "${{ env.CONFIG_FILE }}"
@@ -425,11 +421,18 @@ jobs:
425421
# ————————————————————————————————————————————————————————————————
426422
# Create a new pull request using GitHub CLI
427423
# ————————————————————————————————————————————————————————————————
428-
- name: 🔀 Create new pull request
429-
if: steps.check_changes.outputs.has_changes == 'true' && env.CREATE_PR == 'true' && steps.check_branch.outputs.pr_exists != 'true'
424+
- name: 🔀 Create pull request
425+
if: steps.check_changes.outputs.has_changes == 'true' && env.CREATE_PR == 'true'
430426
run: |
431427
echo "🔀 Creating new pull request..."
432428
429+
# Verify we have commits to create PR with
430+
if ! git log --oneline origin/master..HEAD | grep -q .; then
431+
echo "❌ No commits found between origin/master and HEAD"
432+
echo "This might indicate the branch was created but no changes were committed"
433+
exit 1
434+
fi
435+
433436
# Generate detailed PR body
434437
pr_body="## 🤖 Automated Pre-commit Hooks Update
435438
@@ -463,6 +466,7 @@ jobs:
463466
2. **Check CI status** - automated tests will verify compatibility
464467
3. **Test locally** with \`pre-commit run --all-files\` if desired
465468
4. **Merge when ready** - no manual action needed unless issues arise
469+
5. **Close older pre-commit PRs** if this update supersedes them
466470
467471
### 🔐 Security Notes
468472
- All Python packages installed use hash verification (\`--require-hashes\`)
@@ -472,6 +476,7 @@ jobs:
472476
### 🤖 Automation Details
473477
- **Workflow:** \`${{ github.workflow }}\`
474478
- **Trigger:** ${{ github.event_name }}
479+
- **Branch:** \`${{ env.BRANCH_NAME }}\` (timestamped for uniqueness)
475480
- **Run ID:** [${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
476481
477482
---
@@ -486,7 +491,7 @@ jobs:
486491
--title "chore(deps): update pre-commit hooks" \
487492
--body "$pr_body" \
488493
--head "${{ env.BRANCH_NAME }}" \
489-
--base main \
494+
--base master \
490495
--label "${{ env.PR_LABELS }}" \
491496
--assignee "${{ env.PR_ASSIGNEE }}")
492497
@@ -496,7 +501,7 @@ jobs:
496501
gh pr comment "$pr_url" --body "### 📊 Configuration Changes
497502
498503
\`\`\`diff
499-
$(git diff origin/main...HEAD -- "${{ env.CONFIG_FILE }}" | head -100)
504+
$(git diff origin/master...HEAD -- "${{ env.CONFIG_FILE }}" | head -100)
500505
\`\`\`
501506
502507
### 💡 Tips for Testing
@@ -519,35 +524,6 @@ jobs:
519524
env:
520525
GH_TOKEN: ${{ secrets.GH_PAT_TOKEN || secrets.GITHUB_TOKEN }}
521526

522-
# ————————————————————————————————————————————————————————————————
523-
# Update existing PR if changes detected
524-
# ————————————————————————————————————————————————————————————————
525-
- name: 🔄 Update existing PR
526-
if: steps.check_changes.outputs.has_changes == 'true' && env.CREATE_PR == 'true' && steps.check_branch.outputs.pr_exists == 'true'
527-
run: |
528-
pr_number="${{ steps.check_branch.outputs.pr_number }}"
529-
530-
echo "🔄 Updating existing PR #$pr_number with new changes..."
531-
532-
# Add comment about the update
533-
gh pr comment "$pr_number" --body "### 🔄 Pre-commit Hooks Updated Again
534-
535-
New changes detected and committed to this PR:
536-
- Updated on $(date -u '+%Y-%m-%d %H:%M:%S UTC')
537-
- Number of hooks updated: **${{ steps.analyze_changes.outputs.hooks_updated }}**
538-
- Run ID: [${{ github.run_id }}](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})
539-
540-
### Latest Changes:
541-
\`\`\`diff
542-
$(git diff HEAD~1 -- "${{ env.CONFIG_FILE }}" | head -50)
543-
\`\`\`
544-
545-
The PR now contains the most recent hook versions available."
546-
547-
echo "✅ Existing PR #$pr_number updated with new changes"
548-
env:
549-
GH_TOKEN: ${{ secrets.GH_PAT_TOKEN || secrets.GITHUB_TOKEN }}
550-
551527
# ----------------------------------------------------------------------------------
552528
# Generate Workflow Summary Report
553529
# ----------------------------------------------------------------------------------
@@ -584,13 +560,8 @@ jobs:
584560
BRANCH_NAME=$(echo "$ENV_JSON" | jq -r '.UPDATE_PRE_COMMIT_HOOKS_BRANCH')
585561
586562
echo "| **Config File** | \`$CONFIG_FILE\` |" >> $GITHUB_STEP_SUMMARY
587-
echo "| **Branch** | \`$BRANCH_NAME\` |" >> $GITHUB_STEP_SUMMARY
588-
589-
if [ "${{ needs.update-hooks.outputs.pr_exists }}" = "true" ]; then
590-
echo "| **Action** | Updated existing PR #${{ needs.update-hooks.outputs.pr_number }} |" >> $GITHUB_STEP_SUMMARY
591-
else
592-
echo "| **Action** | Created new pull request |" >> $GITHUB_STEP_SUMMARY
593-
fi
563+
echo "| **Branch Pattern** | \`$BRANCH_NAME\` |" >> $GITHUB_STEP_SUMMARY
564+
echo "| **Action** | Created new timestamped pull request |" >> $GITHUB_STEP_SUMMARY
594565
else
595566
echo "## ℹ️ No Updates Available" >> $GITHUB_STEP_SUMMARY
596567
echo "" >> $GITHUB_STEP_SUMMARY
@@ -616,11 +587,7 @@ jobs:
616587
echo "=== 🪝 Pre-commit Hooks Update Summary ==="
617588
if [ "${{ needs.update-hooks.result }}" = "success" ]; then
618589
if [ "${{ needs.update-hooks.outputs.has_changes }}" = "true" ]; then
619-
if [ "${{ needs.update-hooks.outputs.pr_exists }}" = "true" ]; then
620-
echo "✅ Status: Updated existing PR #${{ needs.update-hooks.outputs.pr_number }}"
621-
else
622-
echo "✅ Status: Created new pull request"
623-
fi
590+
echo "✅ Status: Created new timestamped pull request"
624591
echo "📊 Hooks updated: ${{ needs.update-hooks.outputs.hooks_updated }}"
625592
else
626593
echo "ℹ️ Status: No changes detected - hooks are up to date"

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ repos:
3737

3838
# ------------------------ Security scans ---------------------------
3939
- repo: https://github.com/zricethezav/gitleaks
40-
rev: 47218a628da59ef6b24197d15f4b0248ca87f4f0 # frozen: v8.27.2
40+
rev: 39fdb480a06768cc41a84ef86959c07ff33091c4 # frozen: v8.28.0
4141
hooks:
4242
- id: gitleaks
4343
stages: [pre-commit, pre-push]
@@ -60,8 +60,8 @@ repos:
6060
types: [go, markdown]
6161
pass_filenames: true
6262
additional_dependencies:
63-
- regex==2025.6
64-
- ruamel.yaml==0.18
63+
- regex==2024.11.6
64+
- ruamel.yaml==0.18.18
6565

6666
# --------------------- Commit‑msg validation -----------------------
6767
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook

0 commit comments

Comments
 (0)