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> / inc / wh / build.inc.php (f6949160be7e038fc65c06c5cecbe63ae3b17ef0) (5,580B) (mode 100644) [raw]
<?php
require_once($INC . "/util.inc.php");
require_once($INC . "/log.inc.php");
require_once($INC . "/sql.inc.php");
require_once($INC . "/prof.inc.php");
require_once($INC . "/events.inc.php");
require_once($INC . '/builder.inc.php');
require_once($INC . "/wh/core.inc.php");

$rg_wh_build_functions = array(
	'wh_build_send' => 'rg_wh_build_send',
	'wh_build_send_one' => 'rg_wh_build_send_one'
);
rg_event_register_functions($rg_wh_build_functions);


/*
 * Helper for rg_wh_build_send
 */
function rg_wh_build_send_one($db, $event)
{
	rg_prof_start('wh_build_send_one');
	rg_log_ml('wh_build_send_one: event: ' . print_r($event, TRUE));

	$ret = FALSE;

	$wh = &$event['wh'];

	$last_output = '';
	while (1) {
		// replace ##tags##
		rg_wh_replace_tags($event);

		// we need to copy 'flags'/'url' because we pass idata
		$wh['idata']['flags'] = $wh['flags'];
		$wh['idata']['url'] = $event['ri']['clone_url'];
		$wh['idata']['head'] = $event['new_rev'];

		// TODO: still have to pass env
		// TODO: also, e-mail notification
		$wh['idata']['env'] = 'fedora23-server-x86_64';

		// Call the function
		$r = rg_builder_add($db, $event['ri']['repo_id'], $wh['idata']);
		if ($r['ok'] != 1) {
			$last_output .= $r['error'];
			break;
		}

		$ret = array();
		break;
	}

	rg_wh_set_last_output($db, $event['ui']['uid'], $wh['id'],
		substr($last_output, 0, 4096));

	rg_prof_end('wh_build_send_one');
	return $ret;
}

/*
 * Generic function which will be called when a webhook must be posted
 */
function rg_wh_build_send($db, $event)
{
	rg_prof_start('wh_build_send');
	rg_log_ml('wh_build_send: event: ' . print_r($event, TRUE));

	$ret = array();

	// First, get the list of hooks
	$r = rg_wh_list($db, $event['ui']['uid']);
	if ($r['ok'] != 1)
		return FALSE;

	// Filter
	foreach ($r['list'] as $id => $wh) {
		if (strcmp($wh['htype'], 'build') != 0)
			continue;

		// Diabled?
		if (strchr($wh['flags'], 'D'))
			continue;

		if (isset($event['ri']['name'])
			&& !rg_repo_compare_refs($wh['repo'], $event['ri']['name'])) {
			rg_log('hook is not for this repo');
			continue;
		}

		if (isset($event['refname'])
			&& !rg_repo_compare_refs($wh['refname'], $event['refname'])) {
			rg_log('hook is not for this ref');
			continue;
		}

		$x = $event;
		$x['category'] = 'wh_build_send_one';
		$x['wh'] = $wh;
		$x['debug'] = $wh['idata']['debug'];
		$ret[] = $x;
	}

	rg_prof_end('wh_build_send');
	return $ret;
}

/*
 * Some cosmetics applied to a webhook
 */
function rg_wh_build_cosmetic(&$row)
{
	$f = rg_template('user/settings/wh/build/show_one.html',
		$rg, TRUE/*xss*/);

	$list = '';
	foreach ($row['idata']['cmds'] as $i => &$info) {
		if (empty($info['cmd']))
			continue;
		$list .= str_replace('##i##', $i, $f);

		$info['abort_nice'] =
			$info['abort'] == 1 ? "Yes" : "No";
	}
	$row['idata']['HTML:wh_list'] =
		rg_template_string($list, 0 /*off*/, $row['idata'], TRUE /*xss*/);

	$row['idata']['HTML:private'] = rg_template(
		'user/settings/wh/build/show.html', $row['idata'], TRUE /*xss*/);
}

/*
 * Fill private data based on parameters passed
 */
function rg_wh_build_fill_vars(&$rg)
{
	$a = &$rg['wh']['idata'];

	$a['cmds'] = array();
	for ($i = 1; $i <= 5; $i++) {
		$p = 'wh::idata::cmds::' . $i . '::';

		$cmd = trim(rg_var_str($p . 'cmd'));
		if (empty($cmd)) {
			$a['cmds'][$i] = array(
				'cmd' => '',
				'label_ok' => '',
				'label_nok' => '',
				'abort' => 0
			);
			continue;
		}

		$a['cmds'][$i] = array();
		$a['cmds'][$i]['cmd'] = $cmd;
		$a['cmds'][$i]['label_ok'] = trim(rg_var_str($p . 'label_ok'));
		$a['cmds'][$i]['label_nok'] = trim(rg_var_str($p . 'label_nok'));
		$x = trim(rg_var_str($p . 'abort'));
		$a['cmds'][$i]['abort'] = strcasecmp($x, 'on') == 0 ? 1 : 0;
	}

	//rg_log_ml('DEBUG: after fill_vars: ' . print_r($a, TRUE));
}

/*
 * Validate parameters passed
 */
function rg_wh_build_validate_vars($rg, &$errmsg)
{
	global $rg_wh_build_itypes;

	$a = $rg['wh'];

	$ret = FALSE;
	while (1) {
		$all_empty = TRUE;

		rg_log_ml('DEBUG: cmds:' . print_r($a['idata']['cmds'], TRUE));
		if (empty($a['idata']['cmds'])) {
			$errmsg[] = rg_template('user/settings/wh/build/inv_cmd.txt',
				$rg, TRUE /*xss*/);
			break;
		}

		$ret = TRUE;
		break;
	}

	return $ret;
}

/*
 * Transfers to $rg the custom parameters - used when showing the form
 */
function rg_wh_build_add_form(&$rg)
{
	$f = rg_template_blind('user/settings/wh/build/form_cmd.html');

	$cmds = '';
	for ($i = 1; $i <= 5; $i++)
		$cmds .= str_replace('##i##', $i, $f);

	//rg_log_ml('DEBUG: cmds=' . $cmds);

	$rg['HTML:cmds'] =
		rg_template_string($cmds, 0 /*off*/, $rg, TRUE /*xss*/);

	$rg['HTML:custom_form'] = rg_template('user/settings/wh/build/form.html',
		$rg, TRUE /*xss*/);
}

/*
 * Add custom hints
 */
function rg_wh_build_fill_hints($rg, &$hints)
{
	$hints[]['HTML:hint'] = rg_template('user/settings/wh/build/hints.html',
		$rg, TRUE /*xss*/);
}

/*
 * Loads default paras for a form
 */
function rg_wh_build_default_paras(&$rg)
{
	$a = &$rg['wh']['idata'];

	$t = array(
		'cmd' => '',
		'label_ok' => '',
		'label_nok' => '',
		'abort' => 0
	);

	for ($i = 1; $i <= 5; $i++)
		$a['cmds'][$i] = $t;

	$a['events'] = 'P';
}



$rg_wh_plugins['build'] = array(
	'htype' => 'build',
	'description' => 'Clone, builds and test',
	'cosmetic' => 'rg_wh_build_cosmetic',
	'fill_vars' => 'rg_wh_build_fill_vars',
	'validate_vars' => 'rg_wh_build_validate_vars',
	'add_form' => 'rg_wh_build_add_form',
	'default_paras' => 'rg_wh_build_default_paras',
	'fill_hints' => 'rg_wh_build_fill_hints',
	'have_events' => TRUE,
	'flags' => array()
);

?>
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