diff --git a/src/cli.js b/src/cli.js index 636881dc..b83a5b6b 100755 --- a/src/cli.js +++ b/src/cli.js @@ -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) => { @@ -130,7 +135,7 @@ 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', }, { @@ -138,7 +143,8 @@ function promptForCommand(argv) { value: 'generate', }, { - name: 'Compare contributors from the repository with the credited ones', + name: + 'Compare contributors from the repository with the credited ones', value: 'check', }, ], @@ -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) diff --git a/src/contributors/__tests__/add.js b/src/contributors/__tests__/add.js index b5cbbdf2..2e8f71ce 100644 --- a/src/contributors/__tests__/add.js +++ b/src/contributors/__tests__/add.js @@ -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'], + }) + }, + ) +}) diff --git a/src/contributors/add.js b/src/contributors/add.js index a23a2ddf..d45a2d6f 100644 --- a/src/contributors/add.js +++ b/src/contributors/add.js @@ -4,15 +4,23 @@ function uniqueTypes(contribution) { return contribution.type || contribution } -function formatContributions(options, existing, types) { +function formatContributions(options, existing = [], types) { + const same = _.intersectionBy(uniqueTypes, existing, types) + const remove = types.length < existing.length && same.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) { + return same + } + + return _.uniqBy(uniqueTypes, existing.concat(types)) } function updateContributor(options, contributor, contributions) { @@ -38,12 +46,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(