From 5a3fa9621fcfc784975d2ad465e466fca0c89bfb Mon Sep 17 00:00:00 2001 From: marocchino Date: Sat, 23 Nov 2019 10:43:26 +0900 Subject: [PATCH 1/4] chore: ignore vscode setting --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index ffdd18c..fa6a12f 100644 --- a/.gitignore +++ b/.gitignore @@ -91,3 +91,5 @@ typings/ # DynamoDB Local files .dynamodb/ + +.vscode/ \ No newline at end of file From 0c015d9eeaafc49941cb9e0f56efd6825218e19c Mon Sep 17 00:00:00 2001 From: marocchino Date: Sat, 23 Nov 2019 10:44:03 +0900 Subject: [PATCH 2/4] test: find & write with comment header --- __tests__/comment.test.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/__tests__/comment.test.ts b/__tests__/comment.test.ts index 5a14561..4a9b92d 100644 --- a/__tests__/comment.test.ts +++ b/__tests__/comment.test.ts @@ -4,17 +4,18 @@ import { updateComment } from "../src/comment"; const repo = {}; -const body = "some message"; it("findPreviousComment", async () => { const comment = { user: { login: "github-actions[bot]" - } + }, + body: "\nprevious message" }; const otherComment = { user: { login: "some-user" - } + }, + body: "lgtm" }; const octokit = { issues: { @@ -35,10 +36,12 @@ it("updateComment", async () => { updateComment: jest.fn(() => Promise.resolve()) } }; - expect(await updateComment(octokit, repo, 456, body)).toBeUndefined(); + expect( + await updateComment(octokit, repo, 456, "hello there") + ).toBeUndefined(); expect(octokit.issues.updateComment).toBeCalledWith({ comment_id: 456, - body + body: "\nhello there" }); }); it("createComment", async () => { @@ -47,9 +50,11 @@ it("createComment", async () => { createComment: jest.fn(() => Promise.resolve()) } }; - expect(await createComment(octokit, repo, 456, body)).toBeUndefined(); + expect( + await createComment(octokit, repo, 456, "hello there") + ).toBeUndefined(); expect(octokit.issues.createComment).toBeCalledWith({ issue_number: 456, - body + body: "\nhello there" }); }); From 9550af3011c31590c3be2a1e634b9cecdebc9610 Mon Sep 17 00:00:00 2001 From: marocchino Date: Sat, 23 Nov 2019 10:44:40 +0900 Subject: [PATCH 3/4] feat: find & write with comment header --- src/comment.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/comment.ts b/src/comment.ts index 78504db..7497acc 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -1,21 +1,23 @@ +const HEADER = ""; + 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]"); + return comments.find(comment => comment.body.startsWith(HEADER)); } export async function updateComment(octokit, repo, comment_id, body) { await octokit.issues.updateComment({ ...repo, comment_id, - body + body: `${HEADER}\n${body}` }); } export async function createComment(octokit, repo, issue_number, body) { await octokit.issues.createComment({ ...repo, issue_number, - body + body: `${HEADER}\n${body}` }); } From 1cfcb23af5b9fbff1496ec5f0400d54f498c3209 Mon Sep 17 00:00:00 2001 From: marocchino Date: Sat, 23 Nov 2019 10:47:42 +0900 Subject: [PATCH 4/4] build: comment header --- lib/comment.js | 31 +++++++++++++++++++++++++++++++ lib/main.js | 19 +++++++++---------- 2 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 lib/comment.js diff --git a/lib/comment.js b/lib/comment.js new file mode 100644 index 0000000..e287e6b --- /dev/null +++ b/lib/comment.js @@ -0,0 +1,31 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const HEADER = ""; +function findPreviousComment(octokit, repo, issue_number) { + return __awaiter(this, void 0, void 0, function* () { + const { data: comments } = yield octokit.issues.listComments(Object.assign(Object.assign({}, repo), { issue_number })); + return comments.find(comment => comment.body.startsWith(HEADER)); + }); +} +exports.findPreviousComment = findPreviousComment; +function updateComment(octokit, repo, comment_id, body) { + return __awaiter(this, void 0, void 0, function* () { + yield octokit.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id, body: `${HEADER}\n${body}` })); + }); +} +exports.updateComment = updateComment; +function createComment(octokit, repo, issue_number, body) { + return __awaiter(this, void 0, void 0, function* () { + yield octokit.issues.createComment(Object.assign(Object.assign({}, repo), { issue_number, body: `${HEADER}\n${body}` })); + }); +} +exports.createComment = createComment; diff --git a/lib/main.js b/lib/main.js index 3f28aa8..c6d36b6 100644 --- a/lib/main.js +++ b/lib/main.js @@ -18,31 +18,30 @@ var __importStar = (this && this.__importStar) || function (mod) { Object.defineProperty(exports, "__esModule", { value: true }); const core = __importStar(require("@actions/core")); const github_1 = require("@actions/github"); +const comment_1 = require("./comment"); function run() { var _a, _b, _c; return __awaiter(this, void 0, void 0, function* () { try { const repo = github_1.context.repo; - const issue_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; - if (!issue_number) { + 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 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_1.GitHub(githubToken); - const { data: comments } = yield octokit.issues.listComments(Object.assign(Object.assign({}, repo), { issue_number })); - const myComment = comments.find(comment => comment.user.login === "github-actions[bot]"); - if (myComment) { - yield octokit.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id: myComment.id, body })); + const previous = yield comment_1.findPreviousComment(octokit, repo, number); + if (previous) { + yield comment_1.updateComment(octokit, repo, previous.id, body); } else { - yield octokit.issues.createComment(Object.assign(Object.assign({}, repo), { issue_number, - body })); + yield comment_1.createComment(octokit, repo, number, body); } } catch ({ message }) {