-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New file: src/pullRequestChangedFiles
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
export default async function pullRequestChangedFIles ({ github, githubToken, owner, name, prnumber }) { | ||
if (!github && githubToken) { | ||
const { Octokit } = await import('octokit') | ||
|
||
github = new Octokit({ auth: githubToken }) | ||
} | ||
|
||
if (!github && !githubToken) { | ||
throw new Error('either githubToken or github is required!') | ||
} | ||
|
||
prnumber = parseInt(prnumber, 10) | ||
|
||
const query = `query ($owner: String!, $name: String!, $prnumber: Int!, $cursor: String) { | ||
repository(owner: $owner, name: $name) { | ||
pullRequest(number: $prnumber) { | ||
files(first: 100, after: $cursor) { | ||
pageInfo { | ||
endCursor | ||
hasNextPage | ||
} | ||
nodes { | ||
path | ||
additions | ||
deletions | ||
} | ||
} | ||
} | ||
} | ||
}` | ||
|
||
const vars = { | ||
owner, | ||
name, | ||
prnumber | ||
} | ||
|
||
let hasNextPage = true | ||
let paths = [] | ||
while (hasNextPage) { | ||
const { repository } = await github.graphql(query, vars) | ||
const files = repository.pullRequest.files | ||
|
||
// prepare the next iteration | ||
hasNextPage = files.pageInfo.hasNextPage | ||
vars.cursor = files.pageInfo.endCursor | ||
|
||
// append new paths to paths array | ||
// check for additions only, deletions are not relevant, in this case | ||
paths = paths.concat( | ||
files.nodes.filter(file => file.additions /* + file.deletions */ > 0).map(file => file.path)) | ||
} | ||
|
||
return paths | ||
} |