diff --git a/mass-rebuilder.php b/mass-rebuilder.php index 86ed1f9..efb63ed 100644 --- a/mass-rebuilder.php +++ b/mass-rebuilder.php @@ -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)) { diff --git a/src/RebuildTagClassifier.php b/src/RebuildTagClassifier.php new file mode 100644 index 0000000..bcaebc4 --- /dev/null +++ b/src/RebuildTagClassifier.php @@ -0,0 +1,62 @@ + $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 {} \ No newline at end of file diff --git a/src/TestRebuildTagClassifier.php b/src/TestRebuildTagClassifier.php new file mode 100644 index 0000000..cc95a93 --- /dev/null +++ b/src/TestRebuildTagClassifier.php @@ -0,0 +1,83 @@ +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); + } +} \ No newline at end of file