-
Notifications
You must be signed in to change notification settings - Fork 2
155 lines (140 loc) · 4.43 KB
/
automerge.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
name: automerge
on:
workflow_run:
workflows:
- CI
- Track approved PRs
types:
- completed
concurrency:
group: automerge-${{ github.event.workflow_run.event }}-${{ github.event.workflow_run.pull_requests[0].number || github.event.workflow_run.id }}
cancel-in-progress: true
env:
HOMEBREW_DEVELOPER: 1
HOMEBREW_NO_AUTO_UPDATE: 1
GH_REPO: ${{ github.repository }}
GH_NO_UPDATE_NOTIFIER: 1
GH_PROMPT_DISABLED: 1
jobs:
status-check:
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
if: >
github.event.workflow_run.conclusion == 'success' &&
(github.event.workflow_run.event == 'pull_request' ||
github.event.workflow_run.name != 'CI')
outputs:
pull-number: ${{ steps.pr.outputs.number }}
approved: ${{ steps.approval-status.outputs.approved }}
complete: ${{ steps.approval-status.outputs.complete }}
permissions:
contents: read
pull-requests: read
actions: read
checks: read
steps:
- name: Dump environment
run: |
printf '```\n'
env | tee -a "$GITHUB_STEP_SUMMARY"
printf '```\n'
- name: Dump payload
run: |
printf '```\n'
jq . "$GITHUB_EVENT_PATH" | tee -a "$GITHUB_STEP_SUMMARY"
printf '```\n'
- name: Download `pull-number` artifact
run: gh run download --name pull-number "$WORKFLOW_ID"
env:
WORKFLOW_ID: ${{ github.event.workflow_run.id }}
- run: echo "number=$(cat number)" >> "$GITHUB_OUTPUT"
id: pr
- name: Check PR labels
id: check-labels
env:
PR: ${{ steps.pr.outputs.number }}
run: |
publishable=yes
while IFS='' read -r label
do
if [[ "$label" = "do not merge" ]] ||
[[ "$label" = "new formula" ]] ||
[[ "$label" = "automerge-skip" ]] ||
[[ "$label" = "CI-published-bottle-commits" ]]
then
publishable=no
break
fi
done < <(
gh api \
--header 'Accept: application/vnd.github+json' \
--header 'X-GitHub-Api-Version: 2022-11-28' \
"repos/$GH_REPO/pulls/$PR" \
--jq '.labels[].name'
)
echo "publishable=$publishable" >> "$GITHUB_OUTPUT"
- name: Get approval and CI status
if: steps.check-labels.outputs.publishable == 'yes'
id: approval-status
env:
PR: ${{ steps.pr.outputs.number }}
run: |
attempt=0
max_attempts=10
timeout=10
approved=no
complete=no
while [[ "$attempt" -le "$max_attempts" ]]
do
while IFS='' read -r review
do
if [[ "$review" = "COMMENTED" ]]
then
approved=yes
break
fi
done < <(
[[ "$approved" = "no" ]] &&
gh api \
--header 'Accept: application/vnd.github+json' \
--header 'X-GitHub-Api-Version: 2022-11-28' \
"repos/$GH_REPO/pulls/$PR/reviews" \
--jq '.[].state'
)
if [[ "$complete" = "no" ]] && gh pr checks "$PR"
then
complete=yes
fi
if [[ "$approved" = "yes" ]] && [[ "$complete" = "yes" ]]
then
break
fi
echo "::notice ::PR #$PR not yet approved. Checking again in ${timeout}s..."
sleep "$timeout"
attempt=$(( attempt + 1 ))
timeout=$(( timeout * 2 ))
done
echo "approved=$approved" >> "$GITHUB_OUTPUT"
echo "complete=$complete" >> "$GITHUB_OUTPUT"
merge:
runs-on: ubuntu-latest
needs: status-check
if: needs.status-check.outputs.approved == 'yes' && needs.status-check.outputs.complete == 'yes'
container:
image: ghcr.io/homebrew/ubuntu22.04:master
permissions:
contents: read
pull-requests: read
checks: read
actions: write # to dispatch publish workflow
env:
HOMEBREW_GITHUB_API_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR: ${{ needs.status-check.outputs.pull-number }}
defaults:
run:
shell: bash
steps:
- name: Set up Homebrew
uses: Homebrew/actions/setup-homebrew@master
- run: echo brew pr-publish "$PR"