Skip to content

Commit 5293c3a

Browse files
committed
[Docs Site] Move show-changed-files to be part of publish-preview workflow
1 parent 9c5cd24 commit 5293c3a

File tree

7 files changed

+43
-81
lines changed

7 files changed

+43
-81
lines changed

.github/workflows/publish-preview.yml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,11 @@ jobs:
3636
name: Deploy to Cloudflare Workers
3737
env:
3838
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
39-
WRANGLER_OUTPUT_FILE_PATH: wrangler-logs.json
39+
WRANGLER_OUTPUT_FILE_PATH: wrangler-logs.ndjson
4040
- name: Post preview URL on PR
4141
env:
4242
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43-
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')
48-
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
43+
run: npx tsx bin/post-preview-url-comment/index.ts
5444
- uses: actions/cache/save@v4
5545
if: always()
5646
with:

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

Lines changed: 0 additions & 27 deletions
This file was deleted.
File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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;
5+
export const WRANGLER_LOGS_PATH = "wrangler-logs.ndjson";

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

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import * as core from "@actions/core";
22
import * as github from "@actions/github";
33

4+
import { readFile } from "node:fs/promises";
5+
46
import {
57
CONTENT_BASE_PATH,
68
DOCS_BASE_URL,
7-
EXISTING_COMMENT_SUBSTRING,
89
GITHUB_ACTIONS_BOT_ID,
910
PREVIEW_URL_REGEX,
11+
WRANGLER_LOGS_PATH,
1012
} from "./constants";
1113

1214
import { filenameToPath } from "./util";
@@ -15,57 +17,53 @@ async function run(): Promise<void> {
1517
try {
1618
const token = core.getInput("GITHUB_TOKEN", { required: true });
1719
const octokit = github.getOctokit(token);
18-
1920
const ctx = github.context;
2021

21-
if (!ctx.payload.issue) {
22-
core.setFailed(`Payload ${ctx.payload} is missing an 'issue' property`);
22+
const { data: pulls } = await octokit.rest.pulls.list({
23+
...ctx.repo,
24+
head: ctx.ref,
25+
});
26+
27+
const pull_number = pulls.at(0)?.number;
28+
29+
if (!pull_number) {
30+
core.setFailed(`Could not find pull requests for ${ctx.ref}`);
2331
process.exit();
2432
}
2533

26-
const issue = ctx.payload.issue.number;
27-
2834
const files = await octokit.paginate(octokit.rest.pulls.listFiles, {
2935
...ctx.repo,
30-
pull_number: issue,
36+
pull_number,
3137
per_page: 100,
3238
});
3339

3440
const { data: comments } = await octokit.rest.issues.listComments({
35-
owner: ctx.repo.owner,
36-
repo: ctx.repo.repo,
37-
issue_number: issue,
41+
...ctx.repo,
42+
issue_number: pull_number,
3843
per_page: 100,
3944
});
4045

4146
const existingComment = comments.find(
42-
(comment) =>
43-
comment.user?.id === GITHUB_ACTIONS_BOT_ID &&
44-
comment.body?.includes(EXISTING_COMMENT_SUBSTRING),
45-
);
46-
47-
const urlComment = comments.find(
4847
(comment) =>
4948
comment.user?.id === GITHUB_ACTIONS_BOT_ID &&
5049
PREVIEW_URL_REGEX.test(comment.body ?? ""),
5150
);
5251

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-
}
59-
60-
const match = urlComment.body.match(PREVIEW_URL_REGEX);
61-
62-
if (!match) {
63-
core.setFailed(`Could not extract URL from ${urlComment.body}`);
52+
const previewUrl: string = (
53+
await readFile(WRANGLER_LOGS_PATH, { encoding: "utf-8" })
54+
)
55+
.split("\n")
56+
.filter(Boolean)
57+
.map((json) => JSON.parse(json))
58+
.filter((json) => json.type === "version-upload")
59+
.map((json) => json.preview_url)
60+
.at(0);
61+
62+
if (!previewUrl) {
63+
core.setFailed(`Found no version-upload at ${WRANGLER_LOGS_PATH}`);
6464
process.exit();
6565
}
6666

67-
const previewUrl = match[1];
68-
6967
core.debug(previewUrl);
7068

7169
const changedFiles = files
@@ -86,30 +84,31 @@ async function run(): Promise<void> {
8684
return { original, preview };
8785
});
8886

89-
if (changedFiles.length === 0) {
90-
return;
87+
let comment = `**Preview URL:** ${previewUrl}`;
88+
if (changedFiles.length !== 0) {
89+
comment = comment.concat(
90+
`**Files with changes (up to 15)**\n\n| Original Link | Updated Link |\n| --- | --- |\n${changedFiles
91+
.map(
92+
(file) =>
93+
`| [${file.original}](${file.original}) | [${file.preview}](${file.preview}) |`,
94+
)
95+
.join("\n")}`,
96+
);
9197
}
9298

93-
const commentBody = `**Files with changes (up to 15)**\n\n| Original Link | Updated Link |\n| --- | --- |\n${changedFiles
94-
.map(
95-
(file) =>
96-
`| [${file.original}](${file.original}) | [${file.preview}](${file.preview}) |`,
97-
)
98-
.join("\n")}`;
99-
10099
if (existingComment) {
101100
await octokit.rest.issues.updateComment({
102101
owner: ctx.repo.owner,
103102
repo: ctx.repo.repo,
104103
comment_id: existingComment.id,
105-
body: commentBody,
104+
body: comment,
106105
});
107106
} else {
108107
await octokit.rest.issues.createComment({
109108
owner: ctx.repo.owner,
110109
repo: ctx.repo.repo,
111-
issue_number: issue,
112-
body: commentBody,
110+
issue_number: pull_number,
111+
body: comment,
113112
});
114113
}
115114
} catch (error) {
File renamed without changes.

0 commit comments

Comments
 (0)