@@ -15,6 +15,10 @@ export async function run(): Promise<void> {
1515 run_id : context . runId ,
1616 } ) ;
1717 const startedAt = currentRun . data . run_started_at ;
18+ if ( ! startedAt ) {
19+ throw new Error ( "Missing run_started_at for current workflow run" ) ;
20+ }
21+
1822 const currentRunDurationInMillis =
1923 currentTime - new Date ( startedAt ) . getTime ( ) ;
2024
@@ -38,15 +42,17 @@ export async function run(): Promise<void> {
3842 "No data for historical runs on master/main branch found. Can't compare." ;
3943 } else {
4044 const latestRunOnMaster = latestRunsOnMaster [ 0 ] ;
45+ if ( ! latestRunOnMaster . run_started_at ) {
46+ throw new Error ( "Missing run_started_at for latest run on master" ) ;
47+ }
4148 const latestMasterRunDurationInMillis =
4249 new Date ( latestRunOnMaster . updated_at ) . getTime ( ) -
4350 new Date ( latestRunOnMaster . run_started_at ) . getTime ( ) ;
4451 const diffInSeconds =
4552 ( currentRunDurationInMillis - latestMasterRunDurationInMillis ) / 1000 ;
46- const percentageDiff = (
53+ const percentageDiff =
4754 ( 1 - currentRunDurationInMillis / latestMasterRunDurationInMillis ) *
48- 100
49- ) . toFixed ( 2 ) ;
55+ 100 ;
5056 const outcome = diffInSeconds > 0 ? "an increase" : "a decrease" ;
5157
5258 outputMessage =
@@ -59,44 +65,38 @@ export async function run(): Promise<void> {
5965 " with " +
6066 Math . abs ( diffInSeconds ) +
6167 "s (" +
62- Math . abs ( percentageDiff ) +
68+ Math . abs ( percentageDiff ) . toFixed ( 2 ) +
6369 "%) compared to latest run on master/main." ;
6470 }
6571
66- const commentData = {
72+ const existingComments = await github . rest . issues . listComments ( {
6773 owner : context . repo . owner ,
6874 repo : context . repo . repo ,
6975 issue_number : context . issue . number ,
70- } ;
71-
72- const comments = await github . rest . issues . listComments ( commentData ) ;
73-
74- /**
75- * Add the body content after comments are fetched so that it's
76- * not included in the search for existing comments.
77- */
78- commentData . body = outputMessage ;
79-
80- /**
81- * Search comments from the bottom-up to find the most recent comment
82- * from the GitHub Actions bot that matches our criteria.
83- */
84- const existingComment = comments . data . reverse ( ) . find ( ( comment ) => {
76+ } ) ;
77+ const existingComment = existingComments . data . reverse ( ) . find ( ( comment ) => {
8578 return (
8679 comment ?. user ?. login === "github-actions[bot]" &&
8780 comment ?. user ?. type === "Bot" &&
8881 comment ?. body ?. startsWith ( `🕒 Workflow "${ context . workflow } " took ` )
8982 ) ;
9083 } ) ;
9184
92- // If the comment exists then update instead of creating a new one.
93- const action = existingComment ? "updateComment" : "createComment" ;
85+ const commentInput = {
86+ owner : context . repo . owner ,
87+ repo : context . repo . repo ,
88+ issue_number : context . issue . number ,
89+ body : outputMessage ,
90+ } ;
9491
9592 if ( existingComment ) {
96- commentData . comment_id = existingComment . id ;
93+ await github . rest . issues [ "updateComment" ] ( {
94+ ...commentInput ,
95+ comment_id : existingComment . id ,
96+ } ) ;
97+ } else {
98+ await github . rest . issues [ "createComment" ] ( commentInput ) ;
9799 }
98-
99- await github . rest . issues [ action ] ( commentData ) ;
100100 } catch ( error ) {
101101 if ( error instanceof Error ) core . setFailed ( error . message ) ;
102102 }
0 commit comments