-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclass-wp-codebox-runner-workspace-adapter.php
More file actions
339 lines (296 loc) · 13.5 KB
/
Copy pathclass-wp-codebox-runner-workspace-adapter.php
File metadata and controls
339 lines (296 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/**
* Runner workspace backend adapter.
*
* @package WPCodebox
*/
defined( 'ABSPATH' ) || exit;
final class WP_Codebox_Runner_Workspace_Adapter {
public const BACKEND_SCHEMA = 'wp-codebox/runner-workspace-backend/v1';
public const BACKEND_VERSION = 1;
private const ABILITY_KEYS = array(
'workspace_adopt',
'workspace_show',
'workspace_clone',
'workspace_worktree_add',
'workspace_git_status',
'workspace_git_diff',
'run_runner_workspace_command',
'publish_runner_workspace',
);
private const ABILITY_NAME_PATTERN = '#^[a-z0-9][a-z0-9._-]*/[a-z0-9][a-z0-9._/-]*$#i';
private const BACKEND_ID_PATTERN = '#^[a-z0-9][a-z0-9._-]*$#i';
/** @return array<string,mixed> */
public static function backend_schema(): array {
return array(
'$id' => self::BACKEND_SCHEMA,
'type' => 'object',
'properties' => array(
'schema' => array( 'type' => 'string', 'const' => self::BACKEND_SCHEMA ),
'version' => array( 'type' => 'integer', 'const' => self::BACKEND_VERSION ),
'id' => array( 'type' => 'string', 'description' => 'Opaque integration-owned backend id.' ),
'workspace_root_constant' => array( 'type' => 'string', 'description' => 'Optional constant name used by the backend to find the workspace root.' ),
'abilities' => array(
'type' => 'object',
'additionalProperties' => array( 'type' => 'string' ),
'description' => 'Private backend operation map keyed by WP Codebox runner workspace operation names.',
),
),
);
}
/** @return array<string,mixed> */
public function prepare( array $input ): array {
$operations = $this->operations();
$adopt_ability = (string) ( $operations[ WP_Codebox_Runner_Workspace_Backend::OPERATION_WORKSPACE_ADOPT ] ?? '' );
$show_ability = (string) ( $operations[ WP_Codebox_Runner_Workspace_Backend::OPERATION_WORKSPACE_SHOW ] ?? '' );
$clone_ability = (string) ( $operations[ WP_Codebox_Runner_Workspace_Backend::OPERATION_WORKSPACE_CLONE ] ?? '' );
$add_ability = (string) ( $operations[ WP_Codebox_Runner_Workspace_Backend::OPERATION_WORKSPACE_WORKTREE_ADD ] ?? '' );
$checkout_path = (string) $input['checkout_path'];
$workspace_root_constant = (string) ( $this->config()['workspace_root_constant'] ?? '' );
if ( '' !== $checkout_path && '' !== $workspace_root_constant && ! defined( $workspace_root_constant ) ) {
define( $workspace_root_constant, rtrim( dirname( $checkout_path ), '/' ) );
}
$required = '' !== $checkout_path ? array( $adopt_ability ) : array( $show_ability, $clone_ability, $add_ability );
foreach ( $required as $ability_name ) {
if ( ! $this->ability_available( $ability_name ) ) {
return $this->failure( 'backend_unavailable', 'wp_codebox_runner_workspace_prepare_backend_unavailable', 'Runner workspace backend is not available for preparation.' );
}
}
if ( '' !== $checkout_path ) {
$adopt = $this->execute(
$adopt_ability,
array(
'path' => $checkout_path,
'name' => $input['repo_slug'],
'repo' => $input['repo'],
'target_repo' => $input['target_repo'],
)
);
if ( empty( $adopt['success'] ) ) {
return $adopt;
}
$result = is_array( $adopt['result'] ?? null ) ? $adopt['result'] : array();
return array(
'success' => true,
'result' => array(
'repo' => (string) $input['repo'],
'target_repo' => (string) $input['target_repo'],
'repo_slug' => (string) $input['repo_slug'],
'branch' => (string) $input['branch'],
'handle' => (string) ( $result['handle'] ?? $result['name'] ?? $input['repo_slug'] ),
'path' => (string) ( $result['path'] ?? $checkout_path ),
),
);
}
$show = $this->execute(
$show_ability,
array(
'name' => $input['repo_slug'],
'repo' => $input['repo'],
'target_repo' => $input['target_repo'],
)
);
if ( empty( $show['success'] ) ) {
if ( '' === (string) $input['clone_url'] ) {
return $this->failure( 'clone_url_required', 'wp_codebox_runner_workspace_prepare_clone_url_required', 'Runner workspace primary is missing and clone_url is empty.' );
}
$clone_input = array(
'url' => $input['clone_url'],
'name' => $input['repo_slug'],
'repo' => $input['repo'],
'target_repo' => $input['target_repo'],
);
if ( '' !== (string) $input['github_token_env'] && '' !== trim( (string) getenv( (string) $input['github_token_env'] ) ) && preg_match( '#^https://github\.com/#', (string) $input['clone_url'] ) ) {
$clone_input['auth_token_env'] = $input['github_token_env'];
}
$clone = $this->execute( $clone_ability, array_filter( $clone_input, static fn( mixed $value ): bool => '' !== $value ) );
if ( empty( $clone['success'] ) ) {
return $clone;
}
}
$worktree_input = array_filter(
array(
'repo' => $input['repo_slug'],
'target_repo' => $input['target_repo'],
'branch' => $input['branch'],
'from' => $input['from'],
'inject_context' => $input['inject_context'],
'bootstrap' => $input['bootstrap'],
'allow_stale' => $input['allow_stale'],
'rebase_base' => $input['rebase_base'],
'force' => $input['force'],
),
static fn( mixed $value ): bool => '' !== $value && null !== $value
);
$worktree = $this->execute( $add_ability, $worktree_input );
if ( empty( $worktree['success'] ) ) {
return $worktree;
}
$result = is_array( $worktree['result'] ?? null ) ? $worktree['result'] : array();
return array(
'success' => true,
'result' => array(
'repo' => (string) $input['repo'],
'target_repo' => (string) $input['target_repo'],
'repo_slug' => (string) $input['repo_slug'],
'branch' => (string) ( $result['branch'] ?? $input['branch'] ),
'handle' => (string) ( $result['handle'] ?? '' ),
'path' => (string) ( $result['path'] ?? '' ),
),
);
}
/** @return array{success:bool,result?:array<string,mixed>,failure_type?:string,error?:array<string,mixed>} */
public function publish( array $input ): array {
return $this->execute_named( WP_Codebox_Runner_Workspace_Backend::OPERATION_PUBLISH_RUNNER_WORKSPACE, $input );
}
/** @return array{success:bool,result?:array<string,mixed>,failure_type?:string,error?:array<string,mixed>} */
public function git_status( string $workspace ): array {
return $this->execute_named( WP_Codebox_Runner_Workspace_Backend::OPERATION_WORKSPACE_GIT_STATUS, array( 'name' => $workspace ) );
}
/** @return array{success:bool,result?:array<string,mixed>,failure_type?:string,error?:array<string,mixed>} */
public function git_diff( array $input ): array {
return $this->execute_named( WP_Codebox_Runner_Workspace_Backend::OPERATION_WORKSPACE_GIT_DIFF, $input );
}
/** @return array{success:bool,result?:array<string,mixed>,failure_type?:string,error?:array<string,mixed>} */
public function run_command( array $input ): array {
return $this->execute_named( WP_Codebox_Runner_Workspace_Backend::OPERATION_RUN_WORKSPACE_COMMAND, $input );
}
/** @return array<string,mixed> */
private function config(): array {
$config = function_exists( 'apply_filters' ) ? apply_filters( 'wp_codebox_runner_workspace_backend', array() ) : array();
$config = is_array( $config ) ? $config : array();
$issues = self::backend_config_issues( $config );
$abilities = $this->normalize_abilities( $config['abilities'] ?? null );
return array(
'schema' => self::BACKEND_SCHEMA,
'version' => self::BACKEND_VERSION,
'id' => is_string( $config['id'] ?? null ) ? trim( $config['id'] ) : '',
'workspace_root_constant' => is_string( $config['workspace_root_constant'] ?? null ) ? trim( $config['workspace_root_constant'] ) : '',
'abilities' => $abilities,
'issues' => $issues,
);
}
/** @return array<string,mixed> */
private function operations(): array {
$config = $this->config();
if ( ! empty( $config['issues'] ) ) {
return array();
}
return is_array( $config['abilities'] ?? null ) ? $config['abilities'] : array();
}
/** @param array<string,mixed> $config @return array<int,array<string,string>> */
public static function backend_config_issues( array $config ): array {
$issues = array();
if ( isset( $config['schema'] ) && self::BACKEND_SCHEMA !== (string) $config['schema'] ) {
$issues[] = array( 'field' => 'schema', 'message' => 'runner workspace backend schema must be ' . self::BACKEND_SCHEMA . '.' );
}
if ( isset( $config['version'] ) && self::BACKEND_VERSION !== (int) $config['version'] ) {
$issues[] = array( 'field' => 'version', 'message' => 'runner workspace backend version must be ' . self::BACKEND_VERSION . '.' );
}
$id = is_string( $config['id'] ?? null ) ? trim( $config['id'] ) : '';
if ( '' !== $id && 1 !== preg_match( self::BACKEND_ID_PATTERN, $id ) ) {
$issues[] = array( 'field' => 'id', 'message' => 'runner workspace backend id must be a lowercase slug.' );
}
if ( isset( $config['workspace_root_constant'] ) && ! is_string( $config['workspace_root_constant'] ) ) {
$issues[] = array( 'field' => 'workspace_root_constant', 'message' => 'runner workspace backend workspace_root_constant must be a string.' );
}
if ( isset( $config['abilities'] ) && ! is_array( $config['abilities'] ) ) {
$issues[] = array( 'field' => 'abilities', 'message' => 'runner workspace backend abilities must be an object.' );
return $issues;
}
foreach ( is_array( $config['abilities'] ?? null ) ? $config['abilities'] : array() as $key => $value ) {
if ( ! in_array( (string) $key, self::ABILITY_KEYS, true ) ) {
$issues[] = array( 'field' => 'abilities.' . (string) $key, 'message' => 'runner workspace backend ability key is not supported.' );
continue;
}
if ( ! is_string( $value ) || '' === trim( $value ) || 1 !== preg_match( self::ABILITY_NAME_PATTERN, trim( $value ) ) ) {
$issues[] = array( 'field' => 'abilities.' . (string) $key, 'message' => 'runner workspace backend ability value must be a namespace/name string.' );
}
}
return $issues;
}
/** @return array{success:bool,result?:array<string,mixed>,failure_type?:string,error?:array<string,mixed>} */
private function execute_named( string $key, array $input ): array {
$operations = $this->operations();
return $this->execute( (string) ( $operations[ $key ] ?? '' ), $input, $key );
}
/** @return array<string,string> */
private function normalize_abilities( mixed $abilities ): array {
if ( ! is_array( $abilities ) ) {
return array();
}
$normalized = array();
foreach ( self::ABILITY_KEYS as $key ) {
$value = $abilities[ $key ] ?? null;
if ( ! is_string( $value ) ) {
continue;
}
$value = trim( $value );
if ( '' !== $value && preg_match( self::ABILITY_NAME_PATTERN, $value ) ) {
$normalized[ $key ] = $value;
}
}
return $normalized;
}
/** @return array{success:bool,result?:array<string,mixed>,failure_type?:string,error?:array<string,mixed>} */
private function execute( string $ability_name, array $input, string $operation = '' ): array {
if ( ! $this->valid_ability_name( $ability_name ) ) {
return $this->failure( 'backend_unavailable', 'wp_codebox_runner_workspace_backend_unavailable', 'Runner workspace backend is not available for this operation.' );
}
$ability = function_exists( 'wp_get_ability' ) ? wp_get_ability( $ability_name ) : null;
if ( ! $ability || ! is_callable( array( $ability, 'execute' ) ) ) {
return $this->failure( 'backend_unavailable', 'wp_codebox_runner_workspace_backend_unavailable', 'Runner workspace backend is not available for this operation.' );
}
$result = $ability->execute( $input );
if ( is_wp_error( $result ) ) {
return array(
'success' => false,
'failure_type' => 'backend_error',
'error' => $this->backend_error( $operation ),
);
}
if ( ! is_array( $result ) ) {
return $this->failure( 'backend_invalid_response', 'wp_codebox_runner_workspace_backend_invalid_response', 'Runner workspace backend returned an invalid response.' );
}
if ( false === ( $result['success'] ?? true ) ) {
return array(
'success' => false,
'failure_type' => 'backend_failed',
'error' => $this->backend_error( $operation ),
);
}
return array( 'success' => true, 'result' => $result );
}
private function ability_available( string $ability_name ): bool {
if ( ! $this->valid_ability_name( $ability_name ) ) {
return false;
}
$ability = function_exists( 'wp_get_ability' ) ? wp_get_ability( $ability_name ) : null;
return $ability && is_callable( array( $ability, 'execute' ) );
}
private function valid_ability_name( string $ability_name ): bool {
return '' !== $ability_name && 1 === preg_match( self::ABILITY_NAME_PATTERN, $ability_name );
}
/** @return array<string,string> */
private function backend_error( string $operation ): array {
return array_filter(
array(
'code' => 'wp_codebox_runner_workspace_backend_error',
'message' => 'Runner workspace backend operation failed.',
'operation' => $operation,
),
static fn( mixed $value ): bool => '' !== $value
);
}
/** @return array{success:bool,failure_type:string,error:array<string,string>} */
private function failure( string $failure_type, string $code, string $message ): array {
return array(
'success' => false,
'failure_type' => $failure_type,
'error' => array(
'code' => $code,
'message' => $message,
),
);
}
}