From c873eaf14f997aeb1ff0de0a3aacb49d7f7639dd Mon Sep 17 00:00:00 2001 From: marocchino Date: Tue, 4 Feb 2020 17:19:50 +0900 Subject: [PATCH] feat: add ability to set custom header --- __tests__/comment.test.ts | 45 +++++++++++++++++++++++++++++++++------ action.yml | 3 +++ src/comment.ts | 19 ++++++++++------- src/main.ts | 7 +++--- 4 files changed, 56 insertions(+), 18 deletions(-) diff --git a/__tests__/comment.test.ts b/__tests__/comment.test.ts index 4a9b92d..b57f2d9 100644 --- a/__tests__/comment.test.ts +++ b/__tests__/comment.test.ts @@ -11,23 +11,40 @@ it("findPreviousComment", async () => { }, body: "\nprevious message" }; - const otherComment = { + const commentWithCustomHeader = { user: { - login: "some-user" + login: "github-actions[bot]" }, - body: "lgtm" + body: "\nprevious message" }; + const otherComments = [ + { + user: { + login: "some-user" + }, + body: "lgtm" + }, + { + user: { + login: "github-actions[bot]" + }, + body: "\nprevious message" + } + ]; const octokit = { issues: { listComments: jest.fn(() => Promise.resolve({ - data: [otherComment, comment] + data: [commentWithCustomHeader, comment, ...otherComments] }) ) } }; - expect(await findPreviousComment(octokit, repo, 123)).toBe(comment); + expect(await findPreviousComment(octokit, repo, 123, "")).toBe(comment); + expect(await findPreviousComment(octokit, repo, 123, "TypeA")).toBe( + commentWithCustomHeader + ); expect(octokit.issues.listComments).toBeCalledWith({ issue_number: 123 }); }); it("updateComment", async () => { @@ -37,12 +54,19 @@ it("updateComment", async () => { } }; expect( - await updateComment(octokit, repo, 456, "hello there") + await updateComment(octokit, repo, 456, "hello there", "") ).toBeUndefined(); expect(octokit.issues.updateComment).toBeCalledWith({ comment_id: 456, body: "\nhello there" }); + expect( + await updateComment(octokit, repo, 456, "hello there", "TypeA") + ).toBeUndefined(); + expect(octokit.issues.updateComment).toBeCalledWith({ + comment_id: 456, + body: "\nhello there" + }); }); it("createComment", async () => { const octokit = { @@ -51,10 +75,17 @@ it("createComment", async () => { } }; expect( - await createComment(octokit, repo, 456, "hello there") + await createComment(octokit, repo, 456, "hello there", "") ).toBeUndefined(); expect(octokit.issues.createComment).toBeCalledWith({ issue_number: 456, body: "\nhello there" }); + expect( + await createComment(octokit, repo, 456, "hello there", "TypeA") + ).toBeUndefined(); + expect(octokit.issues.createComment).toBeCalledWith({ + issue_number: 456, + body: "\nhello there" + }); }); diff --git a/action.yml b/action.yml index bd2533e..571964f 100644 --- a/action.yml +++ b/action.yml @@ -2,6 +2,9 @@ name: "Sticky Pull Request Comment" description: "Create comment on pull request, if exists update that comment." author: "marocchino" inputs: + header: + description: "Header to determine if the comment is to be updated, not shown on screen" + required: false message: description: "comment message" required: true diff --git a/src/comment.ts b/src/comment.ts index 7497acc..549a5f1 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -1,23 +1,26 @@ -const HEADER = ""; - -export async function findPreviousComment(octokit, repo, issue_number) { +export async function findPreviousComment(octokit, repo, issue_number, header) { const { data: comments } = await octokit.issues.listComments({ ...repo, issue_number }); - return comments.find(comment => comment.body.startsWith(HEADER)); + const h = headerComment(header); + return comments.find(comment => comment.body.startsWith(h)); } -export async function updateComment(octokit, repo, comment_id, body) { +export async function updateComment(octokit, repo, comment_id, body, header) { await octokit.issues.updateComment({ ...repo, comment_id, - body: `${HEADER}\n${body}` + body: `${headerComment(header)}\n${body}` }); } -export async function createComment(octokit, repo, issue_number, body) { +export async function createComment(octokit, repo, issue_number, body, header) { await octokit.issues.createComment({ ...repo, issue_number, - body: `${HEADER}\n${body}` + body: `${headerComment(header)}\n${body}` }); } + +function headerComment(header) { + return ``; +} diff --git a/src/main.ts b/src/main.ts index 8696371..ed8e2f4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,13 +14,14 @@ async function run() { try { const repo = context.repo; const body = core.getInput("message", { required: true }); + const header = core.getInput("header", { required: false }) || ""; const githubToken = core.getInput("GITHUB_TOKEN", { required: true }); const octokit = new GitHub(githubToken); - const previous = await findPreviousComment(octokit, repo, number); + const previous = await findPreviousComment(octokit, repo, number, header); if (previous) { - await updateComment(octokit, repo, previous.id, body); + await updateComment(octokit, repo, previous.id, body, header); } else { - await createComment(octokit, repo, number, body); + await createComment(octokit, repo, number, body, header); } } catch ({ message }) { core.setFailed(message);