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

Cache certain GitHub URLs #385

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ composer.lock
phpunit.xml
phpcs.xml
.phpcs.xml
.phpunit.result.cache
17 changes: 17 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/4.8/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTodoAnnotatedTests="true"
colors="true"
verbose="true">
<testsuites>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
</phpunit>
20 changes: 20 additions & 0 deletions src/WP_CLI/CommandWithUpgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@
add_filter( 'upgrader_source_selection', $filter, 10, 3 );
}

// Check if the URL the URL is cachable and whitelist it then.
self::maybe_cache( $slug, $this->item_type );

if ( $file_upgrader->install( $slug ) ) {
$slug = $file_upgrader->result['destination_name'];
$result = true;
Expand Down Expand Up @@ -771,4 +774,21 @@
// phpcs:ignore WordPress.WP.AlternativeFunctions.parse_url_parse_url -- parse_url will only be used in absence of wp_parse_url.
return function_exists( 'wp_parse_url' ) ? wp_parse_url( $url, $component ) : parse_url( $url, $component );
}

/**
* Whitelist GitHub URLs for caching.
*
* @param string $url The URL to check.
*/
public static function maybe_cache( $url, $item_type ) {
$matches = [];

// cache release URLs like `https://github.com/wp-cli-test/generic-example-plugin/releases/download/v0.1.0/generic-example-plugin.0.1.0.zip`
if ( preg_match( '#github\.com/[^/]+/([^/]+)/releases/download/tags/([^/]+)/(.+)\.zip#', $url, $matches ) ) {
WP_CLI::get_http_cache_manager()->whitelist_package( $url, $item_type, $matches[2], $matches[3] );
// cache archive URLs like `https://github.com/wp-cli-test/generic-example-plugin/archive/v0.1.0.zip`

Check failure on line 789 in src/WP_CLI/CommandWithUpgrade.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Line indented incorrectly; expected at least 3 tabs, found 2
} elseif ( preg_match( '#github\.com/[^/]+/([^/]+)/archive/(version/|)([^/]+)\.zip#', $url, $matches ) ) {
WP_CLI::get_http_cache_manager()->whitelist_package( $url, $item_type, $matches[1], $matches[3] );
}
}
}
11 changes: 11 additions & 0 deletions tests/PluginCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace WP_CLI\Extension\Tests;

use WP_CLI\Tests\TestCase;

class PluginCommandTest extends TestCase {
public function test_sample() {
$this->assertTrue( true );
}
}
Loading