@@ -7,10 +7,6 @@ const lcovTotal = require("lcov-total");
77const os = require ( 'os' ) ;
88const path = require ( 'path' ) ;
99
10- function commentIdentifier ( workflowName ) {
11- return `### [LCOV](https://github.com/marketplace/actions/report-lcov) of commit`
12- }
13-
1410async function run ( ) {
1511 try {
1612 await exec . exec ( 'sudo apt-get install -y lcov' ) ;
@@ -28,67 +24,73 @@ async function run() {
2824 const minimumCoverage = core . getInput ( 'minimum-coverage' ) ;
2925 const gitHubToken = core . getInput ( 'github-token' ) . trim ( ) ;
3026 const errorMessage = `The code coverage is too low. Expected at least ${ minimumCoverage } .` ;
31- const isFailure = totalCoverage < minimumCoverage ;
27+ const isMinimumCoverageReached = totalCoverage >= minimumCoverage ;
3228
3329 if ( gitHubToken !== '' && github . context . eventName === 'pull_request' ) {
3430 const octokit = await github . getOctokit ( gitHubToken ) ;
3531 const summary = await summarize ( coverageFile ) ;
3632 const details = await detail ( coverageFile , octokit ) ;
3733 const sha = github . context . payload . pull_request . head . sha ;
3834 const shaShort = sha . substr ( 0 , 7 ) ;
39- 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+ const commentHeaderPrefix = '### [LCOV](https://github.com/marketplace/actions/report-lcov) of commit' ;
36+ 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>` ;
4037
41- if ( isFailure ) {
38+ if ( ! isMinimumCoverageReached ) {
4239 body += `\n:no_entry: ${ errorMessage } ` ;
4340 }
4441
45- const updateGitHubComment = commentId =>
46- octokit . issues . updateComment ( {
47- repo : github . context . repo . repo ,
48- owner : github . context . repo . owner ,
49- comment_id : commentId ,
50- body,
51- } )
52-
53- if ( updateComment == "true" ) {
54- const issueComments = await octokit . issues . listComments ( {
55- repo : github . context . repo . repo ,
56- owner : github . context . repo . owner ,
57- issue_number : github . context . payload . pull_request . number ,
58- } )
59-
60- const existingComment = issueComments . data . find ( comment =>
61- comment . body . includes ( commentIdentifier ( process . env . GITHUB_WORKFLOW ) ) ,
62- )
63-
64- if ( existingComment ) {
65- console . log ( 'Update Comment ID: ' + existingComment . id ) ;
66- await updateGitHubComment ( existingComment . id ) ;
67- return
68- }
69- console . log ( 'Comment does not exist, create a new one' ) ;
70- }
71-
72- core . debug ( "Creating a comment in the PR." )
73- await octokit . issues . createComment ( {
74- owner : github . context . repo . owner ,
75- repo : github . context . repo . repo ,
76- issue_number : github . context . payload . pull_request . number ,
77- body : body ,
78- } ) ;
42+ updateComment === "true" ? await upsertComment ( body , commentHeaderPrefix , octokit ) : await createNewComment ( body , octokit ) ;
7943 } else {
8044 core . info ( "github-token received is empty. Skipping writing a comment in the PR." ) ;
8145 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." )
8246 }
8347
84- if ( isFailure ) {
48+ if ( ! isMinimumCoverageReached ) {
8549 throw Error ( errorMessage ) ;
8650 }
8751 } catch ( error ) {
8852 core . setFailed ( error . message ) ;
8953 }
9054}
9155
56+ async function createNewComment ( body , octokit ) {
57+ core . debug ( "Creating a comment in the PR." )
58+
59+ await octokit . issues . createComment ( {
60+ repo : github . context . repo . repo ,
61+ owner : github . context . repo . owner ,
62+ issue_number : github . context . payload . pull_request . number ,
63+ body,
64+ } ) ;
65+ }
66+
67+ async function upsertComment ( body , commentHeaderPrefix , octokit ) {
68+ const issueComments = await octokit . issues . listComments ( {
69+ repo : github . context . repo . repo ,
70+ owner : github . context . repo . owner ,
71+ issue_number : github . context . payload . pull_request . number ,
72+ } ) ;
73+
74+ const existingComment = issueComments . data . find ( comment =>
75+ comment . body . includes ( commentHeaderPrefix ) ,
76+ ) ;
77+
78+ if ( existingComment ) {
79+ core . debug ( `Updating comment, id: ${ existingComment . id } .` ) ;
80+
81+ await octokit . issues . updateComment ( {
82+ repo : github . context . repo . repo ,
83+ owner : github . context . repo . owner ,
84+ comment_id : existingComment . id ,
85+ body,
86+ } ) ;
87+ } else {
88+ core . debug ( `Commend does not exist, new comment will be created.` ) ;
89+
90+ await createNewComment ( ) ;
91+ }
92+ }
93+
9294async function genhtml ( coverageFiles , tmpPath ) {
9395 const workingDirectory = core . getInput ( 'working-directory' ) . trim ( ) || './' ;
9496 const artifactName = core . getInput ( 'artifact-name' ) . trim ( ) ;
0 commit comments