Merge pull request #138 from jakemhiller/replace-option

Add option to delete the previous comment and create a new one instead of editing
This commit is contained in:
marocchino 2020-06-25 14:52:22 +09:00 committed by GitHub
commit 9b57fdffdf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 13 deletions

View file

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

View file

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

View file

@ -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 `<!-- Sticky Pull Request Comment${header} -->`;
}
@ -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;

View file

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

View file

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

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