Remove dead PHP code

This commit is contained in:
Graham Christensen 2017-12-19 21:57:30 -05:00
parent 43dba9c53d
commit 0e3e1a978f
No known key found for this signature in database
GPG key ID: ACA1C1D120C83D5C
10 changed files with 1 additions and 481 deletions

View file

@ -21,11 +21,6 @@ echo "hi\n";
function outrunner($msg) {
try {
runner($msg);
} catch (\GHE\ExecException $e) {
var_dump($msg);
var_dump($e->getMessage());
var_dump($e->getCode());
var_dump($e->getOutput());
} catch (\PhpAmqpLib\Exception\AMQPProtocolChannelException $e) {
echo "Channel exception:\n";
var_dump($e);

View file

@ -76,13 +76,7 @@ function reply_to_issue($body, $output, $success, $system) {
function outrunner($msg) {
try {
return runner($msg);
} catch (GHE\ExecException $e) {
var_dump($e->getMessage());
var_dump($e->getCode());
var_dump($e->getOutput());
}
return runner($msg);
}

View file

@ -10,32 +10,7 @@ class ACL {
];
}
static public function getUsers() {
return [
'7c6f434c',
'copumpkin',
'disassembler',
'domenkozar',
'fpletz',
'fridh',
'globin',
'grahamc',
'lnl7',
'mic92',
'orivej',
'shlevy',
];
}
static public function isRepoEligible($repo) {
return in_array(strtolower($repo), self::getRepos());
}
static public function isUserAuthorized($user) {
return in_array(strtolower($user), self::getUsers());
}
static public function authorizeUserRepo($user, $repo) {
return self::isRepoEligible($repo) && self::isUserAuthorized($user);
}
}

View file

@ -1,119 +0,0 @@
<?php
namespace GHE;
class Checkout {
protected $root;
protected $type;
function __construct($root, $type) {
$this->root = $root;
$this->type = $type;
}
function checkOutRef($repo_name, $clone_url, $id, $ref) {
$this->prefetchRepoCache($repo_name, $clone_url);
$pname = $this->pathToRepoCache($repo_name);
$bname = $this->pathToBuildDir($repo_name, $id);
$guard = $this->guard($bname);
if (!is_dir($bname)) {
echo "Cloning https://github.com/" . $repo_name . "/pull/" . $id . " to $bname\n";
Exec::exec('git clone --shared --reference-if-able %s %s %s',
[
$pname,
$clone_url,
$bname
]);
}
if (!chdir($bname)) {
throw new CoFailedException("Failed to chdir to $bname\n");
}
echo "fetching https://github.com/" . $repo_name . "/pull/" . $id . " in $bname\n";
Exec::exec('git fetch origin');
try {
Exec::exec('git am --abort');
} catch (ExecException $e) {
// non-zero exit if no am is in progress
}
Exec::exec('git reset --hard %s', [$ref]);
$this->release($guard);
return $bname;
}
function applyPatches($bname, $patch_url) {
if (!chdir($bname)) {
throw new CoFailedException("Failed to chdir to $bname\n");
}
$guard = $this->guard($bname);
Exec::exec('curl -L %s | git am --no-gpg-sign -', [$patch_url]);
$this->release($guard);
}
function prefetchRepoCache($name, $clone_url) {
if (!chdir($this->root)) {
throw new CoFailedException("Failed to chdir to " . $this->root);
}
$pname = $this->pathToRepoCache($name);
$guard = $this->guard($pname);
if (!is_dir($pname)) {
echo "Cloning " . $name . " to $pname\n";
Exec::exec('git clone --bare %s %s',
[
$clone_url,
$pname
]);
}
$this->release($guard);
if (!chdir($pname)) {
throw new CoFailedException("Failed to chdir to $pname");
}
echo "Fetching $name in $pname\n";
Exec::exec('git fetch origin');
}
function pathToRepoCache($name) {
return $this->root . "/repo-" . md5($name);
}
function pathToBuildDir($repo, $id_number) {
$id = (int) $id_number;
$repo_hash = md5($repo);
$type = $this->type;
return $this->root . "/$type-$repo_hash-$id";
}
function guard($path) {
echo "about to lock $path\n";
$res = fopen("$path.lock", 'c');
while (!flock($res, LOCK_EX)) {
echo "waiting for lock on $path...\n";
sleep(1);
}
echo "got lock on $path\n";
return $res;
}
function release($res) {
fclose($res);
}
}
class CoFailedException extends \Exception {}

View file

@ -1,49 +0,0 @@
<?php
namespace GHE;
class CommitStatus {
protected $ghclient;
protected $owner;
protected $repo;
protected $sha;
protected $name;
function __construct($ghclient, $owner, $repo, $sha, $name) {
$this->ghclient = $ghclient;
$this->owner = $owner;
$this->repo = $repo;
$this->sha = $sha;
$this->name = $name;
}
public function pending($description) {
$this->mark('pending', $description);
}
public function error($description) {
$this->mark('error', $description);
}
public function failure($description) {
$this->mark('failure', $description);
}
public function success($description) {
$this->mark('success', $description);
}
public function mark($state, $description) {
$this->ghclient->api('repository')->statuses()->create(
$this->owner,
$this->repo,
$this->sha,
[
'state' => $state,
'context' => $this->name,
'description' => $description,
]
);
}
}

View file

@ -1,42 +0,0 @@
<?php
namespace GHE;
class Exec {
public static function exec($cmd, $args = array()) {
$safeArgs = array_map('escapeshellarg', $args);
$interiorCmd = vsprintf($cmd, $safeArgs);
$exteriorCmd = sprintf('/bin/sh -o pipefail -euc %s 2>&1',
escapeshellarg($interiorCmd));
exec($exteriorCmd, $output, $return);
if ($return > 0) {
throw new ExecException($cmd, $args, $output, $return);
}
return $output;
}
}
class ExecException extends \Exception {
protected $args;
protected $output;
public function __construct($cmd, $args, $output, $return) {
$this->args = $args;
$this->output = $output;
parent::__construct("Error calling $cmd", $return);
}
public function getArgs() {
return $this->args;
}
public function getOutput() {
return $this->output;
}
}

View file

@ -1,3 +0,0 @@
<?php
namespace GHE;

View file

@ -1,62 +0,0 @@
<?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 > 0) {
$suffix = "1-10";
} else {
$suffix = "0";
}
return $prefix . $suffix;
}
}
class RebuildTagClassifierArchException extends \Exception {}

