Skip to content

Commit f6aa772

Browse files
committed
[Docs Site] Move show-changed-files to bin and fix workflow condition
1 parent 9fad9e7 commit f6aa772

File tree

15 files changed

+410
-939
lines changed

15 files changed

+410
-939
lines changed

.github/actions/show-changed-files/package-lock.json

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

.github/actions/show-changed-files/package.json

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

.github/actions/show-changed-files/tsconfig.json

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

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@ on:
1313
jobs:
1414
changed-files-links:
1515
name: Show Changed Files
16-
if: github.event.issue.pull_request && github.event.issue.state == 'open' && github.event.comment.user.id == 41898282 && contains(github.event.comment.body, '**Preview URL**')
16+
if: github.event.issue.pull_request && github.event.issue.state == 'open' && github.event.comment.user.id == 41898282 && contains(github.event.comment.body, '**Preview URL:**')
1717
runs-on: ubuntu-latest
1818
steps:
1919
- uses: actions/checkout@v4
20-
- run: cd ./.github/actions/show-changed-files
2120
- uses: actions/setup-node@v4
2221
with:
2322
node-version: 22
2423
cache: "npm"
2524
- run: npm ci
26-
- run: npx tsx index.ts
25+
- run: npx tsx bin/show-changed-files/index.ts
2726
env:
2827
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const GITHUB_ACTIONS_BOT_ID = 41898282;
2+
export const DOCS_BASE_URL = "https://developers.cloudflare.com";
3+
export const CONTENT_BASE_PATH = "src/content";
4+
export const EXISTING_COMMENT_SUBSTRING = "| Original Link | Updated Link |";
5+
export const PREVIEW_URL_REGEX = /^\*\*Preview URL:\*\* (.*)$/m;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { describe, expect, test } from "vitest";
2+
3+
import { DOCS_BASE_URL, PREVIEW_URL_REGEX } from "./constants";
4+
import { filenameToPath } from "./util";
5+
6+
test("PREVIEW_URL_REGEX", () => {
7+
const comment =
8+
"**Preview URL:** https://ac148943-cloudflare-docs.cloudflare-docs.workers.dev";
9+
const matches = comment.match(PREVIEW_URL_REGEX);
10+
11+
expect(matches).toBeDefined();
12+
expect(matches![1]).toEqual(
13+
"https://ac148943-cloudflare-docs.cloudflare-docs.workers.dev",
14+
);
15+
});
16+
17+
describe("filenameToPath", () => {
18+
test("index", () => {
19+
expect(filenameToPath("src/content/docs/workers/index.mdx")).toEqual(
20+
"workers/",
21+
);
22+
});
23+
24+
test("index base", () => {
25+
expect(
26+
`${DOCS_BASE_URL}/${filenameToPath("src/content/docs/workers/index.mdx")}`,
27+
).toEqual("https://developers.cloudflare.com/workers/");
28+
});
29+
30+
test("folder", () => {
31+
expect(
32+
filenameToPath("src/content/docs/workers/get-started/cli.mdx"),
33+
).toEqual("workers/get-started/cli/");
34+
});
35+
36+
test("1.1.1.1", () => {
37+
expect(filenameToPath("src/content/docs/1111/index.mdx")).toEqual(
38+
"1.1.1.1/",
39+
);
40+
});
41+
42+
test("changelog", () => {
43+
expect(
44+
filenameToPath("src/content/changelogs-next/2025-02-05-title.mdx"),
45+
).toEqual("changelog/2025-02-05-title/");
46+
});
47+
48+
test("changelog base", () => {
49+
expect(
50+
`${DOCS_BASE_URL}/${filenameToPath("src/content/changelogs-next/2025-02-05-title.mdx")}`,
51+
).toEqual("https://developers.cloudflare.com/changelog/2025-02-05-title/");
52+
});
53+
});

.github/actions/show-changed-files/index.ts renamed to bin/show-changed-files/index.ts

Lines changed: 35 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
import * as core from "@actions/core";
22
import * as github from "@actions/github";
33

4-
import { slug } from "github-slugger";
4+
import {
5+
CONTENT_BASE_PATH,
6+
DOCS_BASE_URL,
7+
EXISTING_COMMENT_SUBSTRING,
8+
GITHUB_ACTIONS_BOT_ID,
9+
PREVIEW_URL_REGEX,
10+
} from "./constants";
511

6-
const GITHUB_ACTIONS_BOT_ID = 41898282;
7-
const DOCS_BASE_URL = "https://developers.cloudflare.com";
8-
const CONTENT_BASE_PATH = "src/content/docs/";
12+
import { filenameToPath } from "./util";
913

