Skip to content

Commit bb38125

Browse files
authored
Add an option to update comments (#17)
1 parent ea63f2f commit bb38125

File tree

4 files changed

+107
-35
lines changed

4 files changed

+107
-35
lines changed

__tests__/action_single.test.js

Lines changed: 70 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,47 @@ jest.mock("@actions/core");
66
jest.mock("@actions/github");
77

88
describe("Single report", function () {
9-
const comment = jest.fn();
10-
const output = jest.fn();
9+
let createComment;
10+
let listComments;
11+
let updateComment;
12+
let output;
13+
14+
function getInput(key) {
15+
switch (key) {
16+
case "paths":
17+
return "./__tests__/__fixtures__/report.xml";
18+
case "min-coverage-overall":
19+
return 45;
20+
case `min-coverage-changed-files`:
21+
return 60;
22+
}
23+
}
24+
25+
beforeEach(() => {
26+
createComment = jest.fn();
27+
listComments = jest.fn();
28+
updateComment = jest.fn();
29+
output = jest.fn();
30+
31+
core.getInput = jest.fn(getInput);
32+
github.getOctokit = jest.fn(() => {
33+
return {
34+
repos: {
35+
compareCommits: jest.fn(() => {
36+
return compareCommitsResponse;
37+
}),
38+
},
39+
issues: {
40+
createComment: createComment,
41+
listComments: listComments,
42+
updateComment: updateComment,
43+
},
44+
};
45+
});
46+
core.setFailed = jest.fn((c) => {
47+
fail(c);
48+
});
49+
})
1150

1251
const compareCommitsResponse = {
1352
data: {
@@ -26,32 +65,6 @@ describe("Single report", function () {
2665
},
2766
};
2867

29-
core.getInput = jest.fn((c) => {
30-
switch (c) {
31-
case "paths":
32-
return "./__tests__/__fixtures__/report.xml";
33-
case "min-coverage-overall":
34-
return 45;
35-
case `min-coverage-changed-files`:
36-
return 60;
37-
}
38-
});
39-
github.getOctokit = jest.fn(() => {
40-
return {
41-
repos: {
42-
compareCommits: jest.fn(() => {
43-
return compareCommitsResponse;
44-
}),
45-
},
46-
issues: {
47-
createComment: comment,
48-
},
49-
};
50-
});
51-
core.setFailed = jest.fn((c) => {
52-
fail(c);
53-
});
54-
5568
describe("Pull Request event", function () {
5669
const context = {
5770
eventName: "pull_request",
@@ -75,7 +88,7 @@ describe("Single report", function () {
7588

7689
await action.action();
7790

78-
expect(comment.mock.calls[0][0].body)
91+
expect(createComment.mock.calls[0][0].body)
7992
.toEqual(`|File|Coverage [63.64%]|:green_apple:|
8093
|:-|:-:|:-:|
8194
|[StringOp.java](https://github.com/thsaravana/jacoco-playground/blob/77b14eb61efcd211ee93a7d8bac80cf292d207cc/src/main/java/com/madrapps/jacoco/operation/StringOp.java)|100%|:green_apple:|
@@ -85,6 +98,34 @@ describe("Single report", function () {
8598
|:-|:-:|:-:|`);
8699
});
87100

101+
it("updates a previous comment", async () => {
102+
github.context = context;
103+
104+
const title = 'JaCoCo Report'
105+
core.getInput = jest.fn((c) => {
106+
switch (c) {
107+
case "title":
108+
return title;
109+
case "update-comment":
110+
return "true";
111+
default:
112+
return getInput(c)
113+
}
114+
});
115+
116+
listComments.mockReturnValue({
117+
data: [
118+
{id: 1, body: "some comment"},
119+
{id: 2, body: `### ${title}\n to update`},
120+
]
121+
})
122+
123+
await action.action();
124+
125+
expect(updateComment.mock.calls[0][0].comment_id).toEqual(2);
126+
expect(createComment).toHaveBeenCalledTimes(0);
127+
});
128+
88129
it("set overall coverage output", async () => {
89130
github.context = context;
90131
core.setOutput = output;

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ inputs:
1818
title:
1919
description: "Optional title for the Pull Request comment"
2020
required: false
21+
update-comment:
22+
description: "Update the coverage report comment instead of creating new ones. Requires title to works properly."
23+
required: false
24+
default: "false"
2125
debug-mode:
2226
description: "Run the action in debug mode and get debug logs printed in console"
2327
required: false

src/action.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ async function action() {
1717
core.getInput("min-coverage-changed-files")
1818
);
1919
const title = core.getInput("title");
20+
const updateComment = parseBooleans(core.getInput("update-comment"));
2021
const debugMode = parseBooleans(core.getInput("debug-mode"));
2122
const event = github.context.eventName;
2223
core.info(`Event is ${event}`);
@@ -71,6 +72,8 @@ async function action() {
7172
if (prNumber != null) {
7273
await addComment(
7374
prNumber,
75+
updateComment,
76+
render.getTitle(title),
7477
render.getPRComment(
7578
overallCoverage.project,
7679
filesCoverage,
@@ -118,12 +121,35 @@ async function getChangedFiles(base, head, client) {
118121
return changedFiles;
119122
}
120123

121-
async function addComment(prNumber, comment, client) {
122-
await client.issues.createComment({
123-
issue_number: prNumber,
124-
body: comment,
125-
...github.context.repo,
126-
});
124+
async function addComment(prNumber, update, title, body, client) {
125+
let commentUpdated = false;
126+
127+
if (update && title) {
128+
const comments = await client.issues.listComments({
129+
issue_number: prNumber,
130+
...github.context.repo,
131+
});
132+
const comment = comments.data.find((comment) =>
133+
comment.body.startsWith(title),
134+
);
135+
136+
if (comment) {
137+
await client.issues.updateComment({
138+
comment_id: comment.id,
139+
body: body,
140+
...github.context.repo,
141+
});
142+
commentUpdated = true;
143+
}
144+
}
145+
146+
if (!commentUpdated) {
147+
await client.issues.createComment({
148+
issue_number: prNumber,
149+
body: body,
150+
...github.context.repo,
151+
});
152+
}
127153
}
128154

129155
module.exports = {

src/render.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,5 @@ function formatCoverage(coverage) {
7575

7676
module.exports = {
7777
getPRComment,
78+
getTitle,
7879
};

0 commit comments

Comments
 (0)