-
Notifications
You must be signed in to change notification settings - Fork 10.3k
[Docs Site] Deploy preview URLs to WfP and fix Show Changed Files comments #19823
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5293c3a
[Docs Site] Move show-changed-files to be part of publish-preview wor…
KianNH 7cc7551
change env var name
KianNH a375861
read from env var
KianNH 40ee558
Move to WFP for preview deployments
KianNH 1d67dd0
use same logic for creating subdomain
KianNH c824f86
change a file
KianNH 9a3f385
change a file
KianNH e9c79bc
newlines
KianNH 36d317b
change a different file
KianNH 91535c9
move debug logs to normal logs
KianNH b60c0f3
fix pr list filter
KianNH 86c5a59
Remove test
KianNH File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
1 change: 0 additions & 1 deletion
1
bin/show-changed-files/constants.ts → bin/post-preview-url-comment/constants.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| export const GITHUB_ACTIONS_BOT_ID = 41898282; | ||
| export const DOCS_BASE_URL = "https://developers.cloudflare.com"; | ||
| export const CONTENT_BASE_PATH = "src/content"; | ||
| export const EXISTING_COMMENT_SUBSTRING = "| Original Link | Updated Link |"; | ||
| export const PREVIEW_URL_REGEX = /^\*\*Preview URL:\*\* (.*)$/m; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import { describe, expect, test } from "vitest"; | ||
| import { DOCS_BASE_URL, PREVIEW_URL_REGEX } from "./constants"; | ||
| import { filenameToPath, branchToSubdomain } from "./util"; | ||
|
|
||
| describe("PREVIEW_URL_REGEX", () => { | ||
| test("no changed files", () => { | ||
| const comment = | ||
| "**Preview URL:** https://e9c79bc3.preview.developers.cloudflare.com\n**Preview Branch URL:** https://kian-pcx-15803.preview.developers.cloudflare.com"; | ||
|
|
||
| expect(PREVIEW_URL_REGEX.test(comment)).toBe(true); | ||
| }); | ||
|
|
||
| test("changed files", () => { | ||
| const comment = | ||
| "**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/) |"; | ||
|
|
||
| expect(PREVIEW_URL_REGEX.test(comment)).toBe(true); | ||
| }); | ||
|
|
||
| test("empty", () => { | ||
| expect(PREVIEW_URL_REGEX.test("")).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("branchToSubdomain", () => { | ||
| test("slash", () => { | ||
| expect(branchToSubdomain("kian/pcx-15803")).toEqual("kian-pcx-15803"); | ||
| }); | ||
|
|
||
| test("normal", () => { | ||
| expect(branchToSubdomain("pcx-15803")).toEqual("pcx-15803"); | ||
| }); | ||
|
|
||
| test("capitalisation", () => { | ||
| expect(branchToSubdomain("PCX-15803")).toEqual("pcx-15803"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("filenameToPath", () => { | ||
| test("index", () => { | ||
| expect(filenameToPath("src/content/docs/workers/index.mdx")).toEqual( | ||
| "workers/", | ||
| ); | ||
| }); | ||
|
|
||
| test("index base", () => { | ||
| expect( | ||
| `${DOCS_BASE_URL}/${filenameToPath("src/content/docs/workers/index.mdx")}`, | ||
| ).toEqual("https://developers.cloudflare.com/workers/"); | ||
| }); | ||
|
|
||
| test("folder", () => { | ||
| expect( | ||
| filenameToPath("src/content/docs/workers/get-started/cli.mdx"), | ||
| ).toEqual("workers/get-started/cli/"); | ||
| }); | ||
|
|
||
| test("1.1.1.1", () => { | ||
| expect(filenameToPath("src/content/docs/1111/index.mdx")).toEqual( | ||
| "1.1.1.1/", | ||
| ); | ||
| }); | ||
|
|
||
| test("changelog", () => { | ||
| expect( | ||
| filenameToPath("src/content/changelogs-next/2025-02-05-title.mdx"), | ||
| ).toEqual("changelog/2025-02-05-title/"); | ||
| }); | ||
|
|
||
| test("changelog base", () => { | ||
| expect( | ||
| `${DOCS_BASE_URL}/${filenameToPath("src/content/changelogs-next/2025-02-05-title.mdx")}`, | ||
| ).toEqual("https://developers.cloudflare.com/changelog/2025-02-05-title/"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import * as core from "@actions/core"; | ||
| import * as github from "@actions/github"; | ||
|
|
||
| import { | ||
| CONTENT_BASE_PATH, | ||
| DOCS_BASE_URL, | ||
| GITHUB_ACTIONS_BOT_ID, | ||
| PREVIEW_URL_REGEX, | ||
| } from "./constants"; | ||
|
|
||
| import { filenameToPath, branchToSubdomain } from "./util"; | ||
|
|
||
| async function run(): Promise<void> { | ||
| try { | ||
| if (!process.env.GITHUB_TOKEN) { | ||
| core.setFailed(`Could not find GITHUB_TOKEN in env`); | ||
| process.exit(); | ||
| } | ||
|
|
||
| const octokit = github.getOctokit(process.env.GITHUB_TOKEN); | ||
| const ctx = github.context; | ||
| const branch = ctx.ref.replace("refs/heads/", ""); | ||
|
|
||
| core.info(`Finding pull requests for ${ctx.ref}`); | ||
|
|
||
| const { data: pulls } = await octokit.rest.pulls.list({ | ||
| ...ctx.repo, | ||
| head: `${ctx.repo.owner}:${branch}`, | ||
| }); | ||
|
|
||
| const pull_number = pulls.at(0)?.number; | ||
|
|
||
| if (!pull_number) { | ||
| core.setFailed(`Could not find pull requests for ${ctx.ref}`); | ||
| process.exit(); | ||
| } | ||
|
|
||
| core.info(`Found ${pull_number}`); | ||
|
|
||
| const files = await octokit.paginate(octokit.rest.pulls.listFiles, { | ||
| ...ctx.repo, | ||
| pull_number, | ||
| per_page: 100, | ||
| }); | ||
|
|
||
| core.info(`Found ${files.length} changed files for ${pull_number}`); | ||
|
|
||
| const { data: comments } = await octokit.rest.issues.listComments({ | ||
| ...ctx.repo, | ||
| issue_number: pull_number, | ||
| per_page: 100, | ||
| }); | ||
|
|
||
| const existingComment = comments.find( | ||
| (comment) => | ||
| comment.user?.id === GITHUB_ACTIONS_BOT_ID && | ||
| PREVIEW_URL_REGEX.test(comment.body ?? ""), | ||
| ); | ||
|
|
||
| if (existingComment) { | ||
| core.info(`Found existing comment with ID ${existingComment.id}`); | ||
| } else { | ||
| core.info(`No existing comment found`); | ||
| } | ||
|
|
||
| const previewUrl = { | ||
| branch: `https://${branchToSubdomain(branch)}.preview.developers.cloudflare.com`, | ||
| commit: `https://${ctx.sha.slice(0, 8)}.preview.developers.cloudflare.com`, | ||
| }; | ||
|
|
||
| core.info( | ||
| `Commit URL: ${previewUrl.commit}\nBranch URL: ${previewUrl.branch}`, | ||
| ); | ||
|
|
||
| const changedFiles = files | ||
| .filter( | ||
| (file) => | ||
| file.filename.endsWith(".mdx") && | ||
| (file.filename.startsWith(`${CONTENT_BASE_PATH}/docs/`) || | ||
| file.filename.startsWith(`${CONTENT_BASE_PATH}/changelogs-next/`)), | ||
| ) | ||
| .sort((a, b) => b.changes - a.changes) | ||
| .slice(0, 15) // Limit to 15 entries | ||
| .map(({ filename }) => { | ||
| const original = `${DOCS_BASE_URL}/${filenameToPath(filename)}`; | ||
| const preview = `${previewUrl.branch}/${filenameToPath(filename)}`; | ||
|
|
||
| core.info( | ||
| `Filename: ${filename}\nOriginal: ${original}\nPreview: ${preview}`, | ||
| ); | ||
|
|
||
| return { original, preview }; | ||
| }); | ||
|
|
||
| let comment = `**Preview URL:** ${previewUrl.commit}\n**Preview Branch URL:** ${previewUrl.branch}`; | ||
| if (changedFiles.length !== 0) { | ||
| core.info(`Found ${changedFiles.length} after filtering paths`); | ||
|
|
||
| comment = comment.concat( | ||
| `\n\n**Files with changes (up to 15)**\n\n| Original Link | Updated Link |\n| --- | --- |\n${changedFiles | ||
| .map( | ||
| (file) => | ||
| `| [${file.original}](${file.original}) | [${file.preview}](${file.preview}) |`, | ||
| ) | ||
| .join("\n")}`, | ||
| ); | ||
| } | ||
|
|
||
| if (existingComment) { | ||
| core.info( | ||
| `Updating ${existingComment.id} with ${JSON.stringify(comment)}`, | ||
| ); | ||
| await octokit.rest.issues.updateComment({ | ||
| owner: ctx.repo.owner, | ||
| repo: ctx.repo.repo, | ||
| comment_id: existingComment.id, | ||
| body: comment, | ||
| }); | ||
| } else { | ||
| core.info(`Creating new comment with ${JSON.stringify(comment)}`); | ||
| await octokit.rest.issues.createComment({ | ||
| owner: ctx.repo.owner, | ||
| repo: ctx.repo.repo, | ||
| issue_number: pull_number, | ||
| body: comment, | ||
| }); | ||
| } | ||
| } catch (error) { | ||
| if (error instanceof Error) { | ||
| core.setFailed(error.message); | ||
| } | ||
| process.exit(); | ||
| } | ||
| } | ||
|
|
||
| run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
errm. Ideally we have this in code and use it for both CI and this haha