Merge pull request #201 from SvanBoxel/ignore-empty-body-update

Ignore empty body update
This commit is contained in:
marocchino 2020-11-06 09:59:39 +09:00 committed by GitHub
commit 9e8e1d498c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 51 deletions

View file

@ -4,6 +4,13 @@ import {
updateComment,
deleteComment
} from "../src/comment";
import * as core from '@actions/core';
jest.mock('@actions/core', () => ({
warning: jest.fn()
}));
const repo = {};
it("findPreviousComment", async () => {
const comment = {
@ -55,55 +62,85 @@ it("findPreviousComment", async () => {
expect(await findPreviousComment(octokit, repo, 123, "LegacyComment")).toBe(headerFirstComment)
expect(octokit.issues.listComments).toBeCalledWith({ issue_number: 123 });
});
it("updateComment", async () => {
const octokit = {
issues: {
updateComment: jest.fn(() => Promise.resolve())
}
};
expect(
await updateComment(octokit, repo, 456, "hello there", "")
).toBeUndefined();
expect(octokit.issues.updateComment).toBeCalledWith({
comment_id: 456,
body: "hello there\n<!-- Sticky Pull Request Comment -->"
});
expect(
await updateComment(octokit, repo, 456, "hello there", "TypeA")
).toBeUndefined();
expect(octokit.issues.updateComment).toBeCalledWith({
comment_id: 456,
body: "hello there\n<!-- Sticky Pull Request CommentTypeA -->"
describe("updateComment", () => {
let octokit;
beforeEach(() => {
octokit = {
issues: {
updateComment: jest.fn(() => Promise.resolve())
}
};
})
it("with comment body", async() => {
expect(
await updateComment(octokit, repo, 456, "hello there", "")
).toBeUndefined();
expect(octokit.issues.updateComment).toBeCalledWith({
comment_id: 456,
body: "hello there\n<!-- Sticky Pull Request Comment -->"
});
expect(
await updateComment(octokit, repo, 456, "hello there", "TypeA")
).toBeUndefined();
expect(octokit.issues.updateComment).toBeCalledWith({
comment_id: 456,
body: "hello there\n<!-- Sticky Pull Request CommentTypeA -->"
});
expect(
await updateComment(octokit, repo, 456, "hello there", "TypeA", "hello there\n<!-- Sticky Pull Request CommentTypeA -->")
).toBeUndefined();
expect(octokit.issues.updateComment).toBeCalledWith({
comment_id: 456,
body: "hello there\n<!-- Sticky Pull Request CommentTypeA -->\nhello there"
});
});
expect(
await updateComment(octokit, repo, 456, "hello there", "TypeA", "hello there\n<!-- Sticky Pull Request CommentTypeA -->")
).toBeUndefined();
expect(octokit.issues.updateComment).toBeCalledWith({
comment_id: 456,
body: "hello there\n<!-- Sticky Pull Request CommentTypeA -->\nhello there"
});
it("without comment body and previousbody", async() => {
expect(
await updateComment(octokit, repo, 456, "", "")
).toBeUndefined();
expect(octokit.issues.updateComment).not.toBeCalled();
expect(core.warning).toBeCalledWith('Comment body cannot be blank');
})
});
it("createComment", async () => {
const octokit = {
issues: {
createComment: jest.fn(() => Promise.resolve())
}
};
expect(
await createComment(octokit, repo, 456, "hello there", "")
).toBeUndefined();
expect(octokit.issues.createComment).toBeCalledWith({
issue_number: 456,
body: "hello there\n<!-- Sticky Pull Request Comment -->"
});
expect(
await createComment(octokit, repo, 456, "hello there", "TypeA")
).toBeUndefined();
expect(octokit.issues.createComment).toBeCalledWith({
issue_number: 456,
body: "hello there\n<!-- Sticky Pull Request CommentTypeA -->"
});
describe("createComment", () => {
let octokit;
beforeEach(() => {
octokit = {
issues: {
createComment: jest.fn(() => Promise.resolve())
}
};
})
it("with comment body or previousBody", async () => {
expect(
await createComment(octokit, repo, 456, "hello there", "")
).toBeUndefined();
expect(octokit.issues.createComment).toBeCalledWith({
issue_number: 456,
body: "hello there\n<!-- Sticky Pull Request Comment -->"
});
expect(
await createComment(octokit, repo, 456, "hello there", "TypeA")
).toBeUndefined();
expect(octokit.issues.createComment).toBeCalledWith({
issue_number: 456,
body: "hello there\n<!-- Sticky Pull Request CommentTypeA -->"
});
})
it("without comment body and previousBody", async () => {
expect(
await createComment(octokit, repo, 456, "", "")
).toBeUndefined();
expect(octokit.issues.createComment).not.toBeCalled();
expect(core.warning).toBeCalledWith('Comment body cannot be blank');
})
});
it("deleteComment", async () => {

View file

@ -44,15 +44,15 @@ exports.findPreviousComment = findPreviousComment;
function updateComment(octokit, repo, comment_id, body, header, previousBody) {
return __awaiter(this, void 0, void 0, function* () {
if (!body && !previousBody)
core.warning('Comment body cannot be blank');
return core.warning('Comment body cannot be blank');
yield octokit.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id, body: previousBody ? `${previousBody}\n${body}` : `${body}\n${headerComment(header)}` }));
});
}
exports.updateComment = updateComment;
function createComment(octokit, repo, issue_number, body, header, previousBody) {
return __awaiter(this, void 0, void 0, function* () {
if (!body)
core.warning('Comment body cannot be blank');
if (!body && !previousBody)
return core.warning('Comment body cannot be blank');
yield octokit.issues.createComment(Object.assign(Object.assign({}, repo), { issue_number, body: previousBody ? `${previousBody}\n${body}` : `${body}\n${headerComment(header)}` }));
});
}

View file

@ -13,7 +13,7 @@ export async function findPreviousComment(octokit, repo, issue_number, header) {
return comments.find(comment => comment.body.includes(h));
}
export async function updateComment(octokit, repo, comment_id, body, header, previousBody?) {
if (!body && !previousBody) core.warning('Comment body cannot be blank');
if (!body && !previousBody) return core.warning('Comment body cannot be blank');
await octokit.issues.updateComment({
...repo,
@ -22,7 +22,7 @@ export async function updateComment(octokit, repo, comment_id, body, header, pre
});
}
export async function createComment(octokit, repo, issue_number, body, header, previousBody?) {
if (!body) core.warning('Comment body cannot be blank');
if (!body && !previousBody) return core.warning('Comment body cannot be blank');
await octokit.issues.createComment({
...repo,