From a5932f0167d68a64592020be39d57f694f5acf68 Mon Sep 17 00:00:00 2001 From: nsugiyam <6680682+nsugiyam@users.noreply.github.com> Date: Wed, 6 Jul 2022 00:16:25 +0900 Subject: [PATCH] Fix Danger to find its own inline comment with an exact ID match --- source/platforms/github/GitHubAPI.ts | 19 +++++++++---------- .../github/_tests/_github_api.test.ts | 10 +++++----- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/source/platforms/github/GitHubAPI.ts b/source/platforms/github/GitHubAPI.ts index 3bf4253ce..9c919b1ad 100644 --- a/source/platforms/github/GitHubAPI.ts +++ b/source/platforms/github/GitHubAPI.ts @@ -89,10 +89,10 @@ export class GitHubAPI { this.d(`User ID: ${userID}`) this.d(`Looking at ${allComments.length} comments for ${dangerIDMessage}`) return allComments - .filter(comment => comment.body.includes(dangerIDMessage)) // does it contain the right danger ID? - .filter(comment => comment.user.id === userID) // Does it have the right user ID? - .filter(comment => comment.body.includes("Generated by")) // Does it look like a danger message? - .map(comment => comment.id) // only return IDs + .filter((comment) => comment.body.includes(dangerIDMessage)) // does it contain the right danger ID? + .filter((comment) => comment.user.id === userID) // Does it have the right user ID? + .filter((comment) => comment.body.includes("Generated by")) // Does it look like a danger message? + .map((comment) => comment.id) // only return IDs } updateCommentWithID = async (id: string, comment: string): Promise => { @@ -289,9 +289,7 @@ export class GitHubAPI { page = getNextPageFromLinkHeader(response) } else { this.d( - `getPullRequestCommits:: Failed to get response while traverse page ${page} with ${ - response.status - }, bailing rest of pages if exists` + `getPullRequestCommits:: Failed to get response while traverse page ${page} with ${response.status}, bailing rest of pages if exists` ) page = -1 } @@ -317,13 +315,14 @@ export class GitHubAPI { const userID = await this.getUserID() const repo = this.repoMetadata.repoSlug const prID = this.repoMetadata.pullRequestID + const dangerIDMessage = dangerIDToString(dangerID) return await this.getAllOfResource(`repos/${repo}/pulls/${prID}/comments`).then((v: GitHubIssueComment[]) => { return v .filter(Boolean) - .map(i => { - return { ...i, ownedByDanger: i.user.id == userID && i.body.includes(dangerID) } + .map((i) => { + return { ...i, ownedByDanger: i.user.id == userID && i.body.includes(dangerIDMessage) } }) - .filter(i => i.ownedByDanger) + .filter((i) => i.ownedByDanger) }) } diff --git a/source/platforms/github/_tests/_github_api.test.ts b/source/platforms/github/_tests/_github_api.test.ts index 3d333f228..3885d2d17 100644 --- a/source/platforms/github/_tests/_github_api.test.ts +++ b/source/platforms/github/_tests/_github_api.test.ts @@ -100,7 +100,7 @@ describe("API testing", () => { it("getDangerCommentIDs ignores comments not marked as generated", async () => { api.getAllOfResource = await requestWithFixturedJSON("github_inline_comments_with_danger.json") - api.getUserID = () => new Promise(r => r(20229914)) + api.getUserID = () => new Promise((r) => r(20229914)) const commentIDs = await api.getDangerCommentIDs("default") @@ -109,9 +109,9 @@ describe("API testing", () => { it("getPullRequestInlineComment gets only comments for given DangerId", async () => { api.getAllOfResource = await requestWithFixturedJSON("github_inline_comments_with_danger.json") - api.getUserID = () => new Promise(r => r(20229914)) + api.getUserID = () => new Promise((r) => r(20229914)) - const comments = await api.getPullRequestInlineComments("danger-id-default") + const comments = await api.getPullRequestInlineComments("default") expect(comments.length).toEqual(1) expect(comments[0].ownedByDanger).toBeTruthy() @@ -119,9 +119,9 @@ describe("API testing", () => { it("getPullRequestInlineComment doesn't get comments as the DangerId is different", async () => { api.getAllOfResource = await requestWithFixturedJSON("github_inline_comments_with_danger.json") - api.getUserID = () => new Promise(r => r(123)) + api.getUserID = () => new Promise((r) => r(123)) - const comments = await api.getPullRequestInlineComments("danger-id-default") + const comments = await api.getPullRequestInlineComments("default") expect(comments.length).toEqual(0) })