Skip to content

Commit b447fce

Browse files
tgetgoodRichardLitt
authored andcommitted
feat: commit authors now included in results.
1 parent ca585f0 commit b447fce

File tree

5 files changed

+75
-17
lines changed

5 files changed

+75
-17
lines changed

src/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ const cli = meow([`
5656

5757
const token = cli.flags.t || process.env.GITHUB_TOKEN
5858

59-
const after = cli.flags.a && new Date(cli.flags.a)
60-
const before = cli.flags.b && new Date(cli.flags.b)
59+
const after = cli.flags.a && new Date(cli.flags.a) || new Date(0)
60+
const before = cli.flags.b && new Date(cli.flags.b) || new Date()
6161

6262
const debugMode = cli.flags.debug
6363

src/graphql.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const argsString = args => {
1717
return ''
1818
} else {
1919
const s = keys.map(k => {
20-
return encodeURIComponent(k) + ': ' + escapeArgValue(args[k]) + ','
20+
return k + ': ' + escapeArgValue(args[k]) + ','
2121
}).reduce((acc, next) => acc + next, '')
2222
return '(' + s.substr(0, s.length - 1) + ')'
2323
}

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const queries = require('./queries')
1111
* @param after - only return contributions after this timestamp
1212
*/
1313
const repoContributors = ({token, user, repo, before, after, debug}) =>
14-
graphql.executequery(token, queries.repository(repo, user), debug)
14+
graphql.executequery(token, queries.repository(repo, user, before, after), debug)
1515
.then(json => queries.cleanRepo(token, json.repository, before, after))
1616

1717
/** Returns a list of names of all repos belonging to user. */
@@ -26,7 +26,7 @@ const userRepoNames = ({token, login, debug}) =>
2626
* @param after - only return contributions after this timestamp
2727
*/
2828
const orgContributors = ({token, orgName, before, after, debug}) =>
29-
graphql.executequery(token, queries.orgRepos(orgName), debug)
29+
graphql.executequery(token, queries.orgRepos(orgName, before, after), debug)
3030
.then(data => queries.cleanOrgRepos(token, data, before, after))
3131

3232
module.exports = {

src/queries.js

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ const pagination = node('pageInfo')
1111
.addChild(node('endCursor'))
1212
.addChild(node('hasNextPage'))
1313

14+
const userInfo = node('user')
15+
.addChild(node('login'))
16+
.addChild(node('name'))
17+
.addChild(node('url'))
18+
1419
const reactorQ = node('reactions', {first: 10})
1520
.addChild(pagination)
1621
.addChild(node('nodes')
22+
.addChild(node('id'))
1723
.addChild(node('createdAt'))
18-
.addChild(node('user')
19-
.addChild(node('login'))
20-
.addChild(node('name'))
21-
.addChild(node('url'))))
24+
.addChild(userInfo))
2225

2326
const authoredQ = node('nodes')
2427
.addChild(node('id'))
@@ -29,6 +32,30 @@ const authoredQ = node('nodes')
2932
.addChild(node('url'))))
3033
.addChild(node('createdAt'))
3134

35+
const commitAuthorQ = node('author').addChild(userInfo)
36+
37+
const commitHistoryQ = node('nodes')
38+
.addChild(node('id'))
39+
.addChild(commitAuthorQ)
40+
41+
const commitQ = (before, after) => {
42+
const b = before.toISOString()
43+
const a = after.toISOString()
44+
return node('nodes')
45+
.addChild(node('id'))
46+
.addChild(node('target')
47+
.addChild(node('id'))
48+
.addChild(node('... on Commit')
49+
.addChild(
50+
node('history', {first: 100, since: a, until: b})
51+
.addChild(pagination)
52+
.addChild(commitHistoryQ))))
53+
}
54+
55+
const refsQ = (before, after) => node('refs', {first: 100, refPrefix: 'refs/heads/'})
56+
.addChild(pagination)
57+
.addChild(commitQ(before, after))
58+
3259
const authoredWithReactionsQ = authoredQ
3360
.addChild(reactorQ)
3461

@@ -55,10 +82,11 @@ const commitCommentQ = node('commitComments', {first: 100})
5582
.addChild(authoredQ)
5683

5784
/** Returns a query to retrieve all contributors to a repo */
58-
const repository = (repoName, ownerName) =>
85+
const repository = (repoName, ownerName, before, after) =>
5986
node('repository', {name: repoName, owner: ownerName})
6087
.addChild(node('id'))
6188
.addChild(commitCommentQ)
89+
.addChild(refsQ(before, after))
6290
.addChild(prsQ)
6391
.addChild(issuesQ)
6492

@@ -68,15 +96,18 @@ const organization = name =>
6896
.addChild(node('id'))
6997
.addChild(node('repositories', {first: 100}, pagination)
7098
.addChild(node('nodes')
99+
.addChild(node('id'))
71100
.addChild(node('name'))))
72101

73-
const orgRepos = name =>
102+
const orgRepos = (name, before, after) =>
74103
node('organization', {login: name})
75104
.addChild(node('id'))
76105
.addChild(node('repositories', {first: 25})
77106
.addChild(pagination)
78107
.addChild(node('nodes')
79108
.addChild(node('id'))
109+
.addChild(commitCommentQ)
110+
.addChild(refsQ(before, after))
80111
.addChild(prsQ)
81112
.addChild(issuesQ)))
82113

@@ -126,7 +157,7 @@ const fetchAll = async ({token, acc, data, type, key, count, query}) => {
126157
* returns an array containing only those objects created between before and
127158
* after.
128159
*/
129-
const timeFilter = (before = new Date(), after = new Date(0)) =>
160+
const timeFilter = (before, after) =>
130161
data => data.filter(x => {
131162
const date = new Date(x.createdAt)
132163
return after <= date && date <= before
@@ -199,6 +230,28 @@ const cleanRepo = async (token, result, before, after) => {
199230
const tf = timeFilter(before, after)
200231
const process = x => mergeContributions(users(tf(x)))
201232

233+
const branches = await fetchAll({
234+
token,
235+
acc: result.refs.nodes,
236+
data: result,
237+
type: 'Repository',
238+
key: 'refs',
239+
count: 100,
240+
query: commitQ(before, after)
241+
})
242+
243+
const targets = Array.from(branches).map(b => b.target)
244+
245+
const commits = await depaginateAll(targets, {
246+
token,
247+
acc: ref => ref.history.nodes,
248+
type: 'Commit',
249+
key: 'history',
250+
query: commitHistoryQ
251+
})
252+
253+
const commitAuthors = Array.from(commits).map(x => x.author)
254+
202255
const prs = await fetchAll({
203256
token,
204257
acc: result.pullRequests.nodes,
@@ -286,6 +339,7 @@ const cleanRepo = async (token, result, before, after) => {
286339
}))
287340

288341
return {
342+
commitAuthors: mergeContributions(users(commitAuthors)),
289343
commitCommentators: process(commitComments),
290344
prCreators: process(prs),
291345
prCommentators: process(prComments),

test/integration-test.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ test('No contributions in a single second', t => {
3737
token: token,
3838
user: 'RichardLitt',
3939
repo: 'name-your-contributors',
40-
after: '2016-01-01T15:21:08.104Z',
41-
before: '2016-01-02T15:21:08.104Z'
40+
after: new Date('2016-01-01T15:21:08.104Z'),
41+
before: new Date('2016-01-02T15:21:08.104Z')
4242
}).then(result => {
4343
for (let key in result) {
4444
t.deepEqual(result[key], [])
45-
}})
45+
}
46+
})
4647
})
4748

4849
const compareKeys = (x, k) =>
@@ -64,7 +65,8 @@ test('Contributors before a fixed date remain static', t => {
6465
token: token,
6566
user: 'RichardLitt',
6667
repo: 'name-your-contributors',
67-
before: '2017-09-21'
68+
before: new Date('2017-09-21'),
69+
after: new Date(0)
6870
}).then(result => {
6971
t.true(compareKeys(result, 'prCreators'))
7072
t.true(compareKeys(result, 'prCommentators'))
@@ -76,7 +78,9 @@ test('Contributors before a fixed date remain static', t => {
7678
test('Queries without tokens get rejected', t => {
7779
return main.repoContributors({
7880
user: 'RichardLitt',
79-
repo: 'name-your-contributors'
81+
repo: 'name-your-contributors',
82+
before: new Date(),
83+
after: new Date(0)
8084
}).catch(error => t.is(error.message, 'Unauthorized'))
8185
})
8286

0 commit comments

Comments
 (0)