From 97da04af998935f67525e499e26c0523528ee182 Mon Sep 17 00:00:00 2001 From: marocchino Date: Sat, 23 Nov 2019 10:10:39 +0900 Subject: [PATCH 1/3] chore: wallaby config --- wallaby.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 wallaby.js diff --git a/wallaby.js b/wallaby.js new file mode 100644 index 0000000..df2c9e1 --- /dev/null +++ b/wallaby.js @@ -0,0 +1,15 @@ +module.exports = function(wallaby) { + return { + files: ["src/**/*.js?(x)", "!src/**/*.spec.ts?(x)"], + tests: ["__tests__/**/*.test.ts?(x)"], + + env: { + type: "node", + runner: "node" + }, + + testFramework: "jest", + + debug: true + }; +}; From fd286f323081a82abd9d2b98ecdd077eee32b148 Mon Sep 17 00:00:00 2001 From: marocchino Date: Sat, 23 Nov 2019 10:11:28 +0900 Subject: [PATCH 2/3] refactor: extract api wrapper --- src/comment.ts | 21 +++++++++++++++++++++ src/main.ts | 32 +++++++++----------------------- 2 files changed, 30 insertions(+), 23 deletions(-) create mode 100644 src/comment.ts diff --git a/src/comment.ts b/src/comment.ts new file mode 100644 index 0000000..78504db --- /dev/null +++ b/src/comment.ts @@ -0,0 +1,21 @@ +export async function findPreviousComment(octokit, repo, issue_number) { + const { data: comments } = await octokit.issues.listComments({ + ...repo, + issue_number + }); + return comments.find(comment => comment.user.login === "github-actions[bot]"); +} +export async function updateComment(octokit, repo, comment_id, body) { + await octokit.issues.updateComment({ + ...repo, + comment_id, + body + }); +} +export async function createComment(octokit, repo, issue_number, body) { + await octokit.issues.createComment({ + ...repo, + issue_number, + body + }); +} diff --git a/src/main.ts b/src/main.ts index 9172e53..7e6d160 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,40 +1,26 @@ import * as core from "@actions/core"; import { context, GitHub } from "@actions/github"; - +import { findPreviousComment, createComment, updateComment } from "./comment"; async function run() { try { const repo = context.repo; - const issue_number = context?.payload?.pull_request?.number; - if (!issue_number) { + const number = context?.payload?.pull_request?.number; + const body = core.getInput("message"); + const githubToken = core.getInput("GITHUB_TOKEN"); + if (!number) { core.setFailed("This action only works for pull_request"); return; } - const body = core.getInput("message"); - const githubToken = core.getInput("GITHUB_TOKEN"); if (!body || !githubToken) { core.setFailed("invalid input: please check your workflow"); return; } const octokit = new GitHub(githubToken); - const { data: comments } = await octokit.issues.listComments({ - ...repo, - issue_number - }); - const myComment = comments.find( - comment => comment.user.login === "github-actions[bot]" - ); - if (myComment) { - await octokit.issues.updateComment({ - ...repo, - comment_id: myComment.id, - body - }); + const previous = await findPreviousComment(octokit, repo, number); + if (previous) { + await updateComment(octokit, repo, previous.id, body); } else { - await octokit.issues.createComment({ - ...repo, - issue_number, - body - }); + await createComment(octokit, repo, number, body); } } catch ({ message }) { core.setFailed(message); From eba69144cea97d82d3f27e2e5cde5ffb09137a63 Mon Sep 17 00:00:00 2001 From: marocchino Date: Sat, 23 Nov 2019 10:12:15 +0900 Subject: [PATCH 3/3] test: comment --- __tests__/comment.test.ts | 55 +++++++++++++++++++++++++++++++++++++++ __tests__/main.test.ts | 2 -- 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 __tests__/comment.test.ts delete mode 100644 __tests__/main.test.ts diff --git a/__tests__/comment.test.ts b/__tests__/comment.test.ts new file mode 100644 index 0000000..5a14561 --- /dev/null +++ b/__tests__/comment.test.ts @@ -0,0 +1,55 @@ +import { + findPreviousComment, + createComment, + updateComment +} from "../src/comment"; +const repo = {}; +const body = "some message"; +it("findPreviousComment", async () => { + const comment = { + user: { + login: "github-actions[bot]" + } + }; + const otherComment = { + user: { + login: "some-user" + } + }; + const octokit = { + issues: { + listComments: jest.fn(() => + Promise.resolve({ + data: [otherComment, comment] + }) + ) + } + }; + + expect(await findPreviousComment(octokit, repo, 123)).toBe(comment); + 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, body)).toBeUndefined(); + expect(octokit.issues.updateComment).toBeCalledWith({ + comment_id: 456, + body + }); +}); +it("createComment", async () => { + const octokit = { + issues: { + createComment: jest.fn(() => Promise.resolve()) + } + }; + expect(await createComment(octokit, repo, 456, body)).toBeUndefined(); + expect(octokit.issues.createComment).toBeCalledWith({ + issue_number: 456, + body + }); +}); diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts deleted file mode 100644 index 34fbeb9..0000000 --- a/__tests__/main.test.ts +++ /dev/null @@ -1,2 +0,0 @@ -test("create a comment when no comment", async () => {}); -test("update a comment when comment is exists", async () => {});