|
8 | 8 |
|
9 | 9 | permissions: |
10 | 10 | contents: read |
| 11 | + issues: write |
11 | 12 | pull-requests: write |
12 | 13 |
|
13 | 14 | jobs: |
14 | 15 | conflict: |
15 | 16 | name: Check for merge conflicts |
16 | 17 | runs-on: ubuntu-latest |
17 | 18 | steps: |
18 | | - - uses: eps1lon/actions-label-merge-conflict@v3 |
| 19 | + - uses: actions/github-script@v8 |
19 | 20 | with: |
20 | | - dirtyLabel: "has-conflicts" |
21 | | - repoToken: ${{ secrets.GITHUB_TOKEN }} |
22 | | - commentOnDirty: | |
23 | | - This PR has merge conflicts with the target branch and cannot be merged until they are resolved. |
24 | | -
|
25 | | - Please rebase or merge the target branch into your branch and resolve the conflicts locally, then push the updated branch. |
26 | | - commentOnClean: | |
27 | | - Merge conflicts have been resolved. Thank you! |
| 21 | + script: | |
| 22 | + const dirtyLabel = "has-conflicts"; |
| 23 | + const commentOnDirty = `This PR has merge conflicts with the target branch and cannot be merged until they are resolved. |
| 24 | +
|
| 25 | + Please rebase or merge the target branch into your branch and resolve the conflicts locally, then push the updated branch.`; |
| 26 | + const commentOnClean = "Merge conflicts have been resolved. Thank you!"; |
| 27 | + const { owner, repo } = context.repo; |
| 28 | +
|
| 29 | + async function candidatePulls() { |
| 30 | + if (context.payload.pull_request) { |
| 31 | + return [context.payload.pull_request]; |
| 32 | + } |
| 33 | +
|
| 34 | + const base = context.ref.replace("refs/heads/", ""); |
| 35 | + return github.paginate(github.rest.pulls.list, { |
| 36 | + owner, |
| 37 | + repo, |
| 38 | + state: "open", |
| 39 | + base, |
| 40 | + per_page: 100, |
| 41 | + }); |
| 42 | + } |
| 43 | +
|
| 44 | + async function pullWithMergeableState(number) { |
| 45 | + for (let attempt = 0; attempt < 10; attempt += 1) { |
| 46 | + const { data: pull } = await github.rest.pulls.get({ |
| 47 | + owner, |
| 48 | + repo, |
| 49 | + pull_number: number, |
| 50 | + }); |
| 51 | +
|
| 52 | + if (pull.mergeable !== null && pull.mergeable_state !== "unknown") { |
| 53 | + return pull; |
| 54 | + } |
| 55 | +
|
| 56 | + await new Promise((resolve) => setTimeout(resolve, 3000)); |
| 57 | + } |
| 58 | +
|
| 59 | + core.warning(`Mergeability is still unknown for PR #${number}`); |
| 60 | + return null; |
| 61 | + } |
| 62 | +
|
| 63 | + async function updateConflictLabel(pull) { |
| 64 | + const current = await pullWithMergeableState(pull.number); |
| 65 | + if (current === null) { |
| 66 | + return; |
| 67 | + } |
| 68 | +
|
| 69 | + const labels = current.labels.map((label) => label.name); |
| 70 | + const hasDirtyLabel = labels.includes(dirtyLabel); |
| 71 | + const isDirty = current.mergeable_state === "dirty"; |
| 72 | +
|
| 73 | + if (isDirty && !hasDirtyLabel) { |
| 74 | + await github.rest.issues.addLabels({ |
| 75 | + owner, |
| 76 | + repo, |
| 77 | + issue_number: current.number, |
| 78 | + labels: [dirtyLabel], |
| 79 | + }); |
| 80 | + await github.rest.issues.createComment({ |
| 81 | + owner, |
| 82 | + repo, |
| 83 | + issue_number: current.number, |
| 84 | + body: commentOnDirty, |
| 85 | + }); |
| 86 | + } |
| 87 | +
|
| 88 | + if (!isDirty && hasDirtyLabel) { |
| 89 | + await github.rest.issues.removeLabel({ |
| 90 | + owner, |
| 91 | + repo, |
| 92 | + issue_number: current.number, |
| 93 | + name: dirtyLabel, |
| 94 | + }); |
| 95 | + await github.rest.issues.createComment({ |
| 96 | + owner, |
| 97 | + repo, |
| 98 | + issue_number: current.number, |
| 99 | + body: commentOnClean, |
| 100 | + }); |
| 101 | + } |
| 102 | + } |
| 103 | +
|
| 104 | + for (const pull of await candidatePulls()) { |
| 105 | + await updateConflictLabel(pull); |
| 106 | + } |
0 commit comments