From 01a68374852786f8e36f79541c0f35d5b4938804 Mon Sep 17 00:00:00 2001 From: Jeroen Engels Date: Mon, 29 Feb 2016 23:32:46 +0100 Subject: [PATCH] Add ability to update existing contributors --- lib/addContributor.js | 51 +++++++++++++++++++++++--------- lib/addContributor.test.js | 59 +++++++++++++++++++++++++++++--------- package.json | 2 ++ 3 files changed, 85 insertions(+), 27 deletions(-) diff --git a/lib/addContributor.js b/lib/addContributor.js index f8259c5..da70f8c 100644 --- a/lib/addContributor.js +++ b/lib/addContributor.js @@ -1,5 +1,7 @@ 'use strict'; +var uniq = require('lodash.uniq'); +var values = require('lodash.values'); var template = require('lodash.template'); var findIndex = require('lodash.findindex'); @@ -16,12 +18,13 @@ var defaultTemplate = '
<%= user.name %>](<%= user.html_url %>)' + ' | [<%= contributions %>](https://github.com/<%= options.projectOwner %>/<%= options.projectName %>/commits?author=<%= user.login %>)'; -function contributorEntry(options, user) { - var contributions = options.contributions - .map(function(contribution) { - return options.emoji[contribution]; - }) - .join(''); +function contributorEntry(options, user, existingContributions) { + var contributions = uniq((existingContributions || []).concat( + options.contributions + .map(function(contribution) { + return options.emoji[contribution]; + }) + )).join(''); var contributionTemplate = template(options.template || defaultTemplate); @@ -32,21 +35,43 @@ function contributorEntry(options, user) { }); } -module.exports = function addContributor(options, user, fileContent) { - var contributor = contributorEntry(options, user); +function parseContributionTypes(options, line) { + return values(options.emoji) + .filter(function findExistingContribution(type) { + return line.indexOf(type) !== -1; + }); +} +function upsertContributor(options, user, existingContributors) { + var contributor = contributorEntry(options, user); + var existingContributorIndex = findIndex(existingContributors, function(cont) { + return cont.indexOf(user.login) !== 1 && cont.indexOf(user.name) !== -1; + }); + + if (existingContributorIndex === -1) { + return [].concat(existingContributors, contributor); + } + + var contributionTypes = parseContributionTypes(options, existingContributors[existingContributorIndex]); + return [].concat( + existingContributors.slice(0, existingContributorIndex), + contributorEntry(options, user, contributionTypes), + existingContributors.slice(existingContributorIndex + 1) + ).join('\n') +} + +module.exports = function addContributor(options, user, fileContent) { var lines = fileContent.split('\n'); var contributorListStart = findIndex(lines, function(line) { return line.indexOf(':---: | :---:') !== -1; }); - var contributors = [].concat( - listAllContributors(contributorListStart + 1, lines), - contributor - ); + + var existingContributors = listAllContributors(contributorListStart + 1, lines); + var contributors = upsertContributor(options, user, existingContributors); return [].concat( lines.slice(0, contributorListStart + 1), contributors, - lines.slice(contributorListStart + contributors.length) + lines.slice(contributorListStart + existingContributors.length + 1) ).join('\n') } diff --git a/lib/addContributor.test.js b/lib/addContributor.test.js index 290e2c0..a8a9f9b 100644 --- a/lib/addContributor.test.js +++ b/lib/addContributor.test.js @@ -21,13 +21,20 @@ function fixtures() { } }; - const user = { + const jfmengelsUser = { login: 'jfmengels', name: 'Jeroen Engels', html_url: 'https://github.com/jfmengels', avatar_url: 'https://avatars.githubusercontent.com/u/3869412?v=3' }; + const kentcdoddsUser = { + login: 'kentcdodds', + name: 'Kent C. Dodds', + html_url: 'https://github.com/kentcdodds', + avatar_url: 'https://avatars.githubusercontent.com/u/1500684?v=3' + }; + const content = ` # project @@ -36,16 +43,18 @@ Description ## Contributors These people contributed to the project: :---: | :---: -[![Kent C. Dodds](https://avatars1.githubusercontent.com/u/1500684?s=130)
Kent C. Dodds](http://kentcdodds.com) | [📖](https://github.com/kentcdodds/all-contributors/commits?author=kentcdodds) -[![Divjot Singh](https://avatars1.githubusercontent.com/u/6177621?s=130)
Divjot Singh](http://bogas04.github.io) | [📖](https://github.com/kentcdodds/all-contributors/commits?author=bogas04) +[![Kent C. Dodds](https://avatars.githubusercontent.com/u/1500684?v=3&s=100)
Kent C. Dodds](https://github.com/kentcdodds) | [:doc:](https://github.com/kentcdodds/all-contributors/commits?author=kentcdodds) +[![Divjot Singh](https://avatars1.githubusercontent.com/u/6177621?s=130)
Divjot Singh](http://bogas04.github.io) | [:doc:](https://github.com/kentcdodds/all-contributors/commits?author=bogas04) Thanks a lot guys! `; - return {options, user, content}; + + return {options, jfmengelsUser, kentcdoddsUser, content}; } test('should add a new contributor to the end of the list', t => { - const {options, user, content} = fixtures(); + t.pass(1); + const {options, jfmengelsUser, content} = fixtures(); const expected = ` # project @@ -54,32 +63,54 @@ Description ## Contributors These people contributed to the project: :---: | :---: -[![Kent C. Dodds](https://avatars1.githubusercontent.com/u/1500684?s=130)
Kent C. Dodds](http://kentcdodds.com) | [📖](https://github.com/kentcdodds/all-contributors/commits?author=kentcdodds) -[![Divjot Singh](https://avatars1.githubusercontent.com/u/6177621?s=130)
Divjot Singh](http://bogas04.github.io) | [📖](https://github.com/kentcdodds/all-contributors/commits?author=bogas04) +[![Kent C. Dodds](https://avatars.githubusercontent.com/u/1500684?v=3&s=100)
Kent C. Dodds](https://github.com/kentcdodds) | [:doc:](https://github.com/kentcdodds/all-contributors/commits?author=kentcdodds) +[![Divjot Singh](https://avatars1.githubusercontent.com/u/6177621?s=130)
Divjot Singh](http://bogas04.github.io) | [:doc:](https://github.com/kentcdodds/all-contributors/commits?author=bogas04) [![Jeroen Engels](https://avatars.githubusercontent.com/u/3869412?v=3&s=100)
Jeroen Engels](https://github.com/jfmengels) | [:doc:](https://github.com/kentcdodds/all-contributors/commits?author=jfmengels) Thanks a lot guys! `; - t.is(addContributor(options, user, content), expected); + t.is(addContributor(options, jfmengelsUser, content), expected); }); test('should be able to inject several contributions', t => { - const {options, user, content} = fixtures(); + t.pass(1); + const {options, jfmengelsUser, content} = fixtures(); options.contributions = ['doc', 'code']; const expected = '[![Jeroen Engels](https://avatars.githubusercontent.com/u/3869412?v=3&s=100)
Jeroen Engels](https://github.com/jfmengels) | [:doc::code:](https://github.com/kentcdodds/all-contributors/commits?author=jfmengels)'; - const result = addContributor(options, user, content); + const result = addContributor(options, jfmengelsUser, content); - t.is(getUserLine(result, user), expected); + t.is(getUserLine(result, jfmengelsUser), expected); }); test('should be able to specify a new template in options', t => { - const {options, user, content} = fixtures(); + t.pass(1); + const {options, jfmengelsUser, content} = fixtures(); options.template = '<%= user.login %> made awesome contributions!'; const expected = 'jfmengels made awesome contributions!'; - const result = addContributor(options, user, content); + const result = addContributor(options, jfmengelsUser, content); - t.is(getUserLine(result, user), expected); + t.is(getUserLine(result, jfmengelsUser), expected); +}); + +test('should not modify content when adding an existing contributor that has the given contribution types', t => { + t.pass(1); + const {options, kentcdoddsUser, content} = fixtures(); + const expected = content; + + const result = addContributor(options, kentcdoddsUser, content); + t.is(result, expected); +}); + +test('should add new contributions types to an existing contributor', t => { + t.pass(1); + const {options, kentcdoddsUser, content} = fixtures(); + options.contributions = ['doc', 'code']; + const expected = '[![Kent C. Dodds](https://avatars.githubusercontent.com/u/1500684?v=3&s=100)
Kent C. Dodds](https://github.com/kentcdodds) | [:doc::code:](https://github.com/kentcdodds/all-contributors/commits?author=kentcdodds)'; + + const result = addContributor(options, kentcdoddsUser, content); + + t.is(getUserLine(result, kentcdoddsUser), expected); }); diff --git a/package.json b/package.json index c94e792..55b65fd 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,8 @@ "lodash.assign": "^4.0.4", "lodash.findindex": "^4.2.0", "lodash.template": "^4.2.1", + "lodash.uniq": "^4.2.0", + "lodash.values": "^4.1.0", "request": "^2.69.0", "yargs": "^4.2.0" },