Skip to content

Commit b4e72d0

Browse files
authored
[Docs Site] Deploy preview URLs to WfP and fix Show Changed Files comments (#19823)
* [Docs Site] Move show-changed-files to be part of publish-preview workflow * change env var name * read from env var * Move to WFP for preview deployments * use same logic for creating subdomain * change a file * change a file * newlines * change a different file * move debug logs to normal logs * fix pr list filter * Remove test
1 parent 4c9f81e commit b4e72d0

File tree

11 files changed

+269
-547
lines changed

11 files changed

+269
-547
lines changed

.github/workflows/publish-preview.yml

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,19 @@ jobs:
3232
name: Build
3333
env:
3434
NODE_OPTIONS: --max-old-space-size=4096
35-
- run: npx wrangler versions upload
36-
name: Deploy to Cloudflare Workers
35+
- name: Deploy to Cloudflare Workers
3736
env:
3837
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
39-
WRANGLER_OUTPUT_FILE_PATH: wrangler-logs.json
40-
- name: Post preview URL on PR
41-
env:
42-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4338
run: |
44-
PR=$(gh pr list --head ${{ github.ref_name }} --limit 1 --json number --jq '.[].number')
45-
46-
if [ -n "$PR" ]; then
47-
URL=$(cat wrangler-logs.json | jq -r 'select(.type=="version-upload") | .preview_url')
39+
SHORT_SHA=$(git rev-parse --short=8 HEAD)
40+
BRANCH_SLUG=$(git rev-parse --abbrev-ref HEAD | iconv -c -t ascii//TRANSLIT | sed -E 's/[~^]+//g' | sed -E 's/[^a-zA-Z0-9]+/-/g' | sed -E 's/^-+|-+$//g' | tr A-Z a-z)
4841
49-
if [ -n "$URL" ]; then
50-
BODY="**Preview URL:** $URL"
51-
gh pr comment "$PR" --edit-last --body "$BODY" || gh pr comment "$PR" --body "$BODY"
52-
fi
53-
fi
42+
npx wrangler deploy --dispatch-namespace preview-deployments --name $SHORT_SHA
43+
npx wrangler deploy --dispatch-namespace preview-deployments --name $BRANCH_SLUG
44+
- name: Post preview URL on PR
45+
env:
46+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
47+
run: npx tsx bin/post-preview-url-comment/index.ts
5448
- uses: actions/cache/save@v4
5549
if: always()
5650
with:

.github/workflows/show-changed-files.yml

Lines changed: 0 additions & 27 deletions
This file was deleted.
File renamed without changes.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export const GITHUB_ACTIONS_BOT_ID = 41898282;
22
export const DOCS_BASE_URL = "https://developers.cloudflare.com";
33
export const CONTENT_BASE_PATH = "src/content";
4-
export const EXISTING_COMMENT_SUBSTRING = "| Original Link | Updated Link |";
54
export const PREVIEW_URL_REGEX = /^\*\*Preview URL:\*\* (.*)$/m;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { describe, expect, test } from "vitest";
2+
import { DOCS_BASE_URL, PREVIEW_URL_REGEX } from "./constants";
3+
import { filenameToPath, branchToSubdomain } from "./util";
4+
5+
describe("PREVIEW_URL_REGEX", () => {
6+
test("no changed files", () => {
7+
const comment =
8+
"**Preview URL:** https://e9c79bc3.preview.developers.cloudflare.com\n**Preview Branch URL:** https://kian-pcx-15803.preview.developers.cloudflare.com";
9+
10+
expect(PREVIEW_URL_REGEX.test(comment)).toBe(true);
11+
});
12+
13+
test("changed files", () => {
14+
const comment =
15+
"**Preview URL:** https://e9c79bc3.preview.developers.cloudflare.com\n**Preview Branch URL:** https://kian-pcx-15803.preview.developers.cloudflare.com\n\n**Files with changes (up to 15)**\n\n| Original Link | Updated Link |\n| --- | --- |\n| [https://developers.cloudflare.com/workers/get-started/guide/](https://developers.cloudflare.com/workers/get-started/guide/) | [https://kian-pcx-15803.preview.developers.cloudflare.com/workers/get-started/guide/](https://kian-pcx-15803.preview.developers.cloudflare.com/workers/get-started/guide/) |";
16+
17+
expect(PREVIEW_URL_REGEX.test(comment)).toBe(true);
18+
});
19+
20+
test("empty", () => {
21+
expect(PREVIEW_URL_REGEX.test("")).toBe(false);
22+
});
23+
});
24+
25+
describe("branchToSubdomain", () => {
26+
test("slash", () => {
27+
expect(branchToSubdomain("kian/pcx-15803")).toEqual("kian-pcx-15803");
28+
});
29+
30+
test("normal", () => {
31+
expect(branchToSubdomain("pcx-15803")).toEqual("pcx-15803");
32+
});
33+
34+
test("capitalisation", () => {
35+
expect(branchToSubdomain("PCX-15803")).toEqual("pcx-15803");
36+
});
37+
});
38+
39+
describe("filenameToPath", () => {
40+
test("index", () => {
41+
expect(filenameToPath("src/content/docs/workers/index.mdx")).toEqual(
42+
"workers/",
43+
);
44+
});
45+
46+
test("index base", () => {
47+
expect(
48+
`${DOCS_BASE_URL}/${filenameToPath("src/content/docs/workers/index.mdx")}`,
49+
).toEqual("https://developers.cloudflare.com/workers/");
50+
});
51+
52+
test("folder", () => {
53+
expect(
54+
filenameToPath("src/content/docs/workers/get-started/cli.mdx"),
55+
).toEqual("workers/get-started/cli/");
56+
});
57+
58+
test("1.1.1.1", () => {
59+
expect(filenameToPath("src/content/docs/1111/index.mdx")).toEqual(
60+
"1.1.1.1/",
61+
);
62+
});
63+
64+
test("changelog", () => {
65+
expect(
66+
filenameToPath("src/content/changelogs-next/2025-02-05-title.mdx"),
67+
).toEqual("changelog/2025-02-05-title/");
68+
});
69+
70+
test("changelog base", () => {
71+
expect(
72+
`${DOCS_BASE_URL}/${filenameToPath("src/content/changelogs-next/2025-02-05-title.mdx")}`,
73+
).toEqual("https://developers.cloudflare.com/changelog/2025-02-05-title/");
74+
});
75+
});
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import * as core from "@actions/core";
2+
import * as github from "@actions/github";
3+
4+
import {
5+
CONTENT_BASE_PATH,
6+
DOCS_BASE_URL,
7+
GITHUB_ACTIONS_BOT_ID,
8+
PREVIEW_URL_REGEX,
9+
} from "./constants";
10+
11+
import { filenameToPath, branchToSubdomain } from "./util";
12+
13+
async function run(): Promise<void> {
14+
try {
15+
if (!process.env.GITHUB_TOKEN) {
16+
core.setFailed(`Could not find GITHUB_TOKEN in env`);
17+
process.exit();
18+
}
19+
20+
const octokit = github.getOctokit(process.env.GITHUB_TOKEN);
21+
const ctx = github.context;
22+
const branch = ctx.ref.replace("refs/heads/", "");
23+
24+
core.info(`Finding pull requests for ${ctx.ref}`);
25+
26+
const { data: pulls } = await octokit.rest.pulls.list({
27+
...ctx.repo,
28+
head: `${ctx.repo.owner}:${branch}`,
29+
});
30+
31+
const pull_number = pulls.at(0)?.number;
32+
33+
if (!pull_number) {
34+
core.setFailed(`Could not find pull requests for ${ctx.ref}`);
35+
process.exit();
36+
}
37+
38+
core.info(`Found ${pull_number}`);
39+
40+
const files = await octokit.paginate(octokit.rest.pulls.listFiles, {
41+
...ctx.repo,
42+
pull_number,
43+
per_page: 100,
44+
});
45+
46+
core.info(`Found ${files.length} changed files for ${pull_number}`);
47+
48+
const { data: comments } = await octokit.rest.issues.listComments({
49+
...ctx.repo,
50+
issue_number: pull_number,
51+
per_page: 100,
52+
});
53+
54+
const existingComment = comments.find(
55+
(comment) =>
56+
comment.user?.id === GITHUB_ACTIONS_BOT_ID &&
57+
PREVIEW_URL_REGEX.test(comment.body ?? ""),
58+
);
59+
60+
if (existingComment) {
61+
core.info(`Found existing comment with ID ${existingComment.id}`);
62+
} else {
63+
core.info(`No existing comment found`);
64+
}
65+
66+
const previewUrl = {
67+
branch: `https://${branchToSubdomain(branch)}.preview.developers.cloudflare.com`,
68+
commit: `https://${ctx.sha.slice(0, 8)}.preview.developers.cloudflare.com`,
69+
};
70+
71+
core.info(
72+
`Commit URL: ${previewUrl.commit}\nBranch URL: ${previewUrl.branch}`,
73+
);
74+
75+
const changedFiles = files
76+
.filter(
77+
(file) =>
78+
file.filename.endsWith(".mdx") &&
79+
(file.filename.startsWith(`${CONTENT_BASE_PATH}/docs/`) ||
80+
file.filename.startsWith(`${CONTENT_BASE_PATH}/changelogs-next/`)),
81+
)
82+
.sort((a, b) => b.changes - a.changes)
83+
.slice(0, 15) // Limit to 15 entries
84+
.map(({ filename }) => {
85+
const original = `${DOCS_BASE_URL}/${filenameToPath(filename)}`;
86+
const preview = `${previewUrl.branch}/${filenameToPath(filename)}`;
87+
88+
core.info(
89+
`Filename: ${filename}\nOriginal: ${original}\nPreview: ${preview}`,
90+
);
91+
92+
return { original, preview };
93+
});
94+
95+
let comment = `**Preview URL:** ${previewUrl.commit}\n**Preview Branch URL:** ${previewUrl.branch}`;
96+
if (changedFiles.length !== 0) {
97+
core.info(`Found ${changedFiles.length} after filtering paths`);
98+
99+
comment = comment.concat(
100+
`\n\n**Files with changes (up to 15)**\n\n| Original Link | Updated Link |\n| --- | --- |\n${changedFiles
101+
.map(
102+
(file) =>
103+
`| [${file.original}](${file.original}) | [${file.preview}](${file.preview}) |`,
104+
)
105+
.join("\n")}`,
106+
);
107+
}
108+
109+
if (existingComment) {
110+
core.info(
111+
`Updating ${existingComment.id} with ${JSON.stringify(comment)}`,
112+
);
113+
await octokit.rest.issues.updateComment({
114+
owner: ctx.repo.owner,
115+
repo: ctx.repo.repo,
116+
comment_id: existingComment.id,
117+
body: comment,
118+
});
119+
} else {
120+
core.info(`Creating new comment with ${JSON.stringify(comment)}`);
121+
await octokit.rest.issues.createComment({
122+
owner: ctx.repo.owner,
123+
repo: ctx.repo.repo,
124+
issue_number: pull_number,
125+
body: comment,
126+
});
127+
}
128+
} catch (error) {
129+
if (error instanceof Error) {
130+
core.setFailed(error.message);
131+
}
132+
process.exit();
133+
}
134+
}
135+
136+
run();

bin/show-changed-files/util.ts renamed to bin/post-preview-url-comment/util.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { slug } from "github-slugger";
2-
2+
import { execSync } from "node:child_process";
33
import { CONTENT_BASE_PATH } from "./constants";
44

55
export const filenameToPath = (filename: string) => {
@@ -29,3 +29,10 @@ export const filenameToPath = (filename: string) => {
2929
.replace(/\/index$/, "")
3030
.concat("/");
3131
};
32+
33+
export const branchToSubdomain = (branch: string) => {
34+
return execSync(
35+
`echo "${branch}" | iconv -c -t ascii//TRANSLIT | sed -E 's/[~^]+//g' | sed -E 's/[^a-zA-Z0-9]+/-/g' | sed -E 's/^-+|-+$//g' | tr A-Z a-z`,
36+
{ encoding: "utf-8" },
37+
).trim();
38+
};

bin/show-changed-files/index.node.test.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)