Skip to content

Commit

Permalink
Add support to install master branch when using WooCommerce Beta Test…
Browse files Browse the repository at this point in the history
…er (#38536)
  • Loading branch information
samueljseay committed Jun 20, 2023
1 parent 6c3256b commit 924b29f
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 28 deletions.
21 changes: 20 additions & 1 deletion plugins/woocommerce-beta-tester/api/live-branches/manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,34 @@
* 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 );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Add trunk as installable branch to live branches feature.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public function get_branch_info_from_manifest( $branch ) {

$obj = json_decode( $body );

if ( $obj->master->branch === $branch ) {
return $obj->master;
}

foreach ( $obj->pr as $key => $value ) {
if ( $value->branch === $branch ) {
return $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public function __construct() {
* 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';
}
Expand Down
8 changes: 5 additions & 3 deletions plugins/woocommerce-beta-tester/src/live-branches/App.tsx
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 } /> }
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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',
} );

setBranches(
// @ts-ignore
Object.entries( res.pr ).map( ( [ , value ] ) => {
return value;
} ) as Branch[]
);

setLoading( false );
try {
const res = await apiFetch< LiveBranchesResponse >( {
path: `${ API_NAMESPACE }/live-branches/manifest/v1`,
method: 'GET',
} );

const prBranches = Object.entries( res.pr ).map(
( [ , value ] ) => {
return value;
}
) as Branch[];

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 = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions plugins/woocommerce-beta-tester/woocommerce-beta-tester.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,6 @@ function() {
}
}
);

// Initialize the live branches feature.
require_once dirname( __FILE__ ) . '/includes/class-wc-beta-tester-live-branches.php';
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 924b29f

Please sign in to comment.