<?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';
rg_test_create_user($db, $rg_ui);
$info = array('id' => $rg_ui['username']);
prepare_http($info);
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);
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 --initial-branch=main'
. ' && 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 main',
'', 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/main', 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 commit -a -F change2.txt'
. ' && echo "change3" > a && git commit -a -F change3.txt'
. ' && git push origin_ssh main',
'', 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...');
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 commit -a -F change4.txt'
. ' && echo "change5" >> a && git commit -a -F change5.txt'
. ' && git push origin_git main',
'', 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/main', FALSE);
rg_log('');
rg_log_enter('Now, try to see what happens when a merge is with conflicts (this is mr 3)...');
$r = rg_exec('cd temp_repos/pr_anon'
. ' && echo "change2" > a && git commit -a -m "conflict1b"'
. ' && git push origin_git main',
'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('Could not push by git proto: ' . $r['errmsg'] . '!');
exit(1);
}
chdir('temp_repos/pr_anon');
$list = array(
'git reset --hard HEAD^1',
'echo "change1" > a',
'git commit -a -m "conflict1a"',
'git push origin_ssh main'
);
foreach ($list as $cmd) {
$r = rg_exec($cmd, '', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
rg_log_ml('r: ' . print_r($r, TRUE));
rg_log('Could not run [' . $cmd . ']: ' . $r['errmsg'] . '!');
exit(1);
}
}
chdir('../..');
$mri = rg_test_mr_info($db, $repo['repo_id'], 3);
$data = array();
$headers = array();
$r = do_req($info, $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!');