Skip to content

Commit

Permalink
[Danger PR] Support passing through the DANGER_GITHUB_API_TOKEN on th…
Browse files Browse the repository at this point in the history
…e PR
  • Loading branch information
orta committed Jan 20, 2017
1 parent e3c6acc commit 57f9ced
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion dangerfile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { danger, warn } from "danger"
// Removed import

import fs from "fs"
import includes from "lodash.includes"
Expand Down
9 changes: 4 additions & 5 deletions source/commands/danger-pr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ if (program.args.length === 0) {
console.error("Could not get a repo and a PR number from your URL, bad copy & paste?")
process.exitCode = 1
} else {
// TODO: Handle auth token
// TODO: Use custom `fetch` in GitHub that stores and uses local cache if PR is closed, these PRs
// shouldn't change often and there is a limit on API calls per hour.

if (validateDangerfileExists(dangerFile)) {
d(`executing dangerfile at ${dangerFile}`)
const source = new FakeCI({ DANGER_TEST_REPO: pr.repo, DANGER_TEST_PR: pr.pullRequestNumber })
const platform = new GitHub(null, source)
const platform = new GitHub(process.env["DANGER_GITHUB_API_TOKEN"], source)
runDanger(source, platform, dangerFile)
}
}
Expand All @@ -45,16 +44,16 @@ function validateDangerfileExists(filePath: string): boolean {
try {
stat = fs.statSync(filePath)
} catch (error) {
console.error(`Could not find a dangerfile at ${dangerFile}, not running against your PR.`)
console.error(`Could not find a dangerfile at ${filePath}, not running against your PR.`)
process.exitCode = 1
}

if (!!stat && !stat.isFile()) {
console.error(`The resource at ${dangerFile} appears to not be a file, not running against your PR.`)
console.error(`The resource at ${filePath} appears to not be a file, not running against your PR.`)
process.exitCode = 1
}

return !!stat && !stat.isFile()
return !!stat && stat.isFile()
}

async function runDanger(source: FakeCI, platform: GitHub, file: string) {
Expand Down
8 changes: 4 additions & 4 deletions source/platforms/GitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class GitHub {
name: string
fetch: typeof fetch

constructor(public readonly token: APIToken | null, public readonly ciSource: CISource) {
constructor(public readonly token: APIToken | undefined, public readonly ciSource: CISource) {
this.name = "GitHub"

// This allows Peril to DI in a new Fetch function
Expand Down Expand Up @@ -256,7 +256,7 @@ export class GitHub {

// maybe this can move into the stuff below
post(path: string, headers: any = {}, body: any = {}, method: string = "POST"): Promise<any> {
if (this.token !== null) {
if (this.token !== undefined) {
headers["Authorization"] = `token ${this.token}`
}

Expand All @@ -271,7 +271,7 @@ export class GitHub {
}

get(path: string, headers: any = {}, body: any = {}, method: string = "GET"): Promise<any> {
if (this.token !== null) {
if (this.token !== undefined) {
headers["Authorization"] = `token ${this.token}`
}

Expand All @@ -286,7 +286,7 @@ export class GitHub {
}

patch(path: string, headers: any = {}, body: any = {}, method: string = "PATCH"): Promise<any> {
if (this.token !== null) {
if (this.token !== undefined) {
headers["Authorization"] = `token ${this.token}`
}

Expand Down

0 comments on commit 57f9ced

Please sign in to comment.