Skip to content

Commit

Permalink
Merge pull request #800 from cysp/feature/github-api-types
Browse files Browse the repository at this point in the history
Introduce a type for GitHub issue comments
  • Loading branch information
orta authored Dec 27, 2018
2 parents 47641fe + dbc9615 commit ee2a752
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
17 changes: 17 additions & 0 deletions source/dsl/GitHubDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,23 @@ export interface GitHubIssueLabel {
color: string
}

export interface GitHubIssueComment {
/**
* UUID for the comment
*/
id: string

/**
* The User who made the comment
*/
user: GitHubUser

/**
* Textual representation of comment
*/
body: string
}

// This is `danger.github.pr`

/**
Expand Down
23 changes: 11 additions & 12 deletions source/platforms/github/GitHubAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import * as node_fetch from "node-fetch"
import parse from "parse-link-header"
import pLimit from "p-limit"

import { GitHubPRDSL, GitHubUser } from "../../dsl/GitHubDSL"
import { GitHubPRDSL, GitHubIssueComment, GitHubUser } from "../../dsl/GitHubDSL"

import { dangerIDToString } from "../../runner/templates/githubIssueTemplate"
import { api as fetch } from "../../api/fetch"
import { Comment } from "../platform"
import { RepoMetaData } from "../../dsl/BitBucketServerDSL"
import { CheckOptions } from "./comms/checks/resultsToCheck"

Expand Down Expand Up @@ -83,9 +82,9 @@ export class GitHubAPI {

// The above is the API for Platform

getDangerCommentIDs = async (dangerID: string): Promise<number[]> => {
getDangerCommentIDs = async (dangerID: string): Promise<string[]> => {
const userID = await this.getUserID()
const allComments: any[] = await this.getPullRequestComments()
const allComments = await this.getPullRequestComments()
const dangerIDMessage = dangerIDToString(dangerID)
this.d(`User ID: ${userID}`)
this.d(`Looking at ${allComments.length} comments for ${dangerIDMessage}`)
Expand All @@ -96,7 +95,7 @@ export class GitHubAPI {
.map(comment => comment.id) // only return IDs
}

updateCommentWithID = async (id: number, comment: string): Promise<any> => {
updateCommentWithID = async (id: string, comment: string): Promise<any> => {
const repo = this.repoMetadata.repoSlug
const res = await this.patch(
`repos/${repo}/issues/comments/${id}`,
Expand All @@ -109,7 +108,7 @@ export class GitHubAPI {
return res.json()
}

deleteCommentWithID = async (id: number): Promise<boolean> => {
deleteCommentWithID = async (id: string): Promise<boolean> => {
const repo = this.repoMetadata.repoSlug
const res = await this.api(`repos/${repo}/issues/comments/${id}`, {}, null, "DELETE")

Expand Down Expand Up @@ -274,23 +273,23 @@ export class GitHubAPI {
return response.json()
}

getPullRequestComments = async (): Promise<any[]> => {
getPullRequestComments = async (): Promise<GitHubIssueComment[]> => {
const repo = this.repoMetadata.repoSlug
const prID = this.repoMetadata.pullRequestID
return await this.getAllOfResource(`repos/${repo}/issues/${prID}/comments`)
}

getPullRequestInlineComments = async (dangerID: string): Promise<Comment[]> => {
getPullRequestInlineComments = async (dangerID: string): Promise<(GitHubIssueComment & { ownedByDanger: boolean })[]> => {
const userID = await this.getUserID()
const repo = this.repoMetadata.repoSlug
const prID = this.repoMetadata.pullRequestID
return await this.getAllOfResource(`repos/${repo}/pulls/${prID}/comments`).then(v => {
return await this.getAllOfResource(`repos/${repo}/pulls/${prID}/comments`).then((v: GitHubIssueComment[]) => {
return v
.filter(Boolean)
.map((i: any) => {
return { id: i.id, ownedByDanger: i.user.id == userID && i.body.includes(dangerID), body: i.body }
.map(i => {
return { ...i, ownedByDanger: i.user.id == userID && i.body.includes(dangerID) }
})
.filter((i: any) => i.ownedByDanger)
.filter(i => i.ownedByDanger)
})
}

Expand Down
4 changes: 2 additions & 2 deletions source/platforms/github/_tests/_github_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe("API testing", () => {
api.fetch = fetch
api.patch = jest.fn(() => ({ json: jest.fn() }))

await api.updateCommentWithID(123, "Hello!")
await api.updateCommentWithID("123", "Hello!")

expect(api.patch).toHaveBeenCalledWith("repos/artsy/emission/issues/comments/123", {}, { body: "Hello!" })
})
Expand Down Expand Up @@ -234,7 +234,7 @@ describe("API testing", () => {

it("deleteCommentWithID", async () => {
api.fetch = jest.fn().mockReturnValue({ status: 204 })
await api.deleteCommentWithID(123)
await api.deleteCommentWithID("123")

expect(api.fetch).toHaveBeenCalledWith(
"https://api.github.com/repos/artsy/emission/issues/comments/123",
Expand Down

0 comments on commit ee2a752

Please sign in to comment.