Add ability to update existing contributors

This commit is contained in:
Jeroen Engels 2016-02-29 23:32:46 +01:00
parent 5101e9085c
commit 01a6837485
3 changed files with 85 additions and 27 deletions

View file

@ -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 =
'<br /><%= 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')
}

View file

@ -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)<br />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)<br />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)<br />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)<br />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)<br />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)<br />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)<br />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)<br />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)<br />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)<br />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)<br />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);
});

View file

@ -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"
},