1- import { readdirSync , readFileSync } from "fs" ;
21import { App } from "octokit" ;
32
43if ( ! process . env . GITHUB_REF ?. startsWith ( "refs/pull/" ) ) {
@@ -15,10 +14,12 @@ function requireEnv(name: string): string {
1514const githubRepository = requireEnv ( "GITHUB_REPOSITORY" ) ;
1615const githubRef = requireEnv ( "GITHUB_REF" ) ;
1716
17+ const current_run_id = parseInt ( requireEnv ( "INPUT_RUN_ID" ) ) ;
18+ const current_job_id = parseInt ( requireEnv ( "INPUT_JOB_ID" ) ) ;
19+
1820const [ owner , repo ] = githubRepository . split ( "/" ) ;
1921const pull_request_number = parseInt ( githubRef . split ( "/" ) [ 2 ] ) ;
2022
21- const artifact_regex = requireEnv ( "INPUT_ARTIFACT_REGEX" ) ;
2223const job_regex = requireEnv ( "INPUT_JOB_REGEX" ) ;
2324const step_regex = requireEnv ( "INPUT_STEP_REGEX" ) ;
2425
@@ -31,16 +32,6 @@ const octokit = await app.getInstallationOctokit(installation.id);
3132
3233let body : string | null = null ;
3334
34- const readdirRecursively = ( dir : string ) : string [ ] => {
35- const files : string [ ] = [ ] ;
36- for ( const dirent of readdirSync ( dir , { withFileTypes : true } ) ) {
37- const path = `${ dir } /${ dirent . name } ` ;
38- if ( dirent . isDirectory ( ) ) files . push ( ...readdirRecursively ( path ) ) ;
39- else if ( dirent . isFile ( ) ) files . push ( path ) ;
40- }
41- return files ;
42- } ;
43-
4435interface Row {
4536 url : string ;
4637 status : string ;
@@ -49,38 +40,58 @@ interface Row {
4940
5041const rows : Row [ ] = [ ] ;
5142
52- for ( const file of readdirRecursively ( "." ) ) {
53- console . log ( "looking" , file , "deciding whether skip or not..." ) ;
43+ const { data : jobList } = await octokit . rest . actions . listJobsForWorkflowRun ( {
44+ owner,
45+ repo,
46+ run_id : current_run_id ,
47+ } ) ;
5448
55- const artifactMatch = file . match ( artifact_regex ) ;
49+ for ( const job of jobList . jobs ) {
50+ const job_id = job . id ;
5651
57- if ( artifactMatch === null ) {
58- continue ;
59- }
52+ if ( job_id === current_job_id ) continue ;
53+
54+ const { url : redirectUrl } = await octokit . rest . actions . downloadJobLogsForWorkflowRun ( {
55+ owner,
56+ repo,
57+ job_id,
58+ } ) ;
6059
61- if ( ! artifactMatch . groups ?. runId || ! artifactMatch . groups ?. jobId ) {
62- console . log ( "artifact regex matched but missing runId/jobId named groups, skipping" , file ) ;
60+ const response = await fetch ( redirectUrl ) ;
61+ if ( ! response . ok ) {
62+ console . log ( `failed to retrieve job log for ${ job_id } ` ) ;
6363 continue ;
6464 }
65- const { runId, jobId } = artifactMatch . groups ;
65+ const jobLog = await response . text ( ) ;
66+
67+ const warningRegex = / w a r n i n g ( .\d + ) ? : / ;
68+ const errorRegex = / e r r o r ( .\d + ) ? : / ;
6669
67- console . log ( "found" , file , "detecting warnings..." ) ;
70+ const lines = jobLog . split ( "\n" ) ;
71+ console . log ( `total lines: ${ lines . length } ` ) ;
6872
69- const compilationOutput = readFileSync ( file ) . toString ( ) ;
73+ let offset = 0 ;
74+ const offsetIdx = lines . findIndex ( ( line ) => line . match ( "CPPWARNINGNOTIFIER_LOG_MARKER" ) ) ;
75+ if ( offsetIdx !== - 1 ) offset = offsetIdx ;
7076
7177 let compileResult = "✅success" ;
72- if ( compilationOutput . match ( / w a r n i n g ( .\d + ) ? : / ) ) {
78+ let firstIssueLine = 1 ;
79+ const warningIdx = lines . findIndex ( ( line ) => line . match ( warningRegex ) ) ;
80+ console . log ( `warningIdx: ${ warningIdx } ` ) ;
81+ if ( warningIdx !== - 1 ) {
7382 compileResult = "⚠️warning" ;
74- } else if ( compilationOutput . match ( / e r r o r ( .\d + ) ? : / ) ) {
75- compileResult = "❌error" ;
83+ firstIssueLine = warningIdx - offset + 1 ;
84+ console . log ( `matched warning line: ${ lines [ warningIdx ] } ` ) ;
85+ } else {
86+ const errorIdx = lines . findIndex ( ( line ) => line . match ( errorRegex ) ) ;
87+ console . log ( `errorIdx: ${ errorIdx } ` ) ;
88+ if ( errorIdx !== - 1 ) {
89+ compileResult = "❌error" ;
90+ firstIssueLine = errorIdx - offset + 1 ;
91+ console . log ( `matched error line: ${ lines [ errorIdx ] } ` ) ;
92+ }
7693 }
7794
78- const { data : job } = await octokit . rest . actions . getJobForWorkflowRun ( {
79- owner,
80- repo,
81- job_id : parseInt ( jobId ) ,
82- } ) ;
83-
8495 const steps = job . steps ?? [ ] ;
8596 const stepIndex = steps . findIndex (
8697 ( step ) =>
@@ -102,7 +113,7 @@ for (const file of readdirRecursively(".")) {
102113 }
103114
104115 rows . push ( {
105- url : `https://github.com/${ owner } /${ repo } /actions/runs/${ runId } /job/${ jobId } #step:${ stepId } :1 ` ,
116+ url : `https://github.com/${ owner } /${ repo } /actions/runs/${ current_run_id } /job/${ job_id } #step:${ stepId } :${ firstIssueLine } ` ,
106117 status : compileResult ,
107118 ...jobMatch . groups ,
108119 } ) ;
0 commit comments