View file

@ -1,68 +0,0 @@
<?php
namespace GHE;
class TestExec extends \PHPUnit\Framework\TestCase
{
/** Exec::exec('curl -L %s | git am --no-gpg-sign -');
*/
function testExecBasic() {
$this->assertEquals(
['oof'],
Exec::exec('echo foo | rev')
);
}
function testExecArgs() {
$this->assertEquals(
['rab'],
Exec::exec('echo %s | rev', ['bar'])
);
}
function testExecArgsDangerous() {
$this->assertEquals(
['$(whoami)'],
Exec::exec('echo %s', ['$(whoami)'])
);
}
function testExecFailureExceptions() {
$this->expectException(ExecException::class);
$this->expectExceptionCode(123);
$this->expectExceptionMessage("Error calling exit 123");
Exec::exec('exit 123');
}
function testExecFailureExceptionsOutput() {
try {
Exec::exec('echo %s; exit %s', ["heya", 10]);
$this->assertFalse(true, "Should have excepted!");
} catch (ExecException $e) {
$this->assertEquals(10, $e->getCode());
$this->assertEquals(["heya", 10], $e->getArgs());
$this->assertEquals(["heya"], $e->getOutput());
}
}
function testExecFailureExceptionPipefailEnd() {
try {
var_dump(Exec::exec('echo "foo" | (exit 2);'));
$this->assertFalse(true, "Should have excepted!");
} catch (ExecException $e) {
$this->assertEquals(2, $e->getCode());
}
}
function testExecFailureExceptionPipefailStart() {
try {
var_dump(Exec::exec('(echo "foo"; exit 3) | rev;'));
$this->assertFalse(true, "Should have excepted!");
} catch (ExecException $e) {
$this->assertEquals(3, $e->getCode());
}
}
}

View file

@ -1,101 +0,0 @@
<?php
namespace GHE;
class TestRebuildTagClassifier extends \PHPUnit\Framework\TestCase
{
function testParseLabelJustOne() {
$this->assertEquals(
["10.rebuild-linux: 1-10", "10.rebuild-darwin: 0"],
RebuildTagClassifier::parseAndLabel([
"Estimating rebuild amount by counting changed Hydra jobs.",
" 1 x86_64-linux",
]));
}
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 testParseJustOne() {
$this->assertEquals(
["x86_64-linux" => 1],
RebuildTagClassifier::parse([
"Estimating rebuild amount by counting changed Hydra jobs.",
" 1 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);
}
}