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 / mr.inc.php (f597016b09bbeb5bd58316ea0214a2539b374f7f) (3,048B) (mode 100644) [raw]
<?php

/*
 * Check merge request operations
 * @expect_ff - TRUE if we expect a fast-forward to take place,
 * even if merge_ff is 0.
 */
function test_mr_check($db, $t, $rg_ui, $repo, $mr_id, $mr_refname, $expect_ff)
{
	global $test_url;

	rg_log_enter('test_mr_check');

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

	rg_log_enter('Check if merge is in db and is against correct branch...');
	$mri = rg_test_mr_info($db, $repo['repo_id'], $mr_id);
	if (strcmp($mri['refname'], $mr_refname) != 0) {
		rg_log_ml('mri: ' . print_r($mri, TRUE));
		rg_log('Seems the ref is not [' . $mr_refname . ']: ' . $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();
	$r = do_req($test_url . $url . '?t=' . $t, $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();
	$r = do_req($test_url . $url . '/' . $mri['id'] . '?t=' . $t, $data, $headers);
	if (!strstr($r['body'], 'This merge request can be merged without conflicts')) {
		rg_log_ml('r: ' . print_r($r, TRUE));
		rg_log('Merge request does not appear as mergeable');
		exit(1);
	}
	rg_log_exit();


	rg_log('');
	rg_log_enter('Merging merge request...');
	$mm1 = 'This is the merge message <xss>' . rg_id(8);
	$data = array('token' => $r['tokens']['mr_merge'],
		'merge_ff' => 0, 'merge_msg' => $mm1);
	$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 in db!');
			exit(1);
		}
		$tries++;
		sleep(1);
	}

	$r = rg_exec('cd temp_repos/pr_anon'
		. ' && git pull origin_git master',
		'', FALSE, FALSE, FALSE);
	if ($r['ok'] != 1) {
		rg_log('Could not pull: ' . $r['errmsg'] . '!');
		exit(1);
	}

	if ($expect_ff)
		$add = ' --merges';
	else
		$add = '';
	$r = rg_exec('cd temp_repos/pr_anon && git log --oneline -n1' . $add,
		'', FALSE, FALSE, FALSE);
	if ($r['ok'] != 1) {
		rg_log('Could not generate log: ' . $r['errmsg'] . '!');
		exit(1);
	}
	if ($expect_ff) {
		if (!empty($r['data'])) {
			rg_log_ml('r: ' . print_r($r, TRUE));
			rg_log('We found a merge when a fast-forward was expected!');
			exit(1);
		}
	} else {
		if (!strstr($r['data'], $mm1)) {
			rg_log_ml('r: ' . print_r($r, TRUE));
			rg_log('Cannot find the expected merge message'
				. ' [' . $r['data'] . '] != [' . $mm1 . ']!');
			exit(1);
		}
	}
	rg_log_exit();

	rg_log_exit();
}
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