Add support to install master branch when using WooCommerce Beta Tester (#38536)

This commit is contained in:
Sam Seay 2023-06-20 14:54:49 +08:00 committed by GitHub
parent 6c3256b413
commit 924b29fd92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 64 additions and 26 deletions

View File

@ -19,15 +19,34 @@ register_woocommerce_admin_test_helper_rest_route(
* API endpoint to fetch the manifest of live branches.
*/
function fetch_live_branches_manifest() {
$response = wp_remote_get( 'https://betadownload.jetpack.me/woocommerce-branches.json' );
$response = wp_remote_get( 'https://betadownload.jetpack.me/woocommerce-branches.json' );
if ( is_wp_error( $response ) ) {
// Handle the error case.
$error_message = $response->get_error_message();
return new WP_REST_Response( array( 'error' => $error_message ), 500 );
}
$body = wp_remote_retrieve_body( $response );
$installer = new WC_Beta_Tester_Live_Branches_Installer();
$obj = json_decode( $body );
if ( json_last_error() !== JSON_ERROR_NONE ) {
// Handle JSON decoding error.
return new WP_REST_Response( array( 'error' => 'Error decoding JSON' ), 500 );
}
// Check if the expected properties exist in the JSON.
if ( ! isset( $obj->pr ) || ! isset( $obj->master ) ) {
return new WP_REST_Response( array( 'error' => 'Missing properties in JSON' ), 500 );
}
foreach ( $obj->pr as $key => $value ) {
$value->install_status = $installer->check_install_status( $value->version );
}
$obj->master->install_status = $installer->check_install_status( $obj->master->version );
return new WP_REST_Response( $obj, 200 );
}

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Add trunk as installable branch to live branches feature.

View File

@ -57,6 +57,10 @@ class WC_Beta_Tester_Live_Branches_Installer {
$obj = json_decode( $body );
if ( $obj->master->branch === $branch ) {
return $obj->master;
}
foreach ( $obj->pr as $key => $value ) {
if ( $value->branch === $branch ) {
return $value;

View File

@ -15,19 +15,15 @@ class WC_Beta_Tester_Live_Branches {
* Constructor.
*/
public function __construct() {
add_action( 'admin_enqueue_scripts', array( $this, 'register_scripts' ) );
// By the time this code runs it appears too late to hook into `admin_menu`.
$this->register_page();
add_action( 'admin_menu', array( $this, 'register_page' ) );
add_action( 'admin_init', array( $this, 'register_scripts' ) );
}
/**
* Register live branches scripts.
*/
public function register_scripts() {
if ( ! method_exists( 'Automattic\WooCommerce\Admin\PageController', 'is_admin_or_embed_page' ) ||
! \Automattic\WooCommerce\Admin\PageController::is_admin_or_embed_page()
) {
if ( ! is_admin() ) {
return;
}

View File

@ -101,7 +101,6 @@ class WC_Beta_Tester {
* Include any classes we need within admin.
*/
public function includes() {
include_once dirname( __FILE__ ) . '/class-wc-beta-tester-live-branches.php';
include_once dirname( __FILE__ ) . '/class-wc-beta-tester-admin-menus.php';
include_once dirname( __FILE__ ) . '/class-wc-beta-tester-admin-assets.php';
}

View File

@ -1,11 +1,11 @@
/**
* External dependencies
*/
import { Spinner } from '@woocommerce/components';
import {
// @ts-ignore
__experimentalHeading as Heading,
} from '@wordpress/components';
import { Spinner } from '@woocommerce/components';
/**
* Internal dependencies
@ -14,14 +14,16 @@ import { useLiveBranchesData } from './hooks/live-branches';
import { BranchList } from './components/BranchList';
export const App = () => {
const { branches, isLoading } = useLiveBranchesData();
const { branches, isLoading, isError } = useLiveBranchesData();
return (
<>
<Heading level={ 1 }>
Live Branches - Install and test WooCommerce PRs
</Heading>
{ isLoading ? <Spinner /> : <BranchList branches={ branches } /> }
{ isError && <p>Something Went Wrong!</p> }
{ isLoading && <Spinner /> }
{ ! isError && ! isLoading && <BranchList branches={ branches } /> }
</>
);
};

View File

@ -16,31 +16,42 @@ export type Branch = {
install_status: PluginStatus;
};
type LiveBranchesResponse = {
pr: { [ key: string ]: Branch };
master: Branch;
};
export const useLiveBranchesData = () => {
const [ branches, setBranches ] = useState< Branch[] >( [] );
const [ loading, setLoading ] = useState< boolean >( true );
const [ isError, setIsError ] = useState< boolean >( false );
useEffect( () => {
const getBranches = async () => {
const res = await apiFetch( {
path: `${ API_NAMESPACE }/live-branches/manifest/v1`,
method: 'GET',
} );
try {
const res = await apiFetch< LiveBranchesResponse >( {
path: `${ API_NAMESPACE }/live-branches/manifest/v1`,
method: 'GET',
} );
setBranches(
// @ts-ignore
Object.entries( res.pr ).map( ( [ , value ] ) => {
return value;
} ) as Branch[]
);
const prBranches = Object.entries( res.pr ).map(
( [ , value ] ) => {
return value;
}
) as Branch[];
setLoading( false );
setBranches( [ res.master, ...prBranches ] );
setLoading( false );
} catch ( e ) {
setIsError( true );
setLoading( false );
}
};
getBranches();
}, [] );
return { branches, isLoading: loading };
return { branches, isLoading: loading, isError };
};
export const useLiveBranchInstall = (

View File

@ -15,7 +15,7 @@ addFilter( 'woocommerce_admin_pages_list', 'live-branches', ( pages ) => {
wpOpenMenu: 'toplevel_page_woocommerce',
capability: 'read',
breadcrumbs: [ 'Live Branches' ],
navArgs: { id: 'live-branches' },
navArgs: { id: 'woocommerce-beta-tester-live-branches' },
} );
return pages;

View File

@ -132,3 +132,6 @@ add_action(
}
}
);
// Initialize the live branches feature.
require_once dirname( __FILE__ ) . '/includes/class-wc-beta-tester-live-branches.php';

View File

@ -37515,7 +37515,7 @@ packages:
loader-utils: 2.0.4
postcss: 8.4.21
schema-utils: 3.1.1
semver: 7.3.8
semver: 7.5.0
webpack: 5.76.3(uglify-js@3.14.5)(webpack-cli@4.9.2)
dev: true