Skip to content

Commit 6e6c6ee

Browse files
authored
Merge pull request #35 from adRise/allow_ongoing_checks
Add the `allow_ongoing_checks` option to enable updates to PRs with ongoing checks when the action is triggered.
2 parents 5d27394 + c5bcec4 commit 6e6c6ee

File tree

5 files changed

+88
-16
lines changed

5 files changed

+88
-16
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,15 @@ We could retrieve this value from the repo settings through an API call but that
6060

6161
Default: true
6262

63-
The action will skip PRs that have failed checks.
63+
The action will skip updating PRs that have failed checks. Please note that if `allow_ongoing_checks` is set to `false`, the action will skip updating PRs with ongoing checks. This will result in the failure to update PR branches when the action is triggered while checks for those PRs are still in progress.
64+
65+
### `allow_ongoing_checks`
66+
67+
**Optional**
68+
69+
Default: false
70+
71+
The action will consider PRs that have ongoing checks. This is useful when the action is triggered while checks for some otherwise qualified PRs are still in progress. Note, this option works only when `require_passed_checks` is set to `true`.
6472

6573
### `sort`
6674

@@ -76,7 +84,6 @@ Notice: this is an option provided by github rest api. In this github action, we
7684

7785
The direction of the sort. Can be either `asc` or `desc`. Default: `desc` when sort is `created` or sort is not specified, otherwise `asc`.
7886

79-
8087
This github action doesn't set any default parameters.
8188

8289
### `require_auto_merge_enabled`
@@ -87,7 +94,6 @@ Check if having auto-merge enabled in the PR is required, in order for the PR to
8794
be considered. It defaults to `true`, but if set to `false`, all PRs are
8895
considered for update (not just those with auto-merge enabled).
8996

90-
9197
## Example usage
9298

9399
```yml
@@ -107,10 +113,11 @@ jobs:
107113
token: ${{ secrets.ACTION_USER_TOKEN }}
108114
base: 'master'
109115
required_approval_count: 2
110-
require_passed_checks: false
116+
require_passed_checks: true
117+
allow_ongoing_checks: true
111118
sort: 'created'
112119
direction: 'desc'
113-
require_auto_merge_enabled: false
120+
require_auto_merge_enabled: true
114121
```
115122
116123
Replace the `VERSION_YOU_WANT_TO_USE` with the actual version you want to use, check the version format [here](https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsuses)

