diff --git a/__tests__/comment.test.ts b/__tests__/comment.test.ts index 578c3ff..4eaac2b 100644 --- a/__tests__/comment.test.ts +++ b/__tests__/comment.test.ts @@ -1,7 +1,8 @@ import { findPreviousComment, createComment, - updateComment + updateComment, + deleteComment } from "../src/comment"; const repo = {}; it("findPreviousComment", async () => { @@ -97,3 +98,30 @@ it("createComment", async () => { body: "\nhello there" }); }); + +it("removeComment", async () => { + const octokit = { + issues: { + deleteComment: jest.fn(() => Promise.resolve()) + } + }; + expect( + await deleteComment(octokit, repo, 456) + ).toBeUndefined(); + expect(octokit.issues.deleteComment).toBeCalledWith({ + comment_id: 456 + }); + expect( + await deleteComment(octokit, repo, 456) + ).toBeUndefined(); + expect(octokit.issues.deleteComment).toBeCalledWith({ + comment_id: 456 + }); + + expect( + await deleteComment(octokit, repo, 456) + ).toBeUndefined(); + expect(octokit.issues.deleteComment).toBeCalledWith({ + comment_id: 456 + }); +}); diff --git a/action.yml b/action.yml index 4aec2f6..252bbb2 100644 --- a/action.yml +++ b/action.yml @@ -8,6 +8,9 @@ inputs: append: description: "Indicate if new comment messages should be appended to previous comment message" required: false + replace: + description: "Indicate if previous comment should be removed before creating a new comment" + required: false message: description: "comment message" required: false diff --git a/src/comment.ts b/src/comment.ts index b311a86..1d4cab6 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -17,10 +17,16 @@ export async function updateComment(octokit, repo, comment_id, body, header, pre body: previousBody ? `${previousBody}\n${body}` : `${headerComment(header)}\n${body}` }); } -export async function createComment(octokit, repo, issue_number, body, header) { +export async function createComment(octokit, repo, issue_number, body, header, previousBody?) { await octokit.issues.createComment({ ...repo, issue_number, - body: `${headerComment(header)}\n${body}` + body: previousBody ? `${previousBody}\n${body}` : `${headerComment(header)}\n${body}` + }); +} +export async function deleteComment(octokit, repo, comment_id) { + await octokit.issues.deleteComment({ + ...repo, + comment_id, }); } diff --git a/src/main.ts b/src/main.ts index 8a8b036..0d7b4a3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,6 @@ import * as core from "@actions/core"; import { context, GitHub } from "@actions/github"; -import { findPreviousComment, createComment, updateComment } from "./comment"; +import { findPreviousComment, createComment, updateComment, deleteComment } from "./comment"; import { readFileSync } from 'fs'; async function run() { @@ -18,6 +18,7 @@ async function run() { const path = core.getInput("path", { required: false }); const header = core.getInput("header", { required: false }) || ""; const append = core.getInput("append", { required: false }) || false; + const replace = core.getInput("replace", { required: false }) || false; const githubToken = core.getInput("GITHUB_TOKEN", { required: true }); const octokit = new GitHub(githubToken); const previous = await findPreviousComment(octokit, repo, number, header); @@ -34,11 +35,15 @@ async function run() { body = message; } + let previousBody; + if (append && previous) previousBody = previous.body; + if (previous) { - if (append) { - await updateComment(octokit, repo, previous.id, body, header, previous.body); + if (replace) { + await deleteComment(octokit, repo, previous.id); + await createComment(octokit, repo, number, body, header, previousBody); } else { - await updateComment(octokit, repo, previous.id, body, header); + await updateComment(octokit, repo, previous.id, body, header, previousBody); } } else { await createComment(octokit, repo, number, body, header);