-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorktreeInventoryRepository.php
More file actions
302 lines (264 loc) · 10.6 KB
/
Copy pathWorktreeInventoryRepository.php
File metadata and controls
302 lines (264 loc) · 10.6 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
<?php
/**
* DB-backed workspace worktree inventory.
*
* @package DataMachineCode\Storage
*/
namespace DataMachineCode\Storage;
defined( 'ABSPATH' ) || exit;
class WorktreeInventoryRepository {
public const TABLE = 'datamachine_code_worktrees';
/**
* Install or upgrade the worktree inventory table.
*/
public static function install_schema(): void {
global $wpdb;
if ( ! isset( $wpdb ) ) {
return;
}
$table = self::table_name();
$charset_collate = method_exists( $wpdb, 'get_charset_collate' ) ? $wpdb->get_charset_collate() : '';
$sql = "CREATE TABLE {$table} (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
handle varchar(191) NOT NULL,
repo varchar(191) NOT NULL DEFAULT '',
branch varchar(191) DEFAULT NULL,
path text NOT NULL,
primary_path text DEFAULT NULL,
is_primary tinyint(1) NOT NULL DEFAULT 0,
lifecycle_state varchar(64) DEFAULT NULL,
origin_site varchar(191) DEFAULT NULL,
origin_agent varchar(191) DEFAULT NULL,
origin_session varchar(191) DEFAULT NULL,
task_url text DEFAULT NULL,
task_ref varchar(191) DEFAULT NULL,
pr_url text DEFAULT NULL,
created_at datetime DEFAULT NULL,
last_seen_at datetime DEFAULT NULL,
last_probe_at datetime DEFAULT NULL,
last_probe_status varchar(64) DEFAULT NULL,
dirty_count int(11) DEFAULT NULL,
unpushed_count int(11) DEFAULT NULL,
artifact_count int(11) DEFAULT NULL,
artifact_size_bytes bigint(20) unsigned DEFAULT NULL,
size_bytes bigint(20) unsigned DEFAULT NULL,
cleanup_signal varchar(64) DEFAULT NULL,
missing_path tinyint(1) NOT NULL DEFAULT 0,
metadata longtext DEFAULT NULL,
updated_at datetime NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY handle (handle),
KEY repo (repo),
KEY lifecycle_state (lifecycle_state),
KEY last_seen_at (last_seen_at),
KEY missing_path (missing_path)
) {$charset_collate};";
if ( ! function_exists( 'dbDelta' ) && defined( 'ABSPATH' ) && file_exists( ABSPATH . 'wp-admin/includes/upgrade.php' ) ) {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
}
if ( function_exists( 'dbDelta' ) ) {
dbDelta( $sql );
} else {
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Static schema string.
$wpdb->query( $sql );
}
if ( function_exists( 'update_option' ) ) {
update_option( 'datamachine_code_worktrees_schema_version', '1' );
}
}
/**
* Return the prefixed table name.
*/
public static function table_name(): string {
global $wpdb;
$prefix = isset( $wpdb->prefix ) ? (string) $wpdb->prefix : '';
return $prefix . self::TABLE;
}
/**
* Upsert one inventory row.
*
* @param array<string,mixed> $row Inventory row.
* @return bool
*/
public function upsert( array $row ): bool {
global $wpdb;
if ( ! isset( $wpdb ) ) {
return false;
}
$data = $this->normalize_row( $row );
if ( '' === $data['handle'] ) {
return false;
}
if ( method_exists( $wpdb, 'replace' ) ) {
$result = $wpdb->replace( self::table_name(), $data );
return false !== $result;
}
return false;
}
/**
* Delete one inventory row by handle.
*/
public function delete( string $handle ): bool {
global $wpdb;
$handle = trim( $handle );
if ( '' === $handle || ! isset( $wpdb ) || ! method_exists( $wpdb, 'delete' ) ) {
return false;
}
$result = $wpdb->delete( self::table_name(), array( 'handle' => $handle ) );
return false !== $result;
}
/**
* Mark a known row as missing on disk instead of dropping it silently.
*/
public function mark_missing( string $handle ): bool {
global $wpdb;
$handle = trim( $handle );
if ( '' === $handle || ! isset( $wpdb ) || ! method_exists( $wpdb, 'update' ) ) {
return false;
}
$result = $wpdb->update(
self::table_name(),
array(
'missing_path' => 1,
'last_probe_at' => current_time( 'mysql', true ),
'last_probe_status' => 'missing_path',
'updated_at' => current_time( 'mysql', true ),
),
array( 'handle' => $handle )
);
return false !== $result;
}
/**
* Fetch all rows, optionally filtered by repo.
*
* @return array<int,array<string,mixed>>
*/
public function list( ?string $repo = null ): array {
global $wpdb;
if ( ! isset( $wpdb ) || ! method_exists( $wpdb, 'get_results' ) ) {
return array();
}
$table = self::table_name();
if ( null !== $repo && '' !== trim( $repo ) && method_exists( $wpdb, 'prepare' ) ) {
// phpcs:disable WordPress.DB.PreparedSQL -- Table name from $wpdb->prefix, not user input.
$sql = $wpdb->prepare( "SELECT * FROM {$table} WHERE repo = %s ORDER BY handle ASC", $repo );
// phpcs:enable WordPress.DB.PreparedSQL
} else {
$sql = "SELECT * FROM {$table} ORDER BY handle ASC";
}
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Prepared above when dynamic input is present.
$rows = $wpdb->get_results( $sql, ARRAY_A );
return is_array( $rows ) ? array_map( array( $this, 'decode_row' ), $rows ) : array();
}
/**
* Summarize inventory freshness for hygiene output.
*
* @return array<string,mixed>
*/
public function freshness(): array {
$rows = $this->list();
$total = count( $rows );
$missing = 0;
$last_probe = null;
foreach ( $rows as $row ) {
if ( ! empty( $row['missing_path'] ) ) {
++$missing;
}
if ( ! empty( $row['last_probe_at'] ) && ( null === $last_probe || strcmp( (string) $row['last_probe_at'], (string) $last_probe ) > 0 ) ) {
$last_probe = $row['last_probe_at'];
}
}
return array(
'table' => self::table_name(),
'total_rows' => $total,
'missing_paths' => $missing,
'last_probe_at' => $last_probe,
'fresh' => 0 < $total && null !== $last_probe,
);
}
/**
* Normalize runtime/listing rows for DB storage.
*
* @param array<string,mixed> $row Input row.
* @return array<string,mixed>
*/
private function normalize_row( array $row ): array {
$metadata = is_array( $row['metadata'] ?? null ) ? (array) $row['metadata'] : array();
$owner = is_array( $row['owner'] ?? null ) ? (array) $row['owner'] : array();
$session = is_array( $row['session'] ?? null ) ? (array) $row['session'] : array();
$task = is_array( $row['task'] ?? null ) ? (array) $row['task'] : ( is_array( $metadata['origin_task'] ?? null ) ? (array) $metadata['origin_task'] : array() );
return array(
'handle' => (string) ( $row['handle'] ?? $metadata['handle'] ?? '' ),
'repo' => (string) ( $row['repo'] ?? $metadata['repo'] ?? '' ),
'branch' => isset( $row['branch'] ) ? (string) $row['branch'] : ( isset( $metadata['branch'] ) ? (string) $metadata['branch'] : null ),
'path' => (string) ( $row['path'] ?? $metadata['path'] ?? '' ),
'primary_path' => isset( $row['primary_path'] ) ? (string) $row['primary_path'] : null,
'is_primary' => ! empty( $row['is_primary'] ) ? 1 : 0,
'lifecycle_state' => isset( $row['lifecycle_state'] ) ? (string) $row['lifecycle_state'] : ( isset( $metadata['lifecycle_state'] ) ? (string) $metadata['lifecycle_state'] : null ),
'origin_site' => (string) ( $owner['site'] ?? $metadata['origin_site'] ?? $metadata['origin_site_name'] ?? '' ),
'origin_agent' => (string) ( $owner['agent'] ?? $metadata['origin_agent'] ?? '' ),
'origin_session' => isset( $session['primary_id'] ) ? (string) $session['primary_id'] : ( isset( $metadata['origin_session'] ) ? (string) $metadata['origin_session'] : null ),
'task_url' => isset( $task['task_url'] ) ? (string) $task['task_url'] : null,
'task_ref' => isset( $task['task_ref'] ) ? (string) $task['task_ref'] : null,
'pr_url' => isset( $row['pr_url'] ) ? (string) $row['pr_url'] : ( isset( $metadata['pr_url'] ) ? (string) $metadata['pr_url'] : null ),
'created_at' => $this->datetime( $row['created_at'] ?? $metadata['created_at'] ?? null ),
'last_seen_at' => $this->datetime( $row['last_seen_at'] ?? $metadata['last_seen_at'] ?? null ),
'last_probe_at' => current_time( 'mysql', true ),
'last_probe_status' => ! empty( $row['missing_path'] ) ? 'missing_path' : 'present',
'dirty_count' => isset( $row['dirty'] ) ? ( null === $row['dirty'] ? null : (int) $row['dirty'] ) : ( isset( $row['dirty_count'] ) ? (int) $row['dirty_count'] : null ),
'unpushed_count' => isset( $row['unpushed_count'] ) ? (int) $row['unpushed_count'] : null,
'artifact_count' => isset( $row['artifacts'] ) && is_array( $row['artifacts'] ) ? count( $row['artifacts'] ) : ( isset( $row['artifact_count'] ) ? (int) $row['artifact_count'] : null ),
'artifact_size_bytes' => isset( $row['artifact_size_bytes'] ) ? (int) $row['artifact_size_bytes'] : null,
'size_bytes' => isset( $row['size_bytes'] ) ? ( null === $row['size_bytes'] ? null : (int) $row['size_bytes'] ) : null,
'cleanup_signal' => $this->cleanup_signal( $row, $metadata ),
'missing_path' => ! empty( $row['missing_path'] ) ? 1 : 0,
'metadata' => wp_json_encode( $metadata ),
'updated_at' => current_time( 'mysql', true ),
);
}
/**
* Decode JSON columns in DB rows.
*
* @param array<string,mixed> $row DB row.
* @return array<string,mixed>
*/
private function decode_row( array $row ): array {
$decoded = isset( $row['metadata'] ) && is_string( $row['metadata'] ) ? json_decode( $row['metadata'], true ) : null;
$row['metadata'] = is_array( $decoded ) ? $decoded : null;
foreach ( array( 'id', 'is_primary', 'dirty_count', 'unpushed_count', 'artifact_count', 'artifact_size_bytes', 'size_bytes', 'missing_path' ) as $key ) {
if ( isset( $row[ $key ] ) ) {
$row[ $key ] = (int) $row[ $key ];
}
}
return $row;
}
/**
* Normalize ISO or MySQL dates to GMT MySQL format.
*/
private function datetime( mixed $value ): ?string {
if ( ! is_string( $value ) || '' === trim( $value ) ) {
return null;
}
$timestamp = strtotime( $value );
if ( false === $timestamp ) {
return null;
}
return gmdate( 'Y-m-d H:i:s', $timestamp );
}
/**
* Derive the explicit cleanup signal stored in the inventory row.
*
* @param array<string,mixed> $row Inventory row.
* @param array<string,mixed> $metadata Lifecycle metadata.
*/
private function cleanup_signal( array $row, array $metadata ): ?string {
if ( ! empty( $row['cleanup_signal'] ) ) {
return (string) $row['cleanup_signal'];
}
$state = (string) ( $row['lifecycle_state'] ?? $metadata['lifecycle_state'] ?? '' );
if ( in_array( $state, array( 'cleanup_eligible', 'pr_opened', 'merged', 'closed', 'abandoned' ), true ) ) {
return $state;
}
return null;
}
}