diff --git a/index.js b/index.js index 6ec1700..64e7152 100644 --- a/index.js +++ b/index.js @@ -71,3 +71,70 @@ async function initializeKredits () { return kredits; } +async function generateContributionData(reviews, Contributor) { + const contributors = await Contributor.all(); + + const contributionData = {}; + + for (const [username, giteaReviews] of Object.entries(reviews.gitea)) { + const contributor = contributors.find(c => { + return c.gitea_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 + } + } + } + + for (const [username, githubReviews] of Object.entries(reviews.github)) { + const contributor = contributors.find(c => { + return c.github_username === username; + }); + + 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 + } + } + } + } + + return contributionData; +} +