Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@
},
"homepage": "https://github.com/all-contributors/all-contributors-cli#readme",
"dependencies": {
"@babel/runtime": "^7.2.0",
"ac-learn": "^1.0.3",
"@babel/runtime": "^7.7.6",
"ac-learn": "^1.5.1",
"async": "^3.0.1",
"chalk": "^2.3.0",
"clui": "^0.3.6",
"didyoumean": "^1.2.1",
"name-your-contributors": "^3.8.3",
"inquirer": "^6.2.1",
"json-fixer": "^1.3.1-0",
"json-fixer": "^1.4.1",
"inquirer": "^7.0.4",
Comment thread
Berkmann18 marked this conversation as resolved.
Outdated
"lodash": "^4.11.2",
"name-your-contributors": "^3.4.0",
"pify": "^4.0.1",
"request": "^2.72.0",
"yargs": "^13.1.0"
Expand Down
194 changes: 85 additions & 109 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const util = require('./util')
const repo = require('./repo')
const updateContributors = require('./contributors')
const {getContributors} = require('./discover')
const learner = require('./discover/learner')
const {getLearner} = require('./discover/learner')

const cwd = process.cwd()
const defaultRCFile = path.join(cwd, '.all-contributorsrc')
Expand Down Expand Up @@ -130,7 +130,6 @@ function checkContributors(argv) {
configData.repoHost,
)
.then(repoContributors => {
// console.dir(repoContributors) //['jfmengels', 'jakebolam', ...]
const checkKey = repo.getCheckKey(configData.repoType)
const knownContributions = configData.contributors.reduce((obj, item) => {
obj[item[checkKey]] = item.contributions
Expand Down Expand Up @@ -167,120 +166,97 @@ function checkContributors(argv) {
})
}

function fetchContributors(argv) {
// console.log('argv=', argv);
// const configData = util.configFile.readConfig(argv.config)
// console.log('configData')
// console.dir(configData)

return getContributors(argv.projectOwner, argv.projectName).then(
repoContributors => {
// repoContributors = {prCreators, prCommentators, issueCreators, issueCommentators, reviewers, commitAuthors, commitCommentators}
// console.dir(repoContributors)

// const checkKey = repo.getCheckKey(configData.repoType)
// const knownContributions = configData.contributors.reduce((obj, item) => {
// obj[item[checkKey]] = item.contributions
// return obj
// }, {})
// console.log('knownContributions', knownContributions) //{ jfmengels: ['code', 'test', 'doc'], ...}
// const knownContributors = configData.contributors.map(
// contributor => contributor[checkKey],
// )
// console.log('knownContributors', knownContributors) //['kentcdodds', 'ben-eb', ...]

// let contributors = new Set(
// repoContributors.prCreators.map(usr => usr.login),
// )

// repoContributors.issueCreators.forEach(usr => contributors.add(usr.login))
// repoContributors.reviewers.forEach(usr => contributors.add(usr.login))
// repoContributors.commitAuthors.forEach(usr => contributors.add(usr.login))
// contributors = Array.from(contributors)

// console.log('ctbs=', contributors);

//~1. Auto-add reviewers for review~
//~2. Auto-add issue creators for any categories found~
//~3. Auto-add commit authors~
//4. Roll onto other contribution categories following https://www.draw.io/#G1uL9saIuZl3rj8sOo9xsLOPByAe28qhwa

const args = {...argv, _: []}
const contributorsToAdd = []
repoContributors.reviewers.forEach(usr => {
// args._ = ['add', usr.login, 'review']
// addContribution(args)
contributorsToAdd.push({login: usr.login, contributions: ['review']})
// console.log(
// `Adding ${chalk.underline('Reviewer')} ${chalk.blue(usr.login)}`,
// )
})
async function fetchContributors(argv) {
const {reviewers, commitAuthors, issueCreators} = await getContributors(
argv.projectOwner,
argv.projectName,
)
const args = {...argv, _: []}
const contributorsToAdd = []
const learner = await getLearner()

repoContributors.issueCreators.forEach(usr => {
// console.log('usr=', usr.login, 'labels=', usr.labels)
const contributor = {
login: usr.login,
contributions: [],
}
usr.labels.forEach(lbl => {
const guesses = learner.classify(lbl).filter(c => c && c !== 'null')
if (guesses.length) {
const category = guesses[0]
// args._ = ['', usr.login, category]
// addContribution(args)
if (!contributor.contributions.includes(category))
contributor.contributions.push(category)
// console.log(
// `Adding ${chalk.blue(usr.login)} for ${chalk.underline(category)}`,
// )
} //else console.warn(`Oops, I couldn't find any category for the "${lbl}" label`)
})
const existingContributor = contributorsToAdd.filter(
ctrb => ctrb.login === usr.login,
)
if (existingContributor.length) {
existingContributor[0].contributions = [
...new Set(
existingContributor[0].contributions.concat(
contributor.contributions,
),
),
]
} else contributorsToAdd.push(contributor)
})
reviewers.forEach(usr => {
contributorsToAdd.push({login: usr.login, contributions: ['review']})

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

issueCreators.forEach(usr => {
const contributor = {
login: usr.login,
contributions: [],
}

usr.labels.forEach(lbl => {
const guessedCategory = learner.classify(lbl).find(c => c && c !== 'null')
Comment thread
Berkmann18 marked this conversation as resolved.
Outdated

repoContributors.commitAuthors.forEach(usr => {
// const contributor = {
// login: usr.login,
// contributions: [],
// }
// console.log('commit auth:', usr)
const existingContributor = contributorsToAdd.filter(
ctrb => ctrb.login === usr.login,
if (!guessedCategory) {
Comment thread
Berkmann18 marked this conversation as resolved.
console.warn(
`Oops, I couldn't find any category for the "${lbl}" label`,
)
if (existingContributor.length) {
//there's no label or commit message info so use only code for now
if (!existingContributor[0].contributions.includes('code')) {
existingContributor[0].contributions.push('code')
}
} else
contributorsToAdd.push({login: usr.login, contributions: ['code']})
})

// console.log('contributorsToAdd=', contributorsToAdd)
contributorsToAdd.forEach(contributor => {
return
}

if (!contributor.contributions.includes(guessedCategory)) {
contributor.contributions.push(guessedCategory)

console.log(
`Adding ${chalk.blue(contributor.login)} for ${chalk.underline(
contributor.contributions.join('/'),
`Adding ${chalk.blue(usr.login)} for ${chalk.underline(
guessedCategory,
)}`,
)
args._ = ['', contributor.login, contributor.contributions.join(',')]
// if (contributor.contributions.length) addContribution(args)
// else console.log('Skipping', contributor.login)
})
},
err => console.error('fetch error:', err),
)
}
})

const existingContributor = contributorsToAdd.find(
c => c.login === usr.login,
Comment thread
Berkmann18 marked this conversation as resolved.
Outdated
)

if (existingContributor) {
Comment thread
Berkmann18 marked this conversation as resolved.
existingContributor.contributions.push(...contributor.contributions)
} else {
contributorsToAdd.push(contributor)
}
})

commitAuthors.forEach(usr => {
const existingContributor = contributorsToAdd.find(
c => c.login === usr.login,
)

if (existingContributor) {
Comment thread
Berkmann18 marked this conversation as resolved.
// There's no label or commit message info so use only code for now
if (!existingContributor.contributions.includes('code')) {
existingContributor.contributions.push('code')
}
} else {
contributorsToAdd.push({login: usr.login, contributions: ['code']})
}
})

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

for (const contributor of contributorsToAdd) {
if (!contributor.contributions.length) {
console.log('Skipping', contributor.login)

return
Comment thread
Berkmann18 marked this conversation as resolved.
Outdated
}

console.log(
`Adding ${chalk.blue(contributor.login)} for ${chalk.underline(
contributor.contributions.join('/'),
)}`,
)

args._ = ['', contributor.login, contributor.contributions.join(',')]

/* eslint-disable no-await-in-loop */
await addContribution(args)
Comment thread
Berkmann18 marked this conversation as resolved.
Outdated
}
}

function onError(error) {
Expand Down
7 changes: 5 additions & 2 deletions src/discover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ const {Spinner} = require('clui')
const privateToken = (process.env && process.env.PRIVATE_TOKEN) || ''
const loader = new Spinner('Loading...')

const getContributors = function(owner, name, token = privateToken) {
const getContributors = async function(owner, name, token = privateToken) {
loader.start()
const contributors = nyc.repoContributors({

const contributors = await nyc.repoContributors({
token,
user: owner,
repo: name,
commits: true,
})

loader.stop()

return contributors
}

Expand Down
37 changes: 21 additions & 16 deletions src/discover/learner.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@ const Learner = require('ac-learn')

const JSON_PATH = `${__dirname}/learner.json`

//@TODO: Use the JSON methods from `ac-learn` to get the whole thing saveable
const learner = new Learner()
/* eslint-disable no-console */
if (existsSync(JSON_PATH)) {
learner.loadAndDeserializeClassifier(JSON_PATH).then(classifier => {
learner.classifier = classifier
// console.log('Re-using existing classifier')
}, console.error)
} else {
learner.crossValidate(6)
learner.eval()
learner.serializeAndSaveClassifier(JSON_PATH).then(_ => {
// console.log('Classifier saved', classifier)
}, console.error)
async function getLearner() {
const learner = new Learner()

try {
if (existsSync(JSON_PATH)) {
learner.classifier = await learner.loadAndDeserializeClassifier(JSON_PATH)
} else {
learner.crossValidate(6)
learner.eval()

await learner.serializeAndSaveClassifier(JSON_PATH)
}
} catch (e) {
/* eslint-disable no-console */
console.error(e)
}

return learner
}
/* eslint-enable no-console */

module.exports = learner
module.exports = {
getLearner,
Comment thread
Berkmann18 marked this conversation as resolved.
}
Empty file removed src/discover/learner.json
Empty file.