Refactor the labeler to have a 0label

This commit is contained in:
Graham Christensen 2017-11-01 11:48:27 -04:00
parent 2395c403d6
commit cc3ae846f4
No known key found for this signature in database
GPG key ID: ACA1C1D120C83D5C
3 changed files with 146 additions and 34 deletions

View file

@ -100,40 +100,7 @@ function reply_to_issue($repo, $pr, $prev, $current) {
]
);
$labels = [];
foreach ($output as $line) {
if (preg_match('/^\s*(\d+) (.*)$/', $line, $matches)) {
var_dump($matches);
# TODO: separate out the rebuild ranges from the rebuild platform and
# splice the string together, rather than this ugliness
if ($matches[1] > 500) {
if ($matches[2] == "x86_64-darwin") {
$labels[] = "10.rebuild-darwin: 501+";
} else {
$labels[] = "10.rebuild-linux: 501+";
}
} else if ($matches[1] > 100 && $matches[1] <= 500) {
if ($matches[2] == "x86_64-darwin") {
$labels[] = "10.rebuild-darwin: 101-500";
} else {
$labels[] = "10.rebuild-linux: 101-500";
}
} else if ($matches[1] > 10 && $matches[1] <= 100) {
if ($matches[2] == "x86_64-darwin") {
$labels[] = "10.rebuild-darwin: 11-100";
} else {
$labels[] = "10.rebuild-linux: 11-100";
}
} else if ($matches[1] <= 10) {
if ($matches[2] == "x86_64-darwin") {
$labels[] = "10.rebuild-darwin: 1-10";
} else {
$labels[] = "10.rebuild-linux: 1-10";
}
}
}
}
$labels = GHE\RebuildTagClassifier::parseAndLabel($output);
foreach ($labels as $label) {
if (in_array($label, $already_there)) {

View file

@ -0,0 +1,62 @@
<?php
namespace GHE;
class RebuildTagClassifier {
public static function parseAndLabel($text) {
$counts = self::parse($text);
if (!isset($counts['x86_64-darwin'])) {
$counts['x86_64-darwin'] = 0;
}
if (!isset($counts['x86_64-linux'])) {
$counts['x86_64-linux'] = 0;
}
$labels = [];
foreach ($counts as $arch => $count) {
$label[] = self::labelForArchCount($arch, $count);
}
return $label;
}
public static function parse($output) {
$counts = [];
foreach ($output as $line) {
if (preg_match('/^\s*(\d+) (.*)$/', $line, $matches)) {
$counts[$matches[2]] = (int)$matches[1];
}
}
return $counts;
}
public static function labelForArchCount($arch, $count) {
if ($arch == "x86_64-linux") {
$prefix = "10.rebuild-linux: ";
} elseif ($arch == "x86_64-darwin") {
$prefix = "10.rebuild-darwin: ";
} else {
throw new RebuildTagClassifierArchException("Unknown arch $arch");
}
if ($count > 500) {
$suffix = "501+";
} else if ($count > 100) {
$suffix = "101-500";
} else if ($count > 10) {
$suffix = "11-100";
} else if ($count > 1) {
$suffix = "1-10";
} else {
$suffix = "0";
}
return $prefix . $suffix;
}
}
class RebuildTagClassifierArchException extends \Exception {}

View file

@ -0,0 +1,83 @@
<?php
namespace GHE;
class TestRebuildTagClassifier extends \PHPUnit\Framework\TestCase
{
function testExecParseAndLabelGarbage() {
$this->assertEquals(
["10.rebuild-darwin: 0", "10.rebuild-linux: 0", ],
RebuildTagClassifier::parseAndLabel(["foo", "bar"])
);
}
function testExecParseAndLabelLinuxOnly() {
$this->assertEquals(
["10.rebuild-linux: 1-10", "10.rebuild-darwin: 0", ],
RebuildTagClassifier::parseAndLabel([" 5 x86_64-linux"])
);
}
function testExecParseAndLabelDarwinOnly() {
$this->assertEquals(
["10.rebuild-darwin: 1-10", "10.rebuild-linux: 0", ],
RebuildTagClassifier::parseAndLabel([" 5 x86_64-darwin"])
);
}
function testExecParseAndLabelLinuxAndDarwin() {
$this->assertEquals(
["10.rebuild-linux: 1-10", "10.rebuild-darwin: 11-100", ],
RebuildTagClassifier::parseAndLabel([" 5 x86_64-linux", " 17 x86_64-darwin"])
);
}
function testExecParseNone() {
$this->assertEquals(
[],
RebuildTagClassifier::parse([])
);
}
function testExecParseGarbage() {
$this->assertEquals(
[],
RebuildTagClassifier::parse(["foo", "bar"])
);
}
function testExecParseLinuxOnly() {
$this->assertEquals(
["x86_64-linux" => 5],
RebuildTagClassifier::parse([" 5 x86_64-linux"])
);
}
function testExecParseDarwinOnly() {
$this->assertEquals(
["x86_64-darwin" => 5],
RebuildTagClassifier::parse([" 5 x86_64-darwin"])
);
}
function testExecParseLinuxAndDarwin() {
$this->assertEquals(
["x86_64-linux" => 5, "x86_64-darwin" => 17],
RebuildTagClassifier::parse([" 5 x86_64-linux", " 17 x86_64-darwin"])
);
}
function testLabelForArchCount() {
$this->assertEquals("10.rebuild-linux: 501+", RebuildTagClassifier::labelForArchCount("x86_64-linux", 501));
$this->assertEquals("10.rebuild-linux: 101-500", RebuildTagClassifier::labelForArchCount("x86_64-linux", 150));
$this->assertEquals("10.rebuild-darwin: 101-500", RebuildTagClassifier::labelForArchCount("x86_64-darwin", 150));
}
function testLabelForUnknownArch() {
$this->expectException(RebuildTagClassifierArchException::class);
RebuildTagClassifier::labelForArchCount("lmao", 150);
}
}