-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add check command that compares GitHub contributors with credited ones #58
Changes from 3 commits
508f982
4a93be5
bf07fba
d417c9b
82787e1
d763ab3
7a14d4d
8bad09a
b566ded
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
var path = require('path'); | ||
var yargs = require('yargs'); | ||
var chalk = require('chalk'); | ||
var inquirer = require('inquirer'); | ||
|
||
var init = require('./lib/init'); | ||
|
@@ -23,6 +24,8 @@ var argv = yargs | |
.usage('Usage: $0 add <username> <contribution>') | ||
.command('init', 'Prepare the project to be used with this tool') | ||
.usage('Usage: $0 init') | ||
.command('check', 'Compares contributors from GitHub with the ones credited in .all-contributorsrc') | ||
.usage('Usage: $0 check') | ||
.boolean('commit') | ||
.default('files', ['README.md']) | ||
.default('contributorsPerLine', 7) | ||
|
@@ -68,6 +71,37 @@ function addContribution(argv) { | |
}); | ||
} | ||
|
||
function checkContributors() { | ||
var configData = util.configFile.readConfig(argv.config); | ||
|
||
return util.check(configData.projectOwner, configData.projectName) | ||
.then(ghContributors => { | ||
var knownContributions = configData.contributors.reduce((obj, item) => { | ||
obj[item.login] = item.contributions; | ||
return obj; | ||
}, {}); | ||
var knownContributors = configData.contributors.map(contributor => contributor.login); | ||
|
||
var missingInConfig = ghContributors.filter(login => knownContributors.indexOf(login) === -1); | ||
var missingFromGithub = knownContributors.filter(login => { | ||
return ghContributors.indexOf(login) === -1 && ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could probably use |
||
knownContributions[login].includes('code') || | ||
knownContributions[login].includes('tool') | ||
); | ||
}); | ||
|
||
if (missingInConfig.length) { | ||
process.stdout.write(chalk.bold('Missing contributors in .all-contributorsrc:\n')); | ||
process.stdout.write(' ' + missingInConfig.join(', ') + '\n'); | ||
} | ||
|
||
if (missingFromGithub.length) { | ||
process.stdout.write(chalk.bold('Unknown contributors found in .all-contributorsrc:\n')); | ||
process.stdout.write(' ' + missingFromGithub.join(', ') + '\n'); | ||
} | ||
}); | ||
} | ||
|
||
function onError(error) { | ||
if (error) { | ||
console.error(error.message); | ||
|
@@ -87,6 +121,9 @@ function promptForCommand(argv) { | |
}, { | ||
name: 'Re-generate the contributors list', | ||
value: 'generate' | ||
}, { | ||
name: 'Compare contributors from GitHub with the credited ones', | ||
value: 'check' | ||
}], | ||
when: !argv._[0], | ||
default: 0 | ||
|
@@ -107,6 +144,8 @@ promptForCommand(argv) | |
return startGeneration(argv); | ||
case 'add': | ||
return addContribution(argv); | ||
case 'check': | ||
return checkContributors(); | ||
default: | ||
throw new Error(`Unknown command ${command}`); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
var pify = require('pify'); | ||
var request = pify(require('request')); | ||
|
||
function getNextLink(link) { | ||
if (!link) { | ||
return null; | ||
} | ||
|
||
var nextLink = link.split(',').find(s => s.indexOf('rel="next"') > -1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could use |
||
|
||
if (!nextLink) { | ||
return null; | ||
} | ||
|
||
return nextLink.split(';')[0].slice(1, -1); | ||
} | ||
|
||
function getContributorsPage(url) { | ||
return request.get({ | ||
url: url, | ||
headers: { | ||
'User-Agent': 'request' | ||
} | ||
}) | ||
.then(res => { | ||
var body = JSON.parse(res.body); | ||
var contributorsIds = body.map(contributor => contributor.login); | ||
|
||
var nextLink = getNextLink(res.headers.link); | ||
if (nextLink) { | ||
return getContributorsPage(nextLink).then(nextContributors => { | ||
return contributorsIds.concat(nextContributors); | ||
}) | ||
} | ||
|
||
return contributorsIds; | ||
}); | ||
} | ||
|
||
module.exports = function getContributorsFromGithub(owner, name) { | ||
var url = 'https://api.github.com/repos/' + owner + '/' + name + '/contributors?per_page=100'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could use a template literal: var url = `https://api.github.com/repos/${owner}/${name}/contributors?per_page=100`; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There you go. Also use template literals for the command output |
||
return getContributorsPage(url); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could probably use
includes
here.