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 (078a6983366397b86bfc4f4055605b9245a3d7ba) (5,379B) (mode 100644) [raw]
<?php
//
// Tests merge requests done by anonymous push
//
error_reporting(E_ALL | E_STRICT);
ini_set("track_errors", "On");

$test_normal = TRUE;

$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");
require_once("mr.inc.php");

rg_log_set_file("pr_anon.log");

require_once("common.php");

$_testns = 'pr_anon';


prepare_http();

rg_test_create_user($db, $rg_ui);


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


rg_test_upload_ssh_key($db, $rg_ui, 'pr_anon', $kn);

$ssh = 'ssh -o ControlMaster=no -o IdentityFile=../../keys/' . $kn
	. ' -o IdentitiesOnly=yes';
putenv('GIT_SSH_COMMAND=' . $ssh);

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/';


rg_log('');
rg_log_enter('Preparing repo...');
$r = rg_exec('mkdir -p temp_repos && cd temp_repos'
	. ' && rm -rf pr_anon'
	. ' && mkdir pr_anon'
	. ' && cd pr_anon'
	. ' && git init'
	. ' && git remote add origin_ssh '
	. ' ssh://rocketgit@' . $rg_ssh_host . ':' . $rg_ssh_port
	. '/user/' . escapeshellarg($rg_ui['username']) . '/'
	. escapeshellarg($repo['name'])
	. ' && git remote add origin_git '
	. ' git://' . $rg_git_host . ':' . $rg_git_port
	. '/user/' . escapeshellarg($rg_ui['username']) . '/'
	. escapeshellarg($repo['name']),
	'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log('Could not prepare git repo: ' . $r['errmsg'] . '!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Do a merge request against an empty repo...');
file_put_contents('temp_repos/pr_anon/change0.txt', "change0 desc\n\nthis is the body\nline 2 of body");
file_put_contents('temp_repos/pr_anon/change1.txt', "change1 desc\n\nthis is the body\nline 2 of body");
$r = rg_exec('cd temp_repos/pr_anon'
	. ' && echo "change0" > a && git add a && git commit -F change0.txt'
	. ' && echo "change1" > b && git add b && git commit -F change1.txt'
	. ' && git push origin_git master',
	'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log('Anonymous push request was rejected: ' . $r['errmsg'] . '!');
	exit(1);
}
rg_log_exit();


test_mr_check($db, $_testns, $rg_ui, $repo, 1, 'refs/heads/master', TRUE);


rg_log('');
rg_log_enter('Do a non-anonymous push...');
file_put_contents('temp_repos/pr_anon/change2.txt', "change2 desc\n\nthis is the body\nline 2 of body");
file_put_contents('temp_repos/pr_anon/change3.txt', "change3 desc\n\nthis is the body\nline 2 of body");
$r = rg_exec('cd temp_repos/pr_anon'
	. ' && echo "change2" > a && git add a && git commit -F change2.txt'
	. ' && echo "change3" > a && git commit -a -F change3.txt'
	. ' && git push origin_ssh master',
	'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log('Non-anonymous push was rejected: ' . $r['errmsg'] . '!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Do an anonymous push into a non-empty repo...');
putenv('GIT_SSH_COMMAND=');
file_put_contents('temp_repos/pr_anon/change4.txt', "anon change4 desc\n\nthis is the body\nline 2 of body");
file_put_contents('temp_repos/pr_anon/change5.txt', "anon change5 desc\n\nthis is the body\nline 2 of body");
$r = rg_exec('cd temp_repos/pr_anon'
	. ' && echo "change4" >> a && git add a'
	. ' && git commit -F change4.txt'
	. ' && echo "change5" >> a; git commit -a -F change5.txt'
	. ' && git push origin_git master',
	'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log('Anonymous push was rejected: ' . $r['errmsg'] . '!');
	exit(1);
}
rg_log_exit();


test_mr_check($db, $_testns, $rg_ui, $repo, 2, 'refs/heads/master', FALSE);


rg_log('');
rg_log_enter('Now, try to see what happens when a merge is with conflicts...');
$r = rg_exec('cd temp_repos/pr_anon'
	. ' && echo "change2" > a'
	. ' && git commit -a -m "conflict1b"'
	. ' && git push origin_git master',
	'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Could not pull/commit/push by git proto: ' . $r['errmsg'] . '!');
	exit(1);
}
putenv('GIT_SSH_COMMAND=' . $ssh);
$r = rg_exec('cd temp_repos/pr_anon'
	. ' && git reset --hard HEAD^1'
	. ' && echo "change1" > a'
	. ' && echo "commit" && git commit -a -m "conflict1a"'
	. ' && echo "push" && strace -s200 -f -tt -o pr_anon.strace git push origin_ssh master',
	'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Could not reset/commit/push by ssh proto: ' . $r['errmsg'] . '!');
	exit(1);
}
$mri = rg_test_mr_info($db, $repo['repo_id'], 3);
$data = array();
$headers = array();
$r = do_req($test_url . $url . $mri['id'] . '?t=pr_anon', $data, $headers);
if (!strstr($r['body'], 'This merge request cannot be merged without conflicts')) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Merge request appears as mergeable and should not!');
	exit(1);
}
if (!strstr($r['body'], 'Conflicts:')) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Conflicts not shown!');
	exit(1);
}
if (!strstr($r['body'], 'conflict1b')) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('"conflict1b" string not found!');
	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