Skip to content

Commit b2ce6a3

Browse files
committed
fix pr merge bot logic
1 parent d1557fd commit b2ce6a3

File tree

2 files changed

+52
-49
lines changed

2 files changed

+52
-49
lines changed

.github/workflows/pr-merge.yml

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,51 @@
1-
name: PR Merge Validator Bot
2-
3-
on:
4-
pull_request:
5-
types: [opened, synchronize, reopened]
6-
7-
permissions:
8-
contents: read
9-
pull-requests: write
10-
11-
jobs:
12-
validate-pr:
13-
runs-on: ubuntu-latest
14-
steps:
15-
- name: Check PR for merge readiness
16-
uses: actions/github-script@v7
17-
with:
18-
github-token: ${{ secrets.GITHUB_TOKEN }}
19-
script: |
20-
const pr = context.payload.pull_request;
21-
const mergeable = pr.mergeable;
22-
23-
if (mergeable === null) {
24-
throw new Error('Mergeability status is unknown. Please resolve merge conflicts.');
25-
}
26-
27-
if (mergeable === false) {
28-
throw new Error('PR has merge conflicts. Please resolve them before merging.');
29-
}
30-
31-
// Optionally, check for required checks or CI status here
32-
const status = await github.rest.checks.listForRef({
33-
owner: context.repo.owner,
34-
repo: context.repo.repo,
35-
ref: pr.head.sha
36-
});
37-
38-
const allChecksPassed = status.data.every(check => check.conclusion === 'success');
39-
if (!allChecksPassed) {
40-
throw new Error('Not all checks have passed. Please ensure all tests are successful.');
41-
}
42-
43-
// Success message with an emoji
44-
await github.rest.issues.createComment({
45-
owner: context.repo.owner,
46-
repo: context.repo.repo,
47-
issue_number: pr.number,
48-
body: '✅ All checks passed! This PR is ready to be merged! 🎉'
49-
});
1+
- name: Check PR for merge readiness
2+
uses: actions/github-script@v7
3+
with:
4+
github-token: ${{ secrets.GITHUB_TOKEN }}
5+
script: |
6+
const checkMergeability = async () => {
7+
let retries = 5;
8+
while (retries > 0) {
9+
const pr = await github.rest.pulls.get({
10+
owner: context.repo.owner,
11+
repo: context.repo.repo,
12+
pull_number: context.payload.pull_request.number,
13+
});
14+
const mergeable = pr.data.mergeable;
15+
16+
if (mergeable === null) {
17+
console.log('Mergeability status is unknown. Retrying...');
18+
await new Promise(resolve => setTimeout(resolve, 5000)); // Wait 5 seconds
19+
retries--;
20+
continue;
21+
}
22+
23+
if (mergeable === false) {
24+
throw new Error('PR has merge conflicts. Please resolve them before merging.');
25+
}
26+
27+
return true; // Mergeable
28+
}
29+
throw new Error('Mergeability status could not be determined after multiple retries.');
30+
};
31+
32+
await checkMergeability();
33+
34+
const status = await github.rest.checks.listForRef({
35+
owner: context.repo.owner,
36+
repo: context.repo.repo,
37+
ref: context.payload.pull_request.head.sha,
38+
});
39+
40+
const allChecksPassed = status.data.check_runs.every(check => check.conclusion === 'success');
41+
if (!allChecksPassed) {
42+
throw new Error('Not all checks have passed. Please ensure all tests are successful.');
43+
}
44+
45+
// Success message with an emoji
46+
await github.rest.issues.createComment({
47+
owner: context.repo.owner,
48+
repo: context.repo.repo,
49+
issue_number: context.payload.pull_request.number,
50+
body: '✅ All checks passed! This PR is ready to be merged! 🎉'
51+
});

.github/workflows/welcome.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99
permissions:
1010
contents: read
1111
issues: write
12+
pull-requests: write
1213

1314

1415
jobs:

0 commit comments

Comments
 (0)