diff --git a/__tests__/comment.test.ts b/__tests__/comment.test.ts index 8dca126..f8ef90b 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 () => { @@ -104,3 +105,17 @@ it("createComment", async () => { body: "hello there\n" }); }); + +it("deleteComment", async () => { + const octokit = { + issues: { + deleteComment: jest.fn(() => Promise.resolve()) + } + }; + 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..27ab183 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 + recreate: + description: "Indicate if previous comment should be removed before creating a new comment" + required: false message: description: "comment message" required: false diff --git a/lib/comment.js b/lib/comment.js index 88d6b9a..f893cc4 100644 --- a/lib/comment.js +++ b/lib/comment.js @@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.createComment = exports.updateComment = exports.findPreviousComment = void 0; +exports.deleteComment = exports.createComment = exports.updateComment = exports.findPreviousComment = void 0; function headerComment(header) { return ``; } @@ -27,9 +27,15 @@ function updateComment(octokit, repo, comment_id, body, header, previousBody) { }); } exports.updateComment = updateComment; -function createComment(octokit, repo, issue_number, body, header) { +function createComment(octokit, repo, issue_number, body, header, previousBody) { return __awaiter(this, void 0, void 0, function* () { - yield octokit.issues.createComment(Object.assign(Object.assign({}, repo), { issue_number, body: `${body}\n${headerComment(header)}` })); + yield octokit.issues.createComment(Object.assign(Object.assign({}, repo), { issue_number, body: previousBody ? `${previousBody}\n${body}` : `${body}\n${headerComment(header)}` })); }); } exports.createComment = createComment; +function deleteComment(octokit, repo, comment_id) { + return __awaiter(this, void 0, void 0, function* () { + yield octokit.issues.deleteComment(Object.assign(Object.assign({}, repo), { comment_id })); + }); +} +exports.deleteComment = deleteComment; diff --git a/lib/main.js b/lib/main.js index 882773c..33d7274 100644 --- a/lib/main.js +++ b/lib/main.js @@ -47,6 +47,7 @@ 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 recreate = core.getInput("recreate", { required: false }) || false; const githubToken = core.getInput("GITHUB_TOKEN", { required: true }); const octokit = new github_1.GitHub(githubToken); const previous = yield comment_1.findPreviousComment(octokit, repo, number, header); @@ -61,11 +62,13 @@ function run() { body = message; } if (previous) { - if (append) { - yield comment_1.updateComment(octokit, repo, previous.id, body, header, previous.body); + const previousBody = append && previous.body; + if (recreate) { + yield comment_1.deleteComment(octokit, repo, previous.id); + yield comment_1.createComment(octokit, repo, number, body, header, previousBody); } else { - yield comment_1.updateComment(octokit, repo, previous.id, body, header); + yield comment_1.updateComment(octokit, repo, previous.id, body, header, previousBody); } } else { diff --git a/src/comment.ts b/src/comment.ts index aadbb18..5d054d6 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}` : `${body}\n${headerComment(header)}` }); } -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: `${body}\n${headerComment(header)}` + body: previousBody ? `${previousBody}\n${body}` : `${body}\n${headerComment(header)}` + }); +} +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..5a82f92 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 recreate = core.getInput("recreate", { required: false }) || false; const githubToken = core.getInput("GITHUB_TOKEN", { required: true }); const octokit = new GitHub(githubToken); const previous = await findPreviousComment(octokit, repo, number, header); @@ -35,10 +36,12 @@ async function run() { } if (previous) { - if (append) { - await updateComment(octokit, repo, previous.id, body, header, previous.body); + const previousBody = append && previous.body; + if (recreate) { + 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);