|
| 1 | +name: Auto-fix pnpm-lock.yaml Conflicts |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + paths: |
| 7 | + - 'pnpm-lock.yaml' |
| 8 | + - 'package.json' |
| 9 | + - '**/package.json' |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | +jobs: |
| 16 | + autofix-lockfile: |
| 17 | + name: Auto-fix pnpm-lock.yaml |
| 18 | + runs-on: ubuntu-latest |
| 19 | + |
| 20 | + # Only run on PRs from the same repository (not forks) for security |
| 21 | + if: github.event.pull_request.head.repo.full_name == github.repository |
| 22 | + |
| 23 | + steps: |
| 24 | + - name: Checkout PR branch |
| 25 | + uses: actions/checkout@v4 |
| 26 | + with: |
| 27 | + ref: ${{ github.event.pull_request.head.ref }} |
| 28 | + fetch-depth: 0 |
| 29 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 30 | + |
| 31 | + - name: Configure Git |
| 32 | + run: | |
| 33 | + git config user.name "github-actions[bot]" |
| 34 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 35 | + |
| 36 | + - name: Check for merge conflicts |
| 37 | + id: check_conflicts |
| 38 | + run: | |
| 39 | + # Fetch the base branch |
| 40 | + git fetch origin ${{ github.event.pull_request.base.ref }} |
| 41 | + |
| 42 | + # Try to merge base into current branch |
| 43 | + if git merge --no-commit --no-ff origin/${{ github.event.pull_request.base.ref }} 2>&1 | tee merge_output.txt; then |
| 44 | + echo "has_conflicts=false" >> $GITHUB_OUTPUT |
| 45 | + git merge --abort 2>/dev/null || true |
| 46 | + else |
| 47 | + # Check if conflicts exist |
| 48 | + if grep -q "CONFLICT" merge_output.txt; then |
| 49 | + echo "has_conflicts=true" >> $GITHUB_OUTPUT |
| 50 | + |
| 51 | + # Check if pnpm-lock.yaml has conflicts |
| 52 | + if git status | grep -q "pnpm-lock.yaml"; then |
| 53 | + echo "lockfile_conflict=true" >> $GITHUB_OUTPUT |
| 54 | + else |
| 55 | + echo "lockfile_conflict=false" >> $GITHUB_OUTPUT |
| 56 | + fi |
| 57 | + |
| 58 | + git merge --abort 2>/dev/null || true |
| 59 | + else |
| 60 | + echo "has_conflicts=false" >> $GITHUB_OUTPUT |
| 61 | + fi |
| 62 | + fi |
| 63 | + |
| 64 | + - name: Setup pnpm |
| 65 | + if: steps.check_conflicts.outputs.lockfile_conflict == 'true' |
| 66 | + uses: pnpm/action-setup@v4 |
| 67 | + with: |
| 68 | + version: 10 |
| 69 | + |
| 70 | + - name: Setup Node.js |
| 71 | + if: steps.check_conflicts.outputs.lockfile_conflict == 'true' |
| 72 | + uses: actions/setup-node@v4 |
| 73 | + with: |
| 74 | + node-version: '20.x' |
| 75 | + cache: 'pnpm' |
| 76 | + |
| 77 | + - name: Resolve lockfile conflicts |
| 78 | + if: steps.check_conflicts.outputs.lockfile_conflict == 'true' |
| 79 | + id: resolve |
| 80 | + run: | |
| 81 | + echo "🔧 Attempting to resolve pnpm-lock.yaml conflicts..." |
| 82 | + |
| 83 | + # Merge the base branch |
| 84 | + git merge origin/${{ github.event.pull_request.base.ref }} || true |
| 85 | + |
| 86 | + # Check if only pnpm-lock.yaml has conflicts |
| 87 | + CONFLICT_FILES=$(git diff --name-only --diff-filter=U) |
| 88 | + echo "Conflicted files: $CONFLICT_FILES" |
| 89 | + |
| 90 | + if [ "$CONFLICT_FILES" = "pnpm-lock.yaml" ] || [ -z "$CONFLICT_FILES" ]; then |
| 91 | + echo "Only pnpm-lock.yaml has conflicts or no conflicts. Regenerating lockfile..." |
| 92 | + |
| 93 | + # Remove the conflicted lockfile |
| 94 | + git checkout --theirs pnpm-lock.yaml || git checkout --ours pnpm-lock.yaml || rm -f pnpm-lock.yaml |
| 95 | + |
| 96 | + # Regenerate the lockfile |
| 97 | + pnpm install --no-frozen-lockfile |
| 98 | + |
| 99 | + # Check if lockfile was modified |
| 100 | + if git diff --quiet pnpm-lock.yaml; then |
| 101 | + echo "resolved=false" >> $GITHUB_OUTPUT |
| 102 | + echo "reason=no_changes" >> $GITHUB_OUTPUT |
| 103 | + else |
| 104 | + # Stage and commit the resolved lockfile |
| 105 | + git add pnpm-lock.yaml |
| 106 | + git commit -m "chore: auto-resolve pnpm-lock.yaml conflicts" \ |
| 107 | + -m "" \ |
| 108 | + -m "Automatically regenerated pnpm-lock.yaml to resolve merge conflicts." \ |
| 109 | + -m "" \ |
| 110 | + -m "Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>" |
| 111 | + |
| 112 | + git push origin ${{ github.event.pull_request.head.ref }} |
| 113 | + |
| 114 | + echo "resolved=true" >> $GITHUB_OUTPUT |
| 115 | + echo "✅ Successfully resolved and pushed pnpm-lock.yaml" |
| 116 | + fi |
| 117 | + else |
| 118 | + echo "⚠️ Multiple files have conflicts. Manual resolution required." |
| 119 | + echo "Conflicted files:" |
| 120 | + echo "$CONFLICT_FILES" |
| 121 | + echo "resolved=false" >> $GITHUB_OUTPUT |
| 122 | + echo "reason=multiple_conflicts" >> $GITHUB_OUTPUT |
| 123 | + fi |
| 124 | + |
| 125 | + - name: Comment on PR - Success |
| 126 | + if: steps.resolve.outputs.resolved == 'true' |
| 127 | + uses: actions/github-script@v7 |
| 128 | + with: |
| 129 | + script: | |
| 130 | + const message = `🤖 **Auto-fix Applied** |
| 131 | +
|
| 132 | + ✅ Successfully resolved \`pnpm-lock.yaml\` merge conflicts! |
| 133 | +
|
| 134 | + The lockfile has been automatically regenerated and committed to this PR. |
| 135 | + Please review the changes and re-run any necessary checks. |
| 136 | +
|
| 137 | + <details> |
| 138 | + <summary>What happened?</summary> |
| 139 | +
|
| 140 | + When merging branches, \`pnpm-lock.yaml\` conflicts are common because multiple branches may have updated dependencies independently. This automation detected the conflict and regenerated the lockfile by running \`pnpm install\`, which merges the dependency changes from both branches. |
| 141 | +
|
| 142 | + </details>`; |
| 143 | + |
| 144 | + await github.rest.issues.createComment({ |
| 145 | + issue_number: context.issue.number, |
| 146 | + owner: context.repo.owner, |
| 147 | + repo: context.repo.repo, |
| 148 | + body: message |
| 149 | + }); |
| 150 | + |
| 151 | + - name: Comment on PR - Multiple Conflicts |
| 152 | + if: steps.resolve.outputs.reason == 'multiple_conflicts' |
| 153 | + uses: actions/github-script@v7 |
| 154 | + with: |
| 155 | + script: | |
| 156 | + const message = `🤖 **Auto-fix Skipped** |
| 157 | +
|
| 158 | + ⚠️ This PR has merge conflicts in multiple files, not just \`pnpm-lock.yaml\`. |
| 159 | +
|
| 160 | + **Action Required**: Please resolve the conflicts manually: |
| 161 | +
|
| 162 | + \`\`\`bash |
| 163 | + # Merge the base branch into your branch |
| 164 | + git fetch origin |
| 165 | + git merge origin/${{ github.event.pull_request.base.ref }} |
| 166 | +
|
| 167 | + # Resolve conflicts in other files first |
| 168 | + # Then regenerate pnpm-lock.yaml |
| 169 | + pnpm install --no-frozen-lockfile |
| 170 | +
|
| 171 | + # Commit and push |
| 172 | + git add . |
| 173 | + git commit -m "chore: resolve merge conflicts" |
| 174 | + git push |
| 175 | + \`\`\``; |
| 176 | + |
| 177 | + await github.rest.issues.createComment({ |
| 178 | + issue_number: context.issue.number, |
| 179 | + owner: context.repo.owner, |
| 180 | + repo: context.repo.repo, |
| 181 | + body: message |
| 182 | + }); |
0 commit comments