-
Notifications
You must be signed in to change notification settings - Fork 8
135 lines (123 loc) · 4.86 KB
/
Copy pathaffected-pages.yml
File metadata and controls
135 lines (123 loc) · 4.86 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
name: Affected pages
on:
deployment_status:
permissions:
pull-requests: write
deployments: read
contents: read
concurrency:
group: affected-pages-${{ github.event.deployment.sha }}
cancel-in-progress: true
jobs:
affected-pages:
if: |
github.event.deployment_status.state == 'success' &&
startsWith(github.event.deployment.environment, 'Preview') &&
contains(github.event.deployment_status.target_url, 'vercel.app')
runs-on: ubuntu-latest
steps:
- name: Find PR for this deployment
id: pr
uses: actions/github-script@v8
env:
DEPLOY_SHA: ${{ github.event.deployment.sha }}
with:
script: |
const { data: prs } = await github.rest.repos.listPullRequestsAssociatedWithCommit({
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: process.env.DEPLOY_SHA,
});
const pr = prs.find((p) => p.state === "open");
if (!pr) {
core.info("No open PR for this deployment; skipping.");
return;
}
core.setOutput("number", pr.number);
- name: Checkout repo at deploy SHA
if: steps.pr.outputs.number
uses: actions/checkout@v4
with:
ref: ${{ github.event.deployment.sha }}
- name: Collect changed MDX files
id: changed
if: steps.pr.outputs.number
uses: actions/github-script@v8
env:
PR_NUMBER: ${{ steps.pr.outputs.number }}
with:
script: |
const pull_number = parseInt(process.env.PR_NUMBER, 10);
const files = await github.paginate(github.rest.pulls.listFiles, {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number,
});
const matching = files
.filter((f) => f.status !== "removed")
.map((f) => f.filename)
.filter(
(n) =>
(n.startsWith("src/content/") || n.startsWith("src/partials/")) &&
n.endsWith(".mdx"),
);
core.setOutput("any", matching.length > 0 ? "true" : "false");
core.setOutput("files", matching.join("\n"));
- name: Compute affected page URLs
if: steps.pr.outputs.number
env:
CHANGED_FILES: ${{ steps.changed.outputs.files }}
PREVIEW_URL: ${{ github.event.deployment_status.target_url }}
BYPASS_SECRET: ${{ secrets.VERCEL_AUTOMATION_BYPASS_SECRET }}
run: node scripts/affected-pages.mjs
- name: Update PR description
if: steps.pr.outputs.number
uses: actions/github-script@v8
env:
BODY_PATH: pages-comment.md
PR_NUMBER: ${{ steps.pr.outputs.number }}
with:
script: |
const fs = require("fs");
// Match any "## Affected pages" section: from the heading up to (but
// not including) the next `## ` heading or end of body. The `\r?`
// tolerates GitHub's CRLF line endings in PR bodies, and the `g`
// flag lets us strip duplicate sections left behind by earlier runs
// that hit the line-ending bug.
const sectionRe = /## Affected pages[ \t]*\r?\n[\s\S]*?(?=\r?\n## |$)/g;
const pull_number = parseInt(process.env.PR_NUMBER, 10);
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number,
});
const original = pr.body || "";
const content = fs.readFileSync(process.env.BODY_PATH, "utf8").trim();
const block = `## Affected pages\n\n${content}\n`;
// Strip every existing Affected pages section, then reinsert one
// fresh copy in the canonical spot. This is idempotent and also
// cleans up any duplicates accumulated by previous runs.
const stripped = original
.replace(sectionRe, "")
.replace(/(\r?\n){3,}/g, "$1$1");
let next;
const heading = "## Related Issues";
const idx = stripped.indexOf(heading);
if (idx >= 0) {
const before = stripped.slice(0, idx).trimEnd();
const after = stripped.slice(idx);
next = `${before}\n\n${block}\n${after}`;
} else {
next = `${stripped.trim()}\n\n${block}`;
}
if (next !== original) {
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number,
body: next,
});
console.log("PR description updated.");
} else {
console.log("No change to PR description.");
}