From 1ee5fc7fa0cbcbbab1a0c147ccc9795a8160b04d Mon Sep 17 00:00:00 2001 From: Jake Hiller Date: Wed, 24 Jun 2020 10:38:30 -0400 Subject: [PATCH 1/5] update hidden identifier comment to come last instead of first --- __tests__/comment.test.ts | 29 ++++++++++++++++++----------- src/comment.ts | 12 +++++++++--- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/__tests__/comment.test.ts b/__tests__/comment.test.ts index 578c3ff..18faab0 100644 --- a/__tests__/comment.test.ts +++ b/__tests__/comment.test.ts @@ -9,14 +9,20 @@ it("findPreviousComment", async () => { user: { login: "github-actions[bot]" }, - body: "\nprevious message" + body: "previous message\n" }; const commentWithCustomHeader = { user: { login: "github-actions[bot]" }, - body: "\nprevious message" + body: "previous message\n" }; + const headerFirstComment = { + user: { + login: "github-actions[bot]" + }, + body: "\nheader first message" + } const otherComments = [ { user: { @@ -28,14 +34,14 @@ it("findPreviousComment", async () => { user: { login: "github-actions[bot]" }, - body: "\nprevious message" - } + body: "previous message\n" + }, ]; const octokit = { issues: { listComments: jest.fn(() => Promise.resolve({ - data: [commentWithCustomHeader, comment, ...otherComments] + data: [commentWithCustomHeader, comment, headerFirstComment, ...otherComments] }) ) } @@ -45,6 +51,7 @@ it("findPreviousComment", async () => { expect(await findPreviousComment(octokit, repo, 123, "TypeA")).toBe( commentWithCustomHeader ); + expect(await findPreviousComment(octokit, repo, 123, "LegacyComment")).toBe(headerFirstComment) expect(octokit.issues.listComments).toBeCalledWith({ issue_number: 123 }); }); it("updateComment", async () => { @@ -58,22 +65,22 @@ it("updateComment", async () => { ).toBeUndefined(); expect(octokit.issues.updateComment).toBeCalledWith({ comment_id: 456, - body: "\nhello there" + body: "hello there\n" }); expect( await updateComment(octokit, repo, 456, "hello there", "TypeA") ).toBeUndefined(); expect(octokit.issues.updateComment).toBeCalledWith({ comment_id: 456, - body: "\nhello there" + body: "hello there\n" }); expect( - await updateComment(octokit, repo, 456, "hello there", "TypeA", "\nhello there") + await updateComment(octokit, repo, 456, "hello there", "TypeA", "hello there\n") ).toBeUndefined(); expect(octokit.issues.updateComment).toBeCalledWith({ comment_id: 456, - body: "\nhello there\nhello there" + body: "hello there\nhello there\n" }); }); it("createComment", async () => { @@ -87,13 +94,13 @@ it("createComment", async () => { ).toBeUndefined(); expect(octokit.issues.createComment).toBeCalledWith({ issue_number: 456, - body: "\nhello there" + body: "hello there\n" }); expect( await createComment(octokit, repo, 456, "hello there", "TypeA") ).toBeUndefined(); expect(octokit.issues.createComment).toBeCalledWith({ issue_number: 456, - body: "\nhello there" + body: "hello there\n" }); }); diff --git a/src/comment.ts b/src/comment.ts index b311a86..0c21a9d 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -2,25 +2,31 @@ function headerComment(header) { return ``; } +function removeHeaderComment(body, header) { + return body.replace(`\n${header}`, ''); +} + export async function findPreviousComment(octokit, repo, issue_number, header) { const { data: comments } = await octokit.issues.listComments({ ...repo, issue_number }); const h = headerComment(header); - return comments.find(comment => comment.body.startsWith(h)); + return comments.find(comment => comment.body.includes(h)); } export async function updateComment(octokit, repo, comment_id, body, header, previousBody?) { + const headerIdentifier = headerComment(header); + const updatedBody = previousBody ? `${removeHeaderComment(previousBody, headerIdentifier)}\n${body}` : body; await octokit.issues.updateComment({ ...repo, comment_id, - body: previousBody ? `${previousBody}\n${body}` : `${headerComment(header)}\n${body}` + body: `${updatedBody}\n${headerIdentifier}` }); } export async function createComment(octokit, repo, issue_number, body, header) { await octokit.issues.createComment({ ...repo, issue_number, - body: `${headerComment(header)}\n${body}` + body: `${body}\n${headerComment(header)}` }); } From 2015926b3a02df0288b5edb3b0df15a85eeb4935 Mon Sep 17 00:00:00 2001 From: Jake Hiller Date: Wed, 24 Jun 2020 10:47:39 -0400 Subject: [PATCH 2/5] run build --- lib/comment.js | 12 +++++++++--- lib/main.js | 26 +++++++++++++++++++------- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lib/comment.js b/lib/comment.js index 1fd06d5..186a3d6 100644 --- a/lib/comment.js +++ b/lib/comment.js @@ -9,26 +9,32 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }); }; Object.defineProperty(exports, "__esModule", { value: true }); +exports.createComment = exports.updateComment = exports.findPreviousComment = void 0; function headerComment(header) { return ``; } +function removeHeaderComment(body, header) { + return body.replace(`\n${header}`, ''); +} function findPreviousComment(octokit, repo, issue_number, header) { return __awaiter(this, void 0, void 0, function* () { const { data: comments } = yield octokit.issues.listComments(Object.assign(Object.assign({}, repo), { issue_number })); const h = headerComment(header); - return comments.find(comment => comment.body.startsWith(h)); + return comments.find(comment => comment.body.includes(h)); }); } exports.findPreviousComment = findPreviousComment; function updateComment(octokit, repo, comment_id, body, header, previousBody) { return __awaiter(this, void 0, void 0, function* () { - yield octokit.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id, body: previousBody ? `${previousBody}\n${body}` : `${headerComment(header)}\n${body}` })); + const headerIdentifier = headerComment(header); + const updatedBody = previousBody ? `${removeHeaderComment(previousBody, headerIdentifier)}\n${body}` : body; + yield octokit.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id, body: `${updatedBody}\n${headerIdentifier}` })); }); } exports.updateComment = updateComment; function createComment(octokit, repo, issue_number, body, header) { return __awaiter(this, void 0, void 0, function* () { - yield octokit.issues.createComment(Object.assign(Object.assign({}, repo), { issue_number, body: `${headerComment(header)}\n${body}` })); + yield octokit.issues.createComment(Object.assign(Object.assign({}, repo), { issue_number, body: `${body}\n${headerComment(header)}` })); }); } exports.createComment = createComment; diff --git a/lib/main.js b/lib/main.js index 724e15a..882773c 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1,4 +1,23 @@ "use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; 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) { @@ -8,13 +27,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; -var __importStar = (this && this.__importStar) || function (mod) { - if (mod && mod.__esModule) return mod; - var result = {}; - if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; - result["default"] = mod; - return result; -}; Object.defineProperty(exports, "__esModule", { value: true }); const core = __importStar(require("@actions/core")); const github_1 = require("@actions/github"); From 7a2cc8a3dc2491ffbcf5da372eba9e2432c8fd66 Mon Sep 17 00:00:00 2001 From: Jake Hiller Date: Thu, 25 Jun 2020 01:25:52 -0400 Subject: [PATCH 3/5] simplify comment logic --- src/comment.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/comment.ts b/src/comment.ts index 0c21a9d..aadbb18 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -2,10 +2,6 @@ function headerComment(header) { return ``; } -function removeHeaderComment(body, header) { - return body.replace(`\n${header}`, ''); -} - export async function findPreviousComment(octokit, repo, issue_number, header) { const { data: comments } = await octokit.issues.listComments({ ...repo, @@ -15,12 +11,10 @@ export async function findPreviousComment(octokit, repo, issue_number, header) { return comments.find(comment => comment.body.includes(h)); } export async function updateComment(octokit, repo, comment_id, body, header, previousBody?) { - const headerIdentifier = headerComment(header); - const updatedBody = previousBody ? `${removeHeaderComment(previousBody, headerIdentifier)}\n${body}` : body; await octokit.issues.updateComment({ ...repo, comment_id, - body: `${updatedBody}\n${headerIdentifier}` + body: previousBody ? `${previousBody}\n${body}` : `${body}\n${headerComment(header)}` }); } export async function createComment(octokit, repo, issue_number, body, header) { From 8c58ada506f20ef8b59f4954b8bc2ac14e39c3e6 Mon Sep 17 00:00:00 2001 From: Jake Hiller Date: Thu, 25 Jun 2020 01:28:21 -0400 Subject: [PATCH 4/5] build --- lib/comment.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/comment.js b/lib/comment.js index 186a3d6..88d6b9a 100644 --- a/lib/comment.js +++ b/lib/comment.js @@ -13,9 +13,6 @@ exports.createComment = exports.updateComment = exports.findPreviousComment = vo function headerComment(header) { return ``; } -function removeHeaderComment(body, header) { - return body.replace(`\n${header}`, ''); -} function findPreviousComment(octokit, repo, issue_number, header) { return __awaiter(this, void 0, void 0, function* () { const { data: comments } = yield octokit.issues.listComments(Object.assign(Object.assign({}, repo), { issue_number })); @@ -26,9 +23,7 @@ function findPreviousComment(octokit, repo, issue_number, header) { exports.findPreviousComment = findPreviousComment; function updateComment(octokit, repo, comment_id, body, header, previousBody) { return __awaiter(this, void 0, void 0, function* () { - const headerIdentifier = headerComment(header); - const updatedBody = previousBody ? `${removeHeaderComment(previousBody, headerIdentifier)}\n${body}` : body; - yield octokit.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id, body: `${updatedBody}\n${headerIdentifier}` })); + yield octokit.issues.updateComment(Object.assign(Object.assign({}, repo), { comment_id, body: previousBody ? `${previousBody}\n${body}` : `${body}\n${headerComment(header)}` })); }); } exports.updateComment = updateComment; From a4ce3ea5b07fcbbe34ad3ad070e24de459011502 Mon Sep 17 00:00:00 2001 From: Jake Hiller Date: Thu, 25 Jun 2020 01:30:25 -0400 Subject: [PATCH 5/5] update tests --- __tests__/comment.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/__tests__/comment.test.ts b/__tests__/comment.test.ts index 18faab0..8dca126 100644 --- a/__tests__/comment.test.ts +++ b/__tests__/comment.test.ts @@ -80,7 +80,7 @@ it("updateComment", async () => { ).toBeUndefined(); expect(octokit.issues.updateComment).toBeCalledWith({ comment_id: 456, - body: "hello there\nhello there\n" + body: "hello there\n\nhello there" }); }); it("createComment", async () => {