Compare commits
52 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
70a74ba5fb
|
||
|
|
1ab5ae55c6
|
||
|
|
710bd90172
|
||
| c27fefcfdc | |||
| cbfd25e880 | |||
| 8b82b8b159 | |||
| 084dec58a9 | |||
|
9a4e465608
|
|||
| 93d7c8944b | |||
|
ad0e34b990
|
|||
| 063a9c6b37 | |||
|
f05436e9b9
|
|||
| e305643f69 | |||
| 82a003ffeb | |||
|
fc0c113997
|
|||
|
5c6540580b
|
|||
| 078f78417c | |||
| d870099059 | |||
| b7482f2468 | |||
| 94c256e3d9 | |||
| 3b382eadb2 | |||
| ec63980cd3 | |||
|
|
2b86f37fcb | ||
| a8e29f2197 | |||
| b7ff55929c | |||
| 35f6acc150 | |||
| 095a1e0004 | |||
| 95290a7715 | |||
| fca017c61a | |||
| fb1a471303 | |||
| d82e2e9256 | |||
| 634dc207e6 | |||
| 6fd3989118 | |||
| 41f5aef460 | |||
| c121713a13 | |||
| e10dd4abc3 | |||
| e823797ee2 | |||
| 7d3c2cae19 | |||
| 960dcb55de | |||
| 60ed697460 | |||
| 7f653f23ce | |||
| 98ff61ab0a | |||
| e7f8723f6e | |||
| 164782bd25 | |||
| 8f961bb102 | |||
| c4ef8de018 | |||
| 110c4384e0 | |||
| 70ea031b31 | |||
| ab8d043593 | |||
|
|
8b5c2a3274 | ||
| 5cc0116163 | |||
| 708f0b6622 |
2
.env.example
Normal file
2
.env.example
Normal file
@@ -0,0 +1,2 @@
|
||||
GITEA_TOKEN=your-token-here
|
||||
GITHUB_TOKEN=your-token-here
|
||||
4
.github/release-drafter.yml
vendored
Normal file
4
.github/release-drafter.yml
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
template: |
|
||||
## Changes
|
||||
|
||||
$CHANGES
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
||||
node_modules
|
||||
wallet.json
|
||||
.env
|
||||
|
||||
44
README.md
44
README.md
@@ -10,15 +10,20 @@ contributions.
|
||||
|
||||
## Setup
|
||||
|
||||
### Ethereum wallet
|
||||
### Wallet
|
||||
|
||||
You will need an Ethereum wallet for your bot, so it can interact with the
|
||||
Ethereum smart contracts. `npm run create-wallet` will do the job for you.
|
||||
You will need a keypair/wallet for your bot, so it can interact with the smart
|
||||
contracts. `npm run create-wallet` will do the job for you.
|
||||
|
||||
The wallet must be funded with enough ETH to interact with the contracts.
|
||||
The wallet must be funded with enough native chain tokens to interact with the
|
||||
contracts (i.e. it must be able to pay gas/tx fees)
|
||||
|
||||
### Contract permissions
|
||||
|
||||
**Warning: outdated instructions!**
|
||||
|
||||
*TODO adapt instructions for new permission model*
|
||||
|
||||
The bot wallet needs the following Aragon contract permissions to interact
|
||||
with [kredits-contracts]:
|
||||
|
||||
@@ -41,11 +46,10 @@ As usual in Hubot, you can add all config as environment variables.
|
||||
| --- | --- |
|
||||
| `KREDITS_WEBHOOK_TOKEN` | A string for building your secret webhook URLs |
|
||||
| `KREDITS_ROOM` | The bot will talk to you in this room |
|
||||
| `KREDITS_WALLET_PATH` | Path to an Etherum wallet JSON file (default: `./wallet.json`) |
|
||||
| `KREDITS_WALLET_PATH` | Path to an wallet JSON file (default: `./wallet.json`) |
|
||||
| `KREDITS_WALLET_PASSWORD` | Wallet password |
|
||||
| `KREDITS_PROVIDER_URL` | Ethereum JSON-RPC URL (default: `http://localhost:7545`) |
|
||||
| `KREDITS_PROVIDER_URL` | JSON-RPC URL of a blockchain node (default: `http://localhost:7545`) |
|
||||
| `KREDITS_WEB_URL` | URL of the Kredits Web app (default: `https://kredits.kosmos.org`) |
|
||||
| `KREDITS_DAO_ADDRESS` | DAO Kernel address |
|
||||
| `KREDITS_SESSION_SECRET` | Secret used by [grant](https://www.npmjs.com/package/grant) to sign the session ID |
|
||||
| `KREDITS_GRANT_HOST` | Host used by [grant](https://www.npmjs.com/package/grant) to generate OAuth redirect URLs (default: `localhost:8888`) |
|
||||
| `KREDITS_GRANT_PROTOCOL` | Protocol (http or https) used by [grant](https://www.npmjs.com/package/grant") to generate the OAuth redirect URLs (default: "http") |
|
||||
@@ -116,3 +120,29 @@ wiki's API on its own.
|
||||
|
||||
[kredits-contracts]: https://github.com/67P/kredits-contracts
|
||||
[GitHub OAuth app]: https://developer.github.com/apps/about-apps/#about-oauth-apps
|
||||
|
||||
|
||||
### Zoom
|
||||
|
||||
The Zoom integration creates contributions for meeting participations.
|
||||
|
||||
Every meeting that is longer than 15 minutes and with more than 2 participants will be registered.
|
||||
An optional meeting whitelist can be configured to create contributions only for specific meetings.
|
||||
|
||||
|
||||
#### Setup
|
||||
|
||||
A Zoom JWT app has to be set up and an [event webhook subscription](https://marketplace.zoom.us/docs/api-reference/webhook-reference/meeting-events/meeting-ending")
|
||||
on `meeting.ended` has to be configured to the following URL:
|
||||
|
||||
https://your-hubot.example.com/incoming/kredits/zoom/{webhook_token}
|
||||
|
||||
#### Config
|
||||
|
||||
| Key | Description |
|
||||
| --- | --- |
|
||||
| `KREDITS_ZOOM_JWT` | The JWT for the Zoom application (required)
|
||||
| `KREDITS_ZOOM_MEETING_WHITELIST` | Comma separated list of meeting names for which kredits should be tracked (optional)
|
||||
| `KREDITS_ZOOM_CONTRIBUTION_AMOUNT` | The amount of kredits issued for each meeting. (default: 500)
|
||||
|
||||
[Zoom apps](https://marketplace.zoom.us/user/build)
|
||||
|
||||
77
index.js
77
index.js
@@ -1,13 +1,11 @@
|
||||
const fs = require('fs');
|
||||
const util = require('util');
|
||||
const fetch = require('node-fetch');
|
||||
const ethers = require('ethers');
|
||||
const Kredits = require('kredits-contracts');
|
||||
const NonceManager = require('@ethersproject/experimental').NonceManager;
|
||||
const Kredits = require('@kredits/contracts');
|
||||
|
||||
const walletPath = process.env.KREDITS_WALLET_PATH || './wallet.json';
|
||||
const walletJson = fs.readFileSync(walletPath);
|
||||
const providerUrl = process.env.KREDITS_PROVIDER_URL;
|
||||
const daoAddress = process.env.KREDITS_DAO_ADDRESS;
|
||||
const providerUrl = process.env.KREDITS_PROVIDER_URL || 'http://localhost:7545';
|
||||
|
||||
const ipfsConfig = {
|
||||
host: process.env.IPFS_API_HOST || 'localhost',
|
||||
@@ -37,22 +35,16 @@ module.exports = async function(robot) {
|
||||
// Ethereum provider/node setup
|
||||
//
|
||||
|
||||
let ethProvider;
|
||||
if (providerUrl) {
|
||||
ethProvider = new ethers.providers.JsonRpcProvider(providerUrl);
|
||||
} else {
|
||||
ethProvider = new ethers.getDefaultProvider('rinkeby');
|
||||
}
|
||||
const signer = wallet.connect(ethProvider);
|
||||
robot.logger.info('[hubot-kredits] Using blockchain node/API at', providerUrl);
|
||||
|
||||
const ethProvider = new ethers.providers.JsonRpcProvider(providerUrl);
|
||||
const signer = new NonceManager(wallet.connect(ethProvider));
|
||||
|
||||
//
|
||||
// Kredits contracts setup
|
||||
//
|
||||
|
||||
const opts = { ipfsConfig };
|
||||
if (daoAddress) {
|
||||
opts.addresses = { Kernel: daoAddress };
|
||||
}
|
||||
let kredits;
|
||||
|
||||
try {
|
||||
@@ -62,8 +54,8 @@ module.exports = async function(robot) {
|
||||
process.exit(1);
|
||||
}
|
||||
const Contributor = kredits.Contributor;
|
||||
const Proposal = kredits.Proposal;
|
||||
const Contribution = kredits.Contribution;
|
||||
// TODO const Reimbursement = kredits.Reimbursement;
|
||||
|
||||
robot.logger.info('[hubot-kredits] Wallet address: ' + wallet.address);
|
||||
|
||||
@@ -72,9 +64,9 @@ module.exports = async function(robot) {
|
||||
//
|
||||
|
||||
ethProvider.getBalance(wallet.address).then(balance => {
|
||||
robot.logger.info('[hubot-kredits] Wallet balance: ' + ethers.utils.formatEther(balance) + 'ETH');
|
||||
robot.logger.info('[hubot-kredits] Wallet balance: ' + ethers.utils.formatEther(balance) + ' RBTC');
|
||||
if (balance.lt(ethers.utils.parseEther('0.0001'))) {
|
||||
messageRoom(`Yo gang, I\'m broke! Please drop me some ETH to ${wallet.address}. kthxbai.`);
|
||||
messageRoom(`Yo gang, I\'m broke! Please send some RBTC to ${wallet.address}. kthxbai.`);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -82,30 +74,9 @@ module.exports = async function(robot) {
|
||||
// Robot chat commands/interaction
|
||||
//
|
||||
|
||||
robot.respond(/got ETH\??/i, res => {
|
||||
robot.respond(/got RBTC\??/i, res => {
|
||||
ethProvider.getBalance(wallet.address).then((balance) => {
|
||||
res.send(`My wallet contains ${ethers.utils.formatEther(balance)} ETH`);
|
||||
});
|
||||
});
|
||||
|
||||
robot.respond(/propose (\d*)\s?\S*\s?to (\S+)(?:\sfor (.*))?$/i, res => {
|
||||
let [_, amount, githubUser, description] = res.match;
|
||||
let url = null;
|
||||
createProposal(githubUser, amount, description, url).then((result) => {
|
||||
messageRoom('Sounds good! Will be listed on https://kredits.kosmos.org in a bit...');
|
||||
});
|
||||
});
|
||||
|
||||
robot.respond(/list open proposals/i, res => {
|
||||
Proposal.all().then((proposals) => {
|
||||
proposals.forEach((proposal) => {
|
||||
if (!proposal.executed) {
|
||||
Contributor.getById(proposal.contributorId).then((contributor) => {
|
||||
messageRoom(`* ${proposal.amount} kredits to ${contributor.name} for ${proposal.description}`);
|
||||
});
|
||||
}
|
||||
});
|
||||
messageRoom('https://kredits.kosmos.org');
|
||||
res.send(`My wallet contains ${ethers.utils.formatEther(balance)} RBTC`);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -114,31 +85,23 @@ module.exports = async function(robot) {
|
||||
//
|
||||
|
||||
function watchContractEvents() {
|
||||
ethProvider.getBlockNumber().then((blockNumber) => {
|
||||
ethProvider.getBlockNumber().then(blockNumber => {
|
||||
// current block is the last mined one, thus we check from the next
|
||||
// mined one onwards to prevent getting previous events
|
||||
let nextBlock = blockNumber + 1;
|
||||
robot.logger.debug(`[hubot-kredits] Watching events from block ${nextBlock} onward`);
|
||||
ethProvider.resetEventsBlock(nextBlock);
|
||||
|
||||
Proposal.on('ProposalCreated', handleProposalCreated);
|
||||
// TODO handle all known events (that make sense here)
|
||||
// Contribution.on('ContributorAdded', handleContributorAdded);
|
||||
Contribution.on('ContributionAdded', handleContributionAdded);
|
||||
});
|
||||
}
|
||||
|
||||
function handleProposalCreated(proposalId, creatorAccount, contributorId, amount) {
|
||||
Contributor.getById(contributorId).then((contributor) => {
|
||||
Proposal.getById(proposalId).then((proposal) => {
|
||||
robot.logger.debug(`[hubot-kredits] Proposal created (${proposal.description})`);
|
||||
// messageRoom(`Let's give ${contributor.name} some kredits for ${proposal.url} (${proposal.description}): https://kredits.kosmos.org`);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function handleContributionAdded(contributionId, contributorId, amount) {
|
||||
Contributor.getById(contributorId).then((contributor) => {
|
||||
Contribution.getById(contributionId).then((contribution) => {
|
||||
robot.logger.debug(`[hubot-kredits] Contribution #${contribution.id} added (${contribution.description})`);
|
||||
Contributor.getById(contributorId).then(_ => {
|
||||
Contribution.getById(contributionId).then(contribution => {
|
||||
robot.logger.debug(`[hubot-kredits] Contribution #${contribution.id} added (${amount} kredits for "${contribution.description}")`);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -152,6 +115,10 @@ module.exports = async function(robot) {
|
||||
require('./integrations/github')(robot, kredits);
|
||||
require('./integrations/gitea')(robot, kredits);
|
||||
|
||||
if (typeof process.env.KREDITS_ZOOM_JWT !== 'undefined') {
|
||||
require('./integrations/zoom')(robot, kredits);
|
||||
}
|
||||
|
||||
if (typeof process.env.KREDITS_MEDIAWIKI_URL !== 'undefined') {
|
||||
require('./integrations/mediawiki')(robot, kredits);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
const util = require('util');
|
||||
const fetch = require('node-fetch');
|
||||
const amountFromLabels = require('./utils/amount-from-labels');
|
||||
const kindFromLabels = require('./utils/kind-from-labels');
|
||||
|
||||
@@ -56,7 +55,7 @@ module.exports = async function(robot, kredits) {
|
||||
robot.logger.debug(`[hubot-kredits] contribution attributes:`);
|
||||
robot.logger.debug(util.inspect(contributionAttr, { depth: 1, colors: true }));
|
||||
|
||||
return Contribution.addContribution(contributionAttr).catch(error => {
|
||||
return Contribution.add(contributionAttr).catch(error => {
|
||||
robot.logger.error(`[hubot-kredits] Error:`, error);
|
||||
messageRoom(`I tried to add a contribution for ${giteaUser} for ${url}, but I encountered an error when submitting the tx:`);
|
||||
messageRoom(error.message);
|
||||
|
||||
@@ -61,7 +61,7 @@ module.exports = async function(robot, kredits) {
|
||||
robot.logger.debug(`[hubot-kredits] contribution attributes:`);
|
||||
robot.logger.debug(util.inspect(contributionAttr, { depth: 1, colors: true }));
|
||||
|
||||
return Contribution.addContribution(contributionAttr).catch(error => {
|
||||
return Contribution.add(contributionAttr).catch(error => {
|
||||
robot.logger.error(`[hubot-kredits] Error:`, error);
|
||||
messageRoom(`I tried to add a contribution for ${githubUser} for ${url}, but I encountered an error when submitting the tx:`);
|
||||
messageRoom(error.message);
|
||||
|
||||
@@ -48,7 +48,7 @@ module.exports = async function(robot, kredits) {
|
||||
kind: 'docs'
|
||||
};
|
||||
|
||||
return Contribution.addContribution(contribution).catch(error => {
|
||||
return Contribution.add(contribution).catch(error => {
|
||||
robot.logger.error(`[hubot-kredits] Adding contribution failed:`, error);
|
||||
});
|
||||
}).catch(() => {
|
||||
@@ -126,6 +126,7 @@ module.exports = async function(robot, kredits) {
|
||||
return [...new Set(changes.map(c => `"${c.title}"`))].join(', ');
|
||||
}
|
||||
|
||||
// Currently not used
|
||||
function calculateAmountForChanges(details) {
|
||||
let amount;
|
||||
|
||||
@@ -144,9 +145,8 @@ module.exports = async function(robot, kredits) {
|
||||
const dateNow = new Date();
|
||||
const dateYesterday = dateNow.setDate(dateNow.getDate() - 1);
|
||||
const date = (new Date(dateYesterday)).toISOString().split('T')[0];
|
||||
|
||||
const details = analyzeUserChanges(user, changes);
|
||||
const amount = calculateAmountForChanges(details);
|
||||
const amount = 500;
|
||||
|
||||
let desc = `Added ${details.charsAdded} characters of text.`;
|
||||
if (details.pagesChanged.length > 0) {
|
||||
|
||||
102
integrations/zoom.js
Normal file
102
integrations/zoom.js
Normal file
@@ -0,0 +1,102 @@
|
||||
const fetch = require('node-fetch');
|
||||
|
||||
function sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
module.exports = async function(robot, kredits) {
|
||||
|
||||
function messageRoom(message) {
|
||||
robot.messageRoom(process.env.KREDITS_ROOM, message);
|
||||
}
|
||||
|
||||
const { Contributor, Contribution } = kredits;
|
||||
|
||||
const kreditsContributionAmount = process.env.KREDITS_ZOOM_CONTRIBUTION_AMOUNT || 500;
|
||||
const kreditsContributionKind = 'community';
|
||||
|
||||
const zoomAccessToken = process.env.KREDITS_ZOOM_JWT;
|
||||
|
||||
async function createContributionFor (displayName, meeting) {
|
||||
const contributor = await getContributorByZoomDisplayName(displayName);
|
||||
|
||||
if (!contributor) {
|
||||
robot.logger.info(`[hubot-kredits] Contributor not found: Zoom display name: ${displayName}`);
|
||||
messageRoom(`I tried to add a contribution for zoom user ${displayName}, but did not find a matching contributor profile.`);
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
const contribution = {
|
||||
contributorId: contributor.id,
|
||||
contributorIpfsHash: contributor.ipfsHash,
|
||||
amount: kreditsContributionAmount,
|
||||
kind: kreditsContributionKind,
|
||||
description: 'Team/Community Call',
|
||||
date: meeting.end_time.split('T')[0],
|
||||
time: meeting.end_time.split('T')[1]
|
||||
}
|
||||
|
||||
return Contribution.add(contribution)
|
||||
.then(tx => {
|
||||
robot.logger.info(`[hubot-kredits] Contribution created: ${tx.hash}`);
|
||||
})
|
||||
.catch(error => {
|
||||
robot.logger.error(`[hubot-kredits] Adding contribution for Zoom call failed:`, error);
|
||||
});
|
||||
}
|
||||
|
||||
function getContributorByZoomDisplayName(displayName) {
|
||||
return Contributor.findByAccount({ site: 'zoom.us', username: displayName });
|
||||
}
|
||||
|
||||
function request(path) {
|
||||
return fetch(
|
||||
`https://api.zoom.us/v2${path}`,
|
||||
{headers: {authorization: `Bearer ${zoomAccessToken}`}}
|
||||
);
|
||||
}
|
||||
|
||||
function getMeetingParticipants(meetingUUID) {
|
||||
return request(`/past_meetings/${meetingUUID}/participants`)
|
||||
.then(response => response.json())
|
||||
.then(json => json.participants)
|
||||
}
|
||||
|
||||
function getMeetingDetails(meetingUUID) {
|
||||
return request(`/past_meetings/${meetingUUID}`)
|
||||
.then(r => r.json());
|
||||
}
|
||||
|
||||
async function handleZoomMeetingEnded(data) {
|
||||
const meetingDetails = await getMeetingDetails(data.uuid);
|
||||
const participants = await getMeetingParticipants(data.uuid);
|
||||
const names = Array.from(new Set(participants.map(p => p.name)));
|
||||
|
||||
if (meetingDetails.duration < 15 || names.length < 3) {
|
||||
robot.logger.info(`[hubot-kredits] Ignoring zoom call ${data.uuid} (duration: ${meetingDetails.duration}, participants_count: ${meetingDetails.participants_count})`);
|
||||
return;
|
||||
}
|
||||
|
||||
for (const displayName of names) {
|
||||
await createContributionFor(displayName, meetingDetails);
|
||||
await sleep(60000); // potentially to prevent too many transactions at the sametime. transactions need to be ordered because of the nonce. not sure though if this is needed.
|
||||
};
|
||||
}
|
||||
|
||||
robot.router.post('/incoming/kredits/zoom/'+process.env.KREDITS_WEBHOOK_TOKEN, (req, res) => {
|
||||
let data = req.body;
|
||||
const eventName = data.event;
|
||||
const payload = data.payload;
|
||||
const object = payload.object;
|
||||
|
||||
|
||||
if (eventName === 'meeting.ended' && (
|
||||
!process.env.KREDITS_ZOOM_MEETING_WHITELIST ||
|
||||
process.env.KREDITS_ZOOM_MEETING_WHITELIST.split(',').includes(object.id)
|
||||
)) {
|
||||
handleZoomMeetingEnded(object);
|
||||
}
|
||||
|
||||
res.sendStatus(200);
|
||||
})
|
||||
}
|
||||
6467
package-lock.json
generated
6467
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
17
package.json
17
package.json
@@ -1,27 +1,31 @@
|
||||
{
|
||||
"name": "hubot-kredits",
|
||||
"version": "3.4.1",
|
||||
"version": "4.0.0",
|
||||
"description": "Kosmos Kredits functionality for chat bots",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"bin": {
|
||||
"create-wallet": "scripts/create-wallet.js"
|
||||
"create-wallet": "scripts/create-wallet.js",
|
||||
"review-kredits": "scripts/review-kredits.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@ethersproject/experimental": "5.0.0",
|
||||
"@kredits/contracts": "7.0.0",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^8.2.0",
|
||||
"eth-provider": "^0.2.2",
|
||||
"ethers": "^4.0.27",
|
||||
"ethers": "^5.0.5",
|
||||
"express": "^4.17.1",
|
||||
"express-session": "^1.16.2",
|
||||
"grant-express": "^4.6.1",
|
||||
"group-array": "^1.0.0",
|
||||
"kosmos-schemas": "^1.1.2",
|
||||
"kredits-contracts": "^5.4.0",
|
||||
"node-cron": "^2.0.3",
|
||||
"node-fetch": "^2.3.0",
|
||||
"prompt": "^1.0.0"
|
||||
"prompt": "^1.0.0",
|
||||
"yargs": "^16.2.0"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -35,7 +39,8 @@
|
||||
"author": "Kosmos Developers <mail@kosmos.org>",
|
||||
"contributors": [
|
||||
"Sebastian Kippe <sebastian@kip.pe>",
|
||||
"Michael Bumann <hello@michaelbumann.com>"
|
||||
"Michael Bumann <hello@michaelbumann.com>",
|
||||
"Garret Alfert <alfert@wevelop.de>"
|
||||
],
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
|
||||
25
repos.json
Normal file
25
repos.json
Normal file
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"github": [
|
||||
"67P/botka",
|
||||
"67P/hal8000",
|
||||
"67P/hubot-kredits",
|
||||
"67P/hubot-remotestorage-logger",
|
||||
"67P/hyperchannel",
|
||||
"67P/kosmos-schemas",
|
||||
"67P/kredits-contracts",
|
||||
"67P/kredits-signup",
|
||||
"67P/kredits-web",
|
||||
"67P/remotestorage-module-kosmos",
|
||||
"67P/waves",
|
||||
"sockethub/sockethub"
|
||||
],
|
||||
"gitea": [
|
||||
"kosmos/akkounts",
|
||||
"kosmos/chef",
|
||||
"kosmos/gitea.kosmos.org",
|
||||
"kosmos/ipfs-cookbook",
|
||||
"kosmos/kredits-ipfs-pinner",
|
||||
"kosmos/website",
|
||||
"kosmos/wormhole"
|
||||
]
|
||||
}
|
||||
97
scripts/lib/gitea-reviews.js
Normal file
97
scripts/lib/gitea-reviews.js
Normal file
@@ -0,0 +1,97 @@
|
||||
const fetch = require('node-fetch');
|
||||
|
||||
module.exports = class GiteaReviews {
|
||||
|
||||
token = null;
|
||||
kreditsAmounts = null;
|
||||
pageLimit = 100;
|
||||
|
||||
constructor (token, kreditsAmounts) {
|
||||
this.token = token;
|
||||
this.kreditsAmounts = kreditsAmounts;
|
||||
}
|
||||
|
||||
async request (path) {
|
||||
return fetch(
|
||||
`https://gitea.kosmos.org/api/v1${path}`,
|
||||
{
|
||||
headers: {
|
||||
'accepts': 'application/json',
|
||||
'Authorization': `token ${this.token}`
|
||||
}
|
||||
}
|
||||
).then(response => response.json());
|
||||
}
|
||||
|
||||
async getReviewContributions (repos, startDate, endDate) {
|
||||
let reviewContributions = {}
|
||||
|
||||
await Promise.all(repos.map(async (repo) => {
|
||||
let page = 1;
|
||||
let result;
|
||||
|
||||
do {
|
||||
try {
|
||||
result = await this.request(`/repos/${repo}/pulls?state=closed&limit=${this.pageLimit}&page=${page}`);
|
||||
} catch(error) {
|
||||
console.log(`failed to fetch PRs for repo ${repo}:`, error.message);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!result || result.length === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let pullRequests = result.filter(pr => {
|
||||
if (!pr.merged) return false; // only interested in merged PRs
|
||||
|
||||
// check if the PR has been merged in the given timeframe
|
||||
const mergeDate = new Date(pr.merged_at);
|
||||
if (mergeDate < startDate || mergeDate > endDate) return false;
|
||||
|
||||
// check if the PR has a kredits label
|
||||
return pr.labels.some(label => label.name.match(/kredits-[123]/));
|
||||
});
|
||||
|
||||
await Promise.all(pullRequests.map(async (pr) => {
|
||||
let reviews;
|
||||
try {
|
||||
reviews = await this.request(`/repos/${repo}/pulls/${pr.number}/reviews`);
|
||||
} catch(error) {
|
||||
console.log(`failed to fetch reviews for repo ${repo}, PR ${pr.number}:`, error.message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!reviews || reviews.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
reviews = reviews.filter(review => {
|
||||
return ['APPROVED', 'REJECTED'].includes(review.state);
|
||||
});
|
||||
|
||||
reviews.forEach(review => {
|
||||
if (typeof reviewContributions[review.user.login] === 'undefined') {
|
||||
reviewContributions[review.user.login] = [];
|
||||
}
|
||||
|
||||
let kreditsLabel = pr.labels.find(label => label.name.match(/kredits-[123]/));
|
||||
|
||||
reviewContributions[review.user.login].push({
|
||||
pr,
|
||||
prNumber: pr.number,
|
||||
review,
|
||||
reviewState: review.state,
|
||||
kredits: this.kreditsAmounts[kreditsLabel.name]
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
page++;
|
||||
} while (result && result.length > 0);
|
||||
}));
|
||||
|
||||
return reviewContributions;
|
||||
}
|
||||
|
||||
}
|
||||
98
scripts/lib/github-reviews.js
Normal file
98
scripts/lib/github-reviews.js
Normal file
@@ -0,0 +1,98 @@
|
||||
const fetch = require('node-fetch');
|
||||
|
||||
module.exports = class GithubReviews {
|
||||
|
||||
token = null;
|
||||
kreditsAmounts = null;
|
||||
pageLimit = 100;
|
||||
|
||||
constructor (token, kreditsAmounts) {
|
||||
this.token = token;
|
||||
this.kreditsAmounts = kreditsAmounts;
|
||||
}
|
||||
|
||||
async request (path) {
|
||||
return fetch(
|
||||
`https://api.github.com${path}`,
|
||||
{
|
||||
headers: {
|
||||
'Accept': 'application/vnd.github.v3+json',
|
||||
'User-Agent': 'Kosmos Kredits for reviews',
|
||||
'Authorization': `token ${this.token}`
|
||||
}
|
||||
}
|
||||
).then(response => response.json());
|
||||
}
|
||||
|
||||
async getReviewContributions (repos, startDate, endDate) {
|
||||
let reviewContributions = {}
|
||||
|
||||
await Promise.all(repos.map(async (repo) => {
|
||||
let page = 1;
|
||||
let result;
|
||||
|
||||
do {
|
||||
try {
|
||||
result = await this.request(`/repos/${repo}/pulls?state=closed&perPage=${this.pageLimit}&page=${page}`);
|
||||
} catch(error) {
|
||||
console.log(`failed to fetch PRs for repo ${repo}:`, error.message);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!result || result.length === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let pullRequests = result.filter(pr => {
|
||||
if (!pr.merged_at) return false; // only interested in merged PRs
|
||||
|
||||
// check if the PR has been merged in the given timeframe
|
||||
const mergeDate = new Date(pr.merged_at);
|
||||
if (mergeDate < startDate || mergeDate > endDate) return false;
|
||||
|
||||
// check if the PR has a kredits label
|
||||
return pr.labels.some(label => label.name.match(/kredits-[123]/));
|
||||
});
|
||||
|
||||
await Promise.all(pullRequests.map(async (pr) => {
|
||||
let reviews;
|
||||
try {
|
||||
reviews = await this.request(`/repos/${repo}/pulls/${pr.number}/reviews`);
|
||||
} catch(error) {
|
||||
console.log(`failed to fetch reviews for repo ${repo}, PR ${pr.number}:`, error.message);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!reviews || reviews.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
reviews = reviews.filter(review => {
|
||||
return ['APPROVED', 'REJECTED'].includes(review.state);
|
||||
});
|
||||
|
||||
reviews.forEach(review => {
|
||||
if (typeof reviewContributions[review.user.login] === 'undefined') {
|
||||
reviewContributions[review.user.login] = [];
|
||||
}
|
||||
|
||||
let kreditsLabel = pr.labels.find(label => label.name.match(/kredits-[123]/));
|
||||
|
||||
reviewContributions[review.user.login].push({
|
||||
pr,
|
||||
prNumber: pr.number,
|
||||
review,
|
||||
reviewState: review.state,
|
||||
kredits: this.kreditsAmounts[kreditsLabel.name]
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
page++;
|
||||
} while (result && result.length > 0);
|
||||
}));
|
||||
|
||||
return reviewContributions;
|
||||
}
|
||||
|
||||
}
|
||||
189
scripts/review-kredits.js
Executable file
189
scripts/review-kredits.js
Executable file
@@ -0,0 +1,189 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
require('dotenv').config({ path: '../.env' });
|
||||
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 yargs = require('yargs/yargs')
|
||||
const { hideBin } = require('yargs/helpers')
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
const repos = require('../repos.json');
|
||||
|
||||
const argv = yargs(hideBin(process.argv))
|
||||
.option('start', {
|
||||
alias: 's',
|
||||
description: 'Include reviews for PRs merged after this date'
|
||||
})
|
||||
.option('end', {
|
||||
alias: 'e',
|
||||
description: 'Include reviews for PRs merged before this date'
|
||||
})
|
||||
.option('dry', {
|
||||
alias: 'd',
|
||||
type: 'boolean',
|
||||
description: 'Only list contribution details without creating them'
|
||||
})
|
||||
.help()
|
||||
.version()
|
||||
.demandOption('start', 'Please provide a start date')
|
||||
.default('end', function now () {
|
||||
return (new Date()).toISOString().split('.')[0]+"Z";
|
||||
})
|
||||
.example([
|
||||
['$0 --start 2020-11-01 --end 2020-11-30T23:59:59Z', 'Create contributions for reviews of pull requests merged in November 2020'],
|
||||
['$0 --start 2021-01-01', 'Create contributions for reviews of pull requests merged from Januar 2021 until now'],
|
||||
])
|
||||
.argv
|
||||
|
||||
const startTimestamp = Date.parse(argv.start);
|
||||
const endTimestamp = Date.parse(argv.end);
|
||||
|
||||
if (isNaN(startTimestamp)) {
|
||||
console.log('The provided start date is invalid');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (isNaN(endTimestamp)) {
|
||||
console.log('The provided end date is invalid');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// check for existence of GITHUB_TOKEN and GITEA_TOKEN
|
||||
if (!process.env.GITHUB_TOKEN || !process.env.GITEA_TOKEN) {
|
||||
console.log('Please set both GITHUB_TOKEN and GITEA_TOKEN');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const startDate = new Date(startTimestamp);
|
||||
const endDate = new Date(endTimestamp);
|
||||
|
||||
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 => {
|
||||
// console.log(util.inspect(reviews[0], { depth: 3, colors: true }));
|
||||
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, startDate, endDate) {
|
||||
const dateFormatOptions = { year: 'numeric', month: 'long', day: 'numeric' };
|
||||
const contributors = await Contributor.all();
|
||||
const contributionData = {};
|
||||
const now = (new Date()).toISOString().split('.')[0]+"Z";
|
||||
[date, time] = now.split('T');
|
||||
|
||||
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 ${platform} user "${username}"`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const urls = platformReviews.map(review => review.pr.html_url);
|
||||
const kreditsAmount = platformReviews.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 {
|
||||
const formattedStartDate = startDate.toLocaleString('en-us', dateFormatOptions);
|
||||
const formattedEndDate = endDate.toLocaleString('en-us', dateFormatOptions);
|
||||
|
||||
contributionData[contributor.name] = {
|
||||
contributorId: contributor.id,
|
||||
contributorIpfsHash: contributor.ipfsHash,
|
||||
date,
|
||||
time,
|
||||
amount: kreditsAmount,
|
||||
kind: 'dev',
|
||||
description: `PR reviews from ${formattedStartDate} to ${formattedEndDate}`,
|
||||
details: {
|
||||
'pullRequests': urls
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
addContributionDataForPlatform('gitea');
|
||||
addContributionDataForPlatform('github');
|
||||
|
||||
return contributionData;
|
||||
}
|
||||
|
||||
Promise.all([initializeKredits(), getAllReviews(repos, startDate, endDate)]).then((values) => {
|
||||
const kredits = values[0];
|
||||
const reviews = values[1];
|
||||
|
||||
generateContributionData(reviews, kredits.Contributor, startDate, endDate).then(contributionData => {
|
||||
if (argv.dry) {
|
||||
console.log('contributions:');
|
||||
console.log(util.inspect(contributionData, { depth: 3, colors: true }));
|
||||
}
|
||||
|
||||
// TODO create contributions
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user