Skip to content

Commit 00a30a9

Browse files
Refactor worktree active-no-signal input routing (#775)
* Refactor worktree active-no-signal input routing * fix: satisfy worktree dispatch lint --------- Co-authored-by: homeboy-ci[bot] <266378653+homeboy-ci[bot]@users.noreply.github.com>
1 parent 08f322f commit 00a30a9

1 file changed

Lines changed: 66 additions & 32 deletions

File tree

inc/Cli/Commands/WorkspaceCommand.php

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,39 @@ class WorkspaceCommand extends BaseCommand {
3939

4040
private const METADATA_RECONCILE_DEFAULT_BUDGET = '30s';
4141

42-
private const WORKTREE_OPERATION_ABILITIES = array(
43-
'add' => 'datamachine-code/workspace-worktree-add',
44-
'list' => 'datamachine-code/workspace-worktree-list',
45-
'remove' => 'datamachine-code/workspace-worktree-remove',
46-
'prune' => 'datamachine-code/workspace-worktree-prune',
47-
'cleanup' => 'datamachine-code/workspace-worktree-cleanup',
48-
'cleanup-artifacts' => 'datamachine-code/workspace-worktree-cleanup-artifacts',
49-
'bounded-cleanup-eligible-apply' => 'datamachine-code/workspace-worktree-bounded-cleanup-eligible-apply',
50-
'emergency-cleanup' => 'datamachine-code/workspace-worktree-emergency-cleanup',
51-
'reconcile-metadata' => 'datamachine-code/workspace-worktree-reconcile-metadata',
52-
'active-no-signal-report' => 'datamachine-code/workspace-worktree-active-no-signal-report',
53-
'active-no-signal-finalized-apply' => 'datamachine-code/workspace-worktree-active-no-signal-finalized-apply',
54-
'active-no-signal-equivalent-clean-apply' => 'datamachine-code/workspace-worktree-active-no-signal-equivalent-clean-apply',
55-
'active-no-signal-merged-apply' => 'datamachine-code/workspace-worktree-active-no-signal-merged-apply',
56-
'active-no-signal-remote-clean-apply' => 'datamachine-code/workspace-worktree-active-no-signal-remote-clean-apply',
57-
'refresh-context' => 'datamachine-code/workspace-worktree-refresh-context',
58-
'finalize' => 'datamachine-code/workspace-worktree-finalize',
59-
'mark-cleanup-eligible' => 'datamachine-code/workspace-worktree-finalize',
42+
private const WORKTREE_OPERATIONS = array(
43+
'add' => array( 'ability' => 'datamachine-code/workspace-worktree-add' ),
44+
'list' => array( 'ability' => 'datamachine-code/workspace-worktree-list' ),
45+
'remove' => array( 'ability' => 'datamachine-code/workspace-worktree-remove' ),
46+
'prune' => array( 'ability' => 'datamachine-code/workspace-worktree-prune' ),
47+
'cleanup' => array( 'ability' => 'datamachine-code/workspace-worktree-cleanup' ),
48+
'cleanup-artifacts' => array( 'ability' => 'datamachine-code/workspace-worktree-cleanup-artifacts' ),
49+
'bounded-cleanup-eligible-apply' => array( 'ability' => 'datamachine-code/workspace-worktree-bounded-cleanup-eligible-apply' ),
50+
'emergency-cleanup' => array( 'ability' => 'datamachine-code/workspace-worktree-emergency-cleanup' ),
51+
'reconcile-metadata' => array( 'ability' => 'datamachine-code/workspace-worktree-reconcile-metadata' ),
52+
'active-no-signal-report' => array(
53+
'ability' => 'datamachine-code/workspace-worktree-active-no-signal-report',
54+
'input_builder' => 'build_worktree_active_no_signal_input',
55+
),
56+
'active-no-signal-finalized-apply' => array(
57+
'ability' => 'datamachine-code/workspace-worktree-active-no-signal-finalized-apply',
58+
'input_builder' => 'build_worktree_active_no_signal_input',
59+
),
60+
'active-no-signal-equivalent-clean-apply' => array(
61+
'ability' => 'datamachine-code/workspace-worktree-active-no-signal-equivalent-clean-apply',
62+
'input_builder' => 'build_worktree_active_no_signal_input',
63+
),
64+
'active-no-signal-merged-apply' => array(
65+
'ability' => 'datamachine-code/workspace-worktree-active-no-signal-merged-apply',
66+
'input_builder' => 'build_worktree_active_no_signal_input',
67+
),
68+
'active-no-signal-remote-clean-apply' => array(
69+
'ability' => 'datamachine-code/workspace-worktree-active-no-signal-remote-clean-apply',
70+
'input_builder' => 'build_worktree_active_no_signal_input',
71+
),
72+
'refresh-context' => array( 'ability' => 'datamachine-code/workspace-worktree-refresh-context' ),
73+
'finalize' => array( 'ability' => 'datamachine-code/workspace-worktree-finalize' ),
74+
'mark-cleanup-eligible' => array( 'ability' => 'datamachine-code/workspace-worktree-finalize' ),
6075
);
6176

6277
private ?CleanupRunEvidenceStoreInterface $cleanup_run_evidence_store = null;
@@ -3608,7 +3623,8 @@ public function worktree( array $args, array $assoc_args ): void {
36083623
return;
36093624
}
36103625

3611-
$ability_name = self::WORKTREE_OPERATION_ABILITIES[ $operation ] ?? '';
3626+
$operation_config = self::WORKTREE_OPERATIONS[ $operation ] ?? array();
3627+
$ability_name = (string) ( $operation_config['ability'] ?? '' );
36123628

36133629
if ( '' === $ability_name ) {
36143630
WP_CLI::error(sprintf('Unknown worktree operation: %s', $operation));
@@ -3621,7 +3637,11 @@ public function worktree( array $args, array $assoc_args ): void {
36213637
return;
36223638
}
36233639

3624-
$input = array();
3640+
$input = array();
3641+
$input_builder = (string) ( $operation_config['input_builder'] ?? '' );
3642+
if ( '' !== $input_builder && method_exists($this, $input_builder) ) {
3643+
$input = $this->{$input_builder}($operation, $assoc_args);
3644+
}
36253645

36263646
switch ( $operation ) {
36273647
case 'add':
@@ -3807,18 +3827,6 @@ public function worktree( array $args, array $assoc_args ): void {
38073827
case 'active-no-signal-equivalent-clean-apply':
38083828
case 'active-no-signal-merged-apply':
38093829
case 'active-no-signal-remote-clean-apply':
3810-
if ( in_array($operation, array( 'active-no-signal-finalized-apply', 'active-no-signal-equivalent-clean-apply', 'active-no-signal-merged-apply', 'active-no-signal-remote-clean-apply' ), true) ) {
3811-
$input['dry_run'] = ! empty($assoc_args['dry-run']);
3812-
}
3813-
if ( isset($assoc_args['limit']) ) {
3814-
$input['limit'] = (int) $assoc_args['limit'];
3815-
}
3816-
if ( isset($assoc_args['offset']) ) {
3817-
$input['offset'] = (int) $assoc_args['offset'];
3818-
}
3819-
if ( isset($assoc_args['until-budget']) && '' !== trim( (string) $assoc_args['until-budget']) ) {
3820-
$input['until_budget'] = trim( (string) $assoc_args['until-budget']);
3821-
}
38223830
break;
38233831

38243832
case 'cleanup-artifacts':
@@ -3885,6 +3893,32 @@ public function worktree( array $args, array $assoc_args ): void {
38853893
$this->renderWorktreeResult($operation, $result, $assoc_args);
38863894
}
38873895

3896+
/**
3897+
* Build shared input for active-no-signal worktree operations.
3898+
*
3899+
* @param string $operation Worktree operation.
3900+
* @param array<string,mixed> $assoc_args CLI args.
3901+
* @return array<string,mixed>
3902+
*/
3903+
private function build_worktree_active_no_signal_input( string $operation, array $assoc_args ): array {
3904+
$input = array();
3905+
3906+
if ( in_array($operation, array( 'active-no-signal-finalized-apply', 'active-no-signal-equivalent-clean-apply', 'active-no-signal-merged-apply', 'active-no-signal-remote-clean-apply' ), true) ) {
3907+
$input['dry_run'] = ! empty($assoc_args['dry-run']);
3908+
}
3909+
if ( isset($assoc_args['limit']) ) {
3910+
$input['limit'] = (int) $assoc_args['limit'];
3911+
}
3912+
if ( isset($assoc_args['offset']) ) {
3913+
$input['offset'] = (int) $assoc_args['offset'];
3914+
}
3915+
if ( isset($assoc_args['until-budget']) && '' !== trim( (string) $assoc_args['until-budget']) ) {
3916+
$input['until_budget'] = trim( (string) $assoc_args['until-budget']);
3917+
}
3918+
3919+
return $input;
3920+
}
3921+
38883922
/**
38893923
* Run the abandoned-worktree cleanup ability from CLI flags.
38903924
*

0 commit comments

Comments
 (0)