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 (d3f8cdd00dc0059210aebc9039f24c054ca2970f) (3,344B) (mode 100644) [raw]
<?php
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("wh_lambda.log");

$rg_sql = "host=localhost user=rocketgit dbname=rocketgit connect_timeout=10";
$rg_no_db = TRUE;
require_once("common.php");

$_testns = 'wh_lambda';
$rg_cache_enable = TRUE;
$rg_cache_debug = TRUE;
$rg_event_socket = "/var/lib/rocketgit/sockets/event.sock";

$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, $good_sid);
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::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, $good_sid, 'lambda', $extra);
rg_log_exit();


rg_log('');
rg_log_enter('Finding out the hook id...');
for ($i = 0; $i < 10; $i++) {
	$r = rg_cache_get('wh' . '::' . $rg_ui['uid']);
	if ($r !== FALSE)
		break;
	sleep(1);
}
if ($r === FALSE) {
	rg_log('Cannot get id from cache');
	exit(1);
}
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_log('');
rg_log_enter('Creating and upload a ssh key...');
rg_test_upload_ssh_key($db, $rg_ui, "wh_cloud", $good_sid);
rg_log_exit();


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);
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');
for ($i = 0; $i < 10; $i++) {
	$key = 'DEBUG::' . $rg_ui['uid'] . '::webhooks::' . $wh_id;
	$r = rg_cache_get($key);
	rg_log_ml('cache: ' . $r);
	if ($r !== FALSE)
		break;
	sleep(1);
}
if ($r === FALSE) {
	rg_log('Seems the event did not set the cache!');
	exit(1);
}
if (strcmp($r, '"Hello from Lambda; key: aaa branch=master repo=wh-cloud"') != 0) {
	rg_log_ml('r: ' . print_r($r, TRUE));
	rg_log('Seems hook did not executed correctly!');
	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