when multiple matching labels found, sum them

This commit is contained in:
Radek Stepan 2013-10-13 17:53:20 +01:00
parent d81f3e9925
commit 5f378090e3
4 changed files with 52 additions and 77 deletions

View File

@ -46,6 +46,8 @@ The way we are getting a size of an issue from GitHub is by putting a label on i
This is also the default label if no other is specified.
When multiple matching size labels are present on an issue, their **sum** is taken as the size of the whole issue. This allows you to mix & match (if you want) without creating too many labels in the GitHub interface.
###Token
Your OAuth2 token from GitHub. Get it [here](https://github.com/settings/applications). Bear in mind that if you just statically serve the app, everybody will be able to see the token in transmission. If you would like to avoid that, use the [Proxy Mode](#proxy-mode).

View File

@ -29465,48 +29465,24 @@ module.exports = {
return async.parallel([_.partial(one_status, 'open'), _.partial(one_status, 'closed')], cb);
},
'filter': function(collection, regex, cb) {
var err, filtered, total, warnings;
warnings = null;
var filtered, total;
total = 0;
try {
filtered = _.filter(collection, function(issue) {
var labels, name, number;
labels = issue.labels, number = issue.number;
if (number == null) {
number = '?';
filtered = _.filter(collection, function(issue) {
var labels;
if (!(labels = issue.labels)) {
return false;
}
issue.size = _.reduce(labels, function(sum, label) {
var matches;
if (!(matches = label.name.match(regex))) {
return sum;
}
if (!labels) {
return false;
}
switch (((function() {
var _i, _len, _results;
_results = [];
for (_i = 0, _len = labels.length; _i < _len; _i++) {
name = labels[_i].name;
if (name && regex.test(name)) {
_results.push({});
}
}
return _results;
})()).length) {
case 0:
return false;
case 1:
total += issue.size = parseInt(name.match(regex)[1]);
return true;
default:
if (warnings == null) {
warnings = [];
}
warnings.push("Issue #" + number + " has multiple matching size labels");
return true;
}
});
return cb(null, warnings, filtered, total);
} catch (_error) {
err = _error;
return cb(err, warnings);
}
return sum += parseInt(matches[1]);
}, 0);
total += issue.size;
return !!issue.size;
});
return cb(null, filtered, total);
}
};

View File

@ -39,29 +39,25 @@ module.exports =
# Filter an array of incoming issues based on a regex & save size on them.
'filter': (collection, regex, cb) ->
warnings = null ; total = 0
try
filtered = _.filter collection, (issue) ->
{ labels, number } = issue
number ?= '?'
return false unless labels
# Find size labels only
size_labels = _.filter(labels, (label) ->
regex.test label.name
)
switch size_labels.length
when 0 then false
when 1
name = size_labels[0].name
# Provide the size attribute on the issue.
total += issue.size = parseInt name.match(regex)[1]
true
else
warnings ?= []
warnings.push "Issue ##{number} has multiple matching size labels"
true
cb null, warnings, filtered, total
# The total size of all issues.
total = 0
catch err
return cb err, warnings
filtered = _.filter collection, (issue) ->
# Skip if no labels exist.
return no unless labels = issue.labels
# Determine the total issue size from all labels.
issue.size = _.reduce labels, (sum, label) ->
# Not matching.
return sum unless matches = label.name.match(regex)
# Increase sum.
sum += parseInt matches[1]
, 0
# Increase the total.
total += issue.size
# Are we saving it?
!!issue.size
cb null, filtered, total

View File

@ -149,41 +149,42 @@ module.exports =
'issues - filter on existing label regex': (done) ->
issues.filter [ { labels: [ { name: 'size 15' } ] } ]
, regex.size_label, (err, warn, data) ->
, regex.size_label, (err, data) ->
assert.ifError err
assert.ifError warn
assert.equal data.length, 1
assert.equal data[0].size, 15
do done
'issues - filter when no labels': (done) ->
issues.filter [ { } ]
, regex.size_label, (err, warn, data) ->
, regex.size_label, (err, data) ->
assert.ifError err
assert.ifError warn
assert.equal data.length, 0
do done
'issues - filter when empty labels': (done) ->
issues.filter [ { labels: [] } ]
, regex.size_label, (err, warn, data) ->
, regex.size_label, (err, data) ->
assert.ifError err
assert.ifError warn
assert.equal data.length, 0
do done
'issues - filter when not matching regex': (done) ->
issues.filter [ { labels: [ { name: 'size 1A' } ] } ]
, regex.size_label, (err, warn, data) ->
, regex.size_label, (err, data) ->
assert.ifError err
assert.ifError warn
assert.equal data.length, 0
do done
'issues - filter when multiple match the regex': (done) ->
issues.filter [ { labels: [ { name: 'size 1' }, { name: 'size 6' } ] } ]
, regex.size_label, (err, warn, data) ->
issues.filter [
{ labels: [ { name: 'size 1' }, { name: 'size 6' } ] }
{ labels: [ { name: 'size really big' }, { name: 'size 4' } ] }
]
, regex.size_label, (err, data) ->
assert.ifError err
assert.equal warn.length, 1
assert.equal data.length, 1
assert.equal data.length, 2
[ a, b ] = data
assert.equal a.size, 7
assert.equal b.size, 4
do done