Skip to content

Commit 26e7f29

Browse files
authored
Merge pull request #213 from lcdsmao/feat-restore-checks-input
feat: restore checks input
2 parents dfff0d3 + 1d8635a commit 26e7f29

File tree

8 files changed

+60
-34
lines changed

8 files changed

+60
-34
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ jobs:
2222
- uses: ./
2323
with:
2424
token: ${{ secrets.ACTION_PAT }}
25+
requiredApprovals: 2
2526
requiredLabels: auto-merge
27+
requiredStatusChecks: |
28+
build
29+
test

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ Inspired by [Merge Queue feature of Mergify](https://mergify.io/features/merge-q
1616

1717
1. Enable `Allow auto-merge` in `Settings/Options`.
1818

19-
2. Create a branch protection rule in `Settings/Branches`
19+
2. (Optional) Create a branch protection rule in `Settings/Branches`.
2020

2121
Support checks:
2222

2323
- `Require approvals`
2424
- `Status checks that are required`
2525

26+
This feature requires personal access token that has enough permission.
27+
2628
3. Create a workflow file (`.github/workflow/update-branch.yaml`):
2729

2830
```yaml
@@ -48,12 +50,19 @@ jobs:
4850
steps:
4951
- uses: lcdsmao/update-branch@v3
5052
with:
51-
# Personal access token
52-
# https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
53-
token: ${{ secrets.MY_PERSONAL_ACCESS_TOKEN }}
53+
# Or use personal access token
54+
token: ${{ secrets.GITHUB_TOKEN }}
5455
# One of MERGE, SQUASH, REBASE (default: MERGE)
5556
autoMergeMethod: SQUASH
57+
# Required at least 2 approves (default: 0)
58+
requiredApprovals: 2
59+
# Ignore pull requests without these labels
5660
requiredLabels: auto-merge
61+
# Required these status checks success
62+
requiredStatusChecks: |
63+
build_pr
64+
WIP
5765
# Optional branch name pattern instead of main or master
66+
# Status checks will be used
5867
# protectedBranchNamePattern: trunk
5968
```

action.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@ branding:
77
inputs:
88
token:
99
required: true
10-
description: 'Require personal access token to access branch protection rules or trigger other workflows.'
10+
description: 'Github token or personal access token. Require personal access token to access branch protection rules or trigger other workflows.'
1111
autoMergeMethod:
1212
required: false
1313
description: 'Method to use when enable pull request auto merge.'
1414
default: 'MERGE'
1515
requiredLabels:
1616
required: false
1717
description: 'Labels need to be present for a pull request.'
18+
requiredApprovals:
19+
required: false
20+
description: 'Count of required approval'
21+
default: '0'
22+
requiredStatusChecks:
23+
required: false
24+
description: 'Multiple status checks required to be success'
25+
default: ''
1826
protectedBranchNamePattern:
1927
required: false
2028
description: 'The name pattern of the base branch that your pull request wants to merge into. Branch protection rule with the same name pattern must be existed. If not set, the default behavior is to find the name pattern of main or master.'

dist/index.js

Lines changed: 15 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@ import {isPendingMergePr, isStatusCheckPassPr, stringify} from './utils'
1515

1616
async function run(): Promise<void> {
1717
try {
18-
const protectedBranchNamePattern = core.getInput(
19-
'protectedBranchNamePattern'
20-
)
2118
const token = core.getInput('token')
2219
const autoMergeMethod = core.getInput('autoMergeMethod')
2320
const requiredLabels = core
2421
.getInput('requiredLabels')
2522
.split('\n')
2623
.filter(s => s !== '')
24+
const requiredApprovals = parseInt(core.getInput('requiredApprovals'))
25+
const requiredStatusChecks = core
26+
.getInput('requiredStatusChecks')
27+
.split('\n')
28+
.filter(s => s !== '')
29+
const protectedBranchNamePattern = core.getInput(
30+
'protectedBranchNamePattern'
31+
)
2732

2833
const octokit = github.getOctokit(token)
2934
const {owner, repo} = github.context.repo
@@ -33,21 +38,16 @@ async function run(): Promise<void> {
3338
ctx,
3439
protectedBranchNamePattern
3540
)
36-
if (!branchProtectionRule) {
37-
throw Error(
38-
`Not found branch protection rule with name pattern of ${
39-
protectedBranchNamePattern
40-
? protectedBranchNamePattern
41-
: 'main or master'
42-
}.`
43-
)
44-
}
4541

4642
const condition: Condition = {
47-
branchNamePattern: branchProtectionRule.pattern,
48-
requiredApprovals: branchProtectionRule.requiredApprovingReviewCount ?? 0,
49-
requiredStatusChecks:
50-
branchProtectionRule.requiredStatusCheckContexts ?? [],
43+
branchNamePattern: branchProtectionRule?.pattern,
44+
requiredApprovals:
45+
requiredApprovals ||
46+
(branchProtectionRule?.requiredApprovingReviewCount ?? 0),
47+
requiredStatusChecks: [
48+
...requiredStatusChecks,
49+
...(branchProtectionRule?.requiredStatusCheckContexts ?? [])
50+
],
5151
requiredLabels
5252
}
5353

src/type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface GhContext {
88
}
99

1010
export interface Condition {
11-
branchNamePattern: string
11+
branchNamePattern?: string
1212
requiredApprovals: number
1313
requiredStatusChecks: string[]
1414
requiredLabels: string[]

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function isSatisfyBasicConditionPr(
3838
pr.reviews.totalCount >= condition.requiredApprovals &&
3939
pr.reviewRequests.totalCount === 0 &&
4040
hasLabels(pr, condition) &&
41-
minimatch(pr.baseRefName, condition.branchNamePattern)
41+
minimatch(pr.baseRefName, condition.branchNamePattern ?? '*')
4242
)
4343
}
4444

0 commit comments

Comments
 (0)