Commit 88d7df52 authored by Aaron Wells's avatar Aaron Wells

Refactoring "gerrit_query.php" into mahara_jenkins.php

Since they're both PHP now, there's no need to invoke
gerrit_query.php in a separate process.
parent d6bfaf81
<?php
/**
* Using php to do the json_decode / checking of patch status
* as it is easier than trying to get bash to do it
*/
$git_commit_id = $argv[2];
$first_commit = $argv[3];
$gerrit_branch = $argv[4];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://reviews.mahara.org/changes/?q=' . $git_commit_id . '&o=LABELS&o=CURRENT_REVISION&pp=0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);
// We need to fetch the json line from the result
$content = explode("\n", $content);
$content = json_decode($content[1]);
// Find the array hash that relates to this check
$k = 0;
foreach ($content as $key => $item) {
if ($item->branch == $gerrit_branch) {
$k = $key;
}
}
// Doublecheck to see if this has already been merged
if ($content[$k]->status == 'MERGED') {
echo 2;
exit;
}
if ($content[$k]->status == 'ABANDONED') {
echo 4;
exit;
}
// Check that the patch we are testing is the latest (current) patchset in series
if ($content[$k]->current_revision != $git_commit_id) {
echo 3;
exit;
}
// Now check to see if anyone has rejected this.
// We don't want to reject the patch if auto test has failed as we are re-testing now
// but we do want to reject patch if the parent has failed auto test.
if (!empty($first_commit) && empty($content[$k]->labels->{'Verified'}->rejected) &&
empty($content[$k]->labels->{'Code-Review'}->rejected)) {
echo 0;
}
else if (empty($content[$k]->labels->{'Verified'}->rejected) &&
empty($content[$k]->labels->{'Code-Review'}->rejected) &&
empty($content[$k]->labels->{'Automated-Tests'}->rejected)) {
echo 0;
}
else {
echo 1;
}
?>
\ No newline at end of file
......@@ -59,35 +59,84 @@ echo "\n";
# Fetch the git commit ids that exist between this commit and the origin
# that exists when the patch was made.
$headcommithash = shell_exec_or_die("git rev-parse HEAD");
exec_or_die("git log --pretty=format:'%H' origin/$GERRIT_BRANCH..$headcommithash", $the_list);
$firstcommit = 1;
exec_or_die("git log --pretty=format:'%H' origin/$GERRIT_BRANCH..$headcommithash", $commitancestors);
if (empty($commitancestors)) {
// No ancestors means this commit is the head of the branch, or is an ancestor of the branch.
// In which case... well, it's not really either a pass or a failure. But a pass makes more sense.
echo "Patch already merged\n";
exit(0);
}
$firstcommit = true;
$i = 0;
foreach ($commitancestors as $commit) {
$commit = trim($commit);
$i++;
$content = gerrit_query('/changes/?q=commit:' . $commit . '+branch:' . $GERRIT_BRANCH . '&o=LABELS&o=CURRENT_REVISION&pp=0');
// Because we queried by commit and branch, should return exactly one record.
$content = $content[0];
// Doublecheck to see if this has already been merged
if ($content->status == 'MERGED') {
// If this commit has been merged, then there's no reason to check it or any earlier ones.
break;
}
$myurl = 'https://reviews.mahara.org/' . $content->_number;
// Check that the patch we are testing is the latest (current) patchset in series
if ($content->current_revision != $commit) {
if ($firstcommit) {
echo "This patch is not the latest (current) patch in its Gerrit change set";
}
else {
echo "This patch is descended from a patch that is not the latest (current) patch in its Gerrit change set: $myurl\n";
}
exit(1);
}
if ($content->status == 'ABANDONED') {
if ($firstcommit) {
echo "This patch has been abandoned.\n";
}
else {
echo "This patch is descended from abandoned Gerrit patch: $myurl\n";
}
exit(1);
}
if (!empty($content->labels->{'Verified'}->rejected)) {
if ($firstcommit) {
echo "This patch has failed manual testing.\n";
} else {
echo "This patch is descended from a patch that has failed manual testing: $myurl\n";
}
exit(1);
}
foreach ($the_list as $line) {
$line = trim($line);
if (empty($line)) {
echo "Patch already merged\n";
if (!empty($content->labels->{'Code-Review'}->rejected)) {
if ($firstcommit) {
echo "This patch has failed code review.\n";
} else {
echo "This patch is descended from a patch that has failed code review: $myurl\n";
}
exit(1);
}
# check if the commit or it's parents have been rejected
$outcome = shell_exec_or_die(PHP_BINARY . " $HOME/mahara/mahara-scripts/jenkins/gerrit_query.php -- $line $firstcommit $GERRIT_BRANCH");
switch ($outcome) {
case 1:
echo "The patch with git commit id $line has been rejected\n";
exit(1);
break;
case 3:
echo "The patch with git commit id $line is not the latest (current) patch\n";
exit(1);
break;
case 4:
echo "This patch or a parent patch has been abandoned\n";
exit(1);
break;
default:
echo "The patch with git commit id $line looks ok so we will continue\n";
if (!$firstcommit && !empty($content->labels->{'Automated-Tests'}->rejected)) {
echo "This patch is descended from a patch that has failed automated testing: $myurl\n";
exit(1);
}
// SUCCESS!
if ($firstcommit) {
echo "$i. This patch is ready for automated testing, so we will continue\n";
}
else {
echo "$i. Ancestor patch with git commit id $commit looks ok so we will continue\n";
}
$firstcommit = 0;
$firstcommit = false;
}
echo "\n";
......@@ -220,4 +269,24 @@ function log_and_die($commandtolog, $itsreturnvar) {
debug_print_backtrace();
echo "\n";
exit(1);
}
/**
* Make an unauthenticated request to gerrit's REST service.
*
* @param string $relurl The relative URL of the REST service. URL query component should include 'pp=0'
*/
function gerrit_query($relurl) {
$ch = curl_init();
if ($relurl[0] !== '/') {
$relurl = '/' . $relurl;
}
curl_setopt($ch, CURLOPT_URL, 'https://reviews.mahara.org' . $relurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);
// We need to fetch the json line from the result
$content = explode("\n", $content);
return json_decode($content[1]);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment