From d2976b312ea5c19842d3f70cb6d9567bf0d18158 Mon Sep 17 00:00:00 2001 From: Sebastian Kippe Date: Wed, 8 May 2019 16:03:03 +0200 Subject: [PATCH] Create utility modules for label processing --- integrations/github.js | 55 ++++-------------------- integrations/utils/amount-from-labels.js | 21 +++++++++ integrations/utils/kind-from-labels.js | 18 ++++++++ 3 files changed, 47 insertions(+), 47 deletions(-) create mode 100644 integrations/utils/amount-from-labels.js create mode 100644 integrations/utils/kind-from-labels.js diff --git a/integrations/github.js b/integrations/github.js index 3e60498..77996bf 100644 --- a/integrations/github.js +++ b/integrations/github.js @@ -1,5 +1,7 @@ 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)); @@ -62,49 +64,6 @@ 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; - } - - function kindFromIssueLabels(issue) { - const labels = issue.labels.map(l => l.name); - 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; - } - async function handleGitHubIssueClosed(data) { let recipients; const issue = data.issue; @@ -112,8 +71,9 @@ module.exports = async function(robot, kredits) { const web_url = issue.html_url; [date, time] = issue.closed_at.split('T'); - const amount = amountFromIssueLabels(issue); - const kind = kindFromIssueLabels(issue); + 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}`; @@ -165,8 +125,9 @@ module.exports = async function(robot, kredits) { return response.json(); }) .then(async (issue) => { - const amount = amountFromIssueLabels(issue); - const kind = kindFromIssueLabels(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}`; diff --git a/integrations/utils/amount-from-labels.js b/integrations/utils/amount-from-labels.js new file mode 100644 index 0000000..ec433ab --- /dev/null +++ b/integrations/utils/amount-from-labels.js @@ -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; +}; diff --git a/integrations/utils/kind-from-labels.js b/integrations/utils/kind-from-labels.js new file mode 100644 index 0000000..4434e3b --- /dev/null +++ b/integrations/utils/kind-from-labels.js @@ -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; +};