Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix danger to lookup comment for its own only #270

Merged
merged 1 commit into from
Jun 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion source/platforms/GitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class GitHub {
async deleteMainComment(): Promise <boolean> {
const commentID = await this.api.getDangerCommentID()
if (commentID) {
await this.api.deleteCommentWithID(commentID)
return await this.api.deleteCommentWithID(commentID)
}

return commentID !== null
Expand Down
12 changes: 8 additions & 4 deletions source/platforms/github/GitHubAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -72,7 +74,8 @@ export class GitHubAPI {
async getDangerCommentID(): Promise<number | null> {
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
}

Expand All @@ -85,11 +88,12 @@ export class GitHubAPI {
return res.json()
}

async deleteCommentWithID(id: number): Promise<any> {
async deleteCommentWithID(id: number): Promise<boolean> {
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<number> {
Expand All @@ -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<GitHubPRDSL> : {} as GitHubPRDSL
return res.ok ? res.json() as Promise<GitHubPRDSL> : {} as GitHubPRDSL
}

async getPullRequestCommits(): Promise<any> {
Expand Down
7 changes: 6 additions & 1 deletion source/runner/templates/githubIssueTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: <a href="http://github.com/danger/danger-js/">dangerJS</a>`

/**
* A template function for creating a GitHub issue comment from Danger Results
* @param {DangerResults} results Data to work with
Expand All @@ -62,7 +67,7 @@ ${table("Warnings", "warning", results.warnings)}
${table("Messages", "book", results.messages)}
${results.markdowns.join("\n\n")}
<p align="right">
Generated by :no_entry_sign: <a href="http://github.com/danger/danger-js/">dangerJS</a>
${dangerSignaturePostfix}
</p>
`
}