|
| 1 | +name: Auto-Update PR Base |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + |
| 8 | +jobs: |
| 9 | + auto-update-base: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - uses: actions/github-script@v7 |
| 13 | + with: |
| 14 | + github-token: ${{ secrets.ROBOT_TOKEN }} |
| 15 | + script: | |
| 16 | + let retriesNeeded = true; |
| 17 | + let attemptCount = 0; |
| 18 | + const maxAttempts = 10; // Set a limit for the maximum number of attempts |
| 19 | + let errorEncountered = false; // Track if any errors occur |
| 20 | +
|
| 21 | + while (retriesNeeded && attemptCount < maxAttempts) { |
| 22 | + retriesNeeded = false; // Assume no retries needed, unless 'unknown' state is found. |
| 23 | + attemptCount += 1; |
| 24 | +
|
| 25 | + try { |
| 26 | + const { data: pulls } = await github.rest.pulls.list({ |
| 27 | + owner: context.repo.owner, |
| 28 | + repo: context.repo.repo, |
| 29 | + state: "open", |
| 30 | + }); |
| 31 | +
|
| 32 | + for (const pull of pulls) { |
| 33 | + const hasAutoUpdateLabel = pull.labels.some(label => label.name === "auto-update-base"); |
| 34 | +
|
| 35 | + if (!hasAutoUpdateLabel) { |
| 36 | + continue; |
| 37 | + } |
| 38 | +
|
| 39 | + try { |
| 40 | + const { data: pr } = await github.rest.pulls.get({ |
| 41 | + owner: context.repo.owner, |
| 42 | + repo: context.repo.repo, |
| 43 | + pull_number: pull.number, |
| 44 | + }); |
| 45 | +
|
| 46 | + switch (pr.mergeable) { |
| 47 | + case null: |
| 48 | + console.log(`[INFO] PR #${pull.number}: "${pull.title}" - GitHub is still computing the mergeability. Will retry later.`); |
| 49 | + retriesNeeded = true; |
| 50 | + break; |
| 51 | + case false: |
| 52 | + console.log(`[INFO] PR #${pull.number}: "${pull.title}" - Skipping update due to merge conflict.`); |
| 53 | + break; |
| 54 | + case true: |
| 55 | + try { |
| 56 | + const { data: baseBranch } = await github.rest.repos.getBranch({ |
| 57 | + owner: context.repo.owner, |
| 58 | + repo: context.repo.repo, |
| 59 | + branch: pr.base.ref, |
| 60 | + }); |
| 61 | +
|
| 62 | + if (pr.base.sha === baseBranch.commit.sha) { |
| 63 | + console.log(`[INFO] PR #${pull.number}: "${pull.title}" - Base branch is already up-to-date.`); |
| 64 | + break; |
| 65 | + } |
| 66 | +
|
| 67 | + console.log(`[UPDATE] PR #${pull.number}: "${pull.title}" - Updating base branch...`); |
| 68 | + try { |
| 69 | + await github.rest.pulls.updateBranch({ |
| 70 | + owner: context.repo.owner, |
| 71 | + repo: context.repo.repo, |
| 72 | + pull_number: pull.number, |
| 73 | + }); |
| 74 | + console.log(`[SUCCESS] PR #${pull.number}: "${pull.title}" - Base branch updated.`); |
| 75 | + } catch (updateError) { |
| 76 | + console.error(`[ERROR] PR #${pull.number}: Failed to update base branch - ${updateError.message}`); |
| 77 | + errorEncountered = true; |
| 78 | + } |
| 79 | +
|
| 80 | + break; |
| 81 | + } catch (baseError) { |
| 82 | + console.error(`[ERROR] PR #${pull.number}: Failed to get base branch - ${baseError.message}`); |
| 83 | + errorEncountered = true; |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + } catch (prError) { |
| 88 | + console.error(`[ERROR] Failed to get PR #${pull.number} - ${prError.message}`); |
| 89 | + errorEncountered = true; |
| 90 | + } |
| 91 | + } |
| 92 | + } catch (error) { |
| 93 | + console.error(`[ERROR] Failed to list pull requests - ${error.message}`); |
| 94 | + errorEncountered = true; |
| 95 | + } |
| 96 | +
|
| 97 | + if (retriesNeeded && attemptCount < maxAttempts) { |
| 98 | + console.log(`[INFO] Retrying for PRs with 'unknown' mergeable state (Attempt ${attemptCount}/${maxAttempts})...`); |
| 99 | + await new Promise(resolve => setTimeout(resolve, 5000)); |
| 100 | + } |
| 101 | + } |
| 102 | +
|
| 103 | + if (attemptCount >= maxAttempts) { |
| 104 | + console.error(`[ERROR] Maximum attempts (${maxAttempts}) reached. Some PRs may still have 'unknown' mergeable state.`); |
| 105 | + errorEncountered = true; |
| 106 | + } |
| 107 | +
|
| 108 | + if (errorEncountered) { |
| 109 | + core.setFailed(`[ERROR] Some PRs failed to update base branch.`); |
| 110 | + } else { |
| 111 | + console.log(`[INFO] All PRs with 'auto-update-base' label have been processed.`); |
| 112 | + } |
0 commit comments