add option to delete the previous comment and create a new one

This commit is contained in:
Jake Hiller 2020-06-23 11:45:02 -04:00
parent 7235ddcfc1
commit 2f6d0e21be
4 changed files with 49 additions and 7 deletions

View file

@ -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: "<!-- Sticky Pull Request CommentTypeA -->\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
});
});

View file

@ -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

View file

@ -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,
});
}

View file

@ -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);