Skip to content

Commit

Permalink
Fix incremental testing script
Browse files Browse the repository at this point in the history
Earlier, it would take the first page of commits from the GitHub's PR
commits page.
Now, it follows the next link url to fetch all commits.

Tested with

```
CIRCLE_PROJECT_USERNAME=celo-org CIRCLE_PROJECT_REPONAME=celo-monorepo CIRCLE_PULL_REQUEST="#1152" ./scripts/ci_check_if_test_should_run_v2.sh $PWD/packages/protocol
```

Earlier, this would check (First) 30 commits. Now, it checks all 46
commits.
  • Loading branch information
ashishb committed Oct 8, 2019
1 parent d2475de commit 0b11a7c
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions scripts/check_if_test_should_run_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ async function getBranchCommits(): Promise<string[]> {
}

const url = `https://api.github.com/repos/${owner}/${repo}/pulls/${prNumber}/commits`
const commits = await getAllCommits(url)
logMessage(`Found ${commits.length} commits in PR ${prNumber}: ${commits}`)
return commits
}

async function getAllCommits(url: string): Promise<string[]> {
const response: any = await fetch(url, {
headers: {
Accept: 'application/vnd.github.v3+json',
Expand All @@ -114,12 +120,46 @@ async function getBranchCommits(): Promise<string[]> {
)
}
// logMessage(`Commit objects are ${JSON.stringify(commitObjects)}`)
const commits = commitObjects.map((commitObject: any) => commitObject.sha)
logMessage(`Commits corresponding to ${prNumber} are ${commits}`)
let commits = commitObjects.map((commitObject: any) => commitObject.sha)

const nextPageLink: string | null = getNextPageLink(response)
if (nextPageLink) {
logMessage(`Getting commits from the next page: ${nextPageLink}`)
commits = commits.concat(await getAllCommits(nextPageLink))
}
return commits
}
}

// Reference: https://www.w3.org/wiki/LinkHeader
// linkHeaderValue value would look similar to this
// <https://api.github.com/repositories/197642503/pulls/1152/commits?page=2>; rel="next", <https://api.github.com/repositories/197642503/pulls/1152/commits?page=2>; rel="last"
function getNextPageLink(response: Response): string | null {
const linkHeaderValue: string | null = response.headers.get('link')
if (!linkHeaderValue) {
logMessage(`Link header not found in ${response.url}`)
return null
}
logMessage(`Link header value is \"${linkHeaderValue}\"`)
const linkEntries = linkHeaderValue.split(',')
for (let i = 0; i < linkEntries.length; i++) {
if (linkEntries[i].endsWith('rel="next"')) {
const lastSemiColon = linkEntries[i].lastIndexOf(';')
let nextPageLink = linkEntries[i].substring(0, lastSemiColon)
nextPageLink = nextPageLink.trim()
if (nextPageLink.startsWith('<')) {
nextPageLink = nextPageLink.substring(1)
}
if (nextPageLink.endsWith('>')) {
nextPageLink = nextPageLink.substring(0, nextPageLink.length - 1)
}
return nextPageLink
}
}
logMessage(`next page link not found in "${linkHeaderValue}"`)
return null
}

async function getChangeCommit(file: string): Promise<string> {
if (!existsSync(file)) {
logMessage(`File "${file}" does not exist`)
Expand Down

0 comments on commit 0b11a7c

Please sign in to comment.