Map issue/PR labels to contribution kind #39
@ -1,5 +1,7 @@
 | 
				
			|||||||
const util = require('util');
 | 
					const util = require('util');
 | 
				
			||||||
const fetch = require('node-fetch');
 | 
					const fetch = require('node-fetch');
 | 
				
			||||||
 | 
					const amountFromLabels = require('./utils/amount-from-labels');
 | 
				
			||||||
 | 
					const kindFromLabels = require('./utils/kind-from-labels');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function sleep(ms) {
 | 
					function sleep(ms) {
 | 
				
			||||||
  return new Promise(resolve => setTimeout(resolve, ms));
 | 
					  return new Promise(resolve => setTimeout(resolve, ms));
 | 
				
			||||||
@ -35,7 +37,7 @@ module.exports = async function(robot, kredits) {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  function createContribution(giteaUser, date, time, amount, description, url, details) {
 | 
					  function createContribution(giteaUser, date, time, amount, kind, description, url, details) {
 | 
				
			||||||
    return getContributorByGiteaUser(giteaUser).then(contributor => {
 | 
					    return getContributorByGiteaUser(giteaUser).then(contributor => {
 | 
				
			||||||
      robot.logger.debug(`[hubot-kredits] Creating contribution token for ${amount}₭S to ${giteaUser} for ${url}...`);
 | 
					      robot.logger.debug(`[hubot-kredits] Creating contribution token for ${amount}₭S to ${giteaUser} for ${url}...`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -45,12 +47,15 @@ module.exports = async function(robot, kredits) {
 | 
				
			|||||||
        date,
 | 
					        date,
 | 
				
			||||||
        time,
 | 
					        time,
 | 
				
			||||||
        amount,
 | 
					        amount,
 | 
				
			||||||
        url,
 | 
					        kind,
 | 
				
			||||||
        description,
 | 
					        description,
 | 
				
			||||||
        details,
 | 
					        url,
 | 
				
			||||||
        kind: 'dev'
 | 
					        details
 | 
				
			||||||
      };
 | 
					      };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      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.addContribution(contributionAttr).catch(error => {
 | 
				
			||||||
        robot.logger.error(`[hubot-kredits] Error:`, 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(`I tried to add a contribution for ${giteaUser} for ${url}, but I encountered an error when submitting the tx:`);
 | 
				
			||||||
@ -59,35 +64,14 @@ module.exports = async function(robot, kredits) {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  function amountFromLabels(labels) {
 | 
					 | 
				
			||||||
    const kreditsLabel = 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 = 500;
 | 
					 | 
				
			||||||
        break;
 | 
					 | 
				
			||||||
      case 'kredits-2':
 | 
					 | 
				
			||||||
        amount = 1500;
 | 
					 | 
				
			||||||
        break;
 | 
					 | 
				
			||||||
      case 'kredits-3':
 | 
					 | 
				
			||||||
        amount = 5000;
 | 
					 | 
				
			||||||
        break;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return amount;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  async function handleGiteaIssueClosed(data) {
 | 
					  async function handleGiteaIssueClosed(data) {
 | 
				
			||||||
    const issue       = data.issue;
 | 
					    const issue       = data.issue;
 | 
				
			||||||
    const repoName    = data.repository.full_name;
 | 
					    const repoName    = data.repository.full_name;
 | 
				
			||||||
    const web_url     = `${data.repository.html_url}/issues/${issue.number}`;
 | 
					    const web_url     = `${data.repository.html_url}/issues/${issue.number}`;
 | 
				
			||||||
    const description = `${repoName}: ${issue.title}`;
 | 
					    const description = `${repoName}: ${issue.title}`;
 | 
				
			||||||
    const amount      = amountFromLabels(issue.labels);
 | 
					    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) : [];
 | 
					    const assignees   = issue.assignees ? issue.assignees.map(a => a.login) : [];
 | 
				
			||||||
    [ date, time ]    = issue.closed_at.split('T');
 | 
					    [ date, time ]    = issue.closed_at.split('T');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -108,7 +92,8 @@ module.exports = async function(robot, kredits) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    for (const recipient of recipients) {
 | 
					    for (const recipient of recipients) {
 | 
				
			||||||
      try {
 | 
					      try {
 | 
				
			||||||
        await createContribution(recipient, date, time, amount, description, web_url,
 | 
					        await createContribution(recipient, date, time, amount,
 | 
				
			||||||
 | 
					                                 kind, description, web_url,
 | 
				
			||||||
                                 { issue, repository: data.repository });
 | 
					                                 { issue, repository: data.repository });
 | 
				
			||||||
        await sleep(60000);
 | 
					        await sleep(60000);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
@ -123,7 +108,9 @@ module.exports = async function(robot, kredits) {
 | 
				
			|||||||
    const repoName     = data.repository.full_name;
 | 
					    const repoName     = data.repository.full_name;
 | 
				
			||||||
    const web_url      = pull_request.html_url;
 | 
					    const web_url      = pull_request.html_url;
 | 
				
			||||||
    const description  = `${repoName}: ${pull_request.title}`;
 | 
					    const description  = `${repoName}: ${pull_request.title}`;
 | 
				
			||||||
    const amount       = amountFromLabels(pull_request.labels);
 | 
					    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) : [];
 | 
					    const assignees    = pull_request.assignees ? pull_request.assignees.map(a => a.login) : [];
 | 
				
			||||||
    [ date, time ]     = pull_request.merged_at.split('T');
 | 
					    [ date, time ]     = pull_request.merged_at.split('T');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -142,10 +129,10 @@ module.exports = async function(robot, kredits) {
 | 
				
			|||||||
      recipients = [pull_request.user.login];
 | 
					      recipients = [pull_request.user.login];
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
    for (const recipient of recipients) {
 | 
					    for (const recipient of recipients) {
 | 
				
			||||||
      try {
 | 
					      try {
 | 
				
			||||||
        await createContribution(recipient, date, time, amount, description, web_url,
 | 
					        await createContribution(recipient, date, time, amount,
 | 
				
			||||||
 | 
					                                 kind, description, web_url,
 | 
				
			||||||
                                 { pull_request, repository: data.repository });
 | 
					                                 { pull_request, repository: data.repository });
 | 
				
			||||||
        await sleep(60000);
 | 
					        await sleep(60000);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
				
			|||||||
@ -1,5 +1,7 @@
 | 
				
			|||||||
const util = require('util');
 | 
					const util = require('util');
 | 
				
			||||||
const fetch = require('node-fetch');
 | 
					const fetch = require('node-fetch');
 | 
				
			||||||
 | 
					const amountFromLabels = require('./utils/amount-from-labels');
 | 
				
			||||||
 | 
					const kindFromLabels = require('./utils/kind-from-labels');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function sleep(ms) {
 | 
					function sleep(ms) {
 | 
				
			||||||
  return new Promise(resolve => setTimeout(resolve, ms));
 | 
					  return new Promise(resolve => setTimeout(resolve, ms));
 | 
				
			||||||
@ -35,9 +37,9 @@ module.exports = async function(robot, kredits) {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  function createContribution(githubUser, date, time, amount, description, url, details) {
 | 
					  function createContribution(githubUser, date, time, amount, kind, 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.info(`[hubot-kredits] Creating contribution token for ${amount}₭S to ${githubUser} for ${url}...`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      const contributionAttr = {
 | 
					      const contributionAttr = {
 | 
				
			||||||
        contributorId: contributor.id,
 | 
					        contributorId: contributor.id,
 | 
				
			||||||
@ -45,12 +47,15 @@ module.exports = async function(robot, kredits) {
 | 
				
			|||||||
        date,
 | 
					        date,
 | 
				
			||||||
        time,
 | 
					        time,
 | 
				
			||||||
        amount,
 | 
					        amount,
 | 
				
			||||||
        url,
 | 
					        kind,
 | 
				
			||||||
        description,
 | 
					        description,
 | 
				
			||||||
        details,
 | 
					        url,
 | 
				
			||||||
        kind: 'dev'
 | 
					        details
 | 
				
			||||||
      };
 | 
					      };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      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.addContribution(contributionAttr).catch(error => {
 | 
				
			||||||
        robot.logger.error(`[hubot-kredits] Error:`, 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(`I tried to add a contribution for ${githubUser} for ${url}, but I encountered an error when submitting the tx:`);
 | 
				
			||||||
@ -59,38 +64,17 @@ module.exports = async function(robot, kredits) {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  function amountFromIssueLabels(issue) {
 | 
					 | 
				
			||||||
    const 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 = 500;
 | 
					 | 
				
			||||||
        break;
 | 
					 | 
				
			||||||
      case 'kredits-2':
 | 
					 | 
				
			||||||
        amount = 1500;
 | 
					 | 
				
			||||||
        break;
 | 
					 | 
				
			||||||
      case 'kredits-3':
 | 
					 | 
				
			||||||
        amount = 5000;
 | 
					 | 
				
			||||||
        break;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return amount;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  async function handleGitHubIssueClosed(data) {
 | 
					  async function handleGitHubIssueClosed(data) {
 | 
				
			||||||
    let recipients;
 | 
					    let recipients;
 | 
				
			||||||
    const issue        = data.issue;
 | 
					    const issue       = data.issue;
 | 
				
			||||||
    const assignees    = issue.assignees.map(a => a.login);
 | 
					    const assignees   = issue.assignees.map(a => a.login);
 | 
				
			||||||
    const web_url      = issue.html_url;
 | 
					    const web_url     = issue.html_url;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    [date, time] = issue.closed_at.split('T');
 | 
					    [date, time]      = issue.closed_at.split('T');
 | 
				
			||||||
    const amount = amountFromIssueLabels(issue);
 | 
					    const labels      = issue.labels.map(l => l.name);
 | 
				
			||||||
    const repoName = issue.repository_url.match(/.*\/(.+\/.+)$/)[1];
 | 
					    const amount      = amountFromLabels(labels);
 | 
				
			||||||
 | 
					    const kind        = kindFromLabels(labels);
 | 
				
			||||||
 | 
					    const repoName    = issue.repository_url.match(/.*\/(.+\/.+)$/)[1];
 | 
				
			||||||
    const description = `${repoName}: ${issue.title}`;
 | 
					    const description = `${repoName}: ${issue.title}`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (amount === 0) {
 | 
					    if (amount === 0) {
 | 
				
			||||||
@ -109,7 +93,7 @@ module.exports = async function(robot, kredits) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    for (const recipient of recipients) {
 | 
					    for (const recipient of recipients) {
 | 
				
			||||||
      try {
 | 
					      try {
 | 
				
			||||||
        await createContribution(recipient, date, time, amount, description, web_url, issue);
 | 
					        await createContribution(recipient, date, time, amount, kind, description, web_url, issue);
 | 
				
			||||||
        await sleep(60000);
 | 
					        await sleep(60000);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      catch (err) { robot.logger.error(err); }
 | 
					      catch (err) { robot.logger.error(err); }
 | 
				
			||||||
@ -141,8 +125,10 @@ module.exports = async function(robot, kredits) {
 | 
				
			|||||||
        return response.json();
 | 
					        return response.json();
 | 
				
			||||||
      })
 | 
					      })
 | 
				
			||||||
      .then(async (issue) => {
 | 
					      .then(async (issue) => {
 | 
				
			||||||
        const amount = amountFromIssueLabels(issue);
 | 
					        const labels      = issue.labels.map(l => l.name);
 | 
				
			||||||
        const repoName = pull_request.base.repo.full_name;
 | 
					        const amount      = amountFromLabels(labels);
 | 
				
			||||||
 | 
					        const kind        = kindFromLabels(labels);
 | 
				
			||||||
 | 
					        const repoName    = pull_request.base.repo.full_name;
 | 
				
			||||||
        const description = `${repoName}: ${pull_request.title}`;
 | 
					        const description = `${repoName}: ${pull_request.title}`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (amount === 0) {
 | 
					        if (amount === 0) {
 | 
				
			||||||
@ -155,7 +141,7 @@ module.exports = async function(robot, kredits) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        for (const recipient of recipients) {
 | 
					        for (const recipient of recipients) {
 | 
				
			||||||
          try {
 | 
					          try {
 | 
				
			||||||
            await createContribution(recipient, date, time, amount, description, web_url, pull_request);
 | 
					            await createContribution(recipient, date, time, amount, kind, description, web_url, pull_request);
 | 
				
			||||||
            await sleep(60000);
 | 
					            await sleep(60000);
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
          catch (err) { robot.logger.error(err); }
 | 
					          catch (err) { robot.logger.error(err); }
 | 
				
			||||||
@ -176,13 +162,13 @@ module.exports = async function(robot, kredits) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    if (evt === 'pull_request' && data.action === 'closed' && data.pull_request.merged) {
 | 
					    if (evt === 'pull_request' && data.action === 'closed' && data.pull_request.merged) {
 | 
				
			||||||
      handleGitHubPullRequestClosed(data);
 | 
					      handleGitHubPullRequestClosed(data);
 | 
				
			||||||
      res.send(200);
 | 
					      res.sendStatus(200);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    else if (evt === 'issues' && data.action === 'closed') {
 | 
					    else if (evt === 'issues' && data.action === 'closed') {
 | 
				
			||||||
      handleGitHubIssueClosed(data);
 | 
					      handleGitHubIssueClosed(data);
 | 
				
			||||||
      res.send(200);
 | 
					      res.sendStatus(200);
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      res.send(200);
 | 
					      res.sendStatus(200);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										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;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user