Repository navigation
249 lines (219 loc) · 12 KB
/
Copy pathsync.yml
File metadata and controls
249 lines (219 loc) · 12 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
name: Sync PRs
on:
push:
branches: [master, develop]
paths:
- ".github/**"
- "src/**"
- "tests/**"
- "Directory.Build.props"
- "global.json"
- "LocalTelemetry.sln"
- "setup.iss"
pull_request_target:
types: [opened, synchronize, reopened]
pull_request_review:
types: [submitted]
workflow_run:
workflows: ["CI"]
types: [completed]
branches-ignore: [master]
workflow_dispatch:
permissions:
contents: read
pull-requests: read
issues: read
jobs:
sync:
name: Sync & Merge
runs-on: ubuntu-latest
if: >-
github.event_name == 'push' ||
github.event_name == 'workflow_dispatch' ||
github.event_name == 'pull_request_target' ||
(github.event_name == 'pull_request_review' &&
github.event.review.state == 'approved') ||
(github.event_name == 'workflow_run' &&
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.pull_requests[0] != null)
steps:
- name: Generate App Token
id: app_token
uses: actions/create-github-app-token@f418cff399f7aef4c2f0ad47e25ca7a6abbdd6fc # v3.4.0
with:
client-id: ${{ secrets.APP_CLIENT_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
- name: Sync, Normalize & Merge PRs
env:
GH_TOKEN: ${{ steps.app_token.outputs.token }}
REPO: ${{ github.repository }}
EVENT_NAME: ${{ github.event_name }}
REF_NAME: ${{ github.ref_name }}
PR_EVENT_NUM: ${{ github.event.pull_request.number }}
WORKFLOW_PR_NUM: ${{ github.event.workflow_run.pull_requests[0].number }}
run: |
clean_pr_title() {
local title="$1"
if [[ "$title" =~ ^([a-zA-Z0-9_()-]+:\ )([Bb]ump the ([a-zA-Z0-9_-]+)-deps group.*)$ ]]; then
echo "${BASH_REMATCH[1]}bump ${BASH_REMATCH[3]} dependencies"
else
echo "$title" | sed -E \
-e 's/ in \/[^ ]+//g' \
-e 's/ in the [^ ]+ group//g' \
-e 's/ across [0-9]+ (directories|directory)//g' \
-e 's/ with [0-9]+ updates?//g'
fi
}
normalize_title() {
local pr_num="$1"
local current_title="$2"
local cleaned
cleaned=$(clean_pr_title "$current_title")
if [ "$cleaned" != "$current_title" ]; then
echo "Updating PR #$pr_num title to '$cleaned'..." >&2
gh api --method PATCH "repos/$REPO/pulls/$pr_num" -f title="$cleaned" || true
fi
echo "$cleaned"
}
update_pr_branch() {
local pr_num="$1"
echo "Updating PR #$pr_num onto base branch..."
gh api --method PUT "repos/$REPO/pulls/$pr_num/update-branch" -f update_method=rebase --silent || \
gh api --method PUT "repos/$REPO/pulls/$pr_num/update-branch" --silent || true
}
sanitize_dependabot_body() {
local pr_num="$1"
local author="$2"
local title="$3"
if [ "$author" != "dependabot" ] && [ "$author" != "dependabot[bot]" ] && [ "$author" != "app/dependabot" ]; then
return 0
fi
local current_body clean_body
current_body=$(gh pr view "$pr_num" --repo "$REPO" --json body --jq '.body')
clean_body=$(CURRENT_BODY="$current_body" CLEAN_TITLE="$title" node -e '
const body = process.env.CURRENT_BODY || "";
const title = process.env.CLEAN_TITLE || "";
const updateRe = /(?:Updates|Updated|Bumps)\s+(?:\[([^\]]+)\]\([^)]+\)|`([^`]+)`|(\S+))\s+from\s+`?([^\s`]+)`?\s+to\s+`?([^\s`]+)`?/g;
const pinRe = /Pinned\s+(?:\[([^\]]+)\]\([^)]+\)|`([^`]+)`|(\S+))\s+at\s+`?([^\s`]+)`?/g;
const lines = [];
const seen = new Set();
for (const m of body.matchAll(updateRe)) {
const pkg = m[1] || m[2] || m[3];
const oldVer = m[4].replace(/\.+$/, "");
const newVer = m[5].replace(/\.+$/, "");
const key = `update:${pkg}:${oldVer}:${newVer}`;
if (!seen.has(key)) {
seen.add(key);
lines.push(`Updates \`${pkg}\` from \`${oldVer}\` to \`${newVer}\`.`);
}
}
for (const m of body.matchAll(pinRe)) {
const pkg = m[1] || m[2] || m[3];
const ver = m[4].replace(/\.+$/, "");
const key = `pin:${pkg}:${ver}`;
if (!seen.has(key)) {
seen.add(key);
lines.push(`Pins \`${pkg}\` at \`${ver}\`.`);
}
}
if (lines.length === 1) {
console.log(lines[0]);
} else if (lines.length > 1) {
console.log(lines.map(l => `- ${l}`).join("\n"));
} else {
console.log(`Updates dependencies (${title}).`);
}
')
if [ -n "$clean_body" ] && [ "$clean_body" != "$current_body" ]; then
echo "Simplifying Dependabot PR #$pr_num description..." >&2
gh api --method PATCH "repos/$REPO/pulls/$pr_num" -f body="$clean_body" || true
fi
}
try_merge_approved_pr() {
local pr_num="$1"
local clean_title="$2"
local merge_state="$3"
local head_ref="$4"
local base_ref="$5"
local review_decision="$6"
if [ "$head_ref" = "develop" ] && [ "$base_ref" = "master" ]; then
echo "Skipping auto-merge for release PR #$pr_num ($head_ref -> $base_ref)."
return 0
fi
if [ "$review_decision" != "APPROVED" ]; then
echo "PR #$pr_num is not approved by a code owner yet (reviewDecision: $review_decision)."
return 0
fi
echo "PR #$pr_num is approved (mergeStateStatus: $merge_state)."
if [ "$merge_state" = "BEHIND" ]; then
update_pr_branch "$pr_num"
gh pr merge "$pr_num" --repo "$REPO" --auto --squash --subject "$clean_title" --body "" || \
echo "Auto-merge enable deferred for PR #$pr_num after branch update."
return 0
fi
if [ "$merge_state" = "CLEAN" ] || { [ "$merge_state" != "DIRTY" ] && [ "$merge_state" != "BLOCKED" ] && gh pr checks "$pr_num" --repo "$REPO" --required; }; then
echo "PR #$pr_num approved and required checks passed. Merging now (squash)..."
gh pr merge "$pr_num" --repo "$REPO" --squash --subject "$clean_title" --body "" || \
echo "Direct merge failed for PR #$pr_num; check branch protection rules."
else
echo "PR #$pr_num approved (state: $merge_state). Enabling auto-merge (squash)..."
gh pr merge "$pr_num" --repo "$REPO" --auto --squash --subject "$clean_title" --body "" || \
echo "Could not enable auto-merge for PR #$pr_num."
fi
}
if [ "$EVENT_NAME" = "push" ] || [ "$EVENT_NAME" = "workflow_dispatch" ]; then
base_branch="$REF_NAME"
if [ -z "$base_branch" ] || [ "$base_branch" = "HEAD" ]; then
base_branch="develop"
fi
echo "Checking open PRs targeting $base_branch..."
prs=$(gh pr list --repo "$REPO" --base "$base_branch" --state open --json number,author,title,baseRefName,headRefName,mergeStateStatus,reviewDecision --jq '.[]')
if [ -z "$prs" ]; then
echo "No open pull requests found."
exit 0
fi
echo "$prs" | jq -c '.' | while read -r pr; do
pr_num=$(echo "$pr" | jq -r '.number')
pr_author=$(echo "$pr" | jq -r '.author.login')
pr_title=$(echo "$pr" | jq -r '.title')
base_ref=$(echo "$pr" | jq -r '.baseRefName')
head_ref=$(echo "$pr" | jq -r '.headRefName')
merge_state=$(echo "$pr" | jq -r '.mergeStateStatus')
review_decision=$(echo "$pr" | jq -r '.reviewDecision')
clean_title=$(normalize_title "$pr_num" "$pr_title" | tail -n 1)
sanitize_dependabot_body "$pr_num" "$pr_author" "$clean_title"
if [ "$merge_state" = "BEHIND" ] || [ "$merge_state" = "DIRTY" ]; then
update_pr_branch "$pr_num"
else
try_merge_approved_pr "$pr_num" "$clean_title" "$merge_state" "$head_ref" "$base_ref" "$review_decision"
fi
done
exit 0
fi
pr_num="$PR_EVENT_NUM"
if [ "$EVENT_NAME" = "workflow_run" ]; then
pr_num="$WORKFLOW_PR_NUM"
fi
if [ -z "$pr_num" ] || [ "$pr_num" = "null" ]; then
echo "No associated pull request found."
exit 0
fi
pr_json=$(gh pr view "$pr_num" --repo "$REPO" --json state,title,author,baseRefName,headRefName,mergeStateStatus,reviewDecision)
pr_state=$(echo "$pr_json" | jq -r '.state')
if [ "$pr_state" != "OPEN" ]; then
echo "PR #$pr_num is not open (state: $pr_state). Skipping."
exit 0
fi
pr_title=$(echo "$pr_json" | jq -r '.title')
pr_author=$(echo "$pr_json" | jq -r '.author.login')
base_ref=$(echo "$pr_json" | jq -r '.baseRefName')
head_ref=$(echo "$pr_json" | jq -r '.headRefName')
merge_state=$(echo "$pr_json" | jq -r '.mergeStateStatus')
review_decision=$(echo "$pr_json" | jq -r '.reviewDecision')
clean_title=$(normalize_title "$pr_num" "$pr_title" | tail -n 1)
sanitize_dependabot_body "$pr_num" "$pr_author" "$clean_title"
if [ "$EVENT_NAME" = "pull_request_target" ]; then
exit 0
fi
try_merge_approved_pr "$pr_num" "$clean_title" "$merge_state" "$head_ref" "$base_ref" "$review_decision"