Only update posts that are marked as modified - #167
Conversation
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 <dirk@hohndel.org>
mAAdhaTTah
commented
Mar 3, 2017
I haven't forgotten about this. Will take a look this weekend. Do you have this code in place and running on your site? Wondering how it works in the real world. |
dirkhh
commented
Mar 3, 2017
This is NOT ready to pull. |
mAAdhaTTah
commented
Mar 3, 2017
Sounds good. I await your updates. |
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 <dirk@hohndel.org>
dirkhh
commented
Mar 5, 2017
I think this is worth actually taking a look at. |
mAAdhaTTah
left a comment
There was a problem hiding this comment.
If you're not that familiar with PHP & PHPUnit, don't worry about the tests; I'm not sure all the failures are related to your code anyway. But if you want to take a shot at updating those, that would be awesome.
| */ | ||
| public function master() { | ||
| return $this->commit( $this->app->api()->fetch()->master() ); | ||
| return $this->commit( $this->app->api()->fetch()->master(), null ); |
There was a problem hiding this comment.
Instead of passing in null here, we should not pass in anything and update the default value for this method.
| * @return string|WP_Error | ||
| */ | ||
| protected function commit( $commit ) { | ||
| protected function commit( $commit , $commits_array) { |
There was a problem hiding this comment.
...in which case $commits_array = array() should go here.
| $mod_files = array(); | ||
| $update_all = true; | ||
| foreach ( $commits_array as $commit_obj ) { | ||
| if ( $commit_obj != null ) { |
There was a problem hiding this comment.
Then we could remove this null check, unless there's a possibility of getting null from the API.
| } | ||
| $posts[] = $post = $this->blob_to_post( $blob ); | ||
| $post = $this->blob_to_post( $blob ); |
There was a problem hiding this comment.
Because we're still iterating over the tree (see $commit->tree()->blobs() above), is the savings here primarily from making fewer DB calls?
| $update_all = true; | ||
| foreach ( $commits_array as $commit_obj ) { | ||
| if ( $commit_obj != null ) { | ||
| $next_commit = json_decode(json_encode($commit_obj), true); |
There was a problem hiding this comment.
We can cast the object to an array with $next_commit = (array) $commit_obj; instead of the whole JSON thing.
litefeel
commented
Mar 7, 2017
My english is so pool. how to get commits merge commit to find file of update and new. cache something
max_allowed_packet is 1M by default. so dont overtake it. Make the cache smaller. |
dirkhh
commented
Mar 7, 2017
Based on @lite3 's comments I am in way over my head. |
mAAdhaTTah
commented
Mar 7, 2017
@dirkhh If you're willing to make the changes suggested, we could resolve the "20 commits issue" by just falling back to the old behavior (get the entire tree), rather than spiking the work you've done already. |
dirkhh
commented
Mar 7, 2017
via email
On Tue, Mar 07, 2017 at 04:47:57AM -0800, James DiGioia wrote:
@dirkhh If you're willing to make the changes suggested, we could resolve the "20 commits issue" by just falling back to the old behavior (get the entire tree), rather than spiking the work you've done already. Sure, I can do that. It will take me a couple of days (work has me crazy
busy this week). |
litefeel
commented
Mar 8, 2017
Now I am doing a new version, do not cache, do not tree. do not too many commits. |
mAAdhaTTah
commented
Jul 2, 2017
dirkhh
commented
Jul 2, 2017
I have been completely side tracked. I have a hacked version that does what I need, but I'd need to start from scratch to do this right. So if anyone else wants to look, please don't wait for me. |
litefeel
commented
Jul 3, 2017
@mAAdhaTTah I have been complete it. https://github.com/litefeel/writing-on-github/ |
litefeel
commented
Jul 3, 2017
@mAAdhaTTah Replace the tree api with the content api |
IeuanWalker
commented
Apr 2, 2020
@dirkhh@mAAdhaTTah I'm only interested in importing post from Github (not exporting) |
mAAdhaTTah
commented
Apr 3, 2020
@IeuanWalker We used the Git Data API and build up the new tree based on the current tree, then add a new commit pointing to the new tree. This helps us avoid adding multiple commits for things like the initial site export and batch deletion (which is why we don't use the Content API @litefeel). |
IeuanWalker
commented
Apr 5, 2020
@mAAdhaTTah Thanks but that's mostly for exporting isn't it?, do you something different to import the post from GitHub? All I want to be able to do is add/ update a new post to a repository (not from the web application) and for the web application to pick up the change and add/ update the database. Repository won't need to be updated from the web application. |
mAAdhaTTah
commented
Apr 5, 2020
No, we use it to read the tree as well. You can also use the Content API, which we used initially. |
IeuanWalker
commented
Apr 5, 2020
@mAAdhaTTah Oh ok. So are you using the push webhook to know when something has been added then the git data api to update/ add the new posts? |
mAAdhaTTah
commented
Apr 5, 2020
Yeah, basically. |
IeuanWalker
commented
Apr 5, 2020
@mAAdhaTTah cool makes sense thanks :) |
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 dirk@hohndel.org