Skip to content
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

adds ability to remove contribution types when editing existing user #94

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
Binary file added src/.cli.js.swp
Binary file not shown.
32 changes: 19 additions & 13 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ function checkContributors(argv) {
const configData = util.configFile.readConfig(argv.config)

return repo
.getContributors(configData.projectOwner, configData.projectName, configData.repoType, configData.repoHost)
.getContributors(
configData.projectOwner,
configData.projectName,
configData.repoType,
configData.repoHost,
)
.then(repoContributors => {
const checkKey = repo.getCheckKey(configData.repoType)
const knownContributions = configData.contributors.reduce((obj, item) => {
Expand Down Expand Up @@ -130,15 +135,16 @@ function promptForCommand(argv) {
message: 'What do you want to do?',
choices: [
{
name: 'Add a new contributor or add a new contribution type',
name: 'Add new contributor or edit contribution type',
value: 'add',
},
{
name: 'Re-generate the contributors list',
value: 'generate',
},
{
name: 'Compare contributors from the repository with the credited ones',
name:
'Compare contributors from the repository with the credited ones',
value: 'check',
},
],
Expand All @@ -155,16 +161,16 @@ function promptForCommand(argv) {
promptForCommand(yargv)
.then(command => {
switch (command) {
case 'init':
return init()
case 'generate':
return startGeneration(yargv)
case 'add':
return addContribution(yargv)
case 'check':
return checkContributors(yargv)
default:
throw new Error(`Unknown command ${command}`)
case 'init':
return init()
case 'generate':
return startGeneration(yargv)
case 'add':
return addContribution(yargv)
case 'check':
return checkContributors(yargv)
default:
throw new Error(`Unknown command ${command}`)
}
})
.catch(onError)
19 changes: 19 additions & 0 deletions src/contributors/__tests__/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,22 @@ test(`should update an existing contributor's contributions if a new type is add
},
)
})

test(`should update an existing contributor's contributions if an existing type is removed`, () => {
const {options} = fixtures()
const username = 'login2'
const contributions = ['code']

return addContributor(options, username, contributions, mockInfoFetcher).then(
contributors => {
expect(contributors.length).toBe(options.contributors.length)
expect(contributors[1]).toEqual({
login: 'login2',
name: 'Some name',
avatar_url: 'www.avatar.url',
profile: 'www.profile.url',
contributions: ['code'],
})
},
)
})
31 changes: 22 additions & 9 deletions src/contributors/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@ function uniqueTypes(contribution) {
return contribution.type || contribution
}

function formatContributions(options, existing, types) {
function formatContributions(options, existing = [], types) {
const same = _.intersectionBy(uniqueTypes, existing, types)
const diff = _.differenceBy(uniqueTypes, existing, types)

const keep = same.length
const remove = same.length && diff.length

if (options.url) {
return (existing || []).concat(
return existing.concat(
types.map(type => {
return {type, url: options.url}
}),
)
}
return _.uniqBy(uniqueTypes, (existing || []).concat(types))

if (remove || keep) {
return same
}

return _.uniqBy(uniqueTypes, existing.concat(types))
}

function updateContributor(options, contributor, contributions) {
Expand All @@ -38,12 +49,14 @@ function updateExistingContributor(options, username, contributions) {
}

function addNewContributor(options, username, contributions, infoFetcher) {
return infoFetcher(username, options.repoType, options.repoHost).then(userData => {
const contributor = _.assign(userData, {
contributions: formatContributions(options, [], contributions),
})
return options.contributors.concat(contributor)
})
return infoFetcher(username, options.repoType, options.repoHost).then(
userData => {
const contributor = _.assign(userData, {
contributions: formatContributions(options, [], contributions),
})
return options.contributors.concat(contributor)
},
)
}

module.exports = function addContributor(
Expand Down