Skip to content

Commit

Permalink
feat: added config file support.
Browse files Browse the repository at this point in the history
  • Loading branch information
tgetgood committed Nov 27, 2017
1 parent 3d5d85b commit 9324e5c
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
config.json
data
16 changes: 16 additions & 0 deletions config.json.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"token": "123435abcdf",
"repos": [{
"login": "mntnr",
"repo": "name-your-contributors",
"before": "2017-11-30",
"after": "2017-06-01"
}, {
"login": "mntnr",
"repo": "whodidwhat"
}],
"orgs": [{
"login": "adventure-js",
"after": "2017-07-01"
}]
}
51 changes: 51 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,57 @@ $ export GITHUB_TOKEN={your-token}
$ name-your-contributors -u mntnr -r name-your-contributors

$ name-your-contributors -o ipfs -a 2017-01-01 > ipfs-contrib.json

$ name-your-contributors --config config.json > combined-out.json
```

### Config File

For batching convenience, Name Your Contributors takes a config file which
specifies a token, a list of repos, and a list of orgs to grab. The
`config.json.example` is an outline of this file format:

```json
{
"token": "123435abcdf",
"repos": [{
"login": "mntnr",
"repo": "name-your-contributors",
"before": "2017-11-30",
"after": "2017-06-01"
}, {
"login": "mntnr",
"repo": "whodidwhat"
}],
"orgs": [{
"login": "adventure-js",
"after": "2017-07-01"
}]
}
```

A token passed in the config file will override any token present in the
environment.

The output when passed a config file is a mirror of the config file with the
token removed and a `contributions` key added to each object, like so:

```json
{
"repos": [{
"login": "mntnr",
"repo": "name-your-contributors",
"before": "2017-11-30",
"after": "2017-06-01",
"contributions" : {
"commitAuthors": [...],
"commitCommentators: [...],
,,,
},
...
}],
"orgs": [...]
}
```

The output will be in the format:
Expand Down
18 changes: 16 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const cli = meow([`
-r, --repo - Repository to search
-t, --token - GitHub auth token to use
-u, --user - User to which repository belongs
--config - Operate from config file. In this mode only token, verbose, and
debug flags apply.
Authentication
This script looks for an auth token in the env var GITHUB_TOKEN. Make sure
Expand All @@ -25,6 +27,8 @@ const cli = meow([`
$ name-your-contributors -r ipfs -u ipfs --after=2016-01-15T00:20:24Z --before=2016-01-20T00:20:24Z
$ name-your-contributors -o ipfs -a 2017-01-01 > ipfs-contrib-2017.json
$ name-your-contributors --config config.json > combined-out.json
`], {
alias: {
a: 'after',
Expand All @@ -43,7 +47,7 @@ const token = cli.flags.t || process.env.GITHUB_TOKEN
const after = cli.flags.a ? new Date(cli.flags.a) : new Date(0)
const before = cli.flags.b ? new Date(cli.flags.b) : new Date()

if (!token) {
if (!token && !cli.flags.config) {
console.error('A token is needed to access the GitHub API. Please provide one with -t or the GITHUB_TOKEN environment variable.')
process.exit(1)
}
Expand Down Expand Up @@ -76,7 +80,17 @@ const callWithDefaults = (f, opts) => {
const fetchRepo = (user, repo) =>
callWithDefaults(main.repoContributors, {user, repo})

if (cli.flags.o) {
if (cli.flags.config) {
main.fromConfig({
file: cli.flags.config,
token,
verbose: cli.flags.v,
debug: cli.flags.debug,
dryRun: cli.flags.dryRun
}).then(x => JSON.stringify(x, null, 2))
.then(handleOut)
.catch(handleError)
} else if (cli.flags.o) {
callWithDefaults(main.orgContributors, {orgName: cli.flags.o})
} else if (cli.flags.u && cli.flags.r) {
fetchRepo(cli.flags.u, cli.flags.r)
Expand Down
67 changes: 63 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const graphql = require('./graphql')
const queries = require('./queries')
const csv = require('csv-writer').createArrayCsvStringifier
const exec = require('child_process').exec
const fs = require('fs')

//
// Shell Helpers
Expand Down Expand Up @@ -65,6 +66,9 @@ const verifyResultHasKey = (key, query) =>
}
}

//
// Config File Parsing

//
// API
//
Expand All @@ -76,14 +80,14 @@ const verifyResultHasKey = (key, query) =>
* @param before - only return contributions before this timestamp
* @param after - only return contributions after this timestamp
*/
const repoContributors = (
{token, user, repo, before, after, debug, dryRun, verbose}) =>
const repoContributors =
({token, user, repo, before, after, debug, dryRun, verbose}) =>
graphql.executequery({
token,
debug,
dryRun,
verbose,
name: 'repoContributors',
name: `repoContributors: ${user}/${repo}`,
query: queries.repository(repo, user, before, after)
}).then(verifyResultHasKey('repository', user + '/' + repo))
.then(json => {
Expand All @@ -106,7 +110,7 @@ const orgContributors = ({token, orgName, before, after, debug, dryRun, verbose}
debug,
dryRun,
verbose,
name: 'orgContributors',
name: `orgContributors: ${orgName}`,
query: queries.orgRepos(orgName, before, after)
}).then(verifyResultHasKey('organization', orgName))
.then(data => {
Expand All @@ -117,6 +121,60 @@ const orgContributors = ({token, orgName, before, after, debug, dryRun, verbose}
}
})

/** Returns all contributions to repos and orgs specified in `file`
* @param token - GitHub auth token
* @param file - Config file path
*/
const fromConfig = async ({token, file, verbose, debug, dryRun}) => {
const config = JSON.parse(fs.readFileSync(file))
const ght = config.token || token
if (!ght) {
throw new Error('No token specified in config or arguments. Aborting.')
}
const repoResults = config.repos.map(({login, repo, before, after}) => {
const afterDate = after ? new Date(after) : new Date(0)
const beforeDate = before ? new Date(before) : new Date()

return repoContributors({
token: ght,
user: login,
repo,
before: beforeDate,
after: afterDate,
debug,
dryRun,
verbose
})
})

const orgResults = config.orgs.map(({login, before, after}) => {
const afterDate = after ? new Date(after) : new Date(0)
const beforeDate = before ? new Date(before) : new Date()
return orgContributors({
orgName: login,
before: beforeDate,
after: afterDate,
token: ght,
verbose,
debug,
dryRun
})
})

return {
repos: (await Promise.all(repoResults)).map((result, index) => {
const repo = config.repos[index]
repo.contributions = result
return repo
}),
orgs: (await Promise.all(orgResults)).map((result, index) => {
const org = config.orgs[index]
org.contributions = result
return org
})
}
}

/** Returns the login of the user to whom the given token is registered.
* @param token - GitHub Auth token
*/
Expand All @@ -128,6 +186,7 @@ const currentUser = token =>

module.exports = {
toCSV,
fromConfig,
parseGitURL,
getCurrentRepoInfo,
currentUser,
Expand Down

0 comments on commit 9324e5c

Please sign in to comment.