From 5cb5bf96bf6ca512c899e926d3b0cba49c38d21d Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Tue, 28 Feb 2017 09:33:44 -0800 Subject: [PATCH 1/2] Only update posts that are marked as modified Github tells us which files were modified. If we find that information in the webhook callback, let's not blindly update all posts but instead only update those posts that we were told were modified. This adds a new function to the payload class to get the actual head commit included in the payload. json_decode(json_encode(), true) is used to convert the object to an array - I'm not sure if there is a different way that you would like to use for this (I'm not really a PHP developer - but this works). Signed-off-by: Dirk Hohndel --- lib/import.php | 30 ++++++++++++++++++++++++++---- lib/payload.php | 9 +++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/import.php b/lib/import.php index b72cfb0..ef1b371 100644 --- a/lib/import.php +++ b/lib/import.php @@ -41,7 +41,8 @@ public function payload( WordPress_GitHub_Sync_Payload $payload ) { */ $error = false; - $result = $this->commit( $this->app->api()->fetch()->commit( $payload->get_commit_id() ) ); + $head_commit = $payload->get_commit(); + $result = $this->commit( $this->app->api()->fetch()->commit( $payload->get_commit_id() ), $head_commit ); if ( is_wp_error( $result ) ) { $error = $result; @@ -76,7 +77,7 @@ public function payload( WordPress_GitHub_Sync_Payload $payload ) { * @return string|WP_Error */ public function master() { - return $this->commit( $this->app->api()->fetch()->master() ); + return $this->commit( $this->app->api()->fetch()->master(), null ); } /** @@ -86,7 +87,18 @@ public function master() { * * @return string|WP_Error */ - protected function commit( $commit ) { + protected function commit( $commit , $head_commit_obj) { + $head_commit = array(); + $mod_files = array(); + $update_all = true; + if ( $head_commit_obj != null ) { + $head_commit = json_decode(json_encode($head_commit_obj), true); + if ( array_key_exists( 'modified', $head_commit ) ) { + $mod_files = $head_commit['modified']; + $update_all = false; + } + } + if ( is_wp_error( $commit ) ) { return $commit; } @@ -103,10 +115,20 @@ protected function commit( $commit ) { continue; } - $posts[] = $post = $this->blob_to_post( $blob ); + $post = $this->blob_to_post( $blob ); + + $found = $update_all; + foreach ( $mod_files as $modified_file ) { + if ($blob->path() == $modified_file) { + $found = true; + } + } if ( $post->is_new() ) { + $posts[] = $post; $new[] = $post; + } elseif ( $found ) { + $posts[] = $post; } } diff --git a/lib/payload.php b/lib/payload.php index 1c3e714..4c967eb 100644 --- a/lib/payload.php +++ b/lib/payload.php @@ -76,6 +76,15 @@ public function should_import() { return true; } + /** + * Returns the head commit. + * + * @return string + */ + public function get_commit() { + return $this->data->head_commit; + } + /** * Returns the sha of the head commit. * From 207254abf4e14dacb98f7f7c724c1de1bc088fcf Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Sat, 4 Mar 2017 23:18:13 -0800 Subject: [PATCH 2/2] Loop over all commits Instead of just looking at the head commit we need to loop over all of the commits that are in the change set that was pushed. This way we will track all of the modified files. Signed-off-by: Dirk Hohndel --- lib/import.php | 18 ++++++++++-------- lib/payload.php | 9 --------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/lib/import.php b/lib/import.php index ef1b371..b14b302 100644 --- a/lib/import.php +++ b/lib/import.php @@ -41,8 +41,8 @@ public function payload( WordPress_GitHub_Sync_Payload $payload ) { */ $error = false; - $head_commit = $payload->get_commit(); - $result = $this->commit( $this->app->api()->fetch()->commit( $payload->get_commit_id() ), $head_commit ); + $commits = $payload->get_commits(); + $result = $this->commit( $this->app->api()->fetch()->commit( $payload->get_commit_id() ), $commits ); if ( is_wp_error( $result ) ) { $error = $result; @@ -87,15 +87,17 @@ public function master() { * * @return string|WP_Error */ - protected function commit( $commit , $head_commit_obj) { + protected function commit( $commit , $commits_array) { $head_commit = array(); $mod_files = array(); $update_all = true; - if ( $head_commit_obj != null ) { - $head_commit = json_decode(json_encode($head_commit_obj), true); - if ( array_key_exists( 'modified', $head_commit ) ) { - $mod_files = $head_commit['modified']; - $update_all = false; + foreach ( $commits_array as $commit_obj ) { + if ( $commit_obj != null ) { + $next_commit = json_decode(json_encode($commit_obj), true); + if ( array_key_exists( 'modified', $next_commit ) ) { + $mod_files = array_merge($mod_files, $next_commit['modified']); + $update_all = false; + } } } diff --git a/lib/payload.php b/lib/payload.php index 4c967eb..1c3e714 100644 --- a/lib/payload.php +++ b/lib/payload.php @@ -76,15 +76,6 @@ public function should_import() { return true; } - /** - * Returns the head commit. - * - * @return string - */ - public function get_commit() { - return $this->data->head_commit; - } - /** * Returns the sha of the head commit. *