1212
1313defined ('ABSPATH ' ) || exit;
1414
15+ require_once __DIR__ . '/WorkspaceHandle.php ' ;
16+
1517class RunnerWorkspacePublisher {
1618
1719 /**
@@ -41,13 +43,13 @@ public function publish( array $input ): array|\WP_Error {
4143 return new \WP_Error ('runner_workspace_publish_missing_pr_title ' , 'pr_title is required. ' , array ( 'status ' => 400 ));
4244 }
4345
44- $ base = trim ( (string ) ( $ input ['base ' ] ?? $ input ['base_branch ' ] ?? $ input ['base_ref ' ] ?? '' ) );
46+ $ base = trim ( (string ) ( $ input ['base ' ] ?? $ input ['base_branch ' ] ?? $ input ['base_ref ' ] ?? '' ) );
4547 $ target = $ this ->resolve_publication_target ($ input , $ handle , $ target_repo , $ base );
4648 if ( is_wp_error ($ target ) ) {
4749 return $ target ;
4850 }
4951 $ branch = $ target ['push_branch ' ];
50- $ body = $ this ->build_pull_request_body ( (string ) ( $ input ['pr_body ' ] ?? $ input ['body ' ] ?? '' ), $ input );
52+ $ body = $ this ->build_pull_request_body ( (string ) ( $ input ['pr_body ' ] ?? $ input ['body ' ] ?? '' ), $ input );
5153
5254 $ status = WorkspaceAbilities::gitStatus (array ( 'name ' => $ handle ));
5355 if ( is_wp_error ($ status ) ) {
@@ -175,7 +177,8 @@ private function resolve_publication_target( array $input, string $handle, strin
175177 return $ head ;
176178 }
177179
178- $ base_owner = strtolower (strtok ($ base_repo , '/ ' ) ?: '' );
180+ $ base_owner = strtok ($ base_repo , '/ ' );
181+ $ base_owner = false === $ base_owner ? '' : strtolower ($ base_owner );
179182 $ head_owner = $ head ['owner ' ];
180183 $ head_repo = null === $ head_owner ? $ base_repo : $ head_owner . '/ ' . substr ($ base_repo , (int ) strpos ($ base_repo , '/ ' ) + 1 );
181184
@@ -198,12 +201,12 @@ private function resolve_publication_target( array $input, string $handle, strin
198201 $ head_ref = null === $ head_owner ? $ push_branch : $ head_owner . ': ' . $ head ['branch ' ];
199202
200203 return array (
201- 'base_repo ' => $ base_repo ,
202- 'base_ref ' => $ base ,
203- 'head_repo ' => $ head_repo ,
204- 'head_ref ' => $ head_ref ,
205- 'push_remote ' => $ push_remote ,
206- 'push_branch ' => $ push_branch ,
204+ 'base_repo ' => $ base_repo ,
205+ 'base_ref ' => $ base ,
206+ 'head_repo ' => $ head_repo ,
207+ 'head_ref ' => $ head_ref ,
208+ 'push_remote ' => $ push_remote ,
209+ 'push_branch ' => $ push_branch ,
207210 );
208211 }
209212
@@ -220,18 +223,25 @@ private function resolve_head_branch( array $input, string $handle ): array|\WP_
220223 if ( '' === $ owner || '' === $ head_branch ) {
221224 return new \WP_Error ('runner_workspace_publish_invalid_head_branch ' , 'Head must be a branch or owner:branch. ' , array ( 'status ' => 400 ));
222225 }
223- return array ( 'owner ' => $ owner , 'branch ' => $ head_branch );
226+ return array (
227+ 'owner ' => $ owner ,
228+ 'branch ' => $ head_branch ,
229+ );
224230 }
225231
226- return array ( 'owner ' => null , 'branch ' => $ branch );
232+ return array (
233+ 'owner ' => null ,
234+ 'branch ' => $ branch ,
235+ );
227236 }
228237 }
229238
230- if ( str_contains ($ handle , '@ ' ) ) {
231- $ slug = substr ($ handle , (int ) strpos ($ handle , '@ ' ) + 1 );
232- if ( '' !== $ slug ) {
233- return array ( 'owner ' => null , 'branch ' => $ slug );
234- }
239+ $ workspace_handle = WorkspaceHandle::parse ($ handle );
240+ if ( null !== $ workspace_handle ->branch_slug () && '' !== $ workspace_handle ->branch_slug () ) {
241+ return array (
242+ 'owner ' => null ,
243+ 'branch ' => $ workspace_handle ->branch_slug (),
244+ );
235245 }
236246
237247 return new \WP_Error ('runner_workspace_publish_missing_head_branch ' , 'A head branch/ref or branch context is required. ' , array ( 'status ' => 400 ));
0 commit comments