action.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ inputs:
1717
default: '2'
1818
require_passed_checks:
1919
required: false
20-
description: 'If the action should skip PRs that have failed checks, defaults to `true`'
20+
description: 'If the action should skip PRs that have failed checks, defaults to `true`. Please note that if `allow_ongoing_checks` is not set to `true`, the action will skip pull requests with ongoing checks. This may result in the failure to update PR branches when the action is triggered while checks for those pull requests are still in progress.'
2121
default: 'true'
22+
allow_ongoing_checks:
23+
required: false
24+
description: 'If the action should consider PRs that have ongoing checks, defaults to `false`. The action will consider PRs that have ongoing checks. This is useful when the action is triggered while checks for some otherwise qualified PRs are still in progress. Note, this option works only when `require_passed_checks` is set to `true`.'
25+
default: 'false'
2226
require_auto_merge_enabled:
2327
required: false
2428
description: 'When set to false, the action includes PRs without auto-merge; the default true excludes such PRs.'

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
2-
"name": "update-pr-branch",
3-
"version": "0.1.2",
42
"description": "Automatically update PR branch",
53
"main": "dest/index.js",
64
"scripts": {

src/lib/github.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export const getMergeableStatus = async (pullNumber) => {
100100
/**
101101
* whether all checks passed
102102
*/
103-
export const areAllChecksPassed = async (sha) => {
103+
export const areAllChecksPassed = async (sha, allow_ongoing_checks) => {
104104
const octokit = getOctokit();
105105
const repo = github.context.repo;
106106
const {
@@ -110,11 +110,20 @@ export const areAllChecksPassed = async (sha) => {
110110
ref: sha,
111111
});
112112

113-
const hasUnfinishedOrFailedChecks = check_runs.some((item) => {
114-
return item.status !== 'completed' || item.conclusion === 'failure';
115-
});
113+
let hasOffensiveChecks = false;
114+
if (allow_ongoing_checks) {
115+
// check whether there are ongoing checks
116+
hasOffensiveChecks = check_runs.some((item) => {
117+
return item.conclusion === 'failure';
118+
});
119+
} else {
120+
// check whether there are unfinished or failed checks
121+
hasOffensiveChecks = check_runs.some((item) => {
122+
return item.status !== 'completed' || item.conclusion === 'failure';
123+
});
124+
}
116125

117-
return !hasUnfinishedOrFailedChecks;
126+
return !hasOffensiveChecks;
118127
};
119128

120129
/**
@@ -154,7 +163,9 @@ export const getApprovalStatus = async (pullNumber) => {
154163
};
155164

156165
export const filterApplicablePRs = (openPRs) => {
157-
const includeNonAutoMergePRs = isStringFalse(core.getInput('require_auto_merge_enabled'));
166+
const includeNonAutoMergePRs = isStringFalse(
167+
core.getInput('require_auto_merge_enabled'),
168+
);
158169
if (includeNonAutoMergePRs) {
159170
return openPRs;
160171
}
@@ -171,6 +182,9 @@ export const getAutoUpdateCandidate = async (openPRs) => {
171182
const requirePassedChecks = isStringTrue(
172183
core.getInput('require_passed_checks'),
173184
);
185+
const allowOngoingChecks = isStringTrue(
186+
core.getInput('allow_ongoing_checks'),
187+
);
174188
const applicablePRs = filterApplicablePRs(openPRs);
175189

176190
for (const pr of applicablePRs) {
@@ -216,9 +230,13 @@ export const getAutoUpdateCandidate = async (openPRs) => {
216230
* need to note: the mergeable, and mergeable_state don't reflect the checks status
217231
*/
218232
if (requirePassedChecks) {
219-
const didChecksPass = await areAllChecksPassed(sha);
233+
const didChecksPass = await areAllChecksPassed(sha, allowOngoingChecks);
234+
235+
const reasonType = allowOngoingChecks
236+
? 'failed check(s)'
237+
: 'failed or ongoing check(s)';
220238
if (!didChecksPass) {
221-
printFailReason(pullNumber, 'The PR has failed or ongoing check(s)');
239+
printFailReason(pullNumber, `The PR has ${reasonType}`);
222240
continue;
223241
}
224242
}

src/lib/github.test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,51 @@ describe('areAllChecksPassed()', () => {
271271
const result = await gitLib.areAllChecksPassed(sha);
272272
expect(result).toEqual(false);
273273
});
274+
275+
test('should return true if there is ongoing checks but no failed checks', async () => {
276+
// mock pending check
277+
const check = {
278+
...oldCheck,
279+
status: 'queued',
280+
conclusion: null,
281+
};
282+
mockedResponse.data.check_runs[0] = check;
283+
284+
const mockedMethod = jest.fn().mockResolvedValue(mockedResponse);
285+
286+
github.getOctokit.mockReturnValue({
287+
rest: { checks: { listForRef: mockedMethod } },
288+
});
289+
290+
const allowOngoingChecks = true;
291+
const result = await gitLib.areAllChecksPassed(sha, allowOngoingChecks);
292+
expect(result).toEqual(true);
293+
});
294+
295+
test('should return false if there is ongoing checks and failed checks', async () => {
296+
// mock pending check
297+
const check = {
298+
...oldCheck,
299+
status: 'queued',
300+
conclusion: null,
301+
};
302+
const failedCheck = {
303+
...oldCheck,
304+
conclusion: 'failure',
305+
};
306+
mockedResponse.data.check_runs[0] = check;
307+
mockedResponse.data.check_runs[1] = failedCheck;
308+
309+
const mockedMethod = jest.fn().mockResolvedValue(mockedResponse);
310+
311+
github.getOctokit.mockReturnValue({
312+
rest: { checks: { listForRef: mockedMethod } },
313+
});
314+
315+
const allowOngoingChecks = true;
316+
const result = await gitLib.areAllChecksPassed(sha, allowOngoingChecks);
317+
expect(result).toEqual(false);
318+
});
274319
});
275320

276321
describe('getApprovalStatus()', () => {

0 commit comments

Comments
 (0)