Merge pull request #30 from 67P/feature/29-date_time

Add contribution date
This commit is contained in:
bumi 2019-04-13 16:07:46 +00:00 committed by GitHub
commit 92c50ac69e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 37 deletions

View File

@ -13,7 +13,7 @@ module.exports = async function(robot, kredits) {
robot.logger.debug('[hubot-kredits] Loading GitHub integration...'); robot.logger.debug('[hubot-kredits] Loading GitHub integration...');
let repoBlackList = []; const repoBlackList = [];
if (process.env.KREDITS_GITHUB_REPO_BLACKLIST) { if (process.env.KREDITS_GITHUB_REPO_BLACKLIST) {
repoBlackList = process.env.KREDITS_GITHUB_REPO_BLACKLIST.split(','); repoBlackList = process.env.KREDITS_GITHUB_REPO_BLACKLIST.split(',');
robot.logger.debug('[hubot-kredits] Ignoring GitHub actions from ', util.inspect(repoBlackList)); robot.logger.debug('[hubot-kredits] Ignoring GitHub actions from ', util.inspect(repoBlackList));
@ -24,7 +24,7 @@ module.exports = async function(robot, kredits) {
function getContributorByGithubUser(username) { function getContributorByGithubUser(username) {
return Contributor.all().then(contributors => { return Contributor.all().then(contributors => {
let contrib = contributors.find(c => { const contrib = contributors.find(c => {
return c.github_username === username; return c.github_username === username;
}); });
if (!contrib) { if (!contrib) {
@ -35,14 +35,16 @@ module.exports = async function(robot, kredits) {
}); });
} }
function createContribution(githubUser, amount, description, url, details) { function createContribution(githubUser, date, time, amount, description, url, details) {
return getContributorByGithubUser(githubUser).then(contributor => { return getContributorByGithubUser(githubUser).then(contributor => {
robot.logger.debug(`[hubot-kredits] Creating contribution token for ${amount}₭S to ${githubUser} for ${url}...`); robot.logger.debug(`[hubot-kredits] Creating contribution token for ${amount}₭S to ${githubUser} for ${url}...`);
let contributionAttr = { const contributionAttr = {
contributorId: contributor.id, contributorId: contributor.id,
amount: amount,
contributorIpfsHash: contributor.ipfsHash, contributorIpfsHash: contributor.ipfsHash,
date,
time,
amount,
url, url,
description, description,
details, details,
@ -58,8 +60,8 @@ module.exports = async function(robot, kredits) {
} }
function amountFromIssueLabels(issue) { function amountFromIssueLabels(issue) {
let kreditsLabel = issue.labels.map(l => l.name) const kreditsLabel = issue.labels.map(l => l.name)
.filter(n => n.match(/^kredits/))[0]; .filter(n => n.match(/^kredits/))[0];
// No label, no kredits // No label, no kredits
if (typeof kreditsLabel === 'undefined') { return 0; } if (typeof kreditsLabel === 'undefined') { return 0; }
@ -82,13 +84,14 @@ module.exports = async function(robot, kredits) {
async function handleGitHubIssueClosed(data) { async function handleGitHubIssueClosed(data) {
let recipients; let recipients;
let issue = data.issue; const issue = data.issue;
let assignees = issue.assignees.map(a => a.login); const assignees = issue.assignees.map(a => a.login);
let web_url = issue.html_url; const web_url = issue.html_url;
let amount = amountFromIssueLabels(issue); [date, time] = issue.closed_at.split('T');
let repoName = issue.repository_url.match(/.*\/(.+\/.+)$/)[1]; const amount = amountFromIssueLabels(issue);
let description = `${repoName}: ${issue.title}`; const repoName = issue.repository_url.match(/.*\/(.+\/.+)$/)[1];
const description = `${repoName}: ${issue.title}`;
if (amount === 0) { if (amount === 0) {
robot.logger.info('[hubot-kredits] Kredits amount from issue label is zero; ignoring'); robot.logger.info('[hubot-kredits] Kredits amount from issue label is zero; ignoring');
@ -106,7 +109,7 @@ module.exports = async function(robot, kredits) {
for (const recipient of recipients) { for (const recipient of recipients) {
try { try {
await createContribution(recipient, amount, description, web_url, issue); await createContribution(recipient, date, time, amount, description, web_url, issue);
await sleep(60000); await sleep(60000);
} }
catch (err) { robot.logger.error(err); } catch (err) { robot.logger.error(err); }
@ -117,10 +120,12 @@ module.exports = async function(robot, kredits) {
function handleGitHubPullRequestClosed(data) { function handleGitHubPullRequestClosed(data) {
let recipients; let recipients;
let pull_request = data.pull_request; const pull_request = data.pull_request;
let assignees = pull_request.assignees.map(a => a.login); const assignees = pull_request.assignees.map(a => a.login);
let web_url = pull_request._links.html.href; const web_url = pull_request._links.html.href;
let pr_issue_url = pull_request.issue_url; const pr_issue_url = pull_request.issue_url;
[date, time] = pull_request.merged_at.split('T');
if (assignees.length > 0) { if (assignees.length > 0) {
recipients = assignees; recipients = assignees;
@ -136,9 +141,9 @@ module.exports = async function(robot, kredits) {
return response.json(); return response.json();
}) })
.then(async (issue) => { .then(async (issue) => {
let amount = amountFromIssueLabels(issue); const amount = amountFromIssueLabels(issue);
let repoName = pull_request.base.repo.full_name; const repoName = pull_request.base.repo.full_name;
let description = `${repoName}: ${pull_request.title}`; const description = `${repoName}: ${pull_request.title}`;
if (amount === 0) { if (amount === 0) {
robot.logger.info('[hubot-kredits] Kredits amount from issue label is zero; ignoring'); robot.logger.info('[hubot-kredits] Kredits amount from issue label is zero; ignoring');
@ -150,7 +155,7 @@ module.exports = async function(robot, kredits) {
for (const recipient of recipients) { for (const recipient of recipients) {
try { try {
await createContribution(recipient, amount, description, web_url, pull_request); await createContribution(recipient, date, time, amount, description, web_url, pull_request);
await sleep(60000); await sleep(60000);
} }
catch (err) { robot.logger.error(err); } catch (err) { robot.logger.error(err); }
@ -161,15 +166,15 @@ module.exports = async function(robot, kredits) {
} }
robot.router.post('/incoming/kredits/github/'+process.env.KREDITS_WEBHOOK_TOKEN, (req, res) => { robot.router.post('/incoming/kredits/github/'+process.env.KREDITS_WEBHOOK_TOKEN, (req, res) => {
let evt = req.header('X-GitHub-Event'); const evt = req.header('X-GitHub-Event');
let data = req.body; const data = req.body;
// For some reason data is contained in a payload property on one // For some reason data is contained in a payload property on one
// machine, but directly in the root of the object on others // machine, but directly in the root of the object on others
if (data.payload) { data = JSON.parse(data.payload); } if (data.payload) { data = JSON.parse(data.payload); }
robot.logger.info(`Received GitHub hook. Event: ${evt}, action: ${data.action}`); robot.logger.info(`Received GitHub hook. Event: ${evt}, action: ${data.action}`);
if (evt === 'pull_request' && data.action === 'closed') { if (evt === 'pull_request' && data.action === 'closed' && data.pull_request.merged) {
handleGitHubPullRequestClosed(data); handleGitHubPullRequestClosed(data);
res.send(200); res.send(200);
} }

View File

@ -33,14 +33,15 @@ module.exports = async function(robot, kredits) {
}); });
} }
function createContribution(username, amount, description, url, details={}) { function createContribution(username, date, amount, description, url, details={}) {
return getContributorByWikiUser(username).then(contributor => { return getContributorByWikiUser(username).then(contributor => {
robot.logger.debug(`[hubot-kredits] Creating contribution token for ${amount}₭S to ${contributor.name} for ${url}...`); robot.logger.debug(`[hubot-kredits] Creating contribution token for ${amount}₭S to ${contributor.name} for ${url}...`);
let contribution = { let contribution = {
contributorId: contributor.id, contributorId: contributor.id,
amount: amount,
contributorIpfsHash: contributor.ipfsHash, contributorIpfsHash: contributor.ipfsHash,
date,
amount: amount,
url, url,
description, description,
details, details,
@ -141,6 +142,10 @@ module.exports = async function(robot, kredits) {
} }
function createContributionForUserChanges (user, changes) { 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 details = analyzeUserChanges(user, changes);
const amount = calculateAmountForChanges(details); const amount = calculateAmountForChanges(details);
@ -161,7 +166,7 @@ module.exports = async function(robot, kredits) {
url = `${wikiURL}index.php?title=${rc.title}&diff=${rc.revid}&oldid=${rc.old_revid}`; url = `${wikiURL}index.php?title=${rc.title}&diff=${rc.revid}&oldid=${rc.old_revid}`;
} }
return createContribution(user, amount, desc, url, details); return createContribution(user, date, amount, desc, url, details);
} }
function updateTimestampForNextFetch () { function updateTimestampForNextFetch () {

47
package-lock.json generated
View File

@ -255,6 +255,11 @@
"safe-buffer": "^5.0.1" "safe-buffer": "^5.0.1"
} }
}, },
"base64-js": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.0.tgz",
"integrity": "sha512-ccav/yGvoa80BQDljCxsmmQ3Xvx60/UpBIij5QN21W3wBi/hhIC9OoO+KLpu9IJTS9j4DRVJ3aDDF9cMSoa2lw=="
},
"bignumber.js": { "bignumber.js": {
"version": "8.1.1", "version": "8.1.1",
"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-8.1.1.tgz", "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-8.1.1.tgz",
@ -371,6 +376,15 @@
"base-x": "^3.0.2" "base-x": "^3.0.2"
} }
}, },
"buffer": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/buffer/-/buffer-5.2.1.tgz",
"integrity": "sha512-c+Ko0loDaFfuPWiL02ls9Xd3GO3cPVmUobQ6t3rXNUk304u6hGq+8N/kFi+QEIKhzK3uwolVhLzszmfLmMLnqg==",
"requires": {
"base64-js": "^1.0.2",
"ieee754": "^1.1.4"
}
},
"buffer-xor": { "buffer-xor": {
"version": "1.0.3", "version": "1.0.3",
"resolved": "https://registry.npmjs.org/buffer-xor/-/buffer-xor-1.0.3.tgz", "resolved": "https://registry.npmjs.org/buffer-xor/-/buffer-xor-1.0.3.tgz",
@ -841,14 +855,15 @@
} }
}, },
"ipfs-http-client": { "ipfs-http-client": {
"version": "30.1.1", "version": "30.1.3",
"resolved": "https://registry.npmjs.org/ipfs-http-client/-/ipfs-http-client-30.1.1.tgz", "resolved": "https://registry.npmjs.org/ipfs-http-client/-/ipfs-http-client-30.1.3.tgz",
"integrity": "sha512-tMqSwEhW57VnjHDBLjKKlgtAvlhkqVSR3oIFC0IiGnaM1nzcw7pbFBoHaFzl0PKIuXm40a5bJt85Rm2trYx+Ag==", "integrity": "sha512-NQ7WTLKUZeoKaKXrTSLAtjASRYCR0bPdsolZf16Y7Gt7o3RfiPpFF+AqvP0xbekOV3/zhFj2Qyf6ShEV4CCtsQ==",
"requires": { "requires": {
"async": "^2.6.1", "async": "^2.6.1",
"bignumber.js": "^8.0.2", "bignumber.js": "^8.0.2",
"bl": "^3.0.0", "bl": "^3.0.0",
"bs58": "^4.0.1", "bs58": "^4.0.1",
"buffer": "^5.2.1",
"cids": "~0.5.5", "cids": "~0.5.5",
"concat-stream": "github:hugomrdias/concat-stream#feat/smaller", "concat-stream": "github:hugomrdias/concat-stream#feat/smaller",
"debug": "^4.1.0", "debug": "^4.1.0",
@ -863,7 +878,7 @@
"is-ipfs": "~0.6.0", "is-ipfs": "~0.6.0",
"is-pull-stream": "0.0.0", "is-pull-stream": "0.0.0",
"is-stream": "^1.1.0", "is-stream": "^1.1.0",
"iso-stream-http": "~0.1.1", "iso-stream-http": "~0.1.2",
"iso-url": "~0.4.6", "iso-url": "~0.4.6",
"just-kebab-case": "^1.1.0", "just-kebab-case": "^1.1.0",
"just-map-keys": "^1.1.0", "just-map-keys": "^1.1.0",
@ -1160,13 +1175,24 @@
} }
}, },
"kredits-contracts": { "kredits-contracts": {
"version": "4.0.0", "version": "5.1.0",
"resolved": "https://registry.npmjs.org/kredits-contracts/-/kredits-contracts-4.0.0.tgz", "resolved": "https://registry.npmjs.org/kredits-contracts/-/kredits-contracts-5.1.0.tgz",
"integrity": "sha512-N7D1PPrfINXqm7f6SRU3+8eb7EyKjnR7xnBUwfEUUWvocr5xOmpkhekn3gMR6+jLJNvi8XTh0ao0jRWakRSFkw==", "integrity": "sha512-IALPPatInkA0khvSDr1/iooUAD2wnsC56ylKqWjhqq1vdOJHx8C/vpcIaKf3KVXS1RD/JpcBkASeHSv7TIXx/g==",
"requires": { "requires": {
"ethers": "^4.0.27", "ethers": "^4.0.27",
"ipfs-http-client": "^30.1.1", "ipfs-http-client": "^30.1.1",
"rsvp": "^4.8.2" "kosmos-schemas": "github:67P/kosmos-schemas#feature/contribution_date_time",
"rsvp": "^4.8.2",
"tv4": "^1.3.0"
},
"dependencies": {
"kosmos-schemas": {
"version": "github:67P/kosmos-schemas#28a5e9f1fc2e083165e4e02ce3361c3f4fafa592",
"from": "github:67P/kosmos-schemas#feature/contribution_date_time",
"requires": {
"brfs-babel": "^1.0.0"
}
}
} }
}, },
"libp2p-crypto": { "libp2p-crypto": {
@ -1928,6 +1954,11 @@
"resolved": "https://registry.npmjs.org/trim-right/-/trim-right-1.0.1.tgz", "resolved": "https://registry.npmjs.org/trim-right/-/trim-right-1.0.1.tgz",
"integrity": "sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM=" "integrity": "sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM="
}, },
"tv4": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/tv4/-/tv4-1.3.0.tgz",
"integrity": "sha1-0CDIRvrdUMhVq7JeuuzGj8EPeWM="
},
"tweetnacl": { "tweetnacl": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.1.tgz", "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.1.tgz",

View File

@ -14,7 +14,7 @@
"ethers": "^4.0.27", "ethers": "^4.0.27",
"group-array": "^0.3.3", "group-array": "^0.3.3",
"kosmos-schemas": "^1.1.2", "kosmos-schemas": "^1.1.2",
"kredits-contracts": "4.x", "kredits-contracts": "5.x",
"node-cron": "^2.0.3", "node-cron": "^2.0.3",
"node-fetch": "^2.3.0", "node-fetch": "^2.3.0",
"prompt": "^1.0.0" "prompt": "^1.0.0"