Skip to content

Instantly share code, notes, and snippets.

Created January 3, 2012 16:36
Show Gist options
  • Save anonymous/1555672 to your computer and use it in GitHub Desktop.
Save anonymous/1555672 to your computer and use it in GitHub Desktop.
<?php
require_once '/var/lib/jenkins/github/lib/Github/Autoloader.php';
Github_Autoloader::register();
$github = new Github_Client();
$account = "kohana";
$repos = array("kohana", "core", "auth", "cache", "codebench", "database", "image", "orm", "unittest", "userguide", "minion");
$username = "Kohana-Builds";
$secret = "XXX";
$branches = array(
"3.1/develop" => array(
'trigger_url' => 'http://ci.kohanaframework.org/job/kohana-3.1-pull/buildWithParameters?token=XXX&GITHUB_TOKEN='.$secret.'&GITHUB_USERNAME='.$username.'&GITHUB_ACCOUNT='.$account.'&',
),
"3.2/develop" => array(
'trigger_url' => 'http://ci.kohanaframework.org/job/kohana-3.2-pull/buildWithParameters?token=XXX&GITHUB_TOKEN='.$secret.'&GITHUB_USERNAME='.$username.'&GITHUB_ACCOUNT='.$account.'&',
),
"3.3/develop" => array(
'trigger_url' => 'http://ci.kohanaframework.org/job/kohana-3.3-pull/buildWithParameters?token=XXX&GITHUB_TOKEN='.$secret.'&GITHUB_USERNAME='.$username.'&GITHUB_ACCOUNT='.$account.'&',
),
);
foreach ($repos as $repo)
{
try
{
$pulls = $github->getPullRequestApi()->listPullRequests($account, $repo, 'open');
}
catch (Exception $e)
{
echo "Failed to list pull requests. $repo".$e->getMessage()."\n";
sleep(20);
continue;
}
try
{
foreach ($pulls as $pull)
{
try
{
$pull = $github->getPullRequestApi()->show($account, $repo, $pull['number']);
}
catch (Exception $e)
{
echo "Failed to show pull request. $repo / ".$pull['number']." ".$e->getMessage()."\n";
sleep(5);
continue;
}
$needs_testing = TRUE;
foreach ($pull["discussion"] as $comment)
{
if ($comment["type"] == "Commit")
{
$needs_testing = TRUE;
}
elseif ($comment["type"] == "IssueComment")
{
if ($comment["user"]["login"] == $username)
{
$needs_testing = FALSE;
}
// Allow builds to be forced
if (strpos($comment["body"], "!build") !== FALSE)
{
$needs_testing = TRUE;
}
}
}
if ($needs_testing)
{
if ( ! array_key_exists($pull["base"]["ref"], $branches))
{
comment_on_pull($account, $repo, $pull['number'], "Pull request is targeted towards an invalid branch (".$pull["base"]["ref"]."). Usually, this means you have targeted the master, rather than develop branch. Please correct this and try again. Thanks!");
close_issue($account, $repo, $pull['number']);
}
elseif ( ! $pull["mergeable"])
{
echo "Pull request ".$account."/".$repo."/".$pull['number']." is not mergeable, skipping comment due to github API weirdness.\n";
// comment_on_pull($account, $repo, $pull['number'], "Pull request is no longer mergeable. Please update the pull request (if it is still relevant). Thanks :)");
}
else
{
comment_on_pull($account, $repo, $pull['number'], "Build Scheduled");
trigger_build($account, $repo, $pull["base"]["ref"], $pull['number']);
}
}
sleep(0.2);
}
}
catch (Exception $e)
{
sleep(5);
echo "Uncaught Exception. $repo / ".$pull['number']." : ".$e->getMessage()."\n";
}
}
function close_issue($account, $repo, $number)
{
// Eww.. Whatever, this enture thing is eww.
global $username, $secret, $github;
echo "Closing pull request $account / $repo / $number\n";
$github->authenticate($username, $secret);
$github->getIssueApi()->close($account, $repo, $number);
}
function comment_on_pull($account, $repo, $number, $message)
{
// Eww.. Again? Really :'(
global $username, $secret, $github;
echo "Commenting on pull request $account / $repo / $number : ".$message."\n";
$github->authenticate($username, $secret);
$github->getIssueApi()->addComment($account, $repo, $number, $message);
}
function trigger_build($account, $repo, $branch, $number)
{
// Okay, I give up. I can't hide it anymore, I <3 globals.
global $branches;
echo "Triggering build $account / $repo / $number\n";
$url = $branches[$branch]['trigger_url'].'GITHUB_PULL_MODULE='.$repo.'&GITHUB_PULL_NUMBER='.$number;
file_get_contents($url);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment