From 86d0d6c0886fda52139fb5142254c9a93aebfb33 Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Wed, 7 Jun 2017 09:32:04 -0700 Subject: [PATCH] Fix danger to lookup comment for its own only --- changelog.md | 1 + source/platforms/GitHub.ts | 2 +- source/platforms/github/GitHubAPI.ts | 12 ++++++++---- source/runner/templates/githubIssueTemplate.ts | 7 ++++++- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/changelog.md b/changelog.md index 31a56b880..ae00175c7 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,7 @@ ### Master +* Do not delete comment written from user have same userid for danger - kwonoj * Fix link to `jest` in getting started docs - palleas * Fix yarn install instruction in getting started docs - palleas diff --git a/source/platforms/GitHub.ts b/source/platforms/GitHub.ts index bce886579..b4dfc45ac 100644 --- a/source/platforms/GitHub.ts +++ b/source/platforms/GitHub.ts @@ -97,7 +97,7 @@ export class GitHub { async deleteMainComment(): Promise { const commentID = await this.api.getDangerCommentID() if (commentID) { - await this.api.deleteCommentWithID(commentID) + return await this.api.deleteCommentWithID(commentID) } return commentID !== null diff --git a/source/platforms/github/GitHubAPI.ts b/source/platforms/github/GitHubAPI.ts index 60f3d701f..04a5fdaa7 100644 --- a/source/platforms/github/GitHubAPI.ts +++ b/source/platforms/github/GitHubAPI.ts @@ -2,9 +2,11 @@ import { api as fetch } from "../../api/fetch" import { RepoMetaData } from "../../ci_source/ci_source" import { GitHubPRDSL, GitHubUser} from "../../dsl/GitHubDSL" import * as find from "lodash.find" +import * as v from "voca" import * as node_fetch from "node-fetch" import * as GitHubNodeAPI from "github" +import { dangerSignaturePostfix } from "../../runner/templates/githubIssueTemplate" // The Handle the API specific parts of the github @@ -72,7 +74,8 @@ export class GitHubAPI { async getDangerCommentID(): Promise { const userID = await this.getUserID() const allComments: any[] = await this.getPullRequestComments() - const dangerComment = find(allComments, (comment: any) => comment.user.id === userID) + const dangerComment = find(allComments, (comment: any) => + comment.user.id === userID && v.includes(comment.body, dangerSignaturePostfix)) return dangerComment ? dangerComment.id : null } @@ -85,11 +88,12 @@ export class GitHubAPI { return res.json() } - async deleteCommentWithID(id: number): Promise { + async deleteCommentWithID(id: number): Promise { const repo = this.repoMetadata.repoSlug const res = await this.api(`repos/${repo}/issues/comments/${id}`, {}, {}, "DELETE") - return res.json() + //https://developer.github.com/v3/issues/comments/#response-5 + return Promise.resolve(res.status === 204) } async getUserID(): Promise { @@ -112,7 +116,7 @@ export class GitHubAPI { const prID = this.repoMetadata.pullRequestID const res = await this.get(`repos/${repo}/pulls/${prID}`) - return res.ok ? res.json() as Promise : {} as GitHubPRDSL + return res.ok ? res.json() as Promise : {} as GitHubPRDSL } async getPullRequestCommits(): Promise { diff --git a/source/runner/templates/githubIssueTemplate.ts b/source/runner/templates/githubIssueTemplate.ts index b41a3bef0..def3788c5 100644 --- a/source/runner/templates/githubIssueTemplate.ts +++ b/source/runner/templates/githubIssueTemplate.ts @@ -47,6 +47,11 @@ function buildSummaryMessage(results: DangerResults): string { return summary } +/** + * Postfix signature to be attached comment generated / updated by danger. + */ +export const dangerSignaturePostfix = `Generated by :no_entry_sign: dangerJS` + /** * A template function for creating a GitHub issue comment from Danger Results * @param {DangerResults} results Data to work with @@ -62,7 +67,7 @@ ${table("Warnings", "warning", results.warnings)} ${table("Messages", "book", results.messages)} ${results.markdowns.join("\n\n")}

- Generated by :no_entry_sign: dangerJS + ${dangerSignaturePostfix}

` }