@@ -9,10 +9,6 @@ const path = require('path');
99
1010const events = [ 'pull_request' , 'pull_request_target' ] ;
1111
12- function commentIdentifier ( workflowName ) {
13- return `### [LCOV](https://github.com/marketplace/actions/report-lcov) of commit`
14- }
15-
1612async function run ( ) {
1713 try {
1814 await exec . exec ( 'sudo apt-get install -y lcov' ) ;
@@ -31,67 +27,73 @@ async function run() {
3127 const minimumCoverage = core . getInput ( 'minimum-coverage' ) ;
3228 const gitHubToken = core . getInput ( 'github-token' ) . trim ( ) ;
3329 const errorMessage = `The code coverage is too low: ${ totalCoverage } . Expected at least ${ minimumCoverage } .` ;
34- const isFailure = totalCoverage < minimumCoverage ;
30+ const isMinimumCoverageReached = totalCoverage >= minimumCoverage ;
3531
3632 if ( gitHubToken !== '' && events . includes ( github . context . eventName ) ) {
3733 const octokit = await github . getOctokit ( gitHubToken ) ;
3834 const summary = await summarize ( coverageFile ) ;
3935 const details = await detail ( coverageFile , octokit ) ;
4036 const sha = github . context . payload . pull_request . head . sha ;
4137 const shaShort = sha . substr ( 0 , 7 ) ;
42- let body = `### ${ title ? `${ title } ` : '' } [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>` ;
38+ const commentHeaderPrefix = `### ${ title ? `${ title } ` : '' } [LCOV](https://github.com/marketplace/actions/report-lcov) of commit` ;
39+ let body = `${ commentHeaderPrefix } [<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>` ;
4340
44- if ( isFailure ) {
41+ if ( ! isMinimumCoverageReached ) {
4542 body += `\n:no_entry: ${ errorMessage } ` ;
4643 }
4744
48- const updateGitHubComment = commentId =>
49- octokit . issues . updateComment ( {
50- repo : github . context . repo . repo ,
51- owner : github . context . repo . owner ,
52- comment_id : commentId ,
53- body,
54- } )
55-
56- if ( updateComment == "true" ) {
57- const issueComments = await octokit . issues . listComments ( {
58- repo : github . context . repo . repo ,
59- owner : github . context . repo . owner ,
60- issue_number : github . context . payload . pull_request . number ,
61- } )
62-
63- const existingComment = issueComments . data . find ( comment =>
64- comment . body . includes ( commentIdentifier ( process . env . GITHUB_WORKFLOW ) ) ,
65- )
66-
67- if ( existingComment ) {
68- console . log ( 'Update Comment ID: ' + existingComment . id ) ;
69- await updateGitHubComment ( existingComment . id ) ;
70- return
71- }
72- console . log ( 'Comment does not exist, create a new one' ) ;
73- }
74-
75- core . debug ( "Creating a comment in the PR." )
76- await octokit . issues . createComment ( {
77- owner : github . context . repo . owner ,
78- repo : github . context . repo . repo ,
79- issue_number : github . context . payload . pull_request . number ,
80- body : body ,
81- } ) ;
45+ updateComment === "true" ? await upsertComment ( body , commentHeaderPrefix , octokit ) : await createNewComment ( body , octokit ) ;
8246 } else {
8347 core . info ( "github-token received is empty. Skipping writing a comment in the PR." ) ;
8448 core . info ( "Note: This could happen even if github-token was provided in workflow file. It could be because your github token does not have permissions for commenting in target repo." )
8549 }
8650
87- if ( isFailure ) {
51+ if ( ! isMinimumCoverageReached ) {
8852 throw Error ( errorMessage ) ;
8953 }
9054 } catch ( error ) {
9155 core . setFailed ( error . message ) ;
9256 }
9357}
9458
59+ async function createNewComment ( body , octokit ) {
60+ core . debug ( "Creating a comment in the PR." )
61+
62+ await octokit . issues . createComment ( {
63+ repo : github . context . repo . repo ,
64+ owner : github . context . repo . owner ,
65+ issue_number : github . context . payload . pull_request . number ,
66+ body,
67+ } ) ;
68+ }
69+
70+ async function upsertComment ( body , commentHeaderPrefix , octokit ) {
71+ const issueComments = await octokit . issues . listComments ( {
72+ repo : github . context . repo . repo ,
73+ owner : github . context . repo . owner ,
74+ issue_number : github . context . payload . pull_request . number ,
75+ } ) ;
76+
77+ const existingComment = issueComments . data . find ( comment =>
78+ comment . body . includes ( commentHeaderPrefix ) ,
79+ ) ;
80+
81+ if ( existingComment ) {
82+ core . debug ( `Updating comment, id: ${ existingComment . id } .` ) ;
83+
84+ await octokit . issues . updateComment ( {
85+ repo : github . context . repo . repo ,
86+ owner : github . context . repo . owner ,
87+ comment_id : existingComment . id ,
88+ body,
89+ } ) ;
90+ } else {
91+ core . debug ( `Commend does not exist, new comment will be created.` ) ;
92+
93+ await createNewComment ( ) ;
94+ }
95+ }
96+
9597async function genhtml ( coverageFiles , tmpPath ) {
9698 const workingDirectory = core . getInput ( 'working-directory' ) . trim ( ) || './' ;
9799 const artifactName = core . getInput ( 'artifact-name' ) . trim ( ) ;
0 commit comments