2016-03-02 22:45:23 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _ = require('lodash/fp');
|
|
|
|
|
|
|
|
function uniqueTypes(contribution) {
|
|
|
|
return contribution.type || contribution;
|
|
|
|
}
|
|
|
|
|
2016-03-27 15:19:13 +00:00
|
|
|
function formatContributions(options, existing, types) {
|
2016-03-02 22:45:23 +00:00
|
|
|
if (options.url) {
|
2016-03-06 23:20:24 +00:00
|
|
|
return (existing || []).concat(types.map(function (type) {
|
|
|
|
return {type: type, url: options.url};
|
2016-03-02 22:45:23 +00:00
|
|
|
}));
|
|
|
|
}
|
2016-03-06 23:20:24 +00:00
|
|
|
return _.uniqBy(uniqueTypes, (existing || []).concat(types));
|
2016-03-02 22:45:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateContributor(options, contributor, contributions) {
|
|
|
|
return _.assign(contributor, {
|
|
|
|
contributions: formatContributions(options, contributor.contributions, contributions)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateExistingContributor(options, username, contributions) {
|
2016-03-06 23:20:24 +00:00
|
|
|
return options.contributors.map(function (contributor) {
|
2016-03-02 22:45:23 +00:00
|
|
|
if (username !== contributor.login) {
|
|
|
|
return contributor;
|
|
|
|
}
|
|
|
|
return updateContributor(options, contributor, contributions);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-15 21:25:32 +00:00
|
|
|
function addNewContributor(options, username, contributions, infoFetcher) {
|
|
|
|
return infoFetcher(username)
|
|
|
|
.then(userData => {
|
2016-03-02 22:45:23 +00:00
|
|
|
var contributor = _.assign(userData, {
|
|
|
|
contributions: formatContributions(options, [], contributions)
|
|
|
|
});
|
2017-02-15 21:25:32 +00:00
|
|
|
return options.contributors.concat(contributor);
|
2016-03-02 22:45:23 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-02-15 21:25:32 +00:00
|
|
|
module.exports = function addContributor(options, username, contributions, infoFetcher) {
|
2016-03-02 22:45:23 +00:00
|
|
|
if (_.find({login: username}, options.contributors)) {
|
2017-02-15 21:25:32 +00:00
|
|
|
return Promise.resolve(updateExistingContributor(options, username, contributions));
|
2016-03-02 22:45:23 +00:00
|
|
|
}
|
2017-02-15 21:25:32 +00:00
|
|
|
return addNewContributor(options, username, contributions, infoFetcher);
|
2016-03-06 23:20:24 +00:00
|
|
|
};
|