Skip to content

Commit

Permalink
Merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Jul 24, 2017
2 parents 7a143a2 + a0ca932 commit 25d82dc
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 12 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Please add your own contribution below inside the Master section, ideally with a consumer's perspective in mind.

### Master
- Support retrive paginated pull request commit list - kwonoj

- Remove the DSL duplication on the `danger` export, it wasn't needed or used. - orta
- Update to TypeScript 2.4.x - orta
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
},
"dependencies": {
"babel-polyfill": "^6.20.0",
"chalk": "^1.1.1",
"chalk": "^2.0.0",
"commander": "^2.9.0",
"debug": "^2.6.0",
"github": "^9.2.0",
Expand All @@ -124,6 +124,7 @@
"lodash.keys": "^4.0.8",
"node-fetch": "^1.6.3",
"parse-diff": "^0.4.0",
"parse-link-header": "^1.0.1",
"rfc6902": "^1.3.0",
"voca": "^1.2.0"
},
Expand Down
1 change: 1 addition & 0 deletions source/ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ declare module "jest-config"
declare module "voca"
declare module "jsome"
declare module "jsonpointer"
declare module "parse-link-header"

declare module "*/package.json"
54 changes: 52 additions & 2 deletions source/platforms/github/GitHubAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ 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 parse from "parse-link-header"

import * as node_fetch from "node-fetch"
import * as GitHubNodeAPI from "github"
import * as debug from "debug"
import { dangerSignaturePostfix } from "../../runner/templates/githubIssueTemplate"

// The Handle the API specific parts of the github
Expand All @@ -20,6 +22,7 @@ export type APIToken = string
export class GitHubAPI {
fetch: typeof fetch
additionalHeaders: any
private readonly d = debug("danger:GitHubAPI")

constructor(public readonly repoMetadata: RepoMetaData, public readonly token?: APIToken) {
// This allows Peril to DI in a new Fetch function
Expand Down Expand Up @@ -132,12 +135,59 @@ export class GitHubAPI {
return res.ok ? res.json() as Promise<GitHubPRDSL> : {} as GitHubPRDSL
}

/**
* Get list of commits in pull requests. This'll try to iterate all available pages
* Until it reaches hard limit of api itself (250 commits).
* https://developer.github.com/v3/pulls/#list-commits-on-a-pull-request
*
*/
async getPullRequestCommits(): Promise<any> {
const repo = this.repoMetadata.repoSlug
const prID = this.repoMetadata.pullRequestID
const res = await this.get(`repos/${repo}/pulls/${prID}/commits`)

return res.ok ? res.json() : []
const ret: Array<any> = []

/**
* Read response header and locate next page for pagination via link header.
* If not found, will return -1.
*
* @param response Github API response sent via node-fetch
*/
const getNextPageFromLinkHeader = (response: node_fetch.Response): number => {
const linkHeader = response.headers.get("link")
if (!linkHeader) {
this.d(`getNextPageFromLinkHeader:: Given response does not contain link header for pagination`)
return -1
}

const parsedHeader = parse(linkHeader)
this.d(`getNextPageFromLinkHeader:: Link header found`, parsedHeader)
if (!!parsedHeader.next && !!parsedHeader.next.page) {
return parsedHeader.next.page
}
return -1
}

//iterates commit request pages until next page's not available, or response failed for some reason.
let page = 0
while (page >= 0) {
const requestUrl = `repos/${repo}/pulls/${prID}/commits${page > 0 ? `?page=${page}` : ""}`
this.d(`getPullRequestCommits:: Sending pull request commit request for ${page === 0 ? "first" : `${page}`} page`)
this.d(`getPullRequestCommits:: Request url generated "${requestUrl}"`)

const response = await this.get(requestUrl)
if (response.ok) {
ret.push(...(await response.json()))
page = getNextPageFromLinkHeader(response)
} else {
this.d(
`getPullRequestCommits:: Failed to get response while traverse page ${page} with ${response.status}, bailing rest of pages if exists`
)
page = -1
}
}

return ret
}

async getUserInfo(): Promise<GitHubUser> {
Expand Down
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,7 @@ ansi-styles@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-2.2.1.tgz#b432dd3358b634cf75e1e4664368240533c1ddbe"

ansi-styles@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.0.0.tgz#5404e93a544c4fec7f048262977bebfe3155e0c1"
dependencies:
color-convert "^1.0.0"

ansi-styles@^3.1.0:
ansi-styles@^3.0.0, ansi-styles@^3.1.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.2.0.tgz#c159b8d5be0f9e5a6f346dab94f16ce022161b88"
dependencies:
Expand Down Expand Up @@ -1035,7 +1029,7 @@ code-point-at@^1.0.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77"

color-convert@^1.0.0, color-convert@^1.9.0:
color-convert@^1.9.0:
version "1.9.0"
resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-1.9.0.tgz#1accf97dd739b983bf994d56fec8f95853641b7a"
dependencies:
Expand Down Expand Up @@ -3188,6 +3182,12 @@ parse-json@^2.2.0:
dependencies:
error-ex "^1.2.0"

parse-link-header@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/parse-link-header/-/parse-link-header-1.0.1.tgz#bedfe0d2118aeb84be75e7b025419ec8a61140a7"
dependencies:
xtend "~4.0.1"

parse-ms@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/parse-ms/-/parse-ms-1.0.1.tgz#56346d4749d78f23430ca0c713850aef91aa361d"
Expand Down Expand Up @@ -4216,7 +4216,7 @@ wrappy@1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/xml-name-validator/-/xml-name-validator-2.0.1.tgz#4d8b8f1eccd3419aa362061becef515e1e559635"

"xtend@>=4.0.0 <4.1.0-0":
"xtend@>=4.0.0 <4.1.0-0", xtend@~4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"

Expand Down

0 comments on commit 25d82dc

Please sign in to comment.