This repository has been archived on 2025-01-23. You can view files and clone it, but cannot push or open issues or pull requests.
kredits-reviews/index.js

151 lines
4.1 KiB
JavaScript

require('dotenv').config()
const GiteaReviews = require('./lib/gitea-reviews');
const GithubReviews = require('./lib/github-reviews');
const ethers = require('ethers');
const Kredits = require('kredits-contracts');
const util = require('util');
const providerUrl = process.env.KREDITS_PROVIDER_URL;
const daoAddress = process.env.KREDITS_DAO_ADDRESS;
const ipfsConfig = {
host: process.env.IPFS_API_HOST || 'localhost',
port: process.env.IPFS_API_PORT || '5001',
protocol: process.env.IPFS_API_PROTOCOL || 'http'
};
const kreditsAmounts = {
'kredits-1': 100,
'kredits-2': 300,
'kredits-3': 1000
};
// TODO get the dates from params or user input
const startDate = new Date('2020-11-01T00:00:00Z');
const endDate = new Date('2020-12-30T23:59:59Z');
const repos = require('./repos.json');
async function getAllReviews(repos, startDate, endDate) {
const githubReviews = new GithubReviews(process.env.GITHUB_TOKEN, kreditsAmounts);
const giteaReviews = new GiteaReviews(process.env.GITEA_TOKEN, kreditsAmounts);
return Promise.all([
githubReviews.getReviewContributions(repos.github, startDate, endDate),
giteaReviews.getReviewContributions(repos.gitea, startDate, endDate)
]).then(reviews => {
return { github: reviews[0], gitea: reviews[1] }
});
}
async function initializeKredits () {
//
// Ethereum provider/node setup
//
let ethProvider;
if (providerUrl) {
ethProvider = new ethers.providers.JsonRpcProvider(providerUrl);
} else {
ethProvider = new ethers.getDefaultProvider('rinkeby');
}
//
// Kredits contracts setup
//
const opts = { ipfsConfig };
if (daoAddress) {
opts.addresses = { Kernel: daoAddress };
}
let kredits;
try {
kredits = await new Kredits(ethProvider, null, opts).init();
} catch(error) {
console.log('Could not set up kredits:', error);
process.exit(1);
}
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;
}
Promise.all([initializeKredits(), getAllReviews(repos, startDate, endDate)]).then((values) => {
const kredits = values[0];
const reviews = values[1];
generateContributionData(reviews, kredits.Contributor).then(contributionData => {
console.log('contributions:');
console.log(util.inspect(contributionData, { depth: 3, colors: true }));
});
});