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 (796444bd86462a526eff60a0f8dfefd00427ea06) (3,550B) (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("Creating user...");
rg_test_create_user($db, $rg_ui);


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


rg_log('');
rg_log("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('');
rg_log('Do an anonymous push...');
system('rm -rf pr_anon.git');
system('git init pr_anon.git');
system('cd pr_anon.git; echo "change1" > a');
system('cd pr_anon.git; git add a; git commit -m "change1 desc"');
system('cd pr_anon.git; echo "change2" > a; git commit -a -m "change2 desc"');
//system('cd pr_anon.git; git remote add origin '
//	. ' ssh://rocketgit@' . $rg_ssh_host . ':' . $rg_ssh_port
//	. '/user/' . escapeshellarg($rg_ui['username']) . '/'
//	. escapeshellarg($repo['name']));
system('cd pr_anon.git; git remote add origin '
	. ' git://' . $rg_git_host . ':' . $rg_git_port
	. '/user/' . escapeshellarg($rg_ui['username']) . '/'
	. escapeshellarg($repo['name']));
system('cd pr_anon.git; git push origin master');


rg_log('');
rg_log('Check if the pull request has been done...');
$tries = 10;
while ($tries > 0) {
	$sql = 'SELECT * FROM merge_requests WHERE repo_id = ' . $repo['repo_id'];
	$res = rg_sql_query($db, $sql);
	$rows = rg_sql_num_rows($res);
	if ($rows > 0)
		$row = rg_sql_fetch_array($res);
	rg_sql_free_result($res);
	if ($rows > 0)
		break;

	sleep(1);
	$tries--;
}
if ($rows == 0) {
	rg_log('Seems the pull request did not hit the database!');
	exit(1);
}
$ns = $row['namespace'];
$ref = $row['refname'];
if (strcmp($ref, 'refs/heads/master') != 0) {
	rg_log('Seems the ref is not master: ' . $ref . '!');
	exit(1);
}


rg_log('');
rg_log('Login, so we can load the merge request...');
$r = test_login($test_url, $rg_ui, $good_sid);
if ($r === FALSE) {
	rg_log("Cannot login!");
	exit(1);
}


rg_log('');
rg_log('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'], '>' . $ns . '<')) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('namespace link not found!');
	exit(1);
}


rg_log('');
rg_log('Loading the merge request specific page');
$data = array();
$headers = array("Cookie: sid=" . $good_sid);
$r = do_req($test_url . '/user/' . $rg_ui['username']
	. '/' . $repo['name'] . '/mr/' . $ns . '?t=pr_anon', $data, $headers);
rg_log_ml('r: ' . print_r($r, TRUE));


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