Merge pull request #75 from humio/master

feat: append to previous comment message
This commit is contained in:
marocchino 2020-03-31 11:18:25 +09:00 committed by GitHub
commit dc00a2bb42
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 8 deletions

View file

@ -67,6 +67,14 @@ it("updateComment", async () => {
comment_id: 456, comment_id: 456,
body: "<!-- Sticky Pull Request CommentTypeA -->\nhello there" body: "<!-- Sticky Pull Request CommentTypeA -->\nhello there"
}); });
expect(
await updateComment(octokit, repo, 456, "hello there", "TypeA", "<!-- Sticky Pull Request CommentTypeA -->\nhello there")
).toBeUndefined();
expect(octokit.issues.updateComment).toBeCalledWith({
comment_id: 456,
body: "<!-- Sticky Pull Request CommentTypeA -->\nhello there\nhello there"
});
}); });
it("createComment", async () => { it("createComment", async () => {
const octokit = { const octokit = {

View file

@ -5,6 +5,9 @@ inputs:
header: header:
description: "Header to determine if the comment is to be updated, not shown on screen" description: "Header to determine if the comment is to be updated, not shown on screen"
required: false required: false
append:
description: "Indicate if new comment messages should be appended to previous comment message"
required: false
message: message:
description: "comment message" description: "comment message"
required: true required: true

View file

@ -20,9 +20,9 @@ function findPreviousComment(octokit, repo, issue_number, header) {
}); });
} }
exports.findPreviousComment = findPreviousComment; exports.findPreviousComment = findPreviousComment;
function updateComment(octokit, repo, comment_id, body, header) { function updateComment(octokit, repo, comment_id, body, header, previousBody) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
yield octokit.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id, body: `${headerComment(header)}\n${body}` })); yield octokit.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id, body: previousBody ? `${previousBody}\n${body}` : `${headerComment(header)}\n${body}` }));
}); });
} }
exports.updateComment = updateComment; exports.updateComment = updateComment;

View file

@ -20,9 +20,9 @@ const core = __importStar(require("@actions/core"));
const github_1 = require("@actions/github"); const github_1 = require("@actions/github");
const comment_1 = require("./comment"); const comment_1 = require("./comment");
function run() { function run() {
var _a, _b, _c; var _a, _b;
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const number = ((_c = (_b = (_a = github_1.context) === null || _a === void 0 ? void 0 : _a.payload) === null || _b === void 0 ? void 0 : _b.pull_request) === null || _c === void 0 ? void 0 : _c.number) || const number = ((_b = (_a = github_1.context === null || github_1.context === void 0 ? void 0 : github_1.context.payload) === null || _a === void 0 ? void 0 : _a.pull_request) === null || _b === void 0 ? void 0 : _b.number) ||
+core.getInput("number", { required: false }); +core.getInput("number", { required: false });
if (isNaN(number) || number < 1) { if (isNaN(number) || number < 1) {
core.info("no numbers given: skip step"); core.info("no numbers given: skip step");
@ -32,11 +32,17 @@ function run() {
const repo = github_1.context.repo; const repo = github_1.context.repo;
const body = core.getInput("message", { required: true }); const body = core.getInput("message", { required: true });
const header = core.getInput("header", { required: false }) || ""; const header = core.getInput("header", { required: false }) || "";
const append = core.getInput("append", { required: false }) || false;
const githubToken = core.getInput("GITHUB_TOKEN", { required: true }); const githubToken = core.getInput("GITHUB_TOKEN", { required: true });
const octokit = new github_1.GitHub(githubToken); const octokit = new github_1.GitHub(githubToken);
const previous = yield comment_1.findPreviousComment(octokit, repo, number, header); const previous = yield comment_1.findPreviousComment(octokit, repo, number, header);
if (previous) { if (previous) {
yield comment_1.updateComment(octokit, repo, previous.id, body, header); if (append) {
yield comment_1.updateComment(octokit, repo, previous.id, body, header, previous.body);
}
else {
yield comment_1.updateComment(octokit, repo, previous.id, body, header);
}
} }
else { else {
yield comment_1.createComment(octokit, repo, number, body, header); yield comment_1.createComment(octokit, repo, number, body, header);

View file

@ -10,11 +10,11 @@ export async function findPreviousComment(octokit, repo, issue_number, header) {
const h = headerComment(header); const h = headerComment(header);
return comments.find(comment => comment.body.startsWith(h)); return comments.find(comment => comment.body.startsWith(h));
} }
export async function updateComment(octokit, repo, comment_id, body, header) { export async function updateComment(octokit, repo, comment_id, body, header, previousBody?) {
await octokit.issues.updateComment({ await octokit.issues.updateComment({
...repo, ...repo,
comment_id, comment_id,
body: `${headerComment(header)}\n${body}` 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) {

View file

@ -15,11 +15,16 @@ async function run() {
const repo = context.repo; const repo = context.repo;
const body = core.getInput("message", { required: true }); const body = core.getInput("message", { required: true });
const header = core.getInput("header", { required: false }) || ""; const header = core.getInput("header", { required: false }) || "";
const append = core.getInput("append", { required: false }) || false;
const githubToken = core.getInput("GITHUB_TOKEN", { required: true }); const githubToken = core.getInput("GITHUB_TOKEN", { required: true });
const octokit = new GitHub(githubToken); const octokit = new GitHub(githubToken);
const previous = await findPreviousComment(octokit, repo, number, header); const previous = await findPreviousComment(octokit, repo, number, header);
if (previous) { if (previous) {
await updateComment(octokit, repo, previous.id, body, header); if (append) {
await updateComment(octokit, repo, previous.id, body, header, previous.body);
} else {
await updateComment(octokit, repo, previous.id, body, header);
}
} else { } else {
await createComment(octokit, repo, number, body, header); await createComment(octokit, repo, number, body, header);
} }