-
-
Notifications
You must be signed in to change notification settings - Fork 8
75 lines (68 loc) · 2.67 KB
/
auto-merge.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
name: Auto-merge
on:
pull_request_target:
types: ready_for_review
workflow_call:
inputs:
pr:
description: "Pull Request number"
type: number
required: true
url:
description: "Pull Request URL (HTML, not API)"
type: string
required: false
permissions:
contents: write
pull-requests: write
jobs:
approve:
continue-on-error: true
runs-on: ubuntu-latest
if: ${{ github.actor_id == github.repository_owner_id || inputs.pr }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: count requested reviewers
id: reviewers
run: |
REVIEWERS_NUM=$(echo $REQUESTED_REVIEWERS | jq '. | length')
echo "value=$REVIEWERS_NUM" >> "$GITHUB_OUTPUT"
env:
REQUESTED_REVIEWERS: ${{ toJson(github.event.pull_request.requested_reviewers) }}
- name: Linked issues
id: issues
run: |
QUERY='query ($prNumber: Int!, $repositoryName: String!, $repositoryOwner: String!) {
repository(name: $repositoryName, owner: $repositoryOwner) {
pullRequest(number: $prNumber) {
closingIssuesReferences(first: 10) {
nodes {
number
}
}
}
}
}'
ISSUES=$(gh api graphql -f query="$QUERY" -F prNumber=${{ github.event.pull_request.number || inputs.pr }} -F repositoryName=${{ github.event.repository.name }} -F repositoryOwner=${{ github.event.repository.owner.login }} -q '.data.repository.pullRequest.closingIssuesReferences.nodes[] | .number')
echo "num=$ISSUES" >> "$GITHUB_OUTPUT"
if [ -z "$ISSUES" ]
then
echo "No issues linked to this PR."
echo "have=false" >> "$GITHUB_OUTPUT"
else
echo "Issue(s) linked: $ISSUES"
echo "have=true" >> "$GITHUB_OUTPUT"
fi
# better criteria is "is this PR have only one issue and it authored by author of this PR?"
- name: Approve
# if there is no requested reviewers specified and PR don't have any linked issue's:
if: success() && steps.reviewers.outputs.value == '0'
run: gh pr review --approve "${{ github.event.pull_request.html_url || inputs.url || inputs.pr }}"
- name: Auto-merge
# only if no linked issue:
if: success() && steps.reviewers.outputs.value == '0' && steps.issues.outputs.have != 'true'
run: >-
gh pr merge --auto
--${{ github.event.pull_request.commits > 1 && 'squash' || 'merge' }}
"${{ github.event.pull_request.html_url || inputs.url || inputs.pr }}"