feat(discover): updated the learner + look at issue titles

and some visibly forgotten DidYouMean stuff from the person who added that feature
This commit is contained in:
Berkmann18 2023-07-23 13:51:53 +01:00
parent e21f46ea49
commit ace497021f
3 changed files with 56886 additions and 25395 deletions

View file

@ -5,6 +5,7 @@ const path = require('path')
const yargs = require('yargs') const yargs = require('yargs')
const chalk = require('chalk') const chalk = require('chalk')
const inquirer = require('inquirer') const inquirer = require('inquirer')
const didYouMean = require('didyoumean')
const init = require('./init') const init = require('./init')
const generate = require('./generate') const generate = require('./generate')
@ -24,12 +25,22 @@ const yargv = yargs
.alias('v', 'version') .alias('v', 'version')
.version() .version()
.recommendCommands() .recommendCommands()
.command('generate', `Generate the list of contributors\n\nUSAGE: all-contributors generate`) .command(
.command('add', `Add a new contributor\n\nUSAGE: all-contributors add <username> <comma-separated contributions>`) 'generate',
.command('init', `Prepare the project to be used with this tool\n\nUSAGE: all-contributors init`) `Generate the list of contributors\n\nUSAGE: all-contributors generate`,
)
.command(
'add',
`Add a new contributor\n\nUSAGE: all-contributors add <username> <comma-separated contributions>`,
)
.command(
'init',
`Prepare the project to be used with this tool\n\nUSAGE: all-contributors init`,
)
.command( .command(
'check', 'check',
`Compare contributors from the repository with the ones credited in .all-contributorsrc'\n\nUSAGE: all-contributors check`) `Compare contributors from the repository with the ones credited in .all-contributorsrc'\n\nUSAGE: all-contributors check`,
)
.boolean('commit') .boolean('commit')
.default('files', ['README.md']) .default('files', ['README.md'])
.default('contributorsPerLine', 7) .default('contributorsPerLine', 7)
@ -177,10 +188,11 @@ async function checkContributors(argv) {
} }
async function fetchContributors(argv) { async function fetchContributors(argv) {
const {reviewers, commitAuthors, issueCreators} = await getContributors( const {
argv.projectOwner, reviewers,
argv.projectName, commitAuthors,
) issueCreators /* , prCreators */,
} = await getContributors(argv.projectOwner, argv.projectName, true)
const args = {...argv, _: []} const args = {...argv, _: []}
const contributorsToAdd = [] const contributorsToAdd = []
const learner = await getLearner() const learner = await getLearner()
@ -193,20 +205,14 @@ async function fetchContributors(argv) {
) )
}) })
issueCreators.forEach(usr => { const guessCategories = (item, itemType, contributor) => {
const contributor = {
login: usr.login,
contributions: [],
}
usr.labels.forEach(lbl => {
const guessedCategory = learner const guessedCategory = learner
.classify(lbl) .classify(item)
.find(ctr => ctr && ctr !== 'null') .find(ctr => ctr && ctr !== 'null')
if (!guessedCategory) { if (!guessedCategory) {
console.warn( console.warn(
`Oops, I couldn't find any category for the "${lbl}" label`, `Oops, I couldn't find any category for the "${item}" ${itemType}`,
) )
return return
@ -216,12 +222,23 @@ async function fetchContributors(argv) {
contributor.contributions.push(guessedCategory) contributor.contributions.push(guessedCategory)
console.log( console.log(
`Adding ${chalk.blue(usr.login)} for ${chalk.underline( `Adding ${chalk.blue(contributor.login)} for ${chalk.underline(
guessedCategory, guessedCategory,
)}`, )}`,
) )
} }
}) }
issueCreators.forEach(usr => {
const contributor = {
login: usr.login,
contributions: [],
}
//TODO: Look at the titles field and categories based on that.
usr.labels.forEach(label => guessCategories(label, 'label', contributor))
usr.titles.forEach(title => guessCategories(title, 'title', contributor))
const existingContributor = contributorsToAdd.find( const existingContributor = contributorsToAdd.find(
ctr => ctr.login === usr.login, ctr => ctr.login === usr.login,
@ -234,6 +251,7 @@ async function fetchContributors(argv) {
} }
}) })
//TODO Look at prCreators (including its titles field) and add contributions from there
commitAuthors.forEach(usr => { commitAuthors.forEach(usr => {
const existingContributor = contributorsToAdd.find( const existingContributor = contributorsToAdd.find(
ctr => ctr.login === usr.login, ctr => ctr.login === usr.login,
@ -331,6 +349,7 @@ promptForCommand(yargv)
case 'fetch': case 'fetch':
return fetchContributors(yargv) return fetchContributors(yargv)
default: default:
suggestCommands(command)
throw new Error(`Unknown command ${command}`) throw new Error(`Unknown command ${command}`)
} }
}) })

View file

@ -1,22 +1,44 @@
const {join} = require('path')
const {existsSync} = require('fs')
const {writeFile, readFile} = require('fs/promises')
const nyc = require('name-your-contributors') const nyc = require('name-your-contributors')
const {Spinner} = require('clui') const {Spinner} = require('clui')
const privateToken = (process.env && process.env.PRIVATE_TOKEN) || '' const privateToken =
process.env?.PRIVATE_TOKEN ?? process.env?.GITHUB_TOKEN ?? ''
const loader = new Spinner('Loading...') const loader = new Spinner('Loading...')
const getContributors = async function(owner, name, token = privateToken) { const getContributors = async function (owner, name, cacheResult = false) {
loader.start() loader.start()
const contributors = await nyc.repoContributors({ const options = {
token, token: privateToken,
user: owner, user: owner,
repo: name, repo: name,
commits: true, commits: true,
}) }
// console.log('info provided:', options);
if (cacheResult) {
const nycOutputPath = join(__dirname, './nyc-output.json')
if (existsSync(nycOutputPath)) {
const contributors = await readFile(nycOutputPath)
loader.stop()
return JSON.parse(contributors)
} else {
loader.message('Getting repo contributors...')
const contributors = await nyc.repoContributors(options)
await writeFile(nycOutputPath, JSON.stringify(contributors, null, 2))
loader.stop()
return contributors
}
} else {
const contributors = await nyc.repoContributors(options)
loader.stop() loader.stop()
return contributors return contributors
} }
}
module.exports = {getContributors} module.exports = {getContributors}

File diff suppressed because it is too large Load diff