Skip to content

Commit

Permalink
feat: made reactions and commits optional
Browse files Browse the repository at this point in the history
Added individual flags to toggle them.
  • Loading branch information
tgetgood authored and RichardLitt committed Dec 14, 2017
1 parent 1839172 commit 2fff4ce
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 102 deletions.
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ const repoContributors = ({
dryRun,
verbose,
name: `repoContributors: ${user}/${repo}`,
query: queries.repository(repo, user, before, after)
query: queries.repository(repo, user, before, after, commits)
}).then(verifyResultHasKey('repository', user + '/' + repo, dryRun))
.then(json => {
if (dryRun) {
Expand Down Expand Up @@ -119,16 +119,16 @@ const orgContributors = ({
debug,
dryRun,
verbose,
commits,
reactions,
name: `orgContributors: ${orgName}`,
query: queries.orgRepos(orgName, before, after)
query: queries.orgRepos(orgName, before, after, commits)
}).then(verifyResultHasKey('organization', orgName, dryRun))
.then(data => {
if (dryRun) {
return data
} else {
return queries.cleanOrgRepos(token, data, before, after, verbose)
return queries.cleanOrgRepos({
token, result: data, before, after, verbose, commits, reactions
})
}
})

Expand Down
235 changes: 138 additions & 97 deletions src/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,36 +93,43 @@ const commitCommentQ = top =>
.addChild(authoredWithReactionsQ)

/** Returns a query to retrieve all contributors to a repo */
const repository = (repoName, ownerName, before, after) =>
node('repository', {name: repoName, owner: ownerName})
const repository = (repoName, ownerName, before, after, commits) => {
let q = node('repository', {name: repoName, owner: ownerName})
.addChild(node('id'))
.addChild(commitCommentQ(true))
.addChild(refsQ(before, after, true))
.addChild(prsQ(true))
.addChild(issuesQ(true))

const repositoryCont = (before, after) =>
node('nodes')
if (commits) {
q = q.addChild(commitCommentQ(true))
.addChild(refsQ(before, after, true))
}

return q
}

const repositoryCont = (before, after, commits) => {
let q = node('nodes')
.addChild(node('id'))
.addChild(commitCommentQ(false))
.addChild(refsQ(before, after, false))
.addChild(prsQ(false))
.addChild(issuesQ(false))

const repositories = (before, after, top) =>
if (commits) {
q = q.addChild(commitCommentQ(false))
.addChild(refsQ(before, after, false))
}

return q
}

const repositories = (before, after, top, commits) =>
node('repositories', {first: top === true ? 100 : 1})
.addChild(pagination)
.addChild(repositoryCont(before, after))
.addChild(repositoryCont(before, after, commits))

const orgRepos = (name, before, after) =>
const orgRepos = (name, before, after, commits) =>
node('organization', {login: name})
.addChild(node('id'))
.addChild(repositories(before, after, true))

const userRepos = (login, before, after) =>
node('user', {login})
.addChild(node('id'))
.addChild(repositories(before, after))
.addChild(repositories(before, after, true, commits))

const continuationQuery =
({id, parentType, childType, cursor, count, query, before, after}) => {
Expand Down Expand Up @@ -256,38 +263,46 @@ const depaginateAll =
const byCount = (a, b) => b.count - a.count

/** Parse repository query result and filter for date range. */
const cleanRepo = async (token, result, before, after, verbose) => {
const cleanRepo = async ({
token, result, before, after, verbose, commits, reactions
}) => {
verboseCont = verboseCont || verbose

const tf = timeFilter(before, after)
const process = x => mergeContributions(users(tf(x)))
.sort(byCount)

const branches = await fetchAll({
token,
name: 'refs cont',
acc: result.refs.nodes,
data: result,
type: 'Repository',
key: 'refs',
count: 100,
query: commitQ(before, after)
})
let branches = []
if (commits) {
branches = await fetchAll({
token,
name: 'refs cont',
acc: result.refs.nodes,
data: result,
type: 'Repository',
key: 'refs',
count: 100,
query: commitQ(before, after)
})
}

const targets = Array.from(branches).map(b => b.target)

const commits = await depaginateAll(targets, {
token,
name: 'commits cont',
acc: ref => ref.history.nodes,
type: 'Commit',
key: 'history',
query: commitHistoryQ,
before,
after
})
let commitsCont = []
if (commits) {
commitsCont = await depaginateAll(targets, {
token,
name: 'commits cont',
acc: ref => ref.history.nodes,
type: 'Commit',
key: 'history',
query: commitHistoryQ,
before,
after
})
}

const commitAuthors = Array.from(commits).map(x => x.author)
const commitAuthors = Array.from(commitsCont).map(x => x.author)

const prs = await fetchAll({
token,
Expand All @@ -311,16 +326,19 @@ const cleanRepo = async (token, result, before, after, verbose) => {
query: participantsQ
})

const commitComments = await fetchAll({
token,
name: 'commit comments cont',
acc: result.commitComments.nodes,
data: result,
type: 'Repository',
key: 'commitComments',
count: 100,
query: authoredWithReactionsQ
})
let commitComments = []
if (commits) {
commitComments = await fetchAll({
token,
name: 'commit comments cont',
acc: result.commitComments.nodes,
data: result,
type: 'Repository',
key: 'commitComments',
count: 100,
query: authoredWithReactionsQ
})
}

const reviews = await depaginateAll(prs, {
token,
Expand Down Expand Up @@ -349,54 +367,66 @@ const cleanRepo = async (token, result, before, after, verbose) => {
query: authoredQ.addChild(reactorQ)
})

const reactions = Array.from(await depaginateAll(commitComments, {
token,
name: 'reactions cont',
acc: cc => cc.reactions.nodes,
type: 'CommitComment',
key: 'reactions',
query: reactorSubQ
})).concat(await depaginateAll(issueComments, {
token,
name: 'issue comment reactions cont',
acc: ic => ic.reactions.nodes,
type: 'IssueComment',
key: 'reactions',
query: reactorSubQ
})).concat(await depaginateAll(prComments, {
token,
name: 'pr comment reactions cont',
acc: prc => prc.reactions.nodes,
type: 'PullRequestComment',
key: 'reactions',
query: reactorSubQ
})).concat(await depaginateAll(issues, {
token,
name: 'issue reactions cont',
acc: is => is.reactions.nodes,
type: 'Issue',
key: 'reactions',
query: reactorSubQ
})).concat(await depaginateAll(prs, {
token,
name: 'pullrequest reactions cont',
acc: pr => pr.reactions.nodes,
type: 'PullRequest',
key: 'reactions',
query: reactorSubQ
}))

return {
commitAuthors: mergeContributions(users(commitAuthors))
.sort(byCount),
commitCommentators: process(commitComments),
let reactionsCont = []
if (reactions) {
reactionsCont = Array.from(await depaginateAll(commitComments, {
token,
name: 'reactions cont',
acc: cc => cc.reactions.nodes,
type: 'CommitComment',
key: 'reactions',
query: reactorSubQ
})).concat(await depaginateAll(issueComments, {
token,
name: 'issue comment reactions cont',
acc: ic => ic.reactions.nodes,
type: 'IssueComment',
key: 'reactions',
query: reactorSubQ
})).concat(await depaginateAll(prComments, {
token,
name: 'pr comment reactions cont',
acc: prc => prc.reactions.nodes,
type: 'PullRequestComment',
key: 'reactions',
query: reactorSubQ
})).concat(await depaginateAll(issues, {
token,
name: 'issue reactions cont',
acc: is => is.reactions.nodes,
type: 'Issue',
key: 'reactions',
query: reactorSubQ
})).concat(await depaginateAll(prs, {
token,
name: 'pullrequest reactions cont',
acc: pr => pr.reactions.nodes,
type: 'PullRequest',
key: 'reactions',
query: reactorSubQ
}))
}

const processed = {
prCreators: process(prs),
prCommentators: process(prComments),
issueCreators: process(issues),
issueCommentators: process(issueComments),
reactors: process(reactions),
reviewers: process(reviews)
}

if (reactions) {
processed.reactors = process(reactionsCont)
}

if (commits) {
processed.commitAuthors = mergeContributions(users(commitAuthors))
.sort(byCount)

processed.commitCommentators = process(commitComments)
}

return processed
}

const mergeArrays = (a, b) =>
Expand All @@ -412,7 +442,9 @@ const mergeRepoResults = repos =>
return ret
})

const cleanOrgRepos = async (token, result, before, after, verbose) => {
const cleanOrgRepos = async ({
token, result, before, after, verbose, commits, reactions
}) => {
verboseCont = verboseCont || verbose

const repos = await fetchAll({
Expand All @@ -423,12 +455,22 @@ const cleanOrgRepos = async (token, result, before, after, verbose) => {
type: 'Organization',
key: 'repositories',
count: 100,
query: repositoryCont(before, after)
query: repositoryCont(before, after, commits)
})

return mergeRepoResults(
await Promise.all(repos.map(repo =>
cleanRepo(token, repo, before, after, verbose))))
await Promise.all(repos.map(repo => {
return cleanRepo({
token,
result: repo,
commits,
reactions,
before,
after,
verbose
})
}))
)
}

const cleanWhoAmI = x => x.viewer.login
Expand All @@ -446,6 +488,5 @@ module.exports = {
mergeContributions,
mergeArrays,
mergeRepoResults,
authoredQ,
userRepos
authoredQ
}

0 comments on commit 2fff4ce

Please sign in to comment.