11import * as core from "@actions/core" ;
22import * as github from "@actions/github" ;
33
4+ import { readFile } from "node:fs/promises" ;
5+
46import {
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
1214import { 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 ) {
0 commit comments