Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow updating project translations / originals from GitHub #2

Merged
merged 40 commits into from
Feb 28, 2018

Conversation

swissspidy
Copy link
Collaborator

@swissspidy swissspidy commented Feb 21, 2018

See https://github.com/wearerequired/required-network/issues/12.

Usage:

  • Automatically run via incoming webhook
  • Run wp @dev --url=https://translate.required.local traduttore update_from_github https://github.com/wearerequired/klingler-theme on the command line

This relies on the "Source file URL" field being set in a project. Example:

screen shot 2018-02-21 at 19 12 14

Todo:

  • Test with webhook
  • Remove scheduled tasks upon plugin deactivation.
  • Delete temporary files and folders upon plugin uninstall.
  • Perhaps delete temporary POT file after import.

@grappler
Copy link
Contributor

So the idea is that whenever we push to a repo the POT file get automatically updated. We could also manually trigger the update via WP-CLI.

This though only works for plugins and themes that are in a single repo?

Where will the git repos live on the server?

@swissspidy
Copy link
Collaborator Author

Yes, that‘s the idea behind https://github.com/wearerequired/required-network/issues/12.

Why manually when you can do it all automatically? :-)

Yes, it only works for single theme and plugin repos. Those are the only ones we have (and should) set up in GlotPress anyway.

The Git repos currently are being cloned to a location inside /tmp/.

@grappler
Copy link
Contributor

grappler commented Feb 23, 2018

Yes, it only works for single theme and plugin repos. Those are the only ones we have (and should) set up in GlotPress anyway.

I am sorry to burst your bubble 😛 We have PH in Glotpress and we don't have separate repos for the themes. https://translate.required.com/projects/prohelvetia/

@swissspidy
Copy link
Collaborator Author

swissspidy commented Feb 23, 2018

Yeah but all the strings are in the sub projects?

Edit: Scratch that. You're right.

@swissspidy
Copy link
Collaborator Author

@grappler regarding PH: I think it's best to move the themes into their own repository instead of trying to treat it as a special case.

@grappler
Copy link
Contributor

I think we should be OK for PH like it is for the moment. We can open a issue and work on it in the future if need be.

@swissspidy
Copy link
Collaborator Author

Slack notifications now work like a charm:

screen shot 2018-02-26 at 13 49 31

The project can be found via the URL. This matches the CLI command’s behavior.
Adds ZIP URL to the Slack message for easier debugging.
@swissspidy
Copy link
Collaborator Author

@grappler @ocean90 If you could do a quick review before I merge this for further testing, I‘d really appreciate it.

README.md Outdated
Easily update a project's translatable string from a GitHub repository.

```bash
wp traduttore update_from_github <repository_url>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't seen a WP-CLI command using underscores yet. How about

  • wp traduttore translations build <project> to generate a ZIP file
  • wp traduttore translations update <repository_url> to fetch strings from a GitHub repo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering why it has to be the repository URL for updating. Why not <project>?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could change it so that both a URL and an ID would work. The IDs are nowhere revealed in the UI, so a URL might be easier to grasp.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I meant the project path for <project>.

README.md Outdated
3. Set `https://translate.required.com/wp-json/github-webhook/v1/push-event` as the payload URL.
4. Choose `application/json` as the content type.
5. Enter the secret key that can be found in 1Password.
6. Leave the other options unchanged and submit the form.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be explicit here and mention that we only need the push event.

inc/Plugin.php Outdated
}

if ( ! wp_next_scheduled( 'traduttore_update_from_github', [ $params['repository']['url'] ] ) ) {
wp_schedule_single_event( time() + MINUTE_IN_SECONDS * 15, 'traduttore_update_from_github', [ $params['repository']['html_url'] ] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it really have to be 15 minutes? 1-2 minutes should be fine too.


do_action( 'traduttore_zip_generated', $success, $translation_set );
add_action( 'traduttore_update_from_github', function ( $repository ) {
$project = GitHubUpdater::find_project( $repository );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a check whether an update is already running. The lock can be stored as a project meta.

https://github.com/GlotPress/GlotPress-WP/blob/68bffccae4b3d3ee9971e431f6dac262c7b49086/gp-includes/meta.php

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it return a WP_Error object when there are failures like existing locks? Would make for better error messages / logs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe? Looks like you may not be able to use them for the Slack event.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I'll leave that for the future. Not sure it's really worth it right now.

inc/Plugin.php Outdated
'action' => 'traduttore_updated_from_github',
'description' => __( 'When new translations are updated from GitHub', 'traduttore' ),
'message' => function( GP_Project $project, array $stats ) {
list($originals_added, $originals_existing, $originals_fuzzied, $originals_obsoleted, $originals_error) = $stats;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CS: Missing spaces.

if ( ! in_array( $event_name, [ 'push', 'ping' ], true ) ) {
return false;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this ignore commits to branches that are not relevant for the translations? The branch could be extracted from the source file URL.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The branch can be retrieved from the ref field. Will add a check for refs/heads/master.

* @since 2.0.0
*
* @param string $repository GitHub repository URL, e.g. https://github.com/wearerequired/required-valencia.
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CS: No newline between @param and @return tags.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you perhaps know a PhpStorm setting that doesn't automatically add a newline there?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's "Blank lines around parameters"? That's the only one I have not enabled.
bildschirmfoto 2018-02-26 um 22 32 11

Copy link
Contributor

@grappler grappler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good otherwise.
Accessing https://translate.required.local/wp-json/github-webhook/v1/push-event in the browser gave a 404.

* @param array $stats Stats about the number of imported translations.
* @param PO $translations PO object containing all the translations from the POT file.
*/
//do_action( 'traduttore_updated_from_github', $this->project, $stats, $translations );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this commented out?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops testing leftover :-) I didn‘t want to send tons of Slack messages locally.

@swissspidy
Copy link
Collaborator Author

@grappler The REST endpoint only accepts POST requests. GET will result in a 404.

$git_target = get_temp_dir() . 'traduttore-github-' . $slug;
$pot_target = wp_tempnam( 'traduttore-' . $slug . '.pot' );

if ( $this->has_lock() ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be right at the beginning of this method do avoid wp_tempnam() creating a file unnecessarily.


$stats = GP::$original->import_for_project( $this->project, $translations );

$this->remove_lock();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be called after the action because we don't know what functions are hooked into traduttore_updated_from_github.

exec( escapeshellcmd( 'git pull -q' ), $output, $status );
chdir( $current_dir );
} else {
exec( escapeshellcmd( sprintf( 'git clone %1$s %2$s', $source, $target ) ), $output, $status );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

escapeshellarg() is missing.

escapeshellcmd( sprintf( 'git clone %1$s %2$s', escapeshellarg( $source ), escapeshellarg( $target ) ) )

* @return bool True on success, false otherwise.
*/
protected function create_pot_file( $source, $target, $slug ) {
exec( escapeshellcmd( sprintf( 'wp i18n make-pot %1$s %2$s --slug=%3$s --domain=%3$s', $source, $target, $slug ) ), $output, $status );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

escapeshellarg() is missing.

* @since 2.0.0
*/
protected function remove_lock() {
// gp_delete_meta() is internal.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


do_action( 'traduttore_zip_generated', $success, $translation_set );
add_action( 'traduttore_update_from_github', function ( $repository ) {
$project = GitHubUpdater::find_project( $repository );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe? Looks like you may not be able to use them for the Slack event.

@swissspidy swissspidy merged commit addcd52 into master Feb 28, 2018
@swissspidy swissspidy deleted the update-from-github branch February 28, 2018 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants