Skip to content

Commit aa69496

Browse files
authored
fix: normalize mounted workspace writes (#614)
* fix: normalize mounted workspace writes * style: align workspace ability schemas
1 parent eddfd92 commit aa69496

2 files changed

Lines changed: 36 additions & 13 deletions

File tree

inc/Abilities/WorkspaceAbilities.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,17 @@ private function registerAbilities(): void {
194194
'output_schema' => array(
195195
'type' => 'object',
196196
'properties' => array(
197-
'success' => array( 'type' => 'boolean' ),
198-
'name' => array( 'type' => 'string' ),
199-
'repo' => array( 'type' => 'string' ),
200-
'is_worktree' => array( 'type' => 'boolean' ),
201-
'path' => array( 'type' => 'string' ),
197+
'success' => array( 'type' => 'boolean' ),
198+
'name' => array( 'type' => 'string' ),
199+
'repo' => array( 'type' => 'string' ),
200+
'is_worktree' => array( 'type' => 'boolean' ),
201+
'path' => array( 'type' => 'string' ),
202202
// Nullable: detached HEAD has no branch; local-only repos
203203
// have no remote; freshly-init'd repos have no commit yet.
204-
'branch' => array( 'type' => array( 'string', 'null' ) ),
205-
'remote' => array( 'type' => array( 'string', 'null' ) ),
206-
'commit' => array( 'type' => array( 'string', 'null' ) ),
207-
'dirty' => array( 'type' => 'integer' ),
204+
'branch' => array( 'type' => array( 'string', 'null' ) ),
205+
'remote' => array( 'type' => array( 'string', 'null' ) ),
206+
'commit' => array( 'type' => array( 'string', 'null' ) ),
207+
'dirty' => array( 'type' => 'integer' ),
208208
'primary_freshness' => self::primaryFreshnessSchema(),
209209
),
210210
),
@@ -390,19 +390,19 @@ private function registerAbilities(): void {
390390
'input_schema' => array(
391391
'type' => 'object',
392392
'properties' => array(
393-
'url' => array(
393+
'url' => array(
394394
'type' => 'string',
395395
'description' => 'Git repository URL to clone.',
396396
),
397-
'name' => array(
397+
'name' => array(
398398
'type' => 'string',
399399
'description' => 'Directory name override (derived from URL if omitted).',
400400
),
401-
'full' => array(
401+
'full' => array(
402402
'type' => 'boolean',
403403
'description' => 'Disable the default blobless partial clone for remote repositories.',
404404
),
405-
'auth_token_env' => array(
405+
'auth_token_env' => array(
406406
'type' => 'string',
407407
'description' => 'Optional environment variable name containing a bearer token for HTTPS clone authentication.',
408408
),
@@ -2665,6 +2665,7 @@ public static function removeRepo( array $input ): array|\WP_Error {
26652665
* @return array Result.
26662666
*/
26672667
public static function writeFile( array $input ): array|\WP_Error {
2668+
$input = self::normalize_mounted_workspace_path_input($input, array( 'repo' ));
26682669
if ( RemoteWorkspaceBackend::should_handle() ) {
26692670
$result = ( new RemoteWorkspaceBackend() )->write_file(
26702671
$input['repo'] ?? '',

tests/smoke-workspace-alias-tools.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
$GLOBALS['dmc_workspace_alias_ability'] = null;
2020
$GLOBALS['dmc_workspace_alias_registered_abilities'] = array();
2121
$GLOBALS['dmc_workspace_alias_remote_edit_input'] = array();
22+
$GLOBALS['dmc_workspace_alias_remote_write_input'] = array();
2223

2324
function add_filter( string $tag, callable $callback, int $priority = 10, int $accepted_args = 1 ): void
2425
{
@@ -127,6 +128,16 @@ public function edit_file( string $handle, string $path, string $old_string, str
127128
'replacements' => 1,
128129
);
129130
}
131+
132+
public function write_file( string $handle, string $path, string $content ): array
133+
{
134+
$GLOBALS['dmc_workspace_alias_remote_write_input'] = compact('handle', 'path', 'content');
135+
return array(
136+
'success' => true,
137+
'name' => $handle,
138+
'path' => $path,
139+
);
140+
}
130141
}
131142
}
132143

@@ -273,6 +284,17 @@ public function execute( array $input ): array
273284
$assert('workspace_edit ability maps old alias to old_string', 'npm install --silent' === ( $GLOBALS['dmc_workspace_alias_remote_edit_input']['old_string'] ?? '' ));
274285
$assert('workspace_edit ability maps new alias to new_string', 'npm install --legacy-peer-deps' === ( $GLOBALS['dmc_workspace_alias_remote_edit_input']['new_string'] ?? '' ));
275286

287+
$ability_mounted_write = \DataMachineCode\Abilities\WorkspaceAbilities::writeFile(
288+
array(
289+
'path' => '/workspace/example-plugin/wordpress/scripts/build/generated.sh',
290+
'content' => '#!/bin/sh',
291+
)
292+
);
293+
$assert('workspace_write ability accepts mounted absolute path', ! is_wp_error($ability_mounted_write) && true === ( $ability_mounted_write['success'] ?? false ));
294+
$assert('workspace_write ability infers repo from mounted path', 'example-plugin' === ( $GLOBALS['dmc_workspace_alias_remote_write_input']['handle'] ?? '' ));
295+
$assert('workspace_write ability converts mounted path to relative path', 'wordpress/scripts/build/generated.sh' === ( $GLOBALS['dmc_workspace_alias_remote_write_input']['path'] ?? '' ));
296+
$assert('workspace_write ability preserves content', '#!/bin/sh' === ( $GLOBALS['dmc_workspace_alias_remote_write_input']['content'] ?? '' ));
297+
276298
$unsupported_alias = \DataMachineCode\Abilities\WorkspaceAbilities::editFile(
277299
array(
278300
'repo' => 'example-plugin',

0 commit comments

Comments
 (0)