<?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 . "/wh/core.inc.php"); require_once($INC . "/wh/amazon.inc.php"); $rg_wh_lambda_functions = array( 'wh_lambda_send' => 'rg_wh_lambda_send', 'wh_lambda_send_one' => 'rg_wh_lambda_send_one' ); rg_event_register_functions($rg_wh_lambda_functions); /* * Helper for rg_wh_lambda_send */ function rg_wh_lambda_send_one($db, $event) { rg_prof_start('wh_lambda_send_one'); rg_log_ml('wh_lambda_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' because we pass idata $wh['idata']['flags'] = $wh['flags']; // Call the function $r = rg_amazon_lambda_invoke($wh['idata']); if ($r['ok'] != 1) { $last_output .= $r['error']; break; } $ret = array(); $last_output .= $r['answer']; break; } rg_wh_set_last_output($db, $event['ui']['uid'], $wh['id'], substr($last_output, 0, 4096)); rg_prof_end('wh_lambda_send_one'); return $ret; } /* * Generic function which will be called when a webhook must be posted */ function rg_wh_lambda_send($db, $event) { rg_prof_start('wh_lambda_send'); rg_log_ml('wh_lambda_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'], 'lambda') != 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_lambda_send_one'; $x['wh'] = $wh; $x['debug'] = $wh['idata']['debug']; $ret[] = $x; } rg_prof_end('wh_lambda_send'); return $ret; } /* * Some cosmetics applied to a webhook */ function rg_wh_lambda_cosmetic(&$row) { rg_wh_amazon_cosmetic($row); $row['idata']['HTML:payload_nice'] = nl2br(rg_xss_safe($row['idata']['payload'])); $row['idata']['HTML:private'] = rg_template( 'user/settings/wh/lambda/show.html', $row['idata'], TRUE /*xss*/); } /* * Fill private data based on parameters passed */ function rg_wh_lambda_fill_vars(&$rg) { $a = &$rg['wh']['idata']; rg_wh_amazon_fill_vars($a); $a['function'] = trim(rg_var_str('wh::idata::function')); $a['payload'] = trim(rg_var_str('wh::idata::payload')); } /* * Validate parameters passed */ function rg_wh_lambda_validate_vars($rg, &$errmsg) { global $rg_wh_lambda_itypes; $a = $rg['wh']; $ret = FALSE; while (1) { if (!rg_wh_amazon_validate_vars($a['idata'], $errmsg)) break; if (empty($a['idata']['function'])) { $errmsg[] = rg_template('user/settings/wh/lambda/inv_func.txt', $rg, TRUE /*xss*/); break; } $ret = TRUE; break; } return $ret; } /* * Transfers to $rg the custom parameters - used when showing the form */ function rg_wh_lambda_add_form(&$rg) { $rg['HTML:custom_form'] = rg_template('user/settings/wh/lambda/form.html', $rg, TRUE /*xss*/); } /* * Add custom hints */ function rg_wh_lambda_fill_hints($rg, &$hints) { $hints[]['HTML:hint'] = rg_template('user/settings/wh/lambda/hints.html', $rg, TRUE /*xss*/); } /* * Loads default paras for a form * TODO: escape json when replacing */ function rg_wh_lambda_default_paras(&$rg) { $a = &$rg['wh']['idata']; rg_wh_amazon_default_paras($a); $a['function'] = ''; $a['payload'] = '{' . "\n" . '"repo": "##repo##",' . "\n" . '"repo_url": "##repo_url##",' . "\n" . '"branch": "##branch##",' . "\n" . '"hook_id": "##hook_id##",' . "\n" . '"commit": "##commit##",' . "\n" . '"commit_url": "##commit_url##",' . "\n" . '"date": "##date##",' . "\n" . '"time": "##time##",' . "\n" . '"timestamp": "##timestamp##",' . "\n" . '"ip": "##ip##"' . "\n" . '}'; $a['events'] = 'P'; } $rg_wh_plugins['lambda'] = array( 'htype' => 'lambda', 'description' => 'Calls an Amazon Lambda function', 'cosmetic' => 'rg_wh_lambda_cosmetic', 'fill_vars' => 'rg_wh_lambda_fill_vars', 'validate_vars' => 'rg_wh_lambda_validate_vars', 'add_form' => 'rg_wh_lambda_add_form', 'default_paras' => 'rg_wh_lambda_default_paras', 'fill_hints' => 'rg_wh_lambda_fill_hints', 'have_events' => TRUE, 'flags' => array() ); ?>