Skip to content

Commit bc0168d

Browse files
oacikbartosz347
authored andcommitted
Add option to update the PR comment instead of creating a new one
1 parent 06c2e5e commit bc0168d

File tree

5 files changed

+97
-23
lines changed

5 files changed

+97
-23
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ For more information on these inputs, see the [Workflow syntax for GitHub Action
1818
- `github-token`: Set the `${{ secrets.GITHUB_TOKEN }}` token to have the action comment the coverage summary in the pull request. This token is provided by Actions, you do not need to create your own token. Optional. Default: ``
1919
- `working-directory`: The working directory containing the source files referenced in the LCOV files. Optional. Default: `./`
2020
- `title-prefix`: A prefix before the title "LCOV of commit..." Optional. Default: ``
21+
- `update-comment`: Set to `true` to update previous code coverage comment if such exists. When set to `false` new comment is always created. Optional. Default: `false`
2122

2223
### Outputs
2324
- `total-coverage`: The total coverage from scanned files.
@@ -51,6 +52,7 @@ jobs:
5152
artifact-name: code-coverage-report
5253
github-token: ${{ secrets.GITHUB_TOKEN }}
5354
working-directory: apps/my-first-app
55+
update-comment: true
5456
```
5557
*Note:* Only the `pull_request` and `pull_request_target` events are supported. This action does nothing when triggered by other event types.
5658

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ inputs:
1919
title-prefix:
2020
description: 'A prefix before the title "LCOV of commit..." Optional. Default: ``'
2121
required: false
22+
update-comment:
23+
description: 'Update the existing comment if it exists instead of creating a new one'
24+
required: false
25+
default: "false"
2226
outputs:
2327
total-coverage:
2428
description: 'The total coverage from scanned files.'

dist/main/index.js

Lines changed: 45 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.js

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ async function run() {
1616
const globber = await glob.create(coverageFilesPattern);
1717
const coverageFiles = await globber.glob();
1818
const titlePrefix = core.getInput('title-prefix');
19+
const updateComment = core.getInput('update-comment');
1920

2021
await genhtml(coverageFiles, tmpPath);
2122

@@ -24,42 +25,75 @@ async function run() {
2425
const minimumCoverage = core.getInput('minimum-coverage');
2526
const gitHubToken = core.getInput('github-token').trim();
2627
const errorMessage = `The code coverage is too low: ${totalCoverage}. Expected at least ${minimumCoverage}.`;
27-
const isFailure = totalCoverage < minimumCoverage;
28+
const isMinimumCoverageReached = totalCoverage >= minimumCoverage;
2829

2930
if (gitHubToken !== '' && events.includes(github.context.eventName)) {
3031
const octokit = await github.getOctokit(gitHubToken);
3132
const summary = await summarize(coverageFile);
3233
const details = await detail(coverageFile, octokit);
3334
const sha = github.context.payload.pull_request.head.sha;
3435
const shaShort = sha.substr(0, 7);
35-
let body = `### ${titlePrefix ? `${titlePrefix} ` : ''}[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>`;
36+
const commentHeaderPrefix = `### ${titlePrefix ? `${titlePrefix} ` : ''}[LCOV](https://github.com/marketplace/actions/report-lcov) of commit`;
37+
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>`;
3638

37-
if (isFailure) {
39+
if (!isMinimumCoverageReached) {
3840
body += `\n:no_entry: ${errorMessage}`;
3941
}
4042

41-
core.debug("Creating a comment in the PR.")
42-
await octokit.issues.createComment({
43-
owner: github.context.repo.owner,
44-
repo: github.context.repo.repo,
45-
issue_number: github.context.payload.pull_request.number,
46-
body: body,
47-
});
43+
updateComment === "true" ? await upsertComment(body, commentHeaderPrefix, octokit) : await createNewComment(body, octokit);
4844
} else {
4945
core.info("github-token received is empty. Skipping writing a comment in the PR.");
5046
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.")
5147
}
5248

5349
core.setOutput("total-coverage", totalCoverage);
5450

55-
if (isFailure) {
51+
if (!isMinimumCoverageReached) {
5652
throw Error(errorMessage);
5753
}
5854
} catch (error) {
5955
core.setFailed(error.message);
6056
}
6157
}
6258

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(`Comment does not exist, new comment will be created.`);
92+
93+
await createNewComment(body, octokit);
94+
}
95+
}
96+
6397
async function genhtml(coverageFiles, tmpPath) {
6498
const workingDirectory = core.getInput('working-directory').trim() || './';
6599
const artifactName = core.getInput('artifact-name').trim();

0 commit comments

Comments
 (0)