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 / wh_lambda.php (05a04ab421b57dd229cebaae550ef6f503f45116) (2,956B) (mode 100644) [raw]
<?php
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");

rg_log_set_file("wh_lambda.log");

require_once("common.php");

$_testns = 'wh_lambda';
$rg_cache_debug = TRUE;

prepare_http();

$home = getenv('HOME');
if (!file_exists($home . '/.aws/conf.php')) {
	rg_log('no ~/.aws.conf.php file!');
	exit(0);
}

// this will load $a array
include($home . '/.aws/conf.php');


rg_log('');
rg_log("Creating a user...");
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();


$repo = array('name' => 'wh-cloud');
rg_test_create_repo($db, $rg_ui, $repo);


rg_log('');
rg_log_enter('Registering webhook...');
$extra = array(
	'wh::htype' => 'lambda',
	'wh::hsubtype' => 'generic',
	'wh::description' => 'description2 <xss> lambda',
	'wh::repo' => '',
	'wh::refname' => '',
	'wh::idata::events[C]' => 'on', // C = create branch
	'wh::idata::events[P]' => 'on', // P = push
	'wh::idata::access_key_id' => $a['access_key_id'],
	'wh::idata::secret_access_key' => $a['secret_access_key'],
	'wh::idata::region' => 'eu-central-1',
	'wh::idata::function' => 'test-function',
	'wh::idata::payload' => '{"key": "aaa branch=##branch## repo=##repo##"}'
	);
rg_test_wh_add_edit($db, $rg_ui, 'lambda', 'generic', $extra);
rg_log_exit();


rg_log('');
rg_log_enter('Finding out the hook id...');
$r = test_wait_cache('wh' . '::' . $rg_ui['uid']);
rg_log_ml('r=' . print_r($r, TRUE));
$t = array_keys($r['list']);
if (count($t) != 1) {
	rg_log('We do not have 1 id!');
	exit(1);
}
$wh_id = $t[0];
rg_log('wh_id=' . $wh_id);
rg_log_exit();


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


rg_log('');
rg_log_enter('Pushing repo, so the trigger has a chanse to execute...');
// TODO: extract url from $repo
$repo_url = 'ssh://rocketgit@' . $rg_ssh_host . ':' . $rg_ssh_port
	. '/user/' . $rg_ui['username'] . '/' . $repo['name'];
rg_log('repo_url=' . $repo_url);
$r = rg_exec('./wh_cloud.git.sh ' . escapeshellarg($repo_url),
	'', FALSE, FALSE, FALSE);
if ($r['ok'] != 1) {
	rg_log_ml('Could not create local git repo: ' . $r['errmsg'] . '!');
	exit(1);
}
rg_log_exit();


rg_log('');
rg_log_enter('Testing if hook executed with success');
$key = 'DEBUG::' . $rg_ui['uid'] . '::webhooks::' . $wh_id;
$r = test_wait_cache($key);
$a = @json_decode($r, TRUE);
if ($a === NULL) {
	rg_log('Cannot decode JSON: '. $r);
	exit(1);
}
$e = 'Hello from Lambda; key: aaa branch=master repo=wh-cloud';
if (strcmp($a, $e) != 0) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Seems hook did not executed correctly [' . $a . '] != [' . $e . ']!');
	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