Refactor to remove some duplications

This commit is contained in:
galfert 2021-01-07 23:02:35 +01:00
parent bffaae064c
commit 8d15f69c75

View File

@ -76,65 +76,43 @@ async function generateContributionData(reviews, Contributor) {
const contributionData = {};
for (const [username, giteaReviews] of Object.entries(reviews.gitea)) {
const contributor = contributors.find(c => {
return c.gitea_username === username;
});
function addContributionDataForPlatform(platform) {
for (const [username, platformReviews] of Object.entries(reviews[platform])) {
const contributor = contributors.find(c => {
return c[`${platform}_username`] === username;
});
if (!contributor) {
console.log(`Could not find contributor for Gitea username ${username}`);
continue;
}
const urls = giteaReviews.map(review => review.pr.html_url);
const kreditsAmount = giteaReviews.reduce((amount, review) => {
return review.kredits + amount;
}, 0);
contributionData[contributor.name] = {
contributorId: contributor.id,
contributorIpfsHash: contributor.ipfsHash,
kind: 'dev',
amount: kreditsAmount,
description: 'PR reviews',
details: {
'pullRequests': urls
if (!contributor) {
console.log(`Could not find contributor for ${platform} user "${username}"`);
continue;
}
}
}
for (const [username, githubReviews] of Object.entries(reviews.github)) {
const contributor = contributors.find(c => {
return c.github_username === username;
});
const urls = platformReviews.map(review => review.pr.html_url);
const kreditsAmount = platformReviews.reduce((amount, review) => {
return review.kredits + amount;
}, 0);
if (!contributor) {
console.log(`Could not find contributor for Github username ${username}`);
continue;
}
const urls = githubReviews.map(review => review.pr.html_url);
const kreditsAmount = githubReviews.reduce((amount, review) => {
return review.kredits + amount;
}, 0);
if (typeof contributionData[contributor.name] !== 'undefined') {
contributionData[contributor.name].amount += kreditsAmount;
contributionData[contributor.name].details.pullRequests.push(...urls);
} else {
contributionData[contributor.name] = {
contributorId: contributor.id,
contributorIpfsHash: contributor.ipfsHash,
kind: 'dev',
amount: kreditsAmount,
description: 'PR reviews',
details: {
'pullRequests': urls
if (typeof contributionData[contributor.name] !== 'undefined') {
contributionData[contributor.name].amount += kreditsAmount;
contributionData[contributor.name].details.pullRequests.push(...urls);
} else {
contributionData[contributor.name] = {
contributorId: contributor.id,
contributorIpfsHash: contributor.ipfsHash,
kind: 'dev',
amount: kreditsAmount,
description: 'PR reviews',
details: {
'pullRequests': urls
}
}
}
}
}
addContributionDataForPlatform('gitea');
addContributionDataForPlatform('github');
return contributionData;
}