Skip to content

Commit

Permalink
Add an option to update comments (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-V authored Jun 18, 2022
1 parent ea63f2f commit bb38125
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 35 deletions.
99 changes: 70 additions & 29 deletions __tests__/action_single.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,47 @@ jest.mock("@actions/core");
jest.mock("@actions/github");

describe("Single report", function () {
const comment = jest.fn();
const output = jest.fn();
let createComment;
let listComments;
let updateComment;
let output;

function getInput(key) {
switch (key) {
case "paths":
return "./__tests__/__fixtures__/report.xml";
case "min-coverage-overall":
return 45;
case `min-coverage-changed-files`:
return 60;
}
}

beforeEach(() => {
createComment = jest.fn();
listComments = jest.fn();
updateComment = jest.fn();
output = jest.fn();

core.getInput = jest.fn(getInput);
github.getOctokit = jest.fn(() => {
return {
repos: {
compareCommits: jest.fn(() => {
return compareCommitsResponse;
}),
},
issues: {
createComment: createComment,
listComments: listComments,
updateComment: updateComment,
},
};
});
core.setFailed = jest.fn((c) => {
fail(c);
});
})

const compareCommitsResponse = {
data: {
Expand All @@ -26,32 +65,6 @@ describe("Single report", function () {
},
};

core.getInput = jest.fn((c) => {
switch (c) {
case "paths":
return "./__tests__/__fixtures__/report.xml";
case "min-coverage-overall":
return 45;
case `min-coverage-changed-files`:
return 60;
}
});
github.getOctokit = jest.fn(() => {
return {
repos: {
compareCommits: jest.fn(() => {
return compareCommitsResponse;
}),
},
issues: {
createComment: comment,
},
};
});
core.setFailed = jest.fn((c) => {
fail(c);
});

describe("Pull Request event", function () {
const context = {
eventName: "pull_request",
Expand All @@ -75,7 +88,7 @@ describe("Single report", function () {

await action.action();

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

it("updates a previous comment", async () => {
github.context = context;

const title = 'JaCoCo Report'
core.getInput = jest.fn((c) => {
switch (c) {
case "title":
return title;
case "update-comment":
return "true";
default:
return getInput(c)
}
});

listComments.mockReturnValue({
data: [
{id: 1, body: "some comment"},
{id: 2, body: `### ${title}\n to update`},
]
})

await action.action();

expect(updateComment.mock.calls[0][0].comment_id).toEqual(2);
expect(createComment).toHaveBeenCalledTimes(0);
});

it("set overall coverage output", async () => {
github.context = context;
core.setOutput = output;
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ inputs:
title:
description: "Optional title for the Pull Request comment"
required: false
update-comment:
description: "Update the coverage report comment instead of creating new ones. Requires title to works properly."
required: false
default: "false"
debug-mode:
description: "Run the action in debug mode and get debug logs printed in console"
required: false
Expand Down
38 changes: 32 additions & 6 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async function action() {
core.getInput("min-coverage-changed-files")
);
const title = core.getInput("title");
const updateComment = parseBooleans(core.getInput("update-comment"));
const debugMode = parseBooleans(core.getInput("debug-mode"));
const event = github.context.eventName;
core.info(`Event is ${event}`);
Expand Down Expand Up @@ -71,6 +72,8 @@ async function action() {
if (prNumber != null) {
await addComment(
prNumber,
updateComment,
render.getTitle(title),
render.getPRComment(
overallCoverage.project,
filesCoverage,
Expand Down Expand Up @@ -118,12 +121,35 @@ async function getChangedFiles(base, head, client) {
return changedFiles;
}

async function addComment(prNumber, comment, client) {
await client.issues.createComment({
issue_number: prNumber,
body: comment,
...github.context.repo,
});
async function addComment(prNumber, update, title, body, client) {
let commentUpdated = false;

if (update && title) {
const comments = await client.issues.listComments({
issue_number: prNumber,
...github.context.repo,
});
const comment = comments.data.find((comment) =>
comment.body.startsWith(title),
);

if (comment) {
await client.issues.updateComment({
comment_id: comment.id,
body: body,
...github.context.repo,
});
commentUpdated = true;
}
}

if (!commentUpdated) {
await client.issues.createComment({
issue_number: prNumber,
body: body,
...github.context.repo,
});
}
}

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ function formatCoverage(coverage) {

module.exports = {
getPRComment,
getTitle,
};

0 comments on commit bb38125

Please sign in to comment.