From 168a45153d992ae18b74bda2dd18e2ba566d0807 Mon Sep 17 00:00:00 2001 From: Simon Stender Boisen Date: Thu, 26 Mar 2020 14:30:28 +0100 Subject: [PATCH] feat: append to previous comment message --- __tests__/comment.test.ts | 8 ++++++++ action.yml | 3 +++ src/comment.ts | 4 ++-- src/main.ts | 7 ++++++- 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/__tests__/comment.test.ts b/__tests__/comment.test.ts index b57f2d9..578c3ff 100644 --- a/__tests__/comment.test.ts +++ b/__tests__/comment.test.ts @@ -67,6 +67,14 @@ it("updateComment", async () => { comment_id: 456, body: "\nhello there" }); + + expect( + await updateComment(octokit, repo, 456, "hello there", "TypeA", "\nhello there") + ).toBeUndefined(); + expect(octokit.issues.updateComment).toBeCalledWith({ + comment_id: 456, + body: "\nhello there\nhello there" + }); }); it("createComment", async () => { const octokit = { diff --git a/action.yml b/action.yml index 571964f..eb083b3 100644 --- a/action.yml +++ b/action.yml @@ -5,6 +5,9 @@ inputs: header: description: "Header to determine if the comment is to be updated, not shown on screen" required: false + append: + description: "Indicate if new comment messages should be appended to previous comment message" + required: false message: description: "comment message" required: true diff --git a/src/comment.ts b/src/comment.ts index 7544984..b311a86 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -10,11 +10,11 @@ export async function findPreviousComment(octokit, repo, issue_number, header) { const h = headerComment(header); 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({ ...repo, 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) { diff --git a/src/main.ts b/src/main.ts index ed8e2f4..c83c9ff 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,11 +15,16 @@ async function run() { const repo = context.repo; const body = core.getInput("message", { required: true }); const header = core.getInput("header", { required: false }) || ""; + const append = core.getInput("append", { required: false }) || false; const githubToken = core.getInput("GITHUB_TOKEN", { required: true }); const octokit = new GitHub(githubToken); const previous = await findPreviousComment(octokit, repo, number, header); 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 { await createComment(octokit, repo, number, body, header); }