@@ -25,34 +25,84 @@ async function run() {
2525 const errorMessage = `The code coverage is too low. Expected at least ${ minimumCoverage } .` ;
2626 const isFailure = totalCoverage < minimumCoverage ;
2727
28- if ( gitHubToken !== '' && github . context . eventName === 'pull_request' ) {
28+ if ( gitHubToken !== '' ) {
2929 const octokit = await github . getOctokit ( gitHubToken ) ;
30- const summary = await summarize ( coverageFile ) ;
31- const details = await detail ( coverageFile , octokit ) ;
32- const sha = github . context . payload . pull_request . head . sha ;
33- const shaShort = sha . substr ( 0 , 7 ) ;
34- let body = `### [LCOV](https://github.com/marketplace/actions/report-lcov) of commit [<code>${ shaShort } </code>](${ github . context . payload . pull_request . number } /commits/${ sha } ) during [${ github . context . workflow } #${ github . context . runNumber } ](../actions/runs/${ github . context . runId } )\n<pre>${ summary } \n\nFiles changed coverage rate:${ details } </pre>` ;
35-
36- if ( isFailure ) {
37- body += `\n:no_entry: ${ errorMessage } ` ;
38- }
39-
40- await octokit . issues . createComment ( {
41- owner : github . context . repo . owner ,
42- repo : github . context . repo . repo ,
43- issue_number : github . context . payload . pull_request . number ,
44- body : body ,
45- } ) ;
30+ const prs = pullRequests ( github ) ;
31+ for ( let i = 0 ; i < prs . length ; i ++ ) {
32+ const pr = prs [ i ] ;
33+ console . log ( `Calculating coverage for PR ${ pr . number } , sha ${ pr . head . sha } ...` ) ;
34+ const summary = await summarize ( coverageFile ) ;
35+ const details = await detail ( coverageFile , pr , octokit ) ;
36+ const shaShort = pr . head . sha . substr ( 0 , 7 ) ;
37+ let body = `### Coverage of commit [<code>${ shaShort } </code>](${ pr . number } /commits/${ pr . head . sha } )
38+ <pre>${ summary }
39+
40+ Files changed coverage rate:${ details } </pre>
41+
42+ [Download coverage report](../actions/runs/${ github . context . runId } )
43+ ` ;
44+
45+ if ( isFailure ) {
46+ body += `\n:no_entry: ${ errorMessage } ` ;
47+ }
48+
49+ console . log ( "Posting body:" ) ;
50+ console . log ( body ) ;
51+
52+ try {
53+ await octokit . issues . createComment ( {
54+ issue_number : pr . number ,
55+ body : body ,
56+ ...ownerRepo ( pr . url )
57+ } ) ;
58+ } catch ( error ) {
59+ console . log ( "Unable to post coverage report." ) ;
60+ console . log ( error ) ;
61+ }
62+ } ;
63+ } else {
64+ console . log ( "No GITHUB_TOKEN, not posting." ) ;
4665 }
4766
4867 if ( isFailure ) {
4968 throw Error ( errorMessage ) ;
5069 }
5170 } catch ( error ) {
71+ console . error ( error ) ;
5272 core . setFailed ( error . message ) ;
5373 }
5474}
5575
76+ function pullRequests ( github ) {
77+ if ( github . context . eventName === "pull_request" ) {
78+ return [ github . context . payload . pull_request ] ;
79+ } ;
80+ if ( github . context . eventName == "workflow_run" ) {
81+ if ( github . context . payload . workflow_run . pull_requests . length > 0 ) {
82+ return github . context . payload . workflow_run . pull_requests ;
83+ }
84+ }
85+ if ( ! ! process . env . PR_SHA && ! ! process . env . PR_NUMBER &&
86+ process . env . PR_SHA != "" && process . env . PR_NUMBER != "" ) {
87+ return [ {
88+ number : process . env . PR_NUMBER ,
89+ head : {
90+ sha : process . env . PR_SHA ,
91+ } ,
92+ url : `https://api.github.com/repos/${ github . context . repo . owner } /${ github . context . repo . repo } /pulls/${ process . env . PR_NUMBER } `
93+ } ] ;
94+ }
95+ return [ ] ;
96+ }
97+
98+ function ownerRepo ( url ) {
99+ const match = url . match ( / \/ r e p o s \/ (?< owner > [ ^ \/ ] + ) \/ (?< repo > [ ^ \/ ] + ) \/ p u l l s \/ / ) ;
100+ return {
101+ owner : match [ 1 ] ,
102+ repo : match [ 2 ] ,
103+ } ;
104+ }
105+
56106async function genhtml ( coverageFiles , tmpPath ) {
57107 const workingDirectory = core . getInput ( 'working-directory' ) . trim ( ) || './' ;
58108 const artifactName = core . getInput ( 'artifact-name' ) . trim ( ) ;
@@ -116,14 +166,14 @@ async function summarize(coverageFile) {
116166
117167 const lines = output
118168 . trim ( )
119- . split ( / \r ? \n / )
169+ . split ( / \r ? \n / ) ;
120170
121171 lines . shift ( ) ; // Removes "Reading tracefile..."
122172
123173 return lines . join ( '\n' ) ;
124174}
125175
126- async function detail ( coverageFile , octokit ) {
176+ async function detail ( coverageFile , pull_request , octokit ) {
127177 let output = '' ;
128178
129179 const options = { } ;
@@ -144,17 +194,15 @@ async function detail(coverageFile, octokit) {
144194
145195 let lines = output
146196 . trim ( )
147- . split ( / \r ? \n / )
197+ . split ( / \r ? \n / ) ;
148198
149199 lines . shift ( ) ; // Removes "Reading tracefile..."
150200 lines . pop ( ) ; // Removes "Total..."
151201 lines . pop ( ) ; // Removes "========"
152-
153202 const listFilesOptions = octokit
154203 . pulls . listFiles . endpoint . merge ( {
155- owner : github . context . repo . owner ,
156- repo : github . context . repo . repo ,
157- pull_number : github . context . payload . pull_request . number ,
204+ pull_number : pull_request . number ,
205+ ...ownerRepo ( pull_request . url )
158206 } ) ;
159207 const listFilesResponse = await octokit . paginate ( listFilesOptions ) ;
160208 const changedFiles = listFilesResponse . map ( file => file . filename ) ;
@@ -178,4 +226,4 @@ async function detail(coverageFile, octokit) {
178226 return '\n ' + lines . join ( '\n ' ) ;
179227}
180228
181- run ( ) ;
229+ run ( ) ;
0 commit comments