Add date and time for GitHub issues and PRs

This commit is contained in:
Basti 2019-04-13 14:04:41 +01:00
parent 65e34ee4e1
commit 626712356a
No known key found for this signature in database
GPG Key ID: BE4634D632D39B67

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,7 +60,7 @@ 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);
} }