Skip to content

Commit ace4970

Browse files
committed
feat(discover): updated the learner + look at issue titles
and some visibly forgotten DidYouMean stuff from the person who added that feature
1 parent e21f46e commit ace4970

File tree

3 files changed

+56886
-25395
lines changed

3 files changed

+56886
-25395
lines changed

src/cli.js

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require('path')
55
const yargs = require('yargs')
66
const chalk = require('chalk')
77
const inquirer = require('inquirer')
8+
const didYouMean = require('didyoumean')
89

910
const init = require('./init')
1011
const generate = require('./generate')
@@ -24,12 +25,22 @@ const yargv = yargs
2425
.alias('v', 'version')
2526
.version()
2627
.recommendCommands()
27-
.command('generate', `Generate the list of contributors\n\nUSAGE: all-contributors generate`)
28-
.command('add', `Add a new contributor\n\nUSAGE: all-contributors add <username> <comma-separated contributions>`)
29-
.command('init', `Prepare the project to be used with this tool\n\nUSAGE: all-contributors init`)
28+
.command(
29+
'generate',
30+
`Generate the list of contributors\n\nUSAGE: all-contributors generate`,
31+
)
32+
.command(
33+
'add',
34+
`Add a new contributor\n\nUSAGE: all-contributors add <username> <comma-separated contributions>`,
35+
)
36+
.command(
37+
'init',
38+
`Prepare the project to be used with this tool\n\nUSAGE: all-contributors init`,
39+
)
3040
.command(
3141
'check',
32-
`Compare contributors from the repository with the ones credited in .all-contributorsrc'\n\nUSAGE: all-contributors check`)
42+
`Compare contributors from the repository with the ones credited in .all-contributorsrc'\n\nUSAGE: all-contributors check`,
43+
)
3344
.boolean('commit')
3445
.default('files', ['README.md'])
3546
.default('contributorsPerLine', 7)
@@ -177,10 +188,11 @@ async function checkContributors(argv) {
177188
}
178189

179190
async function fetchContributors(argv) {
180-
const {reviewers, commitAuthors, issueCreators} = await getContributors(
181-
argv.projectOwner,
182-
argv.projectName,
183-
)
191+
const {
192+
reviewers,
193+
commitAuthors,
194+
issueCreators /* , prCreators */,
195+
} = await getContributors(argv.projectOwner, argv.projectName, true)
184196
const args = {...argv, _: []}
185197
const contributorsToAdd = []
186198
const learner = await getLearner()
@@ -193,35 +205,40 @@ async function fetchContributors(argv) {
193205
)
194206
})
195207

208+
const guessCategories = (item, itemType, contributor) => {
209+
const guessedCategory = learner
210+
.classify(item)
211+
.find(ctr => ctr && ctr !== 'null')
212+
213+
if (!guessedCategory) {
214+
console.warn(
215+
`Oops, I couldn't find any category for the "${item}" ${itemType}`,
216+
)
217+
218+
return
219+
}
220+
221+
if (!contributor.contributions.includes(guessedCategory)) {
222+
contributor.contributions.push(guessedCategory)
223+
224+
console.log(
225+
`Adding ${chalk.blue(contributor.login)} for ${chalk.underline(
226+
guessedCategory,
227+
)}`,
228+
)
229+
}
230+
}
231+
196232
issueCreators.forEach(usr => {
197233
const contributor = {
198234
login: usr.login,
199235
contributions: [],
200236
}
237+
//TODO: Look at the titles field and categories based on that.
201238

202-
usr.labels.forEach(lbl => {
203-
const guessedCategory = learner
204-
.classify(lbl)
205-
.find(ctr => ctr && ctr !== 'null')
206-
207-
if (!guessedCategory) {
208-
console.warn(
209-
`Oops, I couldn't find any category for the "${lbl}" label`,
210-
)
211-
212-
return
213-
}
214-
215-
if (!contributor.contributions.includes(guessedCategory)) {
216-
contributor.contributions.push(guessedCategory)
239+
usr.labels.forEach(label => guessCategories(label, 'label', contributor))
217240

218-
console.log(
219-
`Adding ${chalk.blue(usr.login)} for ${chalk.underline(
220-
guessedCategory,
221-
)}`,
222-
)
223-
}
224-
})
241+
usr.titles.forEach(title => guessCategories(title, 'title', contributor))
225242

226243
const existingContributor = contributorsToAdd.find(
227244
ctr => ctr.login === usr.login,
@@ -234,6 +251,7 @@ async function fetchContributors(argv) {
234251
}
235252
})
236253

254+
//TODO Look at prCreators (including its titles field) and add contributions from there
237255
commitAuthors.forEach(usr => {
238256
const existingContributor = contributorsToAdd.find(
239257
ctr => ctr.login === usr.login,
@@ -331,6 +349,7 @@ promptForCommand(yargv)
331349
case 'fetch':
332350
return fetchContributors(yargv)
333351
default:
352+
suggestCommands(command)
334353
throw new Error(`Unknown command ${command}`)
335354
}
336355
})

src/discover/index.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,44 @@
1+
const {join} = require('path')
2+
const {existsSync} = require('fs')
3+
const {writeFile, readFile} = require('fs/promises')
14
const nyc = require('name-your-contributors')
25
const {Spinner} = require('clui')
36

4-
const privateToken = (process.env && process.env.PRIVATE_TOKEN) || ''
7+
const privateToken =
8+
process.env?.PRIVATE_TOKEN ?? process.env?.GITHUB_TOKEN ?? ''
59
const loader = new Spinner('Loading...')
610

7-
const getContributors = async function(owner, name, token = privateToken) {
11+
const getContributors = async function (owner, name, cacheResult = false) {
812
loader.start()
913

10-
const contributors = await nyc.repoContributors({
11-
token,
14+
const options = {
15+
token: privateToken,
1216
user: owner,
1317
repo: name,
1418
commits: true,
15-
})
19+
}
1620

17-
loader.stop()
21+
// console.log('info provided:', options);
22+
if (cacheResult) {
23+
const nycOutputPath = join(__dirname, './nyc-output.json')
24+
if (existsSync(nycOutputPath)) {
25+
const contributors = await readFile(nycOutputPath)
26+
loader.stop()
27+
return JSON.parse(contributors)
28+
} else {
29+
loader.message('Getting repo contributors...')
30+
const contributors = await nyc.repoContributors(options)
31+
await writeFile(nycOutputPath, JSON.stringify(contributors, null, 2))
32+
loader.stop()
33+
return contributors
34+
}
35+
} else {
36+
const contributors = await nyc.repoContributors(options)
1837

19-
return contributors
38+
loader.stop()
39+
40+
return contributors
41+
}
2042
}
2143

2244
module.exports = {getContributors}

0 commit comments

Comments
 (0)