Skip to content

Commit 58720d0

Browse files
oacikbartosz347
authored andcommitted
Option to update the PR comment
1 parent 7a0c803 commit 58720d0

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ inputs:
1616
description: 'Set the GitHub 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: ``'
1717
working-directory:
1818
description: 'The working directory containing the source files referenced in the LCOV files. Optional. Default: `./`'
19+
update-comment:
20+
description: Update the existing comment if it exists instead of creating a new one
21+
required: false
22+
default: false
1923
runs:
2024
using: 'node12'
2125
main: 'dist/main/index.js'

src/main.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ const lcovTotal = require("lcov-total");
77
const os = require('os');
88
const path = require('path');
99

10+
function commentIdentifier(workflowName) {
11+
return `### [LCOV](https://github.com/marketplace/actions/report-lcov) of commit`
12+
}
13+
1014
async function run() {
1115
try {
1216
await exec.exec('sudo apt-get install -y lcov');
@@ -15,6 +19,7 @@ async function run() {
1519
const coverageFilesPattern = core.getInput('coverage-files');
1620
const globber = await glob.create(coverageFilesPattern);
1721
const coverageFiles = await globber.glob();
22+
const updateComment = core.getInput('update-comment');
1823

1924
await genhtml(coverageFiles, tmpPath);
2025

@@ -37,6 +42,33 @@ async function run() {
3742
body += `\n:no_entry: ${errorMessage}`;
3843
}
3944

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+
4072
core.debug("Creating a comment in the PR.")
4173
await octokit.issues.createComment({
4274
owner: github.context.repo.owner,

0 commit comments

Comments
 (0)