Skip to content

Commit

Permalink
Merge pull request #303 from kwonoj/feat-pr-commit-page
Browse files Browse the repository at this point in the history
feat(getPullRequestCommits): Support retrive paginated pull request c…
  • Loading branch information
orta authored Jul 24, 2017
2 parents 630c20c + 6c1cee2 commit a0ca932
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 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

### 1.0.0

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 8 additions & 8 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.1.0"
resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-3.1.0.tgz#09c202d5c917ec23188caa5c9cb9179cd9547750"
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 @@ -4210,7 +4210,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 a0ca932

Please sign in to comment.