1+ import { readFileSync } from "node:fs" ;
2+
13import { Octokit } from "@octokit/rest" ;
24import { graphql } from "@octokit/graphql" ;
35
6+ import { decideCommentAction , statuses } from "./comment.ts" ;
7+ import { headSha , renderTrailer } from "./trailer.ts" ;
8+
49if ( ! process . env . GITHUB_REF ?. startsWith ( "refs/pull/" ) ) {
510 console . log ( "not a pull request, exiting." ) ;
611 process . exit ( 0 ) ;
@@ -14,6 +19,8 @@ function requireEnv(name: string): string {
1419
1520const githubRepository = requireEnv ( "GITHUB_REPOSITORY" ) ;
1621const githubRef = requireEnv ( "GITHUB_REF" ) ;
22+ const githubSha = requireEnv ( "GITHUB_SHA" ) ;
23+ const githubEventPath = requireEnv ( "GITHUB_EVENT_PATH" ) ;
1724
1825const runId = parseInt ( requireEnv ( "INPUT_RUN_ID" ) ) ;
1926const jobId = parseInt ( requireEnv ( "INPUT_JOB_ID" ) ) ;
@@ -85,19 +92,19 @@ for (const job of jobList.jobs) {
8592 }
8693 }
8794
88- let compileResult = "✅ success" ;
95+ let compileResult : string = statuses . success ;
8996 let firstIssueLine = 1 ;
9097 const warningIdx = lines . findIndex ( ( line ) => line . match ( warningRegex ) ) ;
9198 console . log ( `warningIdx: ${ warningIdx } ` ) ;
9299 if ( warningIdx !== - 1 ) {
93- compileResult = "⚠️warning" ;
100+ compileResult = statuses . warning ;
94101 firstIssueLine = warningIdx - offset + 1 ;
95102 console . log ( `matched warning line: ${ lines [ warningIdx ] } ` ) ;
96103 } else {
97104 const errorIdx = lines . findIndex ( ( line ) => line . match ( errorRegex ) ) ;
98105 console . log ( `errorIdx: ${ errorIdx } ` ) ;
99106 if ( errorIdx !== - 1 ) {
100- compileResult = "❌ error" ;
107+ compileResult = statuses . error ;
101108 firstIssueLine = errorIdx - offset + 1 ;
102109 console . log ( `matched error line: ${ lines [ errorIdx ] } ` ) ;
103110 }
@@ -208,12 +215,20 @@ function generateTable(entries: Row[]): string {
208215 return `<table>${ thead } ${ tbody } </table>` ;
209216}
210217
211- const body = generateTable ( rows ) ;
218+ const table = generateTable ( rows ) ;
219+ const event : unknown = JSON . parse ( readFileSync ( githubEventPath , "utf8" ) ) ;
220+ const trailer = renderTrailer ( {
221+ owner,
222+ repo,
223+ sha : headSha ( event , githubSha ) ,
224+ updatedAt : new Date ( ) ,
225+ } ) ;
226+ const body = `${ table } \n\n${ trailer } ` ;
212227
213228console . log ( "body is" , body ) ;
214229
215230if ( body ) {
216- console . log ( "outdates previous comments" ) ;
231+ console . log ( "checking previous comments" ) ;
217232 const { data : comments } = await octokit . issues . listComments ( {
218233 owner,
219234 repo,
@@ -234,26 +249,51 @@ if (body) {
234249 . filter ( ( comment ) => comment . user ?. login === "cppwarningnotifier[bot]" )
235250 . toSorted ( ( a , b ) => new Date ( a . created_at ) . getTime ( ) - new Date ( b . created_at ) . getTime ( ) ) ;
236251
237- if ( sortedComments . length > 0 ) {
238- const latestComment = sortedComments [ sortedComments . length - 1 ] ;
239-
240- if ( body . includes ( "warning" ) || latestComment . body ?. includes ( "warning" ) ) {
241- await gql (
242- `mutation MinimizeComment($id: ID!) {
243- minimizeComment(input: { subjectId: $id, classifier: OUTDATED }) {
244- clientMutationId
245- }
246- }` ,
247- { id : latestComment . node_id } ,
248- ) ;
252+ const action = decideCommentAction (
253+ rows . map ( ( r ) => r . status ) ,
254+ sortedComments . at ( - 1 ) ,
255+ ) ;
249256
257+ switch ( action . kind ) {
258+ case "create" :
250259 await postComment ( ) ;
251- }
252- } else {
253- await postComment ( ) ;
260+ break ;
261+ case "update" :
262+ console . log ( "updating the previous comment" ) ;
263+ await octokit . issues . updateComment ( {
264+ owner,
265+ repo,
266+ comment_id : action . previous . id ,
267+ body,
268+ } ) ;
269+ if ( action . unminimize ) await unminimizeIfHidden ( action . previous . node_id ) ;
270+ break ;
254271 }
255272}
256273
274+ async function unminimizeIfHidden ( nodeId : string ) : Promise < void > {
275+ const { node } = await gql < { node : { isMinimized : boolean } | null } > (
276+ `query IsMinimized($id: ID!) {
277+ node(id: $id) {
278+ ... on IssueComment {
279+ isMinimized
280+ }
281+ }
282+ }` ,
283+ { id : nodeId } ,
284+ ) ;
285+ if ( ! node ?. isMinimized ) return ;
286+ console . log ( "unminimizing the previous comment" ) ;
287+ await gql (
288+ `mutation UnminimizeComment($id: ID!) {
289+ unminimizeComment(input: { subjectId: $id }) {
290+ clientMutationId
291+ }
292+ }` ,
293+ { id : nodeId } ,
294+ ) ;
295+ }
296+
257297// ── Worker authentication helper ────────────────────────────────────────────
258298
259299async function getInstallationTokenFromWorker ( workerUrl : string ) : Promise < string > {
0 commit comments