Making WordPress.org


Ignore:
Timestamp:
03/13/2017 05:56:27 AM (10 years ago)
Author:
dd32
Message:

Plugin Directory: ZIPs: Do not build zips on demand, instead store them within a SVN repository.

See #1578

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php

    r4727 r5147  
    88use WordPressdotorg\Plugin_Directory\Tools\Filesystem;
    99use WordPressdotorg\Plugin_Directory\Tools\SVN;
     10use WordPressdotorg\Plugin_Directory\Zip\Builder;
    1011use Exception;
    1112
    … …  
    4748         * @throws \Exception
    4849         *
    49          * @param string $plugin_slug      The slug of the plugin to import.
    50          * @param array  $svn_changed_tags A list of tags/trunk which the SVN change touched. Optional.
    51          */
    52         public function import_from_svn( $plugin_slug, $svn_changed_tags = array( 'trunk' ) ) {
     50         * @param string $plugin_slug            The slug of the plugin to import.
     51         * @param array  $svn_changed_tags       A list of tags/trunk which the SVN change touched. Optional.
     52         * @param array  $svn_revision_triggered The SVN revision which this import has been triggered by.
     53         */
     54        public function import_from_svn( $plugin_slug, $svn_changed_tags = array( 'trunk' ), $svn_revision_triggered = 0 ) {
    5355                global $wpdb;
    5456
    … …  
    190192                $current_stable_tag = get_post_meta( $plugin->ID, 'stable_tag', true ) ?: 'trunk';
    191193
    192                 $this->rebuild_invalidate_zips( $plugin_slug, $stable_tag, $current_stable_tag, $svn_changed_tags );
     194                $this->rebuild_affected_zips( $plugin_slug, $stable_tag, $current_stable_tag, $svn_changed_tags, $svn_revision_triggered );
    193195
    194196                // Finally, set the new version live.
    … …  
    202204
    203205        /**
    204          * Rebuild and Invalidate plugin ZIPs on all web nodes using the REST API Endpoints.
    205          *
    206          * @param string $plugin_slug        The plugin slug.
    207          * @param string $stable_tag         The new stable tag.
    208          * @param string $current_stable_tag The new stable tag.
    209          * @param array  $svn_changed_tags   The list of SVN tags modified since last import.
    210          */
    211         protected function rebuild_invalidate_zips( $plugin_slug, $stable_tag, $current_stable_tag, $svn_changed_tags ) {
    212                 global $wporg_webs;
    213                 $invalidate_zips = $rebuild_zips = array();
    214 
    215                 foreach ( $svn_changed_tags as $tag ) {
    216                         if ( 'trunk' == $tag ) {
    217                                 if ( 'trunk' == $stable_tag ) {
    218                                         // Trunk is stable, so we'll need to rebuild the zip
    219                                         $rebuild_zips[] = "{$plugin_slug}.zip";
    220                                 } else {
    221                                         // Trunk isn't stable, so we'll just remove it so it's rebuilt on demand
    222                                         $invalidate_zips[] = "{$plugin_slug}.zip";
    223                                 }
    224                                 continue;
    225                         }
    226                         if ( $tag == $stable_tag || $tag == $current_stable_tag ) {
    227                                 $rebuild_zips[] = "{$plugin_slug}.{$tag}.zip";
    228                         } else {
    229                                 $invalidate_zips[] = "{$plugin_slug}.{$tag}.zip";
    230                         }
    231                 }
    232                 if ( $stable_tag != $current_stable_tag ) {
    233                         // plugin is updated, ensure that everything is rebuilt.
    234                         if ( ! in_array( $stable_tag, $svn_changed_tags ) ) {
    235                                 $rebuild_zips[] = "{$plugin_slug}" . ( 'trunk' == $stable_tag ? '' : ".{$stable_tag}" ) . '.zip';
    236                         }
    237                 }
    238 
    239                 if ( empty( $wporg_webs ) || ( empty( $invalidate_zips ) && empty( $rebuild_zips ) ) ) {
    240                         return;
    241                 }
    242 
    243                 $urls = array();
    244                 foreach ( $wporg_webs as $node ) {
    245                         $urls[] = preg_replace( '!^https?://wordpress.org/!', "http://$node/", site_url( '/wp-json/plugins/v1/zip-management' ) );
    246                 }
    247                 $headers = array(
    248                         'User-Agent' => 'WordPress.org Plugin Directory',
    249                         'Host' => 'WordPress.org',
    250                         'Authorization' => 'BEARER ' . PLUGIN_API_INTERNAL_BEARER_TOKEN,
    251                 );
    252                 $body = array(
    253                         'plugins' => array(
    254                                 $plugin_slug => array(
    255                                         'invalidate' => $invalidate_zips,
    256                                         'rebuild' => $rebuild_zips,
    257                                 )
    258                         )
    259                 );
    260 
    261                 $results = array();
    262                 foreach ( $urls as $url ) {
    263                         $results[ $url ] = wp_remote_post( $url, array(
    264                                 'body' => $body,
    265                                 'headers' => $headers,
    266                                 'sslverify' => false
    267                         ) );
    268                 }
    269 
    270                 // TODO Do something with $results to verify all servers said the rebuilt zip was correct or something.
     206         * (Re)build plugin ZIPs affected by this commit.
     207         *
     208         * @param string $plugin_slug            The plugin slug.
     209         * @param string $stable_tag             The new stable tag.
     210         * @param string $current_stable_tag     The new stable tag.
     211         * @param array  $svn_changed_tags       The list of SVN tags modified since last import.
     212         * @param string $svn_revision_triggered The SVN revision which triggered the rebuild.
     213         *
     214         * @return bool
     215         */
     216        protected function rebuild_affected_zips( $plugin_slug, $stable_tag, $current_stable_tag, $svn_changed_tags, $svn_revision_triggered = 0 ) {
     217                $versions_to_build = $svn_changed_tags;
     218
     219                // Ensure that the stable zip is built/rebuilt if need be.
     220                if ( $stable_tag != $current_stable_tag && ! in_array( $stable_tag, $versions_to_build ) ) {
     221                        $versions_to_build[] = $stable_tag;
     222                }
     223
     224                // Rebuild/Build $build_zips
     225                try {
     226                        // This will rebuild the ZIP.
     227                        $zip_builder = new Builder();
     228                        $zip_builder->build(
     229                                $plugin_slug,
     230                                array_unique( $versions_to_build ),
     231                                $svn_revision_triggered ?
     232                                        "{$plugin_slug}: ZIP build triggered by https://plugins.trac.wordpress.org/changeset/{$svn_revision_triggered}" :
     233                                        "{$plugin_slug}: ZIP build triggered by " . php_uname('n')
     234                        );
     235                } catch( Exception $e ) {
     236                        return false;
     237                }
     238
     239                return true;
    271240        }
    272241
Note: See TracChangeset for help on using the changeset viewer.