<?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, $ev) { rg_prof_start('wh_lambda_send_one'); rg_log_ml('wh_lambda_send_one: event: ' . print_r($ev, TRUE)); $ret = FALSE; $wh = &$ev['wh']; $last_output = ''; while (1) { // replace ##tags## rg_wh_replace_tags($ev); // 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['errmsg']; break; } $ret = array(); $last_output .= $r['answer']; break; } rg_wh_set_last_output($db, $ev['ui']['uid'], $wh['id'], substr($last_output, 0, 4096)); if ($ev['debug'] == 1) rg_cache_set('DEBUG::' . $ev['ui']['uid'] . '::webhooks::' . $wh['id'], $last_output, RG_SOCKET_NO_WAIT); 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, $ev) { rg_prof_start('wh_lambda_send'); rg_log_ml('wh_lambda_send: event: ' . print_r($ev, TRUE)); $ret = array(); // First, get the list of hooks $r = rg_wh_list($db, $ev['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 the web hook does not contain our type, skip it if (!strchr($wh['idata']['events'], $ev['wh_event'])) { rg_log('DEBUG: ' . $ev['wh_event'] . ' is not present in ' . $wh['idata']['events']); continue; } if (isset($ev['ri']['name']) && !rg_repo_compare_refs($wh['repo'], $ev['ri']['name'])) { rg_log('DEBUG: hook is not for this repo'); continue; } if (isset($ev['refname']) && !rg_repo_compare_refs($wh['refname'], $ev['refname'])) { rg_log('DEBUG: hook is not for this ref'); continue; } $x = $ev; $x['category'] = 'wh_lambda_send_one'; $x['wh'] = $wh; $ret[] = $x; } rg_prof_end('wh_lambda_send'); return $ret; } /* * Some cosmetics applied to a webhook */ function rg_wh_lambda_cosmetic_pre(&$row) { rg_wh_amazon_cosmetic($row); $row['idata']['HTML:payload_nice'] = nl2br(rg_xss_safe($row['idata']['payload'])); } /* * Some cosmetics applied to a webhook */ function rg_wh_lambda_cosmetic_post(&$row) { $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_pre($db, &$rg) { $a = $rg['wh']['idata']; rg_wh_amazon_default_paras($a); if (!isset($a['function'])) $a['function'] = ''; if (!isset($a['payload'])) $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" . '}'; if (!isset($a['events'])) $a['events'] = 'P'; $rg['wh']['idata'] = $a; } /* * Transfers to $rg the custom parameters - used when showing the form */ function rg_wh_lambda_add_form_post($db, &$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*/); } $info = array( 'htype' => 'lambda', 'description' => 'Calls an Amazon Lambda function', 'cb' => array( 'cosmetic_pre' => 'rg_wh_lambda_cosmetic_pre', 'cosmetic_post' => 'rg_wh_lambda_cosmetic_post', 'fill_vars' => 'rg_wh_lambda_fill_vars', 'validate_vars' => 'rg_wh_lambda_validate_vars', 'add_form_pre' => 'rg_wh_lambda_add_form_pre', 'add_form_post' => 'rg_wh_lambda_add_form_post', 'fill_hints' => 'rg_wh_lambda_fill_hints' ), 'have_events' => TRUE, 'flags' => array() ); rg_wh_register_type('lambda', $info); $cb = array(); rg_wh_register_subtype('lambda', 'generic', 'Generic', $cb);