Add kredits script prototype

refs https://github.com/67P/hubot-kredits/issues/1
This commit is contained in:
Basti 2017-02-09 16:01:37 +08:00
parent c569a7fda4
commit 3291b970a0
5 changed files with 135 additions and 3 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
node_modules
.DS_Store*
.hubot_history
/tmp

View File

@ -1 +1,3 @@
[]
[
"hubot-kredits"
]

View File

@ -25,10 +25,11 @@
"hubot-shipit": "^0.2.0",
"hubot-tell": "^1.2.3",
"hubot-yubikey-invalidation": "0.0.3",
"node-fetch": "^1.6.3",
"request": "2.30.0"
},
"engines": {
"node": "0.12.x",
"node": "6.x.x",
"npm": "2.1.x"
}
}

3
run.sh Normal file → Executable file
View File

@ -14,4 +14,5 @@ LOG_HTTP_PORT=7000 \
LOG_STEALTH="true" \
WEBHOOK_TOKEN="kosmosplusplus" \
HUBOT_YUBIKEY_API_ID="change-me" \
bin/hubot -a irc --name hal7000
KREDITS_WEBHOOK_TOKEN="123" \
bin/hubot --name hal7000

127
scripts/hubot-kredts.js Normal file
View File

@ -0,0 +1,127 @@
// 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
//
// Authors:
// Sebastian Kippe <sebastian@kip.pe>
const fs = require('fs');
const fetch = require('node-fetch');
(function() {
"use strict";
module.exports = function(robot) {
function amountFromIssueLabels(issue) {
let kreditsLabel = issue.labels.map(l => l.name)
.filter(n => n.match(/^kredits/))[0];
// No label, no kredits
if (typeof kreditsLabel === 'undefined') { return 0; }
// TODO move to config maybe?
let amount;
switch(kreditsLabel) {
case 'kredits-1':
amount = 50;
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
console.log(`Creating proposal to issue ${amount}₭S to ${recipient} for ${url}...`);
// robot.messageRoom(process.env.KREDITS_ROOM, message);
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.push(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.push(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.router.post('/incoming/kredits/github/'+process.env.KREDITS_WEBHOOK_TOKEN, (req, res) => {
let evt = req.header('X-GitHub-Event');
let data = req.body;
console.log(`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));
}
});
};
}());