Repository navigation
472 lines (427 loc) · 20.8 KB
/
Copy pathpublish-keystatic.yml
File metadata and controls
472 lines (427 loc) · 20.8 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
name: Publish Keystatic Changes
on:
workflow_run:
workflows: ["Test website build"]
types: [completed]
pull_request_review:
types: [submitted]
permissions:
checks: read
contents: write
issues: write
pull-requests: write
concurrency:
group: keystatic-publisher-${{ github.event.workflow_run.head_branch || github.event.pull_request.head.ref }}
cancel-in-progress: false
jobs:
publish:
if: >-
github.event_name == 'workflow_run' &&
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.event == 'push' &&
startsWith(github.event.workflow_run.head_branch, 'keystatic/') &&
github.event.workflow_run.head_repository.full_name == github.repository
runs-on: ubuntu-latest
environment: editorial-publish
env:
REPOSITORY: ${{ github.repository }}
BASE_BRANCH: ${{ github.event.repository.default_branch }}
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
steps:
- name: Create repository-scoped publisher token (optional App)
id: app-token
if: vars.PREFERREDAI_AUTOMATION_APP_ID != ''
uses: actions/create-github-app-token@v3
with:
client-id: ${{ vars.PREFERREDAI_AUTOMATION_APP_ID }}
private-key: ${{ secrets.PREFERREDAI_AUTOMATION_APP_PRIVATE_KEY }}
owner: PreferredAI
repositories: PreferredAI.github.io
permission-contents: write
permission-issues: write
permission-pull-requests: write
- name: Checkout trusted publisher policy
uses: actions/checkout@v7
with:
ref: ${{ github.event.repository.default_branch }}
path: policy
persist-credentials: false
- name: Checkout repository with deploy key
uses: actions/checkout@v7
with:
ref: ${{ github.event.repository.default_branch }}
path: repo
ssh-key: ${{ secrets.DEPLOY_KEY }}
fetch-depth: 0
- name: Inspect branch state and auto-rebase stale editorial drafts
id: sync
shell: bash
run: |
set -euo pipefail
git -C repo fetch --prune origin
if ! git -C repo show-ref --verify --quiet "refs/remotes/origin/$HEAD_BRANCH"; then
echo "status=missing" >> "$GITHUB_OUTPUT"
echo "Branch $HEAD_BRANCH no longer exists on origin." >> "$GITHUB_STEP_SUMMARY"
exit 0
fi
tip_sha="$(git -C repo rev-parse "origin/$HEAD_BRANCH")"
if [[ "$tip_sha" != "$HEAD_SHA" ]]; then
echo "status=superseded" >> "$GITHUB_OUTPUT"
echo "Branch $HEAD_BRANCH advanced from $HEAD_SHA to $tip_sha; skipping superseded run." >> "$GITHUB_STEP_SUMMARY"
exit 0
fi
ahead_count="$(git -C repo rev-list --count "origin/$BASE_BRANCH..origin/$HEAD_BRANCH")"
behind_count="$(git -C repo rev-list --count "origin/$HEAD_BRANCH..origin/$BASE_BRANCH")"
if [[ "$ahead_count" == "0" ]]; then
echo "status=empty" >> "$GITHUB_OUTPUT"
echo "Branch $HEAD_BRANCH has 0 commits ahead of $BASE_BRANCH; skipping PR creation." >> "$GITHUB_STEP_SUMMARY"
exit 0
fi
git -C repo diff --name-only "origin/$BASE_BRANCH...origin/$HEAD_BRANCH" > "$RUNNER_TEMP/branch-paths.txt"
branch_policy="$(node policy/.github/scripts/classify-keystatic-changes.mjs --paths-file "$RUNNER_TEMP/branch-paths.txt")"
branch_eligible="$(printf '%s' "$branch_policy" | node -e 'const fs=require("fs"); const d=JSON.parse(fs.readFileSync(0,"utf8")); process.stdout.write(String(d.eligible));')"
if [[ "$behind_count" != "0" && "$branch_eligible" == "true" ]]; then
git -C repo config --local user.name "github-actions[bot]"
git -C repo config --local user.email "github-actions[bot]@users.noreply.github.com"
git -C repo checkout -B "$HEAD_BRANCH" "origin/$HEAD_BRANCH"
if git -C repo rebase "origin/$BASE_BRANCH"; then
git -C repo push --force-with-lease origin "HEAD:$HEAD_BRANCH"
echo "status=rebased" >> "$GITHUB_OUTPUT"
echo "Rebased $HEAD_BRANCH onto $BASE_BRANCH; Test website build will validate the rebased commit next." >> "$GITHUB_STEP_SUMMARY"
exit 0
else
git -C repo rebase --abort || true
echo "status=conflict" >> "$GITHUB_OUTPUT"
exit 0
fi
fi
echo "status=ready" >> "$GITHUB_OUTPUT"
- name: Classify branch paths for PR metadata
id: branch-meta
if: steps.sync.outputs.status == 'ready' || steps.sync.outputs.status == 'conflict'
run: >-
node policy/.github/scripts/classify-keystatic-changes.mjs
--paths-file "$RUNNER_TEMP/branch-paths.txt"
- name: Create or update pull request
id: pr
if: steps.sync.outputs.status == 'ready' || steps.sync.outputs.status == 'conflict'
env:
GH_TOKEN: ${{ steps.app-token.outputs.token || github.token }}
KIND: ${{ steps.branch-meta.outputs.kind }}
HAS_PEOPLE: ${{ steps.branch-meta.outputs.has_people }}
ELIGIBLE: ${{ steps.branch-meta.outputs.eligible }}
SUMMARY_TITLE: ${{ steps.branch-meta.outputs.summary_title }}
shell: bash
run: |
set -euo pipefail
if [[ "$ELIGIBLE" == "true" && "$KIND" == "blog" ]]; then
pr_body="Created automatically from Keystatic (**Blog**). \`Test website build\` passed — auto-merging into \`main\`."
elif [[ "$ELIGIBLE" == "true" && "$HAS_PEOPLE" == "true" ]]; then
pr_body="Created automatically from Keystatic (**People**). \`Test website build\` passed — awaiting **1 maintainer approval** (\`manhkhanhad\`), after which this PR will auto-merge."
else
pr_body="Created automatically from Keystatic. This branch touches paths outside the approved CMS directories — manual review by \`manhkhanhad\` is required."
fi
pr_json="$(gh pr list \
--repo "$REPOSITORY" \
--state open \
--base "$BASE_BRANCH" \
--head "$HEAD_BRANCH" \
--json number,title,author,reviewDecision,reviewRequests \
--jq '.[0] // empty')"
created_new="false"
if [[ -z "$pr_json" ]]; then
created_new="true"
gh pr create \
--repo "$REPOSITORY" \
--base "$BASE_BRANCH" \
--head "$HEAD_BRANCH" \
--title "$SUMMARY_TITLE" \
--body "$pr_body"
pr_json="$(gh pr list \
--repo "$REPOSITORY" \
--state open \
--base "$BASE_BRANCH" \
--head "$HEAD_BRANCH" \
--json number,title,author,reviewDecision,reviewRequests \
--jq '.[0] // empty')"
else
existing_number="$(printf '%s' "$pr_json" | jq -r '.number')"
existing_title="$(printf '%s' "$pr_json" | jq -r '.title')"
if [[ "$existing_title" == "Keystatic: publish editorial changes" || "$existing_title" == Keystatic* || "$existing_title" == keystatic/* ]]; then
gh pr edit "$existing_number" \
--repo "$REPOSITORY" \
--title "$SUMMARY_TITLE" \
--body "$pr_body"
fi
fi
pr_number="$(printf '%s' "$pr_json" | jq -r '.number // empty')"
pr_author="$(printf '%s' "$pr_json" | jq -r '.author.login // empty')"
review_decision="$(printf '%s' "$pr_json" | jq -r '.reviewDecision // empty')"
already_requested="$(printf '%s' "$pr_json" | jq -r '[.reviewRequests[]?.login // empty] | index("manhkhanhad") != null')"
if [[ -z "$pr_number" ]]; then
echo "Could not create or find the Keystatic pull request." >&2
exit 1
fi
echo "number=$pr_number" >> "$GITHUB_OUTPUT"
# On newly created PRs, .github/CODEOWNERS already requests manhkhanhad once.
# Only re-request on updated existing PRs when neither approved nor already pending.
if [[ "$created_new" == "false" && ( "$HAS_PEOPLE" == "true" || "$ELIGIBLE" != "true" ) ]]; then
if [[ "$pr_author" != "manhkhanhad" && "$review_decision" != "APPROVED" && "$already_requested" != "true" ]]; then
gh pr edit "$pr_number" --repo "$REPOSITORY" --add-reviewer manhkhanhad || true
fi
fi
- name: Notify on rebase conflict
if: steps.sync.outputs.status == 'conflict'
uses: actions/github-script@v9
env:
PR_NUMBER: ${{ steps.pr.outputs.number }}
with:
github-token: ${{ steps.app-token.outputs.token || github.token }}
script: |
const marker = '<!-- preferredai-keystatic-sync -->';
const issue_number = Number(process.env.PR_NUMBER);
const body = `${marker}\n⚠️ **Branch out of date with \`main\` (merge conflict):** Automatic rebase onto \`main\` hit a conflict. Please resolve conflicts on GitHub or start a fresh branch in Keystatic.`;
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number,
per_page: 100,
});
const previous = comments.find((c) => c.body?.includes(marker));
if (previous) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: previous.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number,
body,
});
}
- name: Read complete pull request diff
id: diff
if: steps.sync.outputs.status == 'ready'
env:
GH_TOKEN: ${{ steps.app-token.outputs.token || github.token }}
shell: bash
run: |
set -euo pipefail
changed_files="$(gh pr view \
"${{ steps.pr.outputs.number }}" \
--repo "$REPOSITORY" \
--json changedFiles \
--jq '.changedFiles')"
echo "changed_files=$changed_files" >> "$GITHUB_OUTPUT"
gh api \
--paginate \
--slurp \
"repos/$REPOSITORY/pulls/${{ steps.pr.outputs.number }}/files?per_page=100" \
> "$RUNNER_TEMP/keystatic-pr-files.json"
- name: Classify editorial paths from PR diff
id: policy
if: steps.sync.outputs.status == 'ready'
run: >-
node policy/.github/scripts/classify-keystatic-changes.mjs
--github-json "$RUNNER_TEMP/keystatic-pr-files.json"
--expected-count "${{ steps.diff.outputs.changed_files }}"
- name: Auto-merge verified blog-only PR
if: steps.sync.outputs.status == 'ready' && steps.policy.outputs.auto_merge_blog == 'true'
env:
GH_TOKEN: ${{ steps.app-token.outputs.token || github.token }}
PR_NUMBER: ${{ steps.pr.outputs.number }}
shell: bash
run: |
set -euo pipefail
for _ in 1 2 3 4 5; do
indexed_sha="$(gh pr view "$PR_NUMBER" --repo "$REPOSITORY" --json headRefOid --jq '.headRefOid')"
if [[ "$indexed_sha" == "$HEAD_SHA" ]]; then
break
fi
sleep 2
done
sleep 2
git -C repo fetch --prune origin
current_branch_sha="$(git -C repo rev-parse "origin/$HEAD_BRANCH")"
if [[ "$current_branch_sha" != "$HEAD_SHA" ]]; then
echo "Branch $HEAD_BRANCH advanced from $HEAD_SHA to $current_branch_sha; skipping stale merge." >&2
exit 0
fi
if ! git -C repo merge-base --is-ancestor "origin/$BASE_BRANCH" "$HEAD_SHA"; then
echo "Base branch $BASE_BRANCH moved ahead; rebasing $HEAD_BRANCH for next build cycle." >&2
git -C repo config --local user.name "github-actions[bot]"
git -C repo config --local user.email "github-actions[bot]@users.noreply.github.com"
git -C repo checkout -B "$HEAD_BRANCH" "$HEAD_SHA"
git -C repo rebase "origin/$BASE_BRANCH"
git -C repo push --force-with-lease origin "HEAD:$HEAD_BRANCH"
exit 0
fi
git -C repo checkout "$BASE_BRANCH"
git -C repo reset --hard "origin/$BASE_BRANCH"
git -C repo merge --ff-only "$HEAD_SHA"
git -C repo push origin "$BASE_BRANCH"
for _ in 1 2 3 4 5; do
pr_state="$(gh pr view "$PR_NUMBER" --repo "$REPOSITORY" --json state --jq '.state')"
if [[ "$pr_state" == "MERGED" ]]; then
break
fi
sleep 2
done
git -C repo push origin --delete "$HEAD_BRANCH" || true
echo "Auto-merged blog-only PR #$PR_NUMBER ($HEAD_SHA) into $BASE_BRANCH." >> "$GITHUB_STEP_SUMMARY"
merge-on-approval:
if: >-
github.event_name == 'pull_request_review' &&
github.event.review.state == 'approved' &&
github.event.pull_request.state == 'open' &&
github.event.pull_request.draft == false &&
github.event.pull_request.base.ref == github.event.repository.default_branch &&
startsWith(github.event.pull_request.head.ref, 'keystatic/') &&
github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
environment: editorial-publish
env:
REPOSITORY: ${{ github.repository }}
BASE_BRANCH: ${{ github.event.pull_request.base.ref }}
HEAD_BRANCH: ${{ github.event.pull_request.head.ref }}
REVIEWED_SHA: ${{ github.event.review.commit_id }}
PR_NUMBER: ${{ github.event.pull_request.number }}
REVIEWER: ${{ github.event.review.user.login }}
steps:
- name: Create repository-scoped publisher token (optional App)
id: app-token
if: vars.PREFERREDAI_AUTOMATION_APP_ID != ''
uses: actions/create-github-app-token@v3
with:
client-id: ${{ vars.PREFERREDAI_AUTOMATION_APP_ID }}
private-key: ${{ secrets.PREFERREDAI_AUTOMATION_APP_PRIVATE_KEY }}
owner: PreferredAI
repositories: PreferredAI.github.io
permission-contents: write
permission-issues: write
permission-pull-requests: write
- name: Verify maintainer permission and passing checks
id: gate
env:
GH_TOKEN: ${{ steps.app-token.outputs.token || github.token }}
shell: bash
run: |
set -euo pipefail
perm="$(gh api "repos/$REPOSITORY/collaborators/$REVIEWER/permission" --jq '.permission' 2>/dev/null || echo 'none')"
case "$perm" in
admin|maintain|write) ;;
*)
echo "approved=false" >> "$GITHUB_OUTPUT"
echo "Reviewer $REVIEWER has permission '$perm'; ignoring non-maintainer review." >> "$GITHUB_STEP_SUMMARY"
exit 0
;;
esac
pr_meta="$(gh pr view "$PR_NUMBER" --repo "$REPOSITORY" --json state,headRefOid)"
pr_state="$(printf '%s' "$pr_meta" | jq -r '.state')"
head_oid="$(printf '%s' "$pr_meta" | jq -r '.headRefOid')"
if [[ "$pr_state" != "OPEN" || "$head_oid" != "$REVIEWED_SHA" ]]; then
echo "approved=false" >> "$GITHUB_OUTPUT"
echo "PR #$PR_NUMBER state=$pr_state or head ($head_oid) differs from reviewed SHA ($REVIEWED_SHA); skipping." >> "$GITHUB_STEP_SUMMARY"
exit 0
fi
checks_json="$(gh api "repos/$REPOSITORY/commits/$REVIEWED_SHA/check-runs?per_page=100")"
audit_ok="$(printf '%s' "$checks_json" | jq -r '[.check_runs[] | select(.app.id == 15368 and .name == "audit" and .status == "completed" and .conclusion == "success")] | length > 0')"
build_ok="$(printf '%s' "$checks_json" | jq -r '[.check_runs[] | select(.app.id == 15368 and .name == "build" and .status == "completed" and .conclusion == "success")] | length > 0')"
if [[ "$audit_ok" != "true" || "$build_ok" != "true" ]]; then
echo "approved=false" >> "$GITHUB_OUTPUT"
echo "PR #$PR_NUMBER approved by $REVIEWER, but Test website build checks (audit=$audit_ok, build=$build_ok) have not passed yet." >> "$GITHUB_STEP_SUMMARY"
exit 0
fi
echo "approved=true" >> "$GITHUB_OUTPUT"
- name: Checkout trusted publisher policy
if: steps.gate.outputs.approved == 'true'
uses: actions/checkout@v7
with:
ref: ${{ github.event.repository.default_branch }}
path: policy
persist-credentials: false
- name: Read complete pull request diff
id: diff
if: steps.gate.outputs.approved == 'true'
env:
GH_TOKEN: ${{ steps.app-token.outputs.token || github.token }}
shell: bash
run: |
set -euo pipefail
changed_files="$(gh pr view "$PR_NUMBER" --repo "$REPOSITORY" --json changedFiles --jq '.changedFiles')"
echo "changed_files=$changed_files" >> "$GITHUB_OUTPUT"
gh api \
--paginate \
--slurp \
"repos/$REPOSITORY/pulls/$PR_NUMBER/files?per_page=100" \
> "$RUNNER_TEMP/keystatic-pr-files.json"
- name: Classify editorial paths from PR diff
id: policy
if: steps.gate.outputs.approved == 'true'
run: >-
node policy/.github/scripts/classify-keystatic-changes.mjs
--github-json "$RUNNER_TEMP/keystatic-pr-files.json"
--expected-count "${{ steps.diff.outputs.changed_files }}"
- name: Checkout repository with deploy key
if: steps.gate.outputs.approved == 'true' && steps.policy.outputs.eligible == 'true'
uses: actions/checkout@v7
with:
ref: ${{ github.event.repository.default_branch }}
path: repo
ssh-key: ${{ secrets.DEPLOY_KEY }}
fetch-depth: 0
- name: Merge approved editorial PR into main
if: steps.gate.outputs.approved == 'true' && steps.policy.outputs.eligible == 'true'
env:
GH_TOKEN: ${{ steps.app-token.outputs.token || github.token }}
shell: bash
run: |
set -euo pipefail
git -C repo fetch --prune origin
current_branch_sha="$(git -C repo rev-parse "origin/$HEAD_BRANCH")"
if [[ "$current_branch_sha" != "$REVIEWED_SHA" ]]; then
echo "Branch $HEAD_BRANCH changed from $REVIEWED_SHA to $current_branch_sha; aborting merge." >&2
exit 0
fi
merge_sha="$REVIEWED_SHA"
if ! git -C repo merge-base --is-ancestor "origin/$BASE_BRANCH" "$REVIEWED_SHA"; then
git -C repo config --local user.name "github-actions[bot]"
git -C repo config --local user.email "github-actions[bot]@users.noreply.github.com"
git -C repo checkout -B "$HEAD_BRANCH" "$REVIEWED_SHA"
git -C repo rebase "origin/$BASE_BRANCH"
git -C repo diff --name-only "origin/$BASE_BRANCH...HEAD" > "$RUNNER_TEMP/rebased-paths.txt"
rebased_policy="$(node policy/.github/scripts/classify-keystatic-changes.mjs --paths-file "$RUNNER_TEMP/rebased-paths.txt")"
rebased_eligible="$(printf '%s' "$rebased_policy" | node -e 'const fs=require("fs"); const d=JSON.parse(fs.readFileSync(0,"utf8")); process.stdout.write(String(d.eligible));')"
if [[ "$rebased_eligible" != "true" ]]; then
echo "Rebased diff contains ineligible paths; aborting automatic merge." >&2
exit 1
fi
merge_sha="$(git -C repo rev-parse HEAD)"
git -C repo push --force-with-lease origin "HEAD:$HEAD_BRANCH"
for _ in 1 2 3 4 5; do
indexed_sha="$(gh pr view "$PR_NUMBER" --repo "$REPOSITORY" --json headRefOid --jq '.headRefOid')"
if [[ "$indexed_sha" == "$merge_sha" ]]; then
break
fi
sleep 2
done
sleep 2
fi
git -C repo checkout "$BASE_BRANCH"
git -C repo reset --hard "origin/$BASE_BRANCH"
git -C repo merge --ff-only "$merge_sha"
git -C repo push origin "$BASE_BRANCH"
for _ in 1 2 3 4 5; do
pr_state="$(gh pr view "$PR_NUMBER" --repo "$REPOSITORY" --json state --jq '.state')"
if [[ "$pr_state" == "MERGED" ]]; then
break
fi
sleep 2
done
git -C repo push origin --delete "$HEAD_BRANCH" || true
echo "Auto-merged approved editorial PR #$PR_NUMBER ($merge_sha) into $BASE_BRANCH." >> "$GITHUB_STEP_SUMMARY"