Making WordPress.org

Changeset 5446


Ignore:
Timestamp:
04/30/2017 04:20:16 PM (9 years ago)
Author:
ocean90
Message:

Plugin Directory: Restore Slack notifications for i18n imports.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/bin/import-plugin-to-glotpress.php

    r5435 r5446  
    11<?php
    22namespace WordPressdotorg\Plugin_Directory;
     3
     4use Exception;
     5use WordPressdotorg\Plugin_Directory\Clients\Slack;
    36
    47// This script should only be called in a CLI environment.
     
    710}
    811
    9 $opts = getopt( '', array( 'url:', 'abspath:', 'plugin:', 'tag:', 'type:' ) );
     12$opts = getopt( '', array( 'url:', 'abspath:', 'plugin:', 'tag:', 'type:', 'no-slack' ) );
    1013
    1114// Guess the default parameters:
     
    3033}
    3134
     35if ( ! in_array( $opts['type'], [ 'code', 'readme' ] ) ) {
     36    fwrite( STDERR, "Invalid value for type argument: {$opts['type']}\n" );
     37    die();
     38}
     39
    3240// Bootstrap WordPress
    3341$_SERVER['HTTP_HOST']   = parse_url( $opts['url'], PHP_URL_HOST );
     
    5058$start_time  = microtime( 1 );
    5159
     60$plugin = Plugin_Directory::get_plugin_post( $plugin_slug );
     61if ( ! $plugin ) {
     62    fwrite( STDERR, "[{$plugin_slug}] Plugin I18N Import Failed: Plugin doesn't exist.\n" );
     63    exit( 1 );
     64}
     65
     66// Prepare Slack notification.
     67$send_slack = defined( 'PLUGIN_IMPORTS_SLACK_WEBHOOK' ) && ! isset( $opts['no-slack'] );
     68if ( $send_slack ) {
     69    $slack_client = new Slack( PLUGIN_IMPORTS_SLACK_WEBHOOK );
     70    $slack_client->add_attachment( 'ts', time() );
     71    $slack_client->add_attachment( 'fallback', "{$plugin->post_title} has been imported." );
     72    $slack_client->add_attachment( 'title', "{$plugin->post_title} has been imported" );
     73    $slack_client->add_attachment( 'title_link', "https://translate.wordpress.org/projects/wp-plugins/{$plugin_slug}" );
     74    $fields = [
     75        [
     76            'title' => 'Type',
     77            'value' => ( 'readme' === $type ) ? 'Readme' : 'Code',
     78            'short' => true,
     79        ],
     80        [
     81            'title' => 'Version',
     82            'value' => $tag,
     83            'short' => true,
     84        ],
     85    ];
     86}
     87
    5288echo "Processing I18N Import for $plugin_slug...\n";
    5389try {
     
    6096    }
    6197
    62     echo "OK. Took " . round( microtime(1) - $start_time, 2 )  . "s\n";
    63 } catch( \Exception $e ) {
    64     echo "Failed. Took " . round( microtime(1) - $start_time, 2 )  . "s\n";
     98    $runtime = round( microtime( 1 ) - $start_time, 2 );
     99
     100    // Send Slack notification.
     101    if ( $send_slack ) {
     102        $fields[] = [
     103            'title' => 'Status',
     104            'value' => sprintf( '%s Successfully imported! (%ss)', $slack_client->get_success_emoji(), $runtime ),
     105            'short' => false,
     106        ];
     107        $fields[] = [
     108            'title' => 'Plugin',
     109            'value' => sprintf(
     110                '<%1$s|%2$s> | <https://plugins.trac.wordpress.org/log/%3$s|Log> | <%4$s|SVN>',
     111                get_permalink( $plugin ),
     112                $plugin->post_title,
     113                $plugin_slug,
     114                $importer->get_plugin_svn_url( $tag )
     115            ),
     116            'short' => false,
     117        ];
     118        $slack_client->add_attachment( 'fields', $fields );
     119        $slack_client->set_status( 'success' );
     120        $slack_client->send( '#meta-language-packs' );
     121    }
     122
     123    echo "OK. Took {$runtime}s\n";
     124} catch ( Exception $e ) {
     125    $runtime = round( microtime( 1 ) - $start_time, 2 );
     126
     127    // Send Slack notification.
     128    if ( $send_slack ) {
     129        $fields[] = [
     130            'title' => 'Status',
     131            'value' => sprintf( '%s %s (%ss)', $slack_client->get_failure_emoji(), $e->getMessage(), $runtime ),
     132            'short' => false,
     133        ];
     134        $fields[] = [
     135            'title' => 'Plugin',
     136            'value' => sprintf(
     137                '<%1$s|%2$s> | <https://plugins.trac.wordpress.org/log/%3$s|Log> | <%4$s|SVN>',
     138                get_permalink( $plugin ),
     139                $plugin->post_title,
     140                $plugin_slug,
     141                $importer->get_plugin_svn_url( $tag )
     142            ),
     143            'short' => false,
     144        ];
     145        $slack_client->add_attachment( 'fields', $fields );
     146        $slack_client->set_status( 'failure' );
     147        $slack_client->send( '#meta-language-packs' );
     148    }
     149
     150    echo "Failed. Took {$runtime}s\n";
    65151
    66152    fwrite( STDERR, "[{$plugin_slug}] Plugin I18N Import Failed: " . $e->getMessage() . "\n" );
    67     exit(1);
     153    exit( 2 );
    68154}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/i18n/class-code-import.php

    r5444 r5446  
    5050        $makepot  = new \MakePOT;
    5151
    52         if ( $makepot->wp_plugin( $export_directory, $pot_file, $this->plugin ) || ! file_exists( $pot_file ) ) {
     52        if ( ! $makepot->wp_plugin( $export_directory, $pot_file, $this->plugin ) || ! file_exists( $pot_file ) ) {
    5353            throw new Exception( "POT file couldn't be created." );
    5454        }
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/clients/class-slack.php

    r4223 r5446  
    2121     * @var array
    2222     */
    23     private $attachment = array();
     23    private $attachment = [];
    2424
    2525    /**
     
    2828     * @var array
    2929     */
    30     private $text = array();
     30    private $text = [];
     31
     32    /**
     33     * Holds Emoji codes for success.
     34     *
     35     * @var array
     36     */
     37    private $success_emoji = [
     38        ':green_heart:', ':white_check_mark:', ':smiley:', ':ok: ',
     39    ];
     40
     41    /**
     42     * Holds Emoji codes for failure.
     43     *
     44     * @var array
     45     */
     46    private $failure_emoji = [
     47        ':broken_heart:', ':umbrella_with_rain_drops:', ':cry:', ':sos:',
     48    ];
    3149
    3250    /**
     
    6381     *
    6482     * @param string $key   Key of the attachment property.
    65      * @param string $value Value of the attachment property.
     83     * @param mixed $value Value of the attachment property.
    6684     */
    6785    public function add_attachment( $key, $value ) {
     
    98116
    99117    /**
     118     * Returns a random emoji for a failure message.
     119     *
     120     * @return string Emoji code.
     121     */
     122    public function get_failure_emoji() {
     123        $index = array_rand( $this->failure_emoji, 1 );
     124        return $this->failure_emoji[ $index ];
     125    }
     126
     127    /**
     128     * Returns a random emoji for a success message.
     129     *
     130     * @return string Emoji code.
     131     */
     132    public function get_success_emoji() {
     133        $index = array_rand( $this->success_emoji, 1 );
     134        return $this->success_emoji[ $index ];
     135    }
     136
     137    /**
    100138     * Publishes a Slack notifcation to a channel.
    101139     *
     
    105143    public function send( $channel ) {
    106144        $text = $this->get_text();
    107         if ( empty( $text ) ) {
    108             return false;
     145        if ( ! empty( $text ) ) {
     146            $this->add_attachment( 'text', $text );
    109147        }
    110148
    111         $this->add_attachment( 'text', $text );
    112         $this->add_attachment( 'mrkdwn_in', array( 'text' ) );
     149        $this->add_attachment( 'mrkdwn_in', [ 'text', 'fields' ] );
    113150
    114         $payload = array(
     151        $payload = [
    115152            'icon_emoji'  => ':wordpress:',
    116153            'username'    => 'Plugin Imports',
    117154            'channel'     => $channel,
    118155            'attachments' => $this->get_attachments(),
    119         );
     156        ];
    120157
    121158        $payload = json_encode( $payload );
    122159        $content = http_build_query( compact( 'payload' ) );
    123160
    124         $context = stream_context_create( array(
    125             'http' => array(
     161        $context = stream_context_create( [
     162            'http' => [
    126163                'method'  => 'POST',
    127164                'header'  => 'Content-Type: application/x-www-form-urlencoded' . PHP_EOL,
    128165                'content' => $content,
    129             ),
    130         ) );
     166            ],
     167        ] );
    131168
    132169        $this->flush();
     
    139176     */
    140177    public function flush() {
    141         $this->text       = array();
    142         $this->attachment = array();
     178        $this->text       = [];
     179        $this->attachment = [];
    143180    }
    144181}
Note: See TracChangeset for help on using the changeset viewer.