xaizek / rocketgit (License: AGPLv3+) (since 2018-12-09)
Light and fast Git hosting solution suitable to serve both as a hub or as a personal code storage with its tickets, pull requests, API and much more.
<root> / tests / pr_anon.php (e88c08eea31c0846f2b13d1d4b15df601342b40c) (7,034B) (mode 100644) [raw]
<?php
//
// Tests pull requests done by anonymous push
//
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");

$INC = dirname(__FILE__) . "/../inc";
require_once(dirname(__FILE__) . "/config.php");
require_once($INC . "/init.inc.php");
require_once($INC . "/user.inc.php");
require_once("helpers.inc.php");
require_once("http.inc.php");

rg_log_set_file("pr_anon.log");

$rg_sql = "host=localhost user=rocketgit dbname=rocketgit connect_timeout=10";
$rg_no_db = TRUE;
require_once("common.php");

$_testns = 'pr_anon';
$rg_cache_enable = TRUE;
$rg_cache_debug = TRUE;
$rg_event_socket = "/var/lib/rocketgit/sockets/event.sock";


rg_log('');
rg_log_enter('Creating user...');
rg_test_create_user($db, $rg_ui);
rg_log_exit();


rg_log('');
rg_log_enter('Login...');
$r = test_login($test_url, $rg_ui, $good_sid);
if ($r === FALSE) {
	rg_log("Cannot login!");
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Creating and upload a ssh key...');
rg_test_upload_ssh_key($db, $rg_ui, 'pr_anon', $good_sid);
rg_log_exit();


rg_log('');
rg_log_enter('Creating a repo');
$repo = array('repo_id' => 0, 'public' => 1);
rg_test_create_repo($db, $rg_ui, $repo);
rg_log_exit();


$url = '/user/' . $rg_ui['username'] . '/' . $repo['name'] . '/mr/';


// TODO: still needed?! Now, we have it by default.
if (0) {
rg_log('');
rg_log_enter("Giving anon push rights to any user");
$x = array();
$x['right_id'] = 0;
$x['who'] = $rg_ui['uid'];
$x['obj_id'] = $repo['repo_id'];
$x['uid'] = 0; // any user
$x['rights'] = 'FH'; // fetch and anon push
$x['misc'] = ''; // all refs
$x['ip'] = ''; // any IP
$x['prio'] = 100;
$x['description'] = 'bla bla bla';
$r = rg_rights_set($db, 'repo_refs', $x);
if ($r !== TRUE) {
	rg_log("We cannot give rights to the user");
	exit(1);
}
rg_log_exit();
}


rg_log('');
rg_log_enter('Preparing repo...');
system('rm -rf _pr_anon.git 2>/dev/null');
$r = rg_exec('git init _pr_anon.git', '', FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Could not init repo!');
	exit(1);
}
$r = rg_exec('cd _pr_anon.git; git remote add origin_ssh '
	. ' ssh://rocketgit@' . $rg_ssh_host . ':' . $rg_ssh_port
	. '/user/' . escapeshellarg($rg_ui['username']) . '/'
	. escapeshellarg($repo['name']), '', FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Could not add ssh remote!');
	exit(1);
}
$r = rg_exec('cd _pr_anon.git; git remote add origin_git '
	. ' git://' . $rg_git_host . ':' . $rg_git_port
	. '/user/' . escapeshellarg($rg_ui['username']) . '/'
	. escapeshellarg($repo['name']), '', FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Could not add git remote!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Do a non-anonymous push...');
$r = rg_exec('export GIT_SSH_COMMAND="ssh -o IdentityFile=../keys/pr_anon'
	. ' -o IdentitiesOnly=yes";'
	. 'cd _pr_anon.git; echo "change1" > a;'
	. 'git add a; git commit -m "change1 desc";'
	. 'echo "change2" > a; git commit -a -m "change2 desc";'
	. 'git push origin_ssh master', '', FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Non-anonymous push was rejected!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Do an anonymous push...');
$r = rg_exec('cd _pr_anon.git; echo "change3" >> a;'
	. 'git add a; git commit -m "anon change1 desc";'
	. 'echo "change4" >> a; git commit -a -m "anon change2 desc";'
	. 'git push origin_git master', '', FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Anonymous push was rejected!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Check if merge is in db and is against correct branch...');
$mri = rg_test_mr_in_db($db, $repo['repo_id']);
if (strcmp($mri['refname'], 'refs/heads/master') != 0) {
	rg_log_ml('mri: ' . print_r($mri, TRUE));
	rg_log('Seems the ref is not master: ' . $mri['refname'] . '!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Loading the merge requests page - just to see it appears there');
$data = array();
$headers = array("Cookie: sid=" . $good_sid);
$r = do_req($test_url . '/user/' . $rg_ui['username']
	. '/' . $repo['name'] . '/mr?t=pr_anon', $data, $headers);
if (!strstr($r['body'], '>' . $mri['id'] . '<')) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('id link not found!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Loading the merge request specific page');
$data = array();
$headers = array("Cookie: sid=" . $good_sid);
$r = do_req($test_url . $url . $mri['id'] . '?t=pr_anon', $data, $headers);
if (!strstr($r['body'], 'This pull request can be merged without conflicts')) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Pull request does not appear as mergeable');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Merging pull request...');
$data = array('token' => $r['tokens']['mr_merge'],
	'merge_ff' => 0, 'merge_msg' => 'This is the merge message <xss>');
$r = do_req($test_url . $url . $mri['id'] . '/merge', $data, $headers);
if ($r === FALSE) {
	rg_log('Cannot post merge form');
	exit(1);
}
$tries = 0;
while (1) {
	$sql = 'SELECT * FROM merge_requests'
		. ' WHERE repo_id = ' . $repo['repo_id']
		. ' AND done > 0 AND id = ' . $mri['id'];
	$res = rg_sql_query($db, $sql);
	$rows = rg_sql_num_rows($res);
	rg_sql_free_result($res);
	if ($rows == 1)
		break;

	if ($tries == 10) {
		rg_log('merge_request was not marked as done!');
		exit(1);
	}
	$tries++;
	sleep(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Now, try to see what happens when a merge is with conflicts...');
$r = rg_exec('cd _pr_anon.git;'
	. 'git pull origin_git master;'
	. 'echo "change2" > a;'
	. 'git commit -a -m "conflict1b";'
	. 'git push origin_git master', '', FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Could not pull/commit/push by git proto!');
	exit(1);
}
$r = rg_exec('export GIT_SSH_COMMAND="ssh -o IdentityFile=../keys/pr_anon'
	. ' -o IdentitiesOnly=yes";'
	. 'cd _pr_anon.git;'
	. 'git reset --hard HEAD^1;'
	. ' echo "change1" > a;'
	. 'git commit -a -m "conflict1a";'
	. 'git push origin_ssh master', '', FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Could not reset/commit/push by ssh proto!');
	exit(1);
}
$mri = rg_test_mr_in_db($db, $repo['repo_id']);
$data = array();
$headers = array("Cookie: sid=" . $good_sid);
$r = do_req($test_url . $url . $mri['id'] . '?t=pr_anon', $data, $headers);
if (!strstr($r['body'], 'This pull request cannot be merged without conflicts')) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Pull request does not appear as non-mergeable');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Loading conflicts page...');
$data = array();
$headers = array("Cookie: sid=" . $good_sid);
$r = do_req($test_url . $url . $mri['id'] . '?t=pr_anon', $data, $headers);
if (!strstr($r['body'], 'Conflicts:')) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Errors in conflicts page!');
	exit(1);
}
rg_log_exit();

rg_log("OK!");
?>
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/rocketgit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/rocketgit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master