@@ -7,10 +7,6 @@ const lcovTotal = require("lcov-total");
7
7
const os = require ( 'os' ) ;
8
8
const path = require ( 'path' ) ;
9
9
10
- function commentIdentifier ( workflowName ) {
11
- return `### [LCOV](https://github.com/marketplace/actions/report-lcov) of commit`
12
- }
13
-
14
10
async function run ( ) {
15
11
try {
16
12
await exec . exec ( 'sudo apt-get install -y lcov' ) ;
@@ -28,67 +24,73 @@ async function run() {
28
24
const minimumCoverage = core . getInput ( 'minimum-coverage' ) ;
29
25
const gitHubToken = core . getInput ( 'github-token' ) . trim ( ) ;
30
26
const errorMessage = `The code coverage is too low. Expected at least ${ minimumCoverage } .` ;
31
- const isFailure = totalCoverage < minimumCoverage ;
27
+ const isMinimumCoverageReached = totalCoverage >= minimumCoverage ;
32
28
33
29
if ( gitHubToken !== '' && github . context . eventName === 'pull_request' ) {
34
30
const octokit = await github . getOctokit ( gitHubToken ) ;
35
31
const summary = await summarize ( coverageFile ) ;
36
32
const details = await detail ( coverageFile , octokit ) ;
37
33
const sha = github . context . payload . pull_request . head . sha ;
38
34
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>` ;
40
37
41
- if ( isFailure ) {
38
+ if ( ! isMinimumCoverageReached ) {
42
39
body += `\n:no_entry: ${ errorMessage } ` ;
43
40
}
44
41
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 ) ;
79
43
} else {
80
44
core . info ( "github-token received is empty. Skipping writing a comment in the PR." ) ;
81
45
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." )
82
46
}
83
47
84
- if ( isFailure ) {
48
+ if ( ! isMinimumCoverageReached ) {
85
49
throw Error ( errorMessage ) ;
86
50
}
87
51
} catch ( error ) {
88
52
core . setFailed ( error . message ) ;
89
53
}
90
54
}
91
55
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
+
92
94
async function genhtml ( coverageFiles , tmpPath ) {
93
95
const workingDirectory = core . getInput ( 'working-directory' ) . trim ( ) || './' ;
94
96
const artifactName = core . getInput ( 'artifact-name' ) . trim ( ) ;
0 commit comments