-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-pr-branch-aggregation.yml
More file actions
256 lines (253 loc) · 11.2 KB
/
Copy pathgithub-pr-branch-aggregation.yml
File metadata and controls
256 lines (253 loc) · 11.2 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
---
name: Aggregation of open pull request branches
on:
workflow_call:
inputs:
branch-prefix:
required: false
type: string
description: Prefix of the branches to merge (e.g., dependabot/terraform/)
default: null
delete-merged-branch:
required: false
type: boolean
description: >-
Deprecated compatibility input. Use delete-open-pr-branches to make
the destructive open PR branch deletion explicit.
default: false
delete-open-pr-branches:
required: false
type: boolean
description: >-
Delete same-repository open PR branches after they are merged into
the current pull request branch.
default: false
delete-branch-author-allowlist:
required: false
type: string
description: Comma-separated trusted PR authors whose branches may be deleted
default: github-actions[bot]
delete-branch-head-owner-allowlist:
required: false
type: string
description: >-
Optional comma-separated head repository owners whose branches may be
deleted. Defaults to the current repository owner when omitted.
default: null
runs-on:
required: false
type: string
description: Runner to use
default: ubuntu-slim
outputs:
merged-pr-branches-json:
description: JSON array of the merged branches
value: ${{ jobs.branch-list.outputs.pr_branches_json }}
merged-pr-branch-records-json:
description: JSON array of selected pull request branch records
value: ${{ jobs.branch-list.outputs.pr_branch_records_json }}
secrets:
GH_TOKEN:
required: false
description: GitHub token
permissions:
contents: read
pull-requests: read
defaults:
run:
shell: bash -euo pipefail {0}
working-directory: .
jobs:
branch-list:
if: >
github.event_name == 'pull_request'
&& inputs.branch-prefix != null
&& inputs.branch-prefix != ''
runs-on: ${{ inputs.runs-on }}
env:
BRANCH_PREFIX: ${{ inputs.branch-prefix }}
HEAD_REF: ${{ github.head_ref }}
GH_TOKEN: ${{ secrets.GH_TOKEN || secrets.GITHUB_TOKEN }} # zizmor: ignore[secrets-outside-env] caller-provided secret
outputs:
pr_branches_json: ${{ steps.list-open-prs.outputs.pr_branches_json }}
pr_branch_records_json: ${{ steps.list-open-prs.outputs.pr_branch_records_json }}
steps:
- name: Checkout repository
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
with:
fetch-depth: 1
persist-credentials: false
- name: List open pull request branches
id: list-open-prs
run: |
records_json="$(
gh pr list --state open \
--json author,headRefName,headRepositoryOwner,isCrossRepository,number \
| jq -cr \
--arg prefix "${BRANCH_PREFIX}" \
--arg head_ref "${HEAD_REF}" \
'[.[] | select(
.headRefName != $head_ref
and (.headRefName | startswith($prefix))
and .isCrossRepository == false
) | {
number,
branch: .headRefName,
headOwner: (.headRepositoryOwner.login // ""),
author: (.author.login // ""),
isCrossRepository
}]
| reverse'
)"
records_delimiter="pr_branch_records_json_$(uuidgen)"
branches_delimiter="pr_branches_json_$(uuidgen)"
{
echo "pr_branch_records_json<<${records_delimiter}"
jq -cr . <<< "${records_json}"
echo "${records_delimiter}"
echo "pr_branches_json<<${branches_delimiter}"
jq -cr '[.[].branch]' <<< "${records_json}"
echo "${branches_delimiter}"
} | tee -a "${GITHUB_OUTPUT}"
branch-merge:
if: >
needs.branch-list.result == 'success'
&& needs.branch-list.outputs.pr_branches_json != '[]'
needs:
- branch-list
permissions:
contents: write
pull-requests: read
runs-on: ${{ inputs.runs-on }}
env:
BRANCH_PREFIX: ${{ inputs.branch-prefix }}
BRANCH_RECORDS_JSON: ${{ needs.branch-list.outputs.pr_branch_records_json }}
HEAD_REF: ${{ github.head_ref }}
GH_TOKEN: ${{ secrets.GH_TOKEN || secrets.GITHUB_TOKEN }} # zizmor: ignore[secrets-outside-env] caller-provided secret
steps:
- name: Checkout repository
uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3
with:
fetch-depth: 0
persist-credentials: false
- name: Fetch the remote branches
run: |
validate_branch_record() {
local record="$1"
local branch head_owner is_cross_repository
branch="$(jq -r '.branch' <<< "${record}")"
head_owner="$(jq -r '.headOwner' <<< "${record}")"
is_cross_repository="$(jq -r '.isCrossRepository' <<< "${record}")"
repository_owner="${GITHUB_REPOSITORY%%/*}"
if [[ -z "${branch}" || -z "${BRANCH_PREFIX}" || "${branch}" != "${BRANCH_PREFIX}"* ]]; then
echo "::error::Refusing unexpected branch '${branch}' for prefix '${BRANCH_PREFIX}'" >&2
return 1
elif [[ "${branch}" == "${HEAD_REF}" ]]; then
echo "::error::Refusing to process the current pull request branch '${branch}'" >&2
return 1
elif [[ "${is_cross_repository}" == "true" || "${head_owner}" != "${repository_owner}" ]]; then
echo "::error::Refusing cross-repository branch '${branch}' owned by '${head_owner}'" >&2
return 1
fi
git check-ref-format "refs/heads/${branch}" >/dev/null
echo "${branch}"
}
while IFS= read -r record; do
branch="$(validate_branch_record "${record}")"
git -c "http.https://github.com/.extraheader=AUTHORIZATION: bearer ${GH_TOKEN}" \
fetch --no-tags origin "+refs/heads/${branch}:refs/remotes/origin/${branch}"
done < <(jq -c '.[]' <<< "${BRANCH_RECORDS_JSON}")
- name: Configure the Git user
run: |
git config --global user.name "${GITHUB_ACTOR}"
git config --global user.email "${GITHUB_ACTOR}@users.noreply.github.com"
- name: Merge the branches
run: |
while IFS= read -r branch; do
git merge --allow-unrelated-histories --no-edit "refs/remotes/origin/${branch}"
done < <(jq -r '.[].branch' <<< "${BRANCH_RECORDS_JSON}")
- name: Push the changes
run: |
git check-ref-format "refs/heads/${HEAD_REF}" >/dev/null
git -c "http.https://github.com/.extraheader=AUTHORIZATION: bearer ${GH_TOKEN}" \
push origin "HEAD:${HEAD_REF}"
- name: Delete aggregated open PR branches
if: inputs.delete-open-pr-branches || inputs.delete-merged-branch
env:
DELETE_BRANCH_AUTHOR_ALLOWLIST: ${{ inputs.delete-branch-author-allowlist }}
DELETE_BRANCH_HEAD_OWNER_ALLOWLIST: ${{ inputs.delete-branch-head-owner-allowlist }}
run: |
default_branch="$(gh repo view "${GITHUB_REPOSITORY}" --json defaultBranchRef --jq '.defaultBranchRef.name')"
repository_owner="${GITHUB_REPOSITORY%%/*}"
if [[ -z "${DELETE_BRANCH_AUTHOR_ALLOWLIST}" ]]; then
echo "::error::delete-branch-author-allowlist must not be empty"
exit 1
fi
if [[ -z "${DELETE_BRANCH_HEAD_OWNER_ALLOWLIST}" ]]; then
DELETE_BRANCH_HEAD_OWNER_ALLOWLIST="${repository_owner}"
fi
is_allowed() {
local value="$1"
local allowlist="$2"
local allowed_item
IFS=',' read -r -a allowed_items <<< "${allowlist}"
for allowed_item in "${allowed_items[@]}"; do
allowed_item="${allowed_item#"${allowed_item%%[![:space:]]*}"}"
allowed_item="${allowed_item%"${allowed_item##*[![:space:]]}"}"
if [[ "${value}" == "${allowed_item}" ]]; then
return 0
fi
done
return 1
}
while IFS= read -r record; do
branch="$(jq -r '.branch' <<< "${record}")"
pr_number="$(jq -r '.number' <<< "${record}")"
head_owner="$(jq -r '.headOwner' <<< "${record}")"
author="$(jq -r '.author' <<< "${record}")"
is_cross_repository="$(jq -r '.isCrossRepository' <<< "${record}")"
if [[ -z "${branch}" || -z "${BRANCH_PREFIX}" || "${branch}" != "${BRANCH_PREFIX}"* ]]; then
echo "::error::Refusing unexpected branch '${branch}' for prefix '${BRANCH_PREFIX}'"
exit 1
elif [[ "${branch}" == "${HEAD_REF}" || "${branch}" == "${default_branch}" ]]; then
echo "::error::Refusing to delete protected branch name '${branch}'"
exit 1
elif [[ "${is_cross_repository}" == "true" || "${head_owner}" != "${repository_owner}" ]]; then
echo "::error::Refusing to delete cross-repository branch '${branch}'"
exit 1
elif ! is_allowed "${author}" "${DELETE_BRANCH_AUTHOR_ALLOWLIST}"; then
echo "::error::Refusing to delete '${branch}' from untrusted PR author '${author}'"
exit 1
elif ! is_allowed "${head_owner}" "${DELETE_BRANCH_HEAD_OWNER_ALLOWLIST}"; then
echo "::error::Refusing to delete '${branch}' from untrusted head owner '${head_owner}'"
exit 1
fi
git check-ref-format "refs/heads/${branch}" >/dev/null
if ! git merge-base --is-ancestor "refs/remotes/origin/${branch}" HEAD; then
echo "::error::Refusing to delete '${branch}' because it is not merged into HEAD"
exit 1
fi
open_pr_count="$(gh pr list --state open \
--json headRefName,headRepositoryOwner,number \
| jq -r \
--arg branch "${branch}" \
--arg head_owner "${head_owner}" \
--argjson pr_number "${pr_number}" \
'[.[] | select(
.number != $pr_number
and .headRefName == $branch
and (.headRepositoryOwner.login // "") == $head_owner
)] | length')"
if [[ "${open_pr_count}" != "0" ]]; then
echo "::error::Refusing to delete '${branch}' because it is used by another open PR"
exit 1
fi
encoded_branch="$(jq -nr --arg branch "${branch}" '$branch | @uri')"
protected="$(gh api "repos/${GITHUB_REPOSITORY}/branches/${encoded_branch}" --jq '.protected')"
if [[ "${protected}" == "true" ]]; then
echo "::error::Refusing to delete protected branch '${branch}'"
exit 1
fi
git -c "http.https://github.com/.extraheader=AUTHORIZATION: bearer ${GH_TOKEN}" \
push origin --delete "${branch}"
done < <(jq -c '.[]' <<< "${BRANCH_RECORDS_JSON}")