Compare commits
109 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cd8343516f | |||
|
|
41c5903cf5 | ||
| 9a4dc43aa5 | |||
| 67fd9ff031 | |||
| 800950ebc5 | |||
| 98ccae96f0 | |||
| e6730802f6 | |||
| d2976b312e | |||
| d2feee8b14 | |||
| a5acf466a4 | |||
|
|
f1bb5b391d | ||
| 840fcf9d25 | |||
| c390fc9ce7 | |||
| 23033b3813 | |||
| fb1b10d2a3 | |||
| d3b97d9129 | |||
| d2f487b379 | |||
| ca9732c333 | |||
| c03da5659f | |||
| 5a9edda1cd | |||
| 2a3077d733 | |||
| 1255d7ce19 | |||
| d9e09ce041 | |||
| a0f982432e | |||
| 4550a911f0 | |||
| d1580560b5 | |||
| 92c50ac69e | |||
| fca991c685 | |||
| 626712356a | |||
| 65e34ee4e1 | |||
| 63057191d6 | |||
| 768c24da8e | |||
| 0fdd7d0f1c | |||
| 1e0d637c26 | |||
| dcbce66796 | |||
| 16b62a2545 | |||
| f39bd41098 | |||
| 8cfbc222fc | |||
| 21aaf2f3bf | |||
| 7056772066 | |||
| d854b4e0fb | |||
| 321fe95a27 | |||
| b38e6faf8a | |||
| bfaf26cc9a | |||
| 3ac2774c4f | |||
| 83990a36dc | |||
| c427d7c313 | |||
| 8b1977b53e | |||
| 189f7fe4ee | |||
| a95bf0141e | |||
| b4f6fb7ef7 | |||
| 98f4516217 | |||
| 149bc0c052 | |||
| c190490d9b | |||
| eb767a90f2 | |||
| 156ea44f0d | |||
| ec7f4a4d33 | |||
| 5f9e237f36 | |||
| 38db976c29 | |||
| 36e73beb3d | |||
| 8d067d221f | |||
| 35222456a4 | |||
| 7636e08e8f | |||
| b4229c7879 | |||
| 201c982466 | |||
| bba5407c7d | |||
| 417eb42905 | |||
| b852bd24af | |||
| 581b15da69 | |||
| c97cc82817 | |||
| d4a3a9c9df | |||
| 6c3070b43b | |||
| 9d8b2c08dc | |||
| 2780c87aaa | |||
| 7904aa8c84 | |||
| 3d810287c0 | |||
| 22b480ae92 | |||
| 48a42d4f2c | |||
| aab5b58bab | |||
| c0c2f97dae | |||
| 74429feb9d | |||
| ed5d127b4c | |||
| e8da47db70 | |||
| 462efcefbd | |||
| 5259b56e53 | |||
| 33cefac88e | |||
| 5a2ebb323d | |||
| eecfa7e8f2 | |||
| 5d76b2dead | |||
| 22cb49df2d | |||
| 8c481179b4 | |||
| 2be3d4e8e3 | |||
| f4b484ed5a | |||
| e4fb97c0c9 | |||
| 3fe6dfb2ce | |||
| 9dc1ece94f | |||
| e99addf37e | |||
| 050e8ce79f | |||
| 821543056b | |||
| ce87aaa3fd | |||
| fa86d0a3c0 | |||
| 94baf62c51 | |||
| 0e6898d93b | |||
| a5886bc122 | |||
| 582f6b7f5a | |||
| 9f078a0d84 | |||
| b1ea520fdf | |||
| 6d98c2cf19 | |||
| 7d8c6032c9 |
82
README.md
Normal file
82
README.md
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
[](https://www.npmjs.com/package/hubot-kredits)
|
||||||
|
|
||||||
|
# Hubot Kredits
|
||||||
|
|
||||||
|
This repository provides scripts for integrating [Kosmos
|
||||||
|
Kredits](https://wiki.kosmos.org/Kredits) in [Hubot](http://hubot.github.com/)
|
||||||
|
chatbots. The bot will watch for project-related things happening on the
|
||||||
|
Internet and automatically create ERC721 tokens for issuing kredits for project
|
||||||
|
contributions.
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
As usual in Hubot, you can add all config as environment variables.
|
||||||
|
|
||||||
|
| Key | Description |
|
||||||
|
| --- | --- |
|
||||||
|
| `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_PASSWORD` | Wallet password |
|
||||||
|
| `KREDITS_PROVIDER_URL` | Ethereum JSON-RPC URL (default: `http://localhost:7545`) |
|
||||||
|
|
||||||
|
## Integrations
|
||||||
|
|
||||||
|
### GitHub
|
||||||
|
|
||||||
|
The GitHub integration will watch for closed issues and merged pull requests,
|
||||||
|
which carry a kredits label: `kredits-1`, `kredits-2`, `kredits-3` for small,
|
||||||
|
medium and large contributions. If there are multiple people assigned, it will
|
||||||
|
issue contribution tokens for all of them.
|
||||||
|
|
||||||
|
#### Setup
|
||||||
|
|
||||||
|
Point a GitHub organization webhook to the following URL:
|
||||||
|
|
||||||
|
https://your-hubot.example.com/incoming/kredits/github/{webhook_token}
|
||||||
|
|
||||||
|
#### Config
|
||||||
|
|
||||||
|
| Key | Description |
|
||||||
|
| --- | --- |
|
||||||
|
| `KREDITS_GITHUB_REPO_BLACKLIST` | Repos which you do not want to issue kredits for. Format: `orgname/reponame`, e.g. `67P/test-one-two` |
|
||||||
|
|
||||||
|
### Gitea
|
||||||
|
|
||||||
|
The Gitea integration will watch for closed issues and merged pull requests,
|
||||||
|
which carry a kredits label: `kredits-1`, `kredits-2`, `kredits-3` for small,
|
||||||
|
medium and large contributions. If there are multiple people assigned, it will
|
||||||
|
issue contribution tokens for all of them.
|
||||||
|
|
||||||
|
#### Setup
|
||||||
|
|
||||||
|
Point a Gitea organization webhook to the following URL:
|
||||||
|
|
||||||
|
https://your-hubot.example.com/incoming/kredits/gitea/{webhook_token}
|
||||||
|
|
||||||
|
#### Config
|
||||||
|
|
||||||
|
| Key | Description |
|
||||||
|
| --- | --- |
|
||||||
|
| `KREDITS_GITEA_REPO_BLACKLIST` | Repos which you do not want to issue kredits for. Format: `orgname/reponame`, e.g. `kosmos/test-one-two` |
|
||||||
|
|
||||||
|
### MediaWiki
|
||||||
|
|
||||||
|
The MediaWiki integration will periodically check for wiki page creations and
|
||||||
|
edits. It will create kredits contribution tokens based on amount of text added.
|
||||||
|
|
||||||
|
#### Setup
|
||||||
|
|
||||||
|
No setup needed, except for configuring the wiki URL. The bot will poll your
|
||||||
|
wiki's API on its own.
|
||||||
|
|
||||||
|
#### Config
|
||||||
|
|
||||||
|
| Key | Description |
|
||||||
|
| --- | --- |
|
||||||
|
| `KREDITS_MEDIAWIKI_URL` | Your wiki URL, e.g. `https://wiki.kosmos.org/` |
|
||||||
368
index.js
368
index.js
@@ -1,266 +1,158 @@
|
|||||||
// Description:
|
|
||||||
// Kosmos Kredits chat integration
|
|
||||||
//
|
|
||||||
// Configuration:
|
|
||||||
// KREDITS_WEBHOOK_TOKEN: A string for building your secret webhook URL
|
|
||||||
// KREDITS_ROOM: Kredit proposals are posted to this chatroom
|
|
||||||
// KREDITS_WALLET_PATH: Path to a etherum wallet JSON file
|
|
||||||
// KREDITS_WALLET_PASSWORD: Wallet password
|
|
||||||
//
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const util = require('util');
|
||||||
const fetch = require('node-fetch');
|
const fetch = require('node-fetch');
|
||||||
const kreditsContracts = require('kredits-contracts');
|
const ethers = require('ethers');
|
||||||
const ProviderEngine = require('web3-provider-engine');
|
const Kredits = require('kredits-contracts');
|
||||||
const Wallet = require('ethereumjs-wallet');
|
|
||||||
const WalletSubprovider = require('ethereumjs-wallet/provider-engine');
|
|
||||||
const Web3Subprovider = require('web3-provider-engine/subproviders/web3.js');
|
|
||||||
const Web3 = require('web3');
|
|
||||||
|
|
||||||
(function() {
|
const walletPath = process.env.KREDITS_WALLET_PATH || './wallet.json';
|
||||||
"use strict";
|
const walletJson = fs.readFileSync(walletPath);
|
||||||
|
const providerUrl = process.env.KREDITS_PROVIDER_URL;
|
||||||
|
|
||||||
let engine = new ProviderEngine();
|
const ipfsConfig = {
|
||||||
|
host: process.env.IPFS_API_HOST || 'localhost',
|
||||||
|
port: process.env.IPFS_API_PORT || '5001',
|
||||||
|
protocol: process.env.IPFS_API_PROTOCOL || 'http'
|
||||||
|
};
|
||||||
|
|
||||||
let walletPath = process.env.KREDITS_WALLET_PATH || './wallet.json';
|
module.exports = async function(robot) {
|
||||||
let walletJson = fs.readFileSync(walletPath);
|
|
||||||
let wallet = Wallet.fromV3(JSON.parse(walletJson), process.env.KREDITS_WALLET_PASSWORD);
|
|
||||||
let providerUrl = process.env.KREDITS_PROVIDER_URL || 'http://localhost:8545';
|
|
||||||
let hubotWalletAddress = '0x' + wallet.getAddress().toString('hex');
|
|
||||||
|
|
||||||
let config = {};
|
function messageRoom(message) {
|
||||||
if (process.env.KREDITS_CONTRACT_ADDRESS) {
|
robot.messageRoom(process.env.KREDITS_ROOM, message);
|
||||||
config = { Kredits: { address: process.env.KREDITS_CONTRACT_ADDRESS }};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
engine.addProvider(new WalletSubprovider(wallet, {}));
|
//
|
||||||
engine.addProvider(new Web3Subprovider(new Web3.providers.HttpProvider(providerUrl)));
|
// Ethereum wallet setup
|
||||||
// TODO only start engine if providerURL is accessible
|
//
|
||||||
engine.start();
|
|
||||||
|
|
||||||
let web3 = new Web3(engine);
|
let wallet;
|
||||||
web3.eth.defaultAccount = hubotWalletAddress;
|
try {
|
||||||
|
wallet = await ethers.Wallet.fromEncryptedJson(walletJson, process.env.KREDITS_WALLET_PASSWORD);
|
||||||
|
} catch(error) {
|
||||||
|
robot.logger.warning('[hubot-kredits] Could not load wallet:', error);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
let contracts = kreditsContracts(web3, config);
|
//
|
||||||
let kredits = contracts['Kredits'];
|
// Ethereum provider/node setup
|
||||||
|
//
|
||||||
|
|
||||||
module.exports = function(robot) {
|
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] Wallet address: ' + hubotWalletAddress);
|
//
|
||||||
|
// Kredits contracts setup
|
||||||
|
//
|
||||||
|
|
||||||
getBalance().then(balance => {
|
let kredits;
|
||||||
if (balance <= 0) {
|
try {
|
||||||
messageRoom(`Yo gang, I\'m broke! Please drop me some ETH to ${hubotWalletAddress}. kthxbai.`);
|
kredits = await new Kredits(signer.provider, signer, {
|
||||||
}
|
// TODO support local devchain custom address
|
||||||
|
apm: 'open.aragonpm.eth',
|
||||||
|
// addresses: { Kernel: '0x93aa4531329e4bf3efcd1ec0b74adb6f66d9d10e' }
|
||||||
|
ipfsConfig
|
||||||
|
}).init();
|
||||||
|
} catch(error) {
|
||||||
|
robot.logger.warning('[hubot-kredits] Could not set up kredits:', error);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
const Contributor = kredits.Contributor;
|
||||||
|
const Proposal = kredits.Proposal;
|
||||||
|
const Contribution = kredits.Contribution;
|
||||||
|
|
||||||
|
robot.logger.info('[hubot-kredits] Wallet address: ' + wallet.address);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Check robot's wallet balance and alert when it's broke
|
||||||
|
//
|
||||||
|
|
||||||
|
ethProvider.getBalance(wallet.address).then(balance => {
|
||||||
|
robot.logger.info('[hubot-kredits] Wallet balance: ' + ethers.utils.formatEther(balance) + 'ETH');
|
||||||
|
if (balance.lt(ethers.utils.parseEther('0.0001'))) {
|
||||||
|
messageRoom(`Yo gang, I\'m broke! Please drop me some ETH to ${wallet.address}. kthxbai.`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
//
|
||||||
|
// Robot chat commands/interaction
|
||||||
|
//
|
||||||
|
|
||||||
|
robot.respond(/got ETH\??/i, res => {
|
||||||
|
ethProvider.getBalance(wallet.address).then((balance) => {
|
||||||
|
res.send(`My wallet contains ${ethers.utils.formatEther(balance)} ETH`);
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
function getBalance() {
|
robot.respond(/propose (\d*)\s?\S*\s?to (\S+)(?:\sfor (.*))?$/i, res => {
|
||||||
return new Promise((resolve, reject) => {
|
let [_, amount, githubUser, description] = res.match;
|
||||||
web3.eth.getBalance(hubotWalletAddress, function (err, balance) {
|
let url = null;
|
||||||
if (err) {
|
createProposal(githubUser, amount, description, url).then((result) => {
|
||||||
robot.logger.error('[hubot-kredits] Error checking balance');
|
messageRoom('Sounds good! Will be listed on https://kredits.kosmos.org in a bit...');
|
||||||
reject(err);
|
});
|
||||||
return;
|
});
|
||||||
}
|
|
||||||
resolve(balance);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getValueFromContract(contractMethod, ...args) {
|
robot.respond(/list open proposals/i, res => {
|
||||||
return new Promise((resolve, reject) => {
|
Proposal.all().then((proposals) => {
|
||||||
kredits[contractMethod](...args, (err, data) => {
|
proposals.forEach((proposal) => {
|
||||||
if (err) { reject(err); }
|
if (!proposal.executed) {
|
||||||
resolve(data);
|
Contributor.getById(proposal.contributorId).then((contributor) => {
|
||||||
});
|
messageRoom(`* ${proposal.amount} kredits to ${contributor.name} for ${proposal.description}`);
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getContributorData(i) {
|
|
||||||
let promise = new Promise((resolve, reject) => {
|
|
||||||
getValueFromContract('contributorAddresses', i).then(address => {
|
|
||||||
robot.logger.debug('address', address);
|
|
||||||
getValueFromContract('contributors', address).then(person => {
|
|
||||||
robot.logger.debug('person', person);
|
|
||||||
let contributor = {
|
|
||||||
address: address,
|
|
||||||
github_username: person[1],
|
|
||||||
github_uid: person[0],
|
|
||||||
ipfsHash: person[2]
|
|
||||||
};
|
|
||||||
robot.logger.debug('[kredits] contributor', contributor);
|
|
||||||
resolve(contributor);
|
|
||||||
});
|
});
|
||||||
}).catch(err => reject(err));
|
|
||||||
});
|
|
||||||
return promise;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getContributors() {
|
|
||||||
return getValueFromContract('contributorsCount').then(contributorsCount => {
|
|
||||||
let contributors = [];
|
|
||||||
|
|
||||||
for(var i = 0; i < contributorsCount.toNumber(); i++) {
|
|
||||||
contributors.push(getContributorData(i));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.all(contributors);
|
|
||||||
});
|
});
|
||||||
}
|
messageRoom('https://kredits.kosmos.org');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
function getContributorByGithubUser(username) {
|
//
|
||||||
let promise = new Promise((resolve, reject) => {
|
// Smart contract events
|
||||||
getContributors().then(contributors => {
|
//
|
||||||
let contrib = contributors.find(c => {
|
|
||||||
return c.github_username === username;
|
|
||||||
});
|
|
||||||
if (contrib) {
|
|
||||||
resolve(contrib);
|
|
||||||
} else {
|
|
||||||
reject();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
return promise;
|
|
||||||
}
|
|
||||||
|
|
||||||
function messageRoom(message) {
|
function watchContractEvents() {
|
||||||
robot.messageRoom(process.env.KREDITS_ROOM, message);
|
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);
|
||||||
|
|
||||||
function amountFromIssueLabels(issue) {
|
Proposal.on('ProposalCreated', handleProposalCreated);
|
||||||
let kreditsLabel = issue.labels.map(l => l.name)
|
Contribution.on('ContributionAdded', handleContributionAdded);
|
||||||
.filter(n => n.match(/^kredits/))[0];
|
});
|
||||||
// No label, no kredits
|
}
|
||||||
if (typeof kreditsLabel === 'undefined') { return 0; }
|
|
||||||
|
|
||||||
// TODO move to config maybe?
|
function handleProposalCreated(proposalId, creatorAccount, contributorId, amount) {
|
||||||
let amount;
|
Contributor.getById(contributorId).then((contributor) => {
|
||||||
switch(kreditsLabel) {
|
Proposal.getById(proposalId).then((proposal) => {
|
||||||
case 'kredits-1':
|
robot.logger.debug(`[hubot-kredits] Proposal created (${proposal.description})`);
|
||||||
amount = 50;
|
// messageRoom(`Let's give ${contributor.name} some kredits for ${proposal.url} (${proposal.description}): https://kredits.kosmos.org`);
|
||||||
break;
|
|
||||||
case 'kredits-2':
|
|
||||||
amount = 150;
|
|
||||||
break;
|
|
||||||
case 'kredits-3':
|
|
||||||
amount = 500;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return amount;
|
|
||||||
}
|
|
||||||
|
|
||||||
function createProposal(recipient, amount, url/*, metaData*/) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
// TODO write metaData to IPFS
|
|
||||||
robot.logger.debug(`Creating proposal to issue ${amount}₭S to ${recipient} for ${url}...`);
|
|
||||||
|
|
||||||
getContributorByGithubUser(recipient).then(c => {
|
|
||||||
kredits.addProposal(c.address, amount, url, '', (e/* , d */) => {
|
|
||||||
if (e) { reject(); return; }
|
|
||||||
messageRoom(`New proposal created: ${amount} for ${recipient}`);
|
|
||||||
});
|
|
||||||
}, () => {
|
|
||||||
messageRoom(`Couldn\'t find contributor data for ${recipient}. Please add them first!`);
|
|
||||||
});
|
|
||||||
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleGitHubIssueClosed(data) {
|
|
||||||
return new Promise((resolve/*, reject*/) => {
|
|
||||||
// fs.writeFileSync('tmp/github-issue.json', JSON.stringify(data, null, 4));
|
|
||||||
let recipients;
|
|
||||||
let issue = data.issue;
|
|
||||||
let assignees = issue.assignees.map(a => a.login);
|
|
||||||
let web_url = issue.html_url;
|
|
||||||
|
|
||||||
let amount = amountFromIssueLabels(issue);
|
|
||||||
if (amount === 0) { resolve(); return; }
|
|
||||||
|
|
||||||
if (assignees.length > 0) {
|
|
||||||
recipients = assignees;
|
|
||||||
} else {
|
|
||||||
recipients = [issue.user.login];
|
|
||||||
}
|
|
||||||
|
|
||||||
recipients.forEach(recipient => {
|
|
||||||
createProposal(recipient, amount, web_url, issue);
|
|
||||||
});
|
|
||||||
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleGitHubPullRequestClosed(data) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
// fs.writeFileSync('tmp/github-pr.json', JSON.stringify(data, null, 4));
|
|
||||||
let recipients;
|
|
||||||
let pull_request = data.pull_request;
|
|
||||||
let assignees = pull_request.assignees.map(a => a.login);
|
|
||||||
let web_url = pull_request._links.html.href;
|
|
||||||
let pr_issue_url = pull_request.issue_url;
|
|
||||||
|
|
||||||
if (assignees.length > 0) {
|
|
||||||
recipients = assignees;
|
|
||||||
} else {
|
|
||||||
recipients = [pull_request.user.login];
|
|
||||||
}
|
|
||||||
|
|
||||||
fetch(pr_issue_url)
|
|
||||||
.then(response => {
|
|
||||||
if (response.status >= 400) {
|
|
||||||
reject('Bad response from fetching PR issue');
|
|
||||||
}
|
|
||||||
return response.json();
|
|
||||||
})
|
|
||||||
.then(issue => {
|
|
||||||
// fs.writeFileSync('tmp/github-pr-issue.json', JSON.stringify(data, null, 4));
|
|
||||||
let amount = amountFromIssueLabels(issue);
|
|
||||||
if (amount === 0) { resolve(); return; }
|
|
||||||
|
|
||||||
recipients.forEach(recipient => {
|
|
||||||
createProposal(recipient, amount, web_url, pull_request);
|
|
||||||
});
|
|
||||||
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
robot.respond(/(got ETH)|(got gas)\?/i, res => {
|
|
||||||
getBalance().then(balance => {
|
|
||||||
if (balance <= 0) {
|
|
||||||
res.send(`HALP, I\'m totally broke! Not a single wei in my pocket.`);
|
|
||||||
}
|
|
||||||
else if (balance >= 1e+17) {
|
|
||||||
res.send(`my wallet contains ${web3.fromWei(balance, 'ether')} ETH`);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
res.send(`I\'m almost broke! Only have ${web3.fromWei(balance, 'ether')} ETH left in my pocket. :(`);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
robot.router.post('/incoming/kredits/github/'+process.env.KREDITS_WEBHOOK_TOKEN, (req, res) => {
|
function handleContributionAdded(contributionId, contributorId, amount) {
|
||||||
let evt = req.header('X-GitHub-Event');
|
Contributor.getById(contributorId).then((contributor) => {
|
||||||
let data = req.body;
|
Contribution.getById(contributionId).then((contribution) => {
|
||||||
// For some reason data is contained in a payload property on one
|
robot.logger.debug(`[hubot-kredits] Contribution #${contribution.id} added (${contribution.description})`);
|
||||||
// machine, but directly in the root of the object on others
|
});
|
||||||
if (data.payload) { data = data.payload; }
|
|
||||||
|
|
||||||
robot.logger.info(`Received GitHub hook. Event: ${evt}, action: ${data.action}`);
|
|
||||||
|
|
||||||
if (evt === 'pull_request' && data.action === 'closed') {
|
|
||||||
handleGitHubPullRequestClosed(data).then(() => res.send(200));
|
|
||||||
}
|
|
||||||
else if (evt === 'issues' && data.action === 'closed') {
|
|
||||||
handleGitHubIssueClosed(data).then(() => res.send(200));
|
|
||||||
} else {
|
|
||||||
res.send(200);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
};
|
watchContractEvents();
|
||||||
}());
|
|
||||||
|
//
|
||||||
|
// Integrations
|
||||||
|
//
|
||||||
|
|
||||||
|
require('./integrations/github')(robot, kredits);
|
||||||
|
require('./integrations/gitea')(robot, kredits);
|
||||||
|
|
||||||
|
if (typeof process.env.KREDITS_MEDIAWIKI_URL !== 'undefined') {
|
||||||
|
require('./integrations/mediawiki')(robot, kredits);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|||||||
166
integrations/gitea.js
Normal file
166
integrations/gitea.js
Normal file
@@ -0,0 +1,166 @@
|
|||||||
|
const util = require('util');
|
||||||
|
const fetch = require('node-fetch');
|
||||||
|
const amountFromLabels = require('./utils/amount-from-labels');
|
||||||
|
const kindFromLabels = require('./utils/kind-from-labels');
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
robot.logger.debug('[hubot-kredits] Loading Gitea integration...');
|
||||||
|
|
||||||
|
let repoBlackList = [];
|
||||||
|
if (process.env.KREDITS_GITEA_REPO_BLACKLIST) {
|
||||||
|
repoBlackList = process.env.KREDITS_GITEA_REPO_BLACKLIST.split(',');
|
||||||
|
robot.logger.debug('[hubot-kredits] Ignoring Gitea actions from ', util.inspect(repoBlackList));
|
||||||
|
}
|
||||||
|
|
||||||
|
const Contributor = kredits.Contributor;
|
||||||
|
const Contribution = kredits.Contribution;
|
||||||
|
|
||||||
|
function getContributorByGiteaUser(username) {
|
||||||
|
return Contributor.all().then(contributors => {
|
||||||
|
const contrib = contributors.find(c => {
|
||||||
|
return c.gitea_username === username;
|
||||||
|
});
|
||||||
|
if (!contrib) {
|
||||||
|
throw new Error(`No contributor found for ${username}`);
|
||||||
|
} else {
|
||||||
|
return contrib;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function createContribution(giteaUser, date, time, amount, kind, description, url, details) {
|
||||||
|
return getContributorByGiteaUser(giteaUser).then(contributor => {
|
||||||
|
robot.logger.debug(`[hubot-kredits] Creating contribution token for ${amount}₭S to ${giteaUser} for ${url}...`);
|
||||||
|
|
||||||
|
const contributionAttr = {
|
||||||
|
contributorId: contributor.id,
|
||||||
|
contributorIpfsHash: contributor.ipfsHash,
|
||||||
|
date,
|
||||||
|
time,
|
||||||
|
amount,
|
||||||
|
kind,
|
||||||
|
description,
|
||||||
|
url,
|
||||||
|
details
|
||||||
|
};
|
||||||
|
|
||||||
|
robot.logger.debug(`[hubot-kredits] contribution attributes:`);
|
||||||
|
robot.logger.debug(util.inspect(contributionAttr, { depth: 1, colors: true }));
|
||||||
|
|
||||||
|
return Contribution.addContribution(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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleGiteaIssueClosed(data) {
|
||||||
|
const issue = data.issue;
|
||||||
|
const repoName = data.repository.full_name;
|
||||||
|
const web_url = `${data.repository.html_url}/issues/${issue.number}`;
|
||||||
|
const description = `${repoName}: ${issue.title}`;
|
||||||
|
const labels = issue.labels.map(l => l.name);
|
||||||
|
const amount = amountFromLabels(labels);
|
||||||
|
const kind = kindFromLabels(labels);
|
||||||
|
const assignees = issue.assignees ? issue.assignees.map(a => a.login) : [];
|
||||||
|
[ date, time ] = issue.closed_at.split('T');
|
||||||
|
|
||||||
|
if (amount === 0) {
|
||||||
|
robot.logger.info('[hubot-kredits] Kredits amount from issue label is zero; ignoring');
|
||||||
|
return Promise.resolve();
|
||||||
|
} else if (repoBlackList.includes(repoName)) {
|
||||||
|
robot.logger.debug(`[hubot-kredits] ${repoName} is on black list; ignoring`);
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
let recipients;
|
||||||
|
if (assignees.length > 0) {
|
||||||
|
recipients = assignees;
|
||||||
|
} else {
|
||||||
|
recipients = [issue.user.login];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const recipient of recipients) {
|
||||||
|
try {
|
||||||
|
await createContribution(recipient, date, time, amount,
|
||||||
|
kind, description, web_url,
|
||||||
|
{ issue, repository: data.repository });
|
||||||
|
await sleep(60000);
|
||||||
|
}
|
||||||
|
catch (err) { robot.logger.error(err); }
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleGiteaPullRequestClosed(data) {
|
||||||
|
const pull_request = data.pull_request;
|
||||||
|
const repoName = data.repository.full_name;
|
||||||
|
const web_url = pull_request.html_url;
|
||||||
|
const description = `${repoName}: ${pull_request.title}`;
|
||||||
|
const labels = pull_request.labels.map(l => l.name);
|
||||||
|
const amount = amountFromLabels(labels);
|
||||||
|
const kind = kindFromLabels(labels);
|
||||||
|
const assignees = pull_request.assignees ? pull_request.assignees.map(a => a.login) : [];
|
||||||
|
[ date, time ] = pull_request.merged_at.split('T');
|
||||||
|
|
||||||
|
if (amount === 0) {
|
||||||
|
robot.logger.info('[hubot-kredits] Kredits amount from issue label is zero; ignoring');
|
||||||
|
return Promise.resolve();
|
||||||
|
} else if (repoBlackList.includes(repoName)) {
|
||||||
|
robot.logger.debug(`[hubot-kredits] ${repoName} is on black list; ignoring`);
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
let recipients;
|
||||||
|
if (assignees.length > 0) {
|
||||||
|
recipients = assignees;
|
||||||
|
} else {
|
||||||
|
recipients = [pull_request.user.login];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const recipient of recipients) {
|
||||||
|
try {
|
||||||
|
await createContribution(recipient, date, time, amount,
|
||||||
|
kind, description, web_url,
|
||||||
|
{ pull_request, repository: data.repository });
|
||||||
|
await sleep(60000);
|
||||||
|
}
|
||||||
|
catch (err) { robot.logger.error(err); }
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
robot.router.post('/incoming/kredits/gitea/'+process.env.KREDITS_WEBHOOK_TOKEN, (req, res) => {
|
||||||
|
const evt = req.header('X-Gitea-Event');
|
||||||
|
let data = req.body;
|
||||||
|
// For some reason data is contained in a payload property on one
|
||||||
|
// machine, but directly in the root of the object on others
|
||||||
|
if (data.payload) { data = JSON.parse(data.payload); }
|
||||||
|
|
||||||
|
robot.logger.info(`Received Gitea hook. Event: ${evt}, action: ${data.action}`);
|
||||||
|
|
||||||
|
if (evt === 'pull_request' && data.action === 'closed' && data.pull_request.merged) {
|
||||||
|
handleGiteaPullRequestClosed(data);
|
||||||
|
res.sendStatus(200);
|
||||||
|
}
|
||||||
|
else if (evt === 'issues' && data.action === 'closed') {
|
||||||
|
handleGiteaIssueClosed(data);
|
||||||
|
res.sendStatus(200);
|
||||||
|
} else {
|
||||||
|
res.sendStatus(200);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
};
|
||||||
175
integrations/github.js
Normal file
175
integrations/github.js
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
const util = require('util');
|
||||||
|
const fetch = require('node-fetch');
|
||||||
|
const amountFromLabels = require('./utils/amount-from-labels');
|
||||||
|
const kindFromLabels = require('./utils/kind-from-labels');
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
robot.logger.debug('[hubot-kredits] Loading GitHub integration...');
|
||||||
|
|
||||||
|
let repoBlackList = [];
|
||||||
|
if (process.env.KREDITS_GITHUB_REPO_BLACKLIST) {
|
||||||
|
repoBlackList = process.env.KREDITS_GITHUB_REPO_BLACKLIST.split(',');
|
||||||
|
robot.logger.debug('[hubot-kredits] Ignoring GitHub actions from ', util.inspect(repoBlackList));
|
||||||
|
}
|
||||||
|
|
||||||
|
const Contributor = kredits.Contributor;
|
||||||
|
const Contribution = kredits.Contribution;
|
||||||
|
|
||||||
|
function getContributorByGithubUser(username) {
|
||||||
|
return Contributor.all().then(contributors => {
|
||||||
|
const contrib = contributors.find(c => {
|
||||||
|
return c.github_username === username;
|
||||||
|
});
|
||||||
|
if (!contrib) {
|
||||||
|
throw new Error(`No contributor found for ${username}`);
|
||||||
|
} else {
|
||||||
|
return contrib;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function createContribution(githubUser, date, time, amount, kind, description, url, details) {
|
||||||
|
return getContributorByGithubUser(githubUser).then(contributor => {
|
||||||
|
robot.logger.info(`[hubot-kredits] Creating contribution token for ${amount}₭S to ${githubUser} for ${url}...`);
|
||||||
|
|
||||||
|
const contributionAttr = {
|
||||||
|
contributorId: contributor.id,
|
||||||
|
contributorIpfsHash: contributor.ipfsHash,
|
||||||
|
date,
|
||||||
|
time,
|
||||||
|
amount,
|
||||||
|
kind,
|
||||||
|
description,
|
||||||
|
url,
|
||||||
|
details
|
||||||
|
};
|
||||||
|
|
||||||
|
robot.logger.debug(`[hubot-kredits] contribution attributes:`);
|
||||||
|
robot.logger.debug(util.inspect(contributionAttr, { depth: 1, colors: true }));
|
||||||
|
|
||||||
|
return Contribution.addContribution(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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleGitHubIssueClosed(data) {
|
||||||
|
let recipients;
|
||||||
|
const issue = data.issue;
|
||||||
|
const assignees = issue.assignees.map(a => a.login);
|
||||||
|
const web_url = issue.html_url;
|
||||||
|
|
||||||
|
[date, time] = issue.closed_at.split('T');
|
||||||
|
const labels = issue.labels.map(l => l.name);
|
||||||
|
const amount = amountFromLabels(labels);
|
||||||
|
const kind = kindFromLabels(labels);
|
||||||
|
const repoName = issue.repository_url.match(/.*\/(.+\/.+)$/)[1];
|
||||||
|
const description = `${repoName}: ${issue.title}`;
|
||||||
|
|
||||||
|
if (amount === 0) {
|
||||||
|
robot.logger.info('[hubot-kredits] Kredits amount from issue label is zero; ignoring');
|
||||||
|
return Promise.resolve();
|
||||||
|
} else if (repoBlackList.includes(repoName)) {
|
||||||
|
robot.logger.debug(`[hubot-kredits] ${repoName} is on black list; ignoring`);
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (assignees.length > 0) {
|
||||||
|
recipients = assignees;
|
||||||
|
} else {
|
||||||
|
recipients = [issue.user.login];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const recipient of recipients) {
|
||||||
|
try {
|
||||||
|
await createContribution(recipient, date, time, amount, kind, description, web_url, issue);
|
||||||
|
await sleep(60000);
|
||||||
|
}
|
||||||
|
catch (err) { robot.logger.error(err); }
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleGitHubPullRequestClosed(data) {
|
||||||
|
let recipients;
|
||||||
|
const pull_request = data.pull_request;
|
||||||
|
const assignees = pull_request.assignees.map(a => a.login);
|
||||||
|
const web_url = pull_request._links.html.href;
|
||||||
|
const pr_issue_url = pull_request.issue_url;
|
||||||
|
|
||||||
|
[date, time] = pull_request.merged_at.split('T');
|
||||||
|
|
||||||
|
if (assignees.length > 0) {
|
||||||
|
recipients = assignees;
|
||||||
|
} else {
|
||||||
|
recipients = [pull_request.user.login];
|
||||||
|
}
|
||||||
|
|
||||||
|
return fetch(pr_issue_url)
|
||||||
|
.then(response => {
|
||||||
|
if (response.status >= 400) {
|
||||||
|
throw new Error('Bad response from fetching PR issue');
|
||||||
|
}
|
||||||
|
return response.json();
|
||||||
|
})
|
||||||
|
.then(async (issue) => {
|
||||||
|
const labels = issue.labels.map(l => l.name);
|
||||||
|
const amount = amountFromLabels(labels);
|
||||||
|
const kind = kindFromLabels(labels);
|
||||||
|
const repoName = pull_request.base.repo.full_name;
|
||||||
|
const description = `${repoName}: ${pull_request.title}`;
|
||||||
|
|
||||||
|
if (amount === 0) {
|
||||||
|
robot.logger.info('[hubot-kredits] Kredits amount from issue label is zero; ignoring');
|
||||||
|
return Promise.resolve();
|
||||||
|
} else if (repoBlackList.includes(repoName)) {
|
||||||
|
robot.logger.debug(`[hubot-kredits] ${repoName} is on black list; ignoring`);
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const recipient of recipients) {
|
||||||
|
try {
|
||||||
|
await createContribution(recipient, date, time, amount, kind, description, web_url, pull_request);
|
||||||
|
await sleep(60000);
|
||||||
|
}
|
||||||
|
catch (err) { robot.logger.error(err); }
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
robot.router.post('/incoming/kredits/github/'+process.env.KREDITS_WEBHOOK_TOKEN, (req, res) => {
|
||||||
|
const evt = req.header('X-GitHub-Event');
|
||||||
|
let data = req.body;
|
||||||
|
// For some reason data is contained in a payload property on one
|
||||||
|
// machine, but directly in the root of the object on others
|
||||||
|
if (data.payload) { data = JSON.parse(data.payload); }
|
||||||
|
|
||||||
|
robot.logger.info(`Received GitHub hook. Event: ${evt}, action: ${data.action}`);
|
||||||
|
|
||||||
|
if (evt === 'pull_request' && data.action === 'closed' && data.pull_request.merged) {
|
||||||
|
handleGitHubPullRequestClosed(data);
|
||||||
|
res.sendStatus(200);
|
||||||
|
}
|
||||||
|
else if (evt === 'issues' && data.action === 'closed') {
|
||||||
|
handleGitHubIssueClosed(data);
|
||||||
|
res.sendStatus(200);
|
||||||
|
} else {
|
||||||
|
res.sendStatus(200);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
};
|
||||||
185
integrations/mediawiki.js
Normal file
185
integrations/mediawiki.js
Normal file
@@ -0,0 +1,185 @@
|
|||||||
|
const url = require('url');
|
||||||
|
const util = require('util');
|
||||||
|
const fetch = require('node-fetch');
|
||||||
|
const groupArray = require('group-array');
|
||||||
|
const cron = require('node-cron');
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
robot.logger.debug('[hubot-kredits] Loading MediaWiki integration...')
|
||||||
|
|
||||||
|
const Contributor = kredits.Contributor;
|
||||||
|
const Contribution = kredits.Contribution;
|
||||||
|
|
||||||
|
const wikiURL = process.env.KREDITS_MEDIAWIKI_URL;
|
||||||
|
const apiURL = wikiURL + 'api.php';
|
||||||
|
|
||||||
|
function getContributorByWikiUser(username) {
|
||||||
|
let account = {
|
||||||
|
site: url.parse(process.env.KREDITS_MEDIAWIKI_URL).hostname,
|
||||||
|
username: username
|
||||||
|
}
|
||||||
|
return Contributor.findByAccount(account).then(contributor => {
|
||||||
|
robot.logger.debug('CONTRIBUTOR: ', contributor)
|
||||||
|
if (contributor) { return contributor; } else { throw new Error(); }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function createContribution(username, date, amount, description, url, details={}) {
|
||||||
|
return getContributorByWikiUser(username).then(contributor => {
|
||||||
|
robot.logger.debug(`[hubot-kredits] Creating contribution token for ${amount}₭S to ${contributor.name} for ${url}...`);
|
||||||
|
|
||||||
|
let contribution = {
|
||||||
|
contributorId: contributor.id,
|
||||||
|
contributorIpfsHash: contributor.ipfsHash,
|
||||||
|
date,
|
||||||
|
amount: amount,
|
||||||
|
url,
|
||||||
|
description,
|
||||||
|
details,
|
||||||
|
kind: 'docs'
|
||||||
|
};
|
||||||
|
|
||||||
|
return Contribution.addContribution(contribution).catch(error => {
|
||||||
|
robot.logger.error(`[hubot-kredits] Adding contribution failed:`, error);
|
||||||
|
});
|
||||||
|
}).catch(() => {
|
||||||
|
robot.logger.info(`[hubot-kredits] No contributor found for ${username}`);
|
||||||
|
messageRoom(`I wanted to propose giving kredits to wiki user ${username}, but I cannot find their info. Please add them as a contributor: https://kredits.kosmos.org`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function fetchChanges () {
|
||||||
|
const params = [
|
||||||
|
'action=query',
|
||||||
|
'format=json',
|
||||||
|
'list=recentchanges',
|
||||||
|
'rctype=edit|new',
|
||||||
|
'rcshow=!minor|!bot|!anon|!redirect',
|
||||||
|
'rclimit=max',
|
||||||
|
'rcprop=ids|title|timestamp|user|sizes|comment|flags'
|
||||||
|
];
|
||||||
|
|
||||||
|
let endTime = robot.brain.get('kredits:mediawiki:last_processed_at');
|
||||||
|
if (endTime) {
|
||||||
|
robot.logger.debug(`[hubot-kredits] Fetching wiki edits since ${endTime}`);
|
||||||
|
params.push(`rcend=${endTime}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = `${apiURL}?${params.join('&')}`;
|
||||||
|
|
||||||
|
return fetch(url).then(res => {
|
||||||
|
if (res.status === 200) {
|
||||||
|
return res.json();
|
||||||
|
} else {
|
||||||
|
robot.logger.info(`Fetching ${url} returned HTTP status ${res.status}:`);
|
||||||
|
robot.logger.info(res.body);
|
||||||
|
throw Error('Unexpected response from '+url);
|
||||||
|
}
|
||||||
|
}).then(res => {
|
||||||
|
return res.query.recentchanges;
|
||||||
|
}).catch(res => {
|
||||||
|
robot.logger.error(`[hubot-kredits] Failed to fetch ${url} (likely due to a network issue)`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function groupChangesByUser (changes) {
|
||||||
|
return Promise.resolve(groupArray(changes, 'user'));
|
||||||
|
}
|
||||||
|
|
||||||
|
function analyzeUserChanges (user, changes) {
|
||||||
|
// robot.logger.debug(`Analyzing ${changes.length} edits from ${user} ...`);
|
||||||
|
const results = {};
|
||||||
|
|
||||||
|
results.pagesCreated = changes.filter(c => c.type === 'new');
|
||||||
|
results.pagesChanged = changes.filter(c => c.type === 'edit');
|
||||||
|
results.charsAdded = changes
|
||||||
|
.map(c => { return (c.oldlen < c.newlen) ? (c.newlen - c.oldlen) : 0; })
|
||||||
|
.reduce((a, b) => a + b);
|
||||||
|
|
||||||
|
// robot.logger.debug(`Created ${results.pagesCreated.length} pages`);
|
||||||
|
// robot.logger.debug(`Edited ${results.pagesChanged.length} pages`);
|
||||||
|
// robot.logger.debug(`Added ${results.charsAdded} lines of text\n`);
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createContributions (changes) {
|
||||||
|
let promises = [];
|
||||||
|
|
||||||
|
for (const user of Object.keys(changes)) {
|
||||||
|
await createContributionForUserChanges(user, changes[user]);
|
||||||
|
await sleep(60000);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve();
|
||||||
|
}
|
||||||
|
|
||||||
|
function pageTitlesFromChanges(changes) {
|
||||||
|
return [...new Set(changes.map(c => `"${c.title}"`))].join(', ');
|
||||||
|
}
|
||||||
|
|
||||||
|
function calculateAmountForChanges(details) {
|
||||||
|
let amount;
|
||||||
|
|
||||||
|
if (details.charsAdded < 500) {
|
||||||
|
amount = 500;
|
||||||
|
} else if (details.charsAdded < 2000) {
|
||||||
|
amount = 1500;
|
||||||
|
} else {
|
||||||
|
amount = 5000;
|
||||||
|
}
|
||||||
|
|
||||||
|
return amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createContributionForUserChanges (user, changes) {
|
||||||
|
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);
|
||||||
|
|
||||||
|
let desc = `Added ${details.charsAdded} characters of text.`;
|
||||||
|
if (details.pagesChanged.length > 0) {
|
||||||
|
desc = `Edited ${pageTitlesFromChanges(details.pagesChanged)}. ${desc}`;
|
||||||
|
}
|
||||||
|
if (details.pagesCreated.length > 0) {
|
||||||
|
desc = `Created ${pageTitlesFromChanges(details.pagesCreated)}. ${desc}`;
|
||||||
|
}
|
||||||
|
desc = `Wiki contributions: ${desc}`;
|
||||||
|
|
||||||
|
let url;
|
||||||
|
if (changes.length > 1) {
|
||||||
|
url = `${wikiURL}Special:Contributions/${user}?hideMinor=1`;
|
||||||
|
} else {
|
||||||
|
rc = changes[0];
|
||||||
|
url = `${wikiURL}index.php?title=${rc.title}&diff=${rc.revid}&oldid=${rc.old_revid}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return createContribution(user, date, amount, desc, url, details);
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateTimestampForNextFetch () {
|
||||||
|
robot.logger.debug(`[hubot-kredits] Set timestamp for wiki changes fetch`);
|
||||||
|
robot.brain.set('kredits:mediawiki:last_processed_at', new Date().toISOString());
|
||||||
|
}
|
||||||
|
|
||||||
|
function processWikiChangesSinceLastRun () {
|
||||||
|
fetchChanges()
|
||||||
|
.then(res => groupChangesByUser(res))
|
||||||
|
.then(res => createContributions(res))
|
||||||
|
.then(() => updateTimestampForNextFetch());
|
||||||
|
}
|
||||||
|
|
||||||
|
cron.schedule('0 7 * * *', processWikiChangesSinceLastRun);
|
||||||
|
|
||||||
|
};
|
||||||
21
integrations/utils/amount-from-labels.js
Normal file
21
integrations/utils/amount-from-labels.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
module.exports = function (labels) {
|
||||||
|
const kreditsLabel = labels.filter(n => n.match(/^kredits/))[0];
|
||||||
|
// No label, no kredits
|
||||||
|
if (typeof kreditsLabel === 'undefined') { return 0; }
|
||||||
|
|
||||||
|
// TODO move amounts to config?
|
||||||
|
let amount;
|
||||||
|
switch(kreditsLabel) {
|
||||||
|
case 'kredits-1':
|
||||||
|
amount = 500;
|
||||||
|
break;
|
||||||
|
case 'kredits-2':
|
||||||
|
amount = 1500;
|
||||||
|
break;
|
||||||
|
case 'kredits-3':
|
||||||
|
amount = 5000;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return amount;
|
||||||
|
};
|
||||||
18
integrations/utils/kind-from-labels.js
Normal file
18
integrations/utils/kind-from-labels.js
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
module.exports = function (labels) {
|
||||||
|
let kind = 'dev';
|
||||||
|
|
||||||
|
if (labels.find(l => l.match(/ops|operations/))) {
|
||||||
|
kind = 'ops';
|
||||||
|
}
|
||||||
|
else if (labels.find(l => l.match(/docs|documentation/))) {
|
||||||
|
kind = 'docs';
|
||||||
|
}
|
||||||
|
else if (labels.find(l => l.match(/design/))) {
|
||||||
|
kind = 'design';
|
||||||
|
}
|
||||||
|
else if (labels.find(l => l.match(/community/))) {
|
||||||
|
kind = 'community';
|
||||||
|
}
|
||||||
|
|
||||||
|
return kind;
|
||||||
|
};
|
||||||
2100
package-lock.json
generated
Normal file
2100
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
22
package.json
22
package.json
@@ -1,28 +1,32 @@
|
|||||||
{
|
{
|
||||||
"name": "hubot-kredits",
|
"name": "hubot-kredits",
|
||||||
"version": "1.4.5",
|
"version": "3.3.1",
|
||||||
"description": "Kosmos Kredits functionality for chat bots",
|
"description": "Kosmos Kredits functionality for chat bots",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"bin": {
|
"bin": {
|
||||||
"kredits-wallet-create": "scripts/create-wallet.js"
|
"create-wallet": "scripts/create-wallet.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"ethereumjs-wallet": "mvayngrib/ethereumjs-wallet",
|
"eth-provider": "^0.2.2",
|
||||||
"kredits-contracts": "2.0.0",
|
"ethers": "^4.0.27",
|
||||||
"node-fetch": "^1.6.3",
|
"group-array": "^0.3.3",
|
||||||
"prompt": "^1.0.0",
|
"kosmos-schemas": "^1.1.2",
|
||||||
"web3": "^0.18.4",
|
"kredits-contracts": "^5.3.0",
|
||||||
"web3-provider-engine": "^12.0.3"
|
"node-cron": "^2.0.3",
|
||||||
|
"node-fetch": "^2.3.0",
|
||||||
|
"prompt": "^1.0.0"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git+https://github.com/67P/hubot-kredits.git"
|
"url": "git+https://github.com/67P/hubot-kredits.git"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"kosmos"
|
"kosmos",
|
||||||
|
"kredits",
|
||||||
|
"hubot-scripts"
|
||||||
],
|
],
|
||||||
"author": "Kosmos Developers <mail@kosmos.org>",
|
"author": "Kosmos Developers <mail@kosmos.org>",
|
||||||
"contributors": [
|
"contributors": [
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const Wallet = require('ethereumjs-wallet');
|
const ethers = require('ethers');
|
||||||
const userPrompt = require('prompt');
|
const userPrompt = require('prompt');
|
||||||
|
|
||||||
let schema = {
|
let schema = {
|
||||||
@@ -24,10 +24,10 @@ userPrompt.start();
|
|||||||
userPrompt.get(schema, (err, result) => {
|
userPrompt.get(schema, (err, result) => {
|
||||||
if (err) { throw(err); }
|
if (err) { throw(err); }
|
||||||
|
|
||||||
let wallet = Wallet.generate();
|
let wallet = ethers.Wallet.createRandom();
|
||||||
let content = JSON.stringify(wallet.toV3(result.password));
|
wallet.encrypt(result.password).then((walletJSON) => {
|
||||||
|
fs.writeFileSync(result.path, walletJSON);
|
||||||
|
console.log(`\nWrote encrypted wallet config to ${result.path}`);
|
||||||
|
});
|
||||||
|
|
||||||
fs.writeFileSync(result.path, content);
|
|
||||||
|
|
||||||
console.log(`\nWrote encrypted wallet config to ${result.path}`);
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user