1014
async function run(): Promise<void> {
1115
try {
1216
const token = core.getInput("GITHUB_TOKEN", { required: true });
1317
const octokit = github.getOctokit(token);
1418

1519
const ctx = github.context;
20+
21+
if (!ctx.payload.issue) {
22+
core.setFailed(`Payload ${ctx.payload} is missing an 'issue' property`);
23+
process.exit();
24+
}
25+
1626
const issue = ctx.payload.issue.number;
1727

1828
const files = await octokit.paginate(octokit.rest.pulls.listFiles, {
@@ -30,63 +40,44 @@ async function run(): Promise<void> {
3040

3141
const existingComment = comments.find(
3242
(comment) =>
33-
comment.user.id === GITHUB_ACTIONS_BOT_ID &&
34-
comment.body.includes("| Original Link | Updated Link |"),
43+
comment.user?.id === GITHUB_ACTIONS_BOT_ID &&
44+
comment.body?.includes(EXISTING_COMMENT_SUBSTRING),
3545
);
3646

3747
const urlComment = comments.find(
3848
(comment) =>
39-
comment.user.id === GITHUB_ACTIONS_BOT_ID &&
40-
comment.body.includes("**Preview URL:**"),
49+
comment.user?.id === GITHUB_ACTIONS_BOT_ID &&
50+
PREVIEW_URL_REGEX.test(comment.body ?? ""),
4151
);
4252

43-
let previewUrl: string;
44-
45-
if (urlComment) {
46-
if (!urlComment.body) {
47-
core.setFailed(`${urlComment.id} has no body`);
48-
process.exit();
49-
}
50-
51-
const match = urlComment.body.match(/^\*\*Preview URL:\*\* (.*)$/m)[1];
53+
if (!urlComment || !urlComment.body) {
54+
core.setFailed(
55+
`Could not find a comment from ${GITHUB_ACTIONS_BOT_ID} on ${issue}`,
56+
);
57+
process.exit();
58+
}
5259

53-
if (!match) {
54-
core.setFailed(`Could not extract URL from ${urlComment.body}`);
55-
process.exit();
56-
}
60+
const match = urlComment.body.match(PREVIEW_URL_REGEX);
5761

58-
previewUrl = match;
62+
if (!match) {
63+
core.setFailed(`Could not extract URL from ${urlComment.body}`);
64+
process.exit();
5965
}
6066

67+
const previewUrl = match[1];
68+
6169
core.debug(previewUrl);
6270

6371
const changedFiles = files
6472
.filter(
6573
(file) =>
6674
file.filename.endsWith(".mdx") &&
67-
file.filename.startsWith(CONTENT_BASE_PATH),
75+
(file.filename.startsWith(`${CONTENT_BASE_PATH}/docs/`) ||
76+
file.filename.startsWith(`${CONTENT_BASE_PATH}/changelogs-next/`)),
6877
)
6978
.sort((a, b) => b.changes - a.changes)
7079
.slice(0, 15) // Limit to 15 entries
7180
.map(({ filename }) => {
72-
const filenameToPath = (filename: string) => {
73-
return filename
74-
.replace(".mdx", "")
75-
.split("/")
76-
.map((segment, idx) => {
77-
const slugified = slug(segment);
78-
79-
if (idx === 0 && slugified === "1111") {
80-
return "1.1.1.1";
81-
}
82-
83-
return slugified;
84-
})
85-
.join("/")
86-
.replace(/\/index$/, "")
87-
.concat("/");
88-
};
89-
9081
const original = `${DOCS_BASE_URL}/${filenameToPath(filename)}`;
9182
const preview = `${previewUrl}/${filenameToPath(filename)}`;
9283

@@ -122,7 +113,9 @@ async function run(): Promise<void> {
122113
});
123114
}
124115
} catch (error) {
125-
core.setFailed(error.message);
116+
if (error instanceof Error) {
117+
core.setFailed(error.message);
118+
}
126119
process.exit();
127120
}
128121
}

bin/show-changed-files/util.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { slug } from "github-slugger";
2+
3+
import { CONTENT_BASE_PATH } from "./constants";
4+
5+
export const filenameToPath = (filename: string) => {
6+
return filename
7+
.replace(CONTENT_BASE_PATH, "")
8+
.replace(".mdx", "")
9+
.split("/")
10+
.filter(Boolean)
11+
.flatMap((segment) => {
12+
if (segment === "docs") {
13+
return [];
14+
}
15+
16+
if (segment === "changelogs-next") {
17+
segment = "changelog";
18+
}
19+
20+
const slugified = slug(segment);
21+
22+
if (slugified === "1111") {
23+
return "1.1.1.1";
24+
}
25+
26+
return slugified;
27+
})
28+
.join("/")
29+
.replace(/\/index$/, "")
30+
.concat("/");
31+
};

bin/vitest.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { defineConfig } from "vite";
2+
3+
export default defineConfig({});

0 commit comments

Comments
 (0)