From fcfc1d82c91a97270d83848f6ed58a1f6ec18240 Mon Sep 17 00:00:00 2001 From: JJRcop Date: Tue, 31 Oct 2017 16:20:28 -0400 Subject: [PATCH 1/2] Webhook now comments its diff (#32151) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Webhook now comments its diff instead of its dump * Fixes variable access in webhook * Fixes webhook regex flaw * Missing semicolon in webhook well... I don't have much to say to you this is... i mean... 😀😀😁😁😁😁😂😂😂😂😂😂😂😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭😭 * Fixes anoher webhook regex issue * Re-adds content to webhook update * Encapsulates webhook diff in
--- .../github_webhook_processor.php | 50 ++++++++++++------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/tools/WebhookProcessor/github_webhook_processor.php b/tools/WebhookProcessor/github_webhook_processor.php index 37805aae7a..20d5631b73 100644 --- a/tools/WebhookProcessor/github_webhook_processor.php +++ b/tools/WebhookProcessor/github_webhook_processor.php @@ -519,32 +519,46 @@ function update_pr_balance($payload) { fclose($balances_file); } -function auto_update($payload){ - global $enable_live_tracking; - global $path_to_script; - global $repoOwnerAndName; - global $tracked_branch; - if(!$enable_live_tracking || !has_tree_been_edited($payload, $path_to_script) || $payload['pull_request']['base']['ref'] != $tracked_branch) - return; - - $content = file_get_contents('https://raw.githubusercontent.com/' . $repoOwnerAndName . '/' . $tracked_branch . '/'. $path_to_script); - - create_comment($payload, "Edit detected. Self updating... Here is my new code:\n``" . "`HTML+PHP\n" . $content . "\n``" . '`'); - - $code_file = fopen(basename($path_to_script), 'w'); - fwrite($code_file, $content); - fclose($code_file); -} - $github_diff = null; -function has_tree_been_edited($payload, $tree){ +function get_diff($payload) { global $github_diff; if ($github_diff === null) { //go to the diff url $url = $payload['pull_request']['diff_url']; $github_diff = file_get_contents($url); } + return $github_diff; +} + +function auto_update($payload){ + global $enable_live_tracking; + global $path_to_script; + global $repoOwnerAndName; + global $tracked_branch; + global $github_diff; + if(!$enable_live_tracking || !has_tree_been_edited($payload, $path_to_script) || $payload['pull_request']['base']['ref'] != $tracked_branch) + return; + + get_diff($payload); + $content = file_get_contents('https://raw.githubusercontent.com/' . $repoOwnerAndName . '/' . $tracked_branch . '/'. $path_to_script); + $content_diff = "### Diff not available. :slightly_frowning_face:"; + if($github_diff && preg_match('/(diff --git a\/' . preg_quote($path_to_script, '/') . '.+?)(?:^diff)?/sm', $github_diff, $matches)) { + $script_diff = matches[1]; + if($script_diff) { + $content_diff = "``" . "`DIFF\n" . $script_diff ."\n``" . "`"; + } + } + create_comment($payload, "Edit detected. Self updating... \n
Here are my changes:\n\n" . $content_diff . "\n
\n
Here is my new code:\n\n``" . "`HTML+PHP\n" . $content . "\n``" . '`\n
'); + + $code_file = fopen(basename($path_to_script), 'w'); + fwrite($code_file, $content); + fclose($code_file); +} + +function has_tree_been_edited($payload, $tree){ + global $github_diff; + get_diff($payload); //find things in the _maps/map_files tree //e.g. diff --git a/_maps/map_files/Cerestation/cerestation.dmm b/_maps/map_files/Cerestation/cerestation.dmm return $github_diff !== FALSE && preg_match('/^diff --git a\/' . preg_quote($tree, '/') . '/m') !== FALSE;