-
Notifications
You must be signed in to change notification settings - Fork 12
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
Conversation
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? |
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/. |
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/ |
Edit: Scratch that. You're right. |
@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. |
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. |
Makes removing files and folders created by the plugin easier
Removes all temporary files and folder upon plugin uninstall.
Access restriction runs before that constant is defined.
Otherwise the custom events won’t get added.
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.
README.md
Outdated
Easily update a project's translatable string from a GitHub repository. | ||
|
||
```bash | ||
wp traduttore update_from_github <repository_url> |
There was a problem hiding this comment.
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 filewp traduttore translations update <repository_url>
to fetch strings from a GitHub repo
There was a problem hiding this comment.
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>
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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'] ] ); |
There was a problem hiding this comment.
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 ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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; | ||
} | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
inc/GitHubUpdater.php
Outdated
* @since 2.0.0 | ||
* | ||
* @param string $repository GitHub repository URL, e.g. https://github.com/wearerequired/required-valencia. | ||
* |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this 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.
inc/GitHubUpdater.php
Outdated
* @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 ); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
@grappler The REST endpoint only accepts POST requests. GET will result in a 404. |
inc/GitHubUpdater.php
Outdated
$git_target = get_temp_dir() . 'traduttore-github-' . $slug; | ||
$pot_target = wp_tempnam( 'traduttore-' . $slug . '.pot' ); | ||
|
||
if ( $this->has_lock() ) { |
There was a problem hiding this comment.
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.
inc/GitHubUpdater.php
Outdated
|
||
$stats = GP::$original->import_for_project( $this->project, $translations ); | ||
|
||
$this->remove_lock(); |
There was a problem hiding this comment.
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
.
inc/GitHubUpdater.php
Outdated
exec( escapeshellcmd( 'git pull -q' ), $output, $status ); | ||
chdir( $current_dir ); | ||
} else { | ||
exec( escapeshellcmd( sprintf( 'git clone %1$s %2$s', $source, $target ) ), $output, $status ); |
There was a problem hiding this comment.
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 ) ) )
inc/GitHubUpdater.php
Outdated
* @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 ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
escapeshellarg()
is missing.
inc/GitHubUpdater.php
Outdated
* @since 2.0.0 | ||
*/ | ||
protected function remove_lock() { | ||
// gp_delete_meta() is internal. |
There was a problem hiding this comment.
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 ); |
There was a problem hiding this comment.
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.
See https://github.com/wearerequired/required-network/issues/12.
Usage:
wp @dev --url=https://translate.required.local traduttore update_from_github https://github.com/wearerequired/klingler-theme
on the command lineThis relies on the "Source file URL" field being set in a project. Example:
Todo: