Skip to content

Commit

Permalink
Support running on a Github App
Browse files Browse the repository at this point in the history
In this case the get /user API is not avaliable

See: https://platform.git.luolix.topmunity/t/obtaining-the-id-of-the-bot-user/2076
  • Loading branch information
clintam committed Jun 19, 2017
1 parent 9aa4ab8 commit c40a233
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Use HTTP for the GitHub status check target URL - macklinu
- Correct some examples in node-app - clintam
- Add support for buddybuild CI - benkraus/clintam
- Add support for GithHub Apps API (no GET /user) - clintam

### 0.21.0

Expand Down
7 changes: 5 additions & 2 deletions source/platforms/github/GitHubAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class GitHubAPI {
const allComments: any[] = await this.getPullRequestComments()
const dangerComment = find(
allComments,
(comment: any) => comment.user.id === userID && v.includes(comment.body, dangerSignaturePostfix)
(comment: any) => (!userID || comment.user.id === userID) && v.includes(comment.body, dangerSignaturePostfix)
)
return dangerComment ? dangerComment.id : null
}
Expand All @@ -102,7 +102,10 @@ export class GitHubAPI {
return Promise.resolve(res.status === 204)
}

async getUserID(): Promise<number> {
async getUserID(): Promise<number | undefined> {
if (process.env["DANGER_GITHUB_APP"]) {
return
}
const info = await this.getUserInfo()
return info.id
}
Expand Down
22 changes: 20 additions & 2 deletions source/platforms/github/_tests/_github_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ describe("API testing", () => {
})

describe("Peril", () => {
it("Allows setting additional headers", async () => {
let api: GitHubAPI

beforeEach(() => {
const mockSource = new FakeCI({})
const api = new GitHubAPI(mockSource, "ABCDE")
api = new GitHubAPI(mockSource, "ABCDE")
api.fetch = jest.fn()
api.additionalHeaders = { CUSTOM: "HEADER" }
})

it("Allows setting additional headers", async () => {
const request = await api.get("user")
expect(api.fetch).toHaveBeenCalledWith("https://api.github.com/user", {
body: {},
Expand All @@ -78,4 +82,18 @@ describe("Peril", () => {
method: "GET",
})
})

describe("Allows setting DANGER_GITHUB_APP env variable", () => {
beforeEach(() => {
process.env.DANGER_GITHUB_APP = "1"
})

afterEach(() => {
delete process.env.DANGER_GITHUB_APP
})

it("Makes getUserId return undefined", async () => {
expect(await api.getUserID()).toBeUndefined()
})
})
})

0 comments on commit c40a233

Please sign in to comment.