Skip to content

Commit

Permalink
Only use a single comment when the action fails
Browse files Browse the repository at this point in the history
* Update existing comment if found
* Delete comment when action passes
  • Loading branch information
mheap committed Apr 2, 2023
1 parent 1fc8581 commit bfcecaf
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 5 deletions.
39 changes: 37 additions & 2 deletions index.js
@@ -1,6 +1,7 @@
const core = require("@actions/core");
const github = require("@actions/github");

const matchToken = `<!-- reqlabelmessage -->`;
async function action() {
try {
const token = core.getInput("token", { required: true });
Expand Down Expand Up @@ -84,6 +85,24 @@ async function action() {
return;
}

// Remove the comment if it exists
if (shouldAddComment) {
const { data: existing } = await octokit.rest.issues.listComments({
...github.context.repo,
issue_number: github.context.issue.number,
});

const generatedComment = existing.find((c) =>
c.body.includes(matchToken)
);
if (generatedComment) {
await octokit.rest.issues.deleteComment({
...github.context.repo,
comment_id: generatedComment.id,
});
}
}

core.setOutput("status", "success");
} catch (e) {
core.setFailed(e.message);
Expand All @@ -98,11 +117,27 @@ function tmpl(t, o) {

async function exitWithError(exitType, octokit, shouldAddComment, message) {
if (shouldAddComment) {
await octokit.rest.issues.createComment({
// Is there an existing comment?
const { data: existing } = await octokit.rest.issues.listComments({
...github.context.repo,
issue_number: github.context.issue.number,
body: message,
});

const generatedComment = existing.find((c) => c.body.includes(matchToken));

const params = {
...github.context.repo,
issue_number: github.context.issue.number,
body: `${matchToken}${message}`,
};

// If so, update it
let method = "createComment";
if (generatedComment) {
method = "updateComment";
params.comment_id = generatedComment.id;
}
await octokit.rest.issues[method](params);
}

core.setOutput("status", "failure");
Expand Down
90 changes: 87 additions & 3 deletions index.test.js
Expand Up @@ -6,6 +6,8 @@ const mockedEnv = require("mocked-env");
const nock = require("nock");
nock.disableNetConnect();

const matchToken = `<!-- reqlabelmessage -->`;

describe("Required Labels", () => {
let restore;
let restoreTest;
Expand Down Expand Up @@ -90,14 +92,35 @@ describe("Required Labels", () => {

mockLabels(["bug"]);

mockListComments([]);

nock("https://api.github.com")
.post("/repos/mheap/missing-repo/issues/28/comments", {
body: "Label error. Requires exactly 1 of: enhancement. Found: bug",
body: `${matchToken}Label error. Requires exactly 1 of: enhancement. Found: bug`,
})
.reply(201);

await action();
});

it("deletes a comment when passing", async () => {
restoreTest = mockPr({
INPUT_LABELS: "bug",
INPUT_MODE: "exactly",
INPUT_COUNT: "1",
INPUT_ADD_COMMENT: "true",
GITHUB_TOKEN: "mock-token-here-abc",
});

mockLabels(["bug"]);
mockListComments([{ id: "12345", body: `${matchToken}This` }]);

nock("https://api.github.com")
.delete("/repos/mheap/missing-repo/issues/comments/12345")
.reply(200);

await action();
});
});

describe("success", () => {
Expand Down Expand Up @@ -369,9 +392,58 @@ describe("Required Labels", () => {

mockLabels(["enhancement", "bug"]);

mockListComments([]);

nock("https://api.github.com")
.post("/repos/mheap/missing-repo/issues/28/comments", {
body: `${matchToken}This is a static comment`,
})
.reply(201);

await action();
});

it("updates an existing comment when one is found", async () => {
restoreTest = mockPr({
GITHUB_TOKEN: "abc123",
INPUT_LABELS: "enhancement,bug",
INPUT_MODE: "exactly",
INPUT_COUNT: "1",
INPUT_ADD_COMMENT: "true",
INPUT_MESSAGE: "This is a static comment",
});

mockLabels(["enhancement", "bug"]);

mockListComments([{ id: "12345", body: `${matchToken}This` }]);

nock("https://api.github.com")
.patch("/repos/mheap/missing-repo/issues/comments/12345", {
issue_number: 28,
body: `${matchToken}This is a static comment`,
})
.reply(200);

await action();
});

it("creates when comments exist but don't match", async () => {
restoreTest = mockPr({
GITHUB_TOKEN: "abc123",
INPUT_LABELS: "enhancement,bug",
INPUT_MODE: "exactly",
INPUT_COUNT: "1",
INPUT_ADD_COMMENT: "true",
INPUT_MESSAGE: "This is a static comment",
});

mockLabels(["enhancement", "bug"]);

mockListComments([{ id: "12345", body: `No Match` }]);

nock("https://api.github.com")
.post("/repos/mheap/missing-repo/issues/28/comments", {
body: "This is a static comment",
body: `${matchToken}This is a static comment`,
})
.reply(201);

Expand All @@ -392,9 +464,10 @@ describe("Required Labels", () => {

mockLabels(["enhancement", "bug"]);

mockListComments([]);
nock("https://api.github.com")
.post("/repos/mheap/missing-repo/issues/28/comments", {
body: "Mode: exactly, Count: 1, Error String: exactly, Provided: enhancement, bug, Applied: enhancement, bug",
body: `${matchToken}Mode: exactly, Count: 1, Error String: exactly, Provided: enhancement, bug, Applied: enhancement, bug`,
})
.reply(201);

Expand Down Expand Up @@ -428,6 +501,17 @@ function mockLabels(labels) {
);
}

function mockListComments(comments) {
nock("https://api.github.com")
.get("/repos/mheap/missing-repo/issues/28/comments")
.reply(
200,
comments.map((c) => {
return { body: c.body, id: c.id };
})
);
}

function mockEvent(eventName, mockPayload, additionalParams = {}) {
github.context.payload = mockPayload;

Expand Down

0 comments on commit bfcecaf

Please sign in to comment.