Skip to content

Commit

Permalink
feat(cli): include pr based contributions
Browse files Browse the repository at this point in the history
  • Loading branch information
Berkmann18 committed Jul 23, 2023
1 parent ace4970 commit b0e1213
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"json-fixer": "^1.6.8",
"lodash": "^4.11.2",
"name-your-contributors": "^3.10.0",
"nclr": "^2.2.5",
"node-fetch": "^2.6.0",
"pify": "^5.0.0",
"yargs": "^15.0.1",
Expand Down
57 changes: 43 additions & 14 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const yargs = require('yargs')
const chalk = require('chalk')
const inquirer = require('inquirer')
const didYouMean = require('didyoumean')
const {info, warn, use} = require('nclr')

const init = require('./init')
const generate = require('./generate')
Expand Down Expand Up @@ -191,7 +192,8 @@ async function fetchContributors(argv) {
const {
reviewers,
commitAuthors,
issueCreators /* , prCreators */,
issueCreators,
prCreators,
} = await getContributors(argv.projectOwner, argv.projectName, true)
const args = {...argv, _: []}
const contributorsToAdd = []
Expand All @@ -201,18 +203,21 @@ async function fetchContributors(argv) {
contributorsToAdd.push({login: usr.login, contributions: ['review']})

console.log(
`Adding ${chalk.underline('Reviewer')} ${chalk.blue(usr.login)}`,
`Including ${chalk.underline('Reviewer')} ${use('info', usr.login)}`,
)
})

const guessCategories = (item, itemType, contributor) => {
const guessedCategory = learner
.classify(item)
.find(ctr => ctr && ctr !== 'null')
.find(ctr => ctr && ctr !== 'null' && ctr !== 'undefined')

if (!guessedCategory) {
console.warn(
`Oops, I couldn't find any category for the "${item}" ${itemType}`,
warn(
`Oops, I couldn't find any category for the "${use(
'inp',
item,
)}" ${itemType}`,
)

return
Expand All @@ -222,22 +227,21 @@ async function fetchContributors(argv) {
contributor.contributions.push(guessedCategory)

console.log(
`Adding ${chalk.blue(contributor.login)} for ${chalk.underline(
`Including ${use('info', contributor.login)} for ${chalk.underline(
guessedCategory,
)}`,
)}, based on "${use('inp', item)}"`,
)
}
}

info('Looking at issue creators')
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(
Expand All @@ -251,14 +255,35 @@ async function fetchContributors(argv) {
}
})

//TODO Look at prCreators (including its titles field) and add contributions from there
info('Looking at PR creators')
prCreators.forEach(usr => {
const contributor = {
login: usr.login,
contributions: [],
}

usr.labels.forEach(label => guessCategories(label, 'PR label', contributor))
usr.titles.forEach(title => guessCategories(title, 'PR title', contributor))

const existingContributor = contributorsToAdd.find(
ctr => ctr.login === usr.login,
)

if (existingContributor) {
existingContributor.contributions.push(...contributor.contributions)
} else {
contributorsToAdd.push(contributor)
}
})

info('Looking at commit authors')
commitAuthors.forEach(usr => {
const existingContributor = contributorsToAdd.find(
ctr => ctr.login === usr.login,
)

if (existingContributor) {
// There's no label or commit message info so use only code for now
// TODO: See how the commit message could be added (this may require the full output) to not just assume it's a code contribution
if (!existingContributor.contributions.includes('code')) {
existingContributor.contributions.push('code')
}
Expand All @@ -269,8 +294,12 @@ async function fetchContributors(argv) {

// TODO: Roll onto other contribution categories following https://www.draw.io/#G1uL9saIuZl3rj8sOo9xsLOPByAe28qhwa

info('Finalising')
for (const contributor of contributorsToAdd) {
if (!contributor.contributions.length) {
const isDependabotDuplicates = /dependabot(\[bot\]|-\w+)/.test(
contributor.login,
)
if (!contributor.contributions.length || isDependabotDuplicates) {
console.log('Skipping', contributor.login)

continue
Expand All @@ -280,7 +309,7 @@ async function fetchContributors(argv) {
const contributions = contributor.contributions.join('/')

console.log(
`Adding ${chalk.blue(contributor.login)} for ${chalk.underline(
`Adding ${use('info', contributor.login)} for ${chalk.underline(
contributions,
)}`,
)
Expand All @@ -292,7 +321,7 @@ async function fetchContributors(argv) {
await addContribution(args)
} catch (error) {
console.error(
`Adding ${chalk.blue(contributor.login)} for ${chalk.underline(
`Adding ${use('info', contributor.login)} for ${chalk.underline(
contributions,
)} Failed: ${JSON.stringify(error)}`,
)
Expand Down

0 comments on commit b0e1213

Please sign in to comment.