Skip to content

Commit 9c6fe3c

Browse files
oacikbartosz347
authored andcommitted
Option to update the PR comment
1 parent ce8a425 commit 9c6fe3c

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

action.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@ 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
runs:
2327
using: 'node16'
2428
main: 'dist/main/index.js'
2529
post: 'dist/post/index.js'
2630
branding:
2731
icon: umbrella
28-
color: purple
32+
color: purple

src/main.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const path = require('path');
99

1010
const 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+
1216
async function run() {
1317
try {
1418
await exec.exec('sudo apt-get install -y lcov');
@@ -18,6 +22,7 @@ async function run() {
1822
const globber = await glob.create(coverageFilesPattern);
1923
const coverageFiles = await globber.glob();
2024
const title = core.getInput('title');
25+
const updateComment = core.getInput('update-comment');
2126

2227
await genhtml(coverageFiles, tmpPath);
2328

@@ -40,6 +45,33 @@ async function run() {
4045
body += `\n:no_entry: ${errorMessage}`;
4146
}
4247

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

0 commit comments

Comments
 (0)