Skip to content

Commit

Permalink
feat(cli): user now inferred from auth token
Browse files Browse the repository at this point in the history
If just a repo is specified we assume the intended user is the owner of
the auth token.
  • Loading branch information
tgetgood committed Oct 25, 2017
1 parent 6b239e7 commit cfca394
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
50 changes: 18 additions & 32 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ const handleError = e => {
process.exit(1)
}

const fetchRepo = (user, repo) =>
main.repoContributors({
user,
repo,
before,
after,
token,
debug: debugMode
}).then(formatReturn)
.then(handleOut)
.catch(handleError)

if (cli.flags.o) {
main.orgContributors({
debug: debugMode,
Expand All @@ -75,37 +87,11 @@ if (cli.flags.o) {
.then(handleOut)
.catch(handleError)
} else if (cli.flags.u && cli.flags.r) {
main.repoContributors({
debug: debugMode,
token: token,
user: cli.flags.u,
repo: cli.flags.r,
before: before,
after: after
}).then(formatReturn)
.then(handleOut)
.catch(handleError)
} else {
(async () => {
const creds = await main.getCurrentRepoInfo()
fetchRepo(cli.flags.u, cli.flags.r)
} else if (cli.flags.r) {
main.currentUser(token).then(user => fetchRepo(user, cli.flags.r))
} else if (cli.flags.u) {

return main.repoContributors({
token,
debug: debugMode,
user: creds.user,
repo: creds.repo,
before,
after
}).then(x => {
if (!x.user || !x.repo) {
return x
} else {
console.error('Not in a git repository')
console.error(cli.help)
process.exit(1)
}
}).then(formatReturn)
.then(handleOut)
.catch(handleError)
})()
} else {
main.getCurrentRepoInfo().then(({user, repo}) => fetchRepo(user, repo))
}
8 changes: 8 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,17 @@ const orgContributors = ({token, orgName, before, after, debug}) =>
graphql.executequery(token, queries.orgRepos(orgName, before, after), debug)
.then(data => queries.cleanOrgRepos(token, data, before, after))

/** Returns the login of the user to whom the given token is registered.
* @param token - GitHub Auth token
*/
const currentUser = token =>
graphql.executequery(token, queries.whoAmI)
.then(queries.cleanWhoAmI)

module.exports = {
toCSV,
getCurrentRepoInfo,
currentUser,
repoContributors,
orgContributors,
userRepoNames
Expand Down
6 changes: 6 additions & 0 deletions src/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const node = graphql.queryNode
// Queries
/// //

const whoAmI = node('viewer').addChild(node('login'))

const pagination = node('pageInfo')
.addChild(node('endCursor'))
.addChild(node('hasNextPage'))
Expand Down Expand Up @@ -378,7 +380,11 @@ const cleanOrgRepos = async (token, result, before, after) => {
await Promise.all(repos.map(repo => cleanRepo(token, repo, before, after))))
}

const cleanWhoAmI = x => x.viewer.login

module.exports = {
whoAmI,
cleanWhoAmI,
repository,
organization,
orgRepos,
Expand Down

0 comments on commit cfca394

Please sign in to comment.