Commit d8cd74e
committed
feat(scan): add --apply + structured updates for auto-update bot workflows
Enables `socket-patch scan` as the engine for an automated "update all
patches" workflow — a cron job or PR check that runs scan, detects new
or updated patches against the local manifest, applies them, and either
commits the change or opens a PR. Today this isn't quite possible because:
* `scan --json` is read-only — it prints the discovery JSON and exits
before the apply path runs, so there's no clean way to make it
mutate the manifest from a bot.
* Updates aren't reported in JSON — update detection (existing
manifest entry with same PURL but different UUID) only runs in the
non-JSON table-print path, so a `--json` consumer can't tell which
patches would be updates vs net-new additions.
* Per-patch JSON records lose the added-vs-updated distinction — every
successful download is reported as `action: "added"` even when it's
replacing an existing entry with a newer UUID.
Three additive (semver-MINOR) changes resolve all of the above:
1. `commands/get.rs` — `download_and_apply_patches` now emits per-patch
`{action: "updated", oldUuid}` when the PURL already had a different
UUID before insert. A new pure helper `decide_patch_action(manifest,
purl, new_uuid)` returns `Added | Updated{old_uuid} | Skipped` and is
unit-tested independently.
2. `commands/scan.rs` — new `--apply` flag (default `false`) opts JSON
callers into the full discover → select → apply pipeline. Without
`--apply`, `scan --json` keeps its prior read-only contract; with it,
`scan --json --apply` runs the same selection + download path the
non-JSON branch uses and emits one combined JSON object with an
`apply` sub-object reporting per-patch outcomes. The JSON discovery
emission also now always includes a top-level `updates` array (with
`purl`, `oldUuid`, `newUuid`) computed via a new pure helper
`detect_updates`. `severity_order` is exposed as `pub(crate)` so it
can be unit-tested.
3. `CLI_CONTRACT.md` documents the new `--apply` flag, the full
`scan` discovery and `--apply` JSON shapes, and pins the per-patch
action vocabulary (`added`/`updated`/`skipped`/`failed`) with semver
policy clauses for adding (MINOR) or renaming/removing (MAJOR) values.
## Tests
* scan.rs inline #[cfg(test)] mod tests — 4 severity_order cases +
8 detect_updates cases covering: no manifest, empty packages, no
overlap, same UUID, different UUID, multiple updates, empty patch
list, first-patch candidate selection.
* get.rs inline test module — 4 decide_patch_action cases covering
Added (no existing entry), Skipped (same UUID), Updated (different
UUID with oldUuid populated), and Added-for-different-PURL (keying
on PURL not UUID).
* tests/cli_parse_scan.rs — `--apply` parser tests (defaults false,
long form, combines with --json/--yes) + a subprocess JSON-shape
test that runs the compiled binary against an empty tempdir and
asserts the new `updates: []` key is present in stdout.
All 416 lib tests pass, all integration tests pass, clippy clean.
## How a bot uses this
```bash
socket-patch scan --json --apply --yes > scan-result.json
jq '.apply.patches[] | select(.action == "updated") | {purl, oldUuid, uuid}' scan-result.json
# Pipe into peter-evans/create-pull-request with a PR body summarizing the diff.
```
Exit code: 0 on full success (every selected patch added/updated/skipped),
1 if any `failed` records are present (and top-level `status` becomes
`"partial_failure"`).
Assisted-by: Claude Code:claude-opus-4-71 parent b96a13f commit d8cd74e
4 files changed
Lines changed: 540 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
86 | 89 | | |
87 | 90 | | |
88 | 91 | | |
| |||
212 | 215 | | |
213 | 216 | | |
214 | 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 | + | |
215 | 283 | | |
216 | 284 | | |
217 | 285 | | |
| |||
235 | 303 | | |
236 | 304 | | |
237 | 305 | | |
| 306 | + | |
238 | 307 | | |
239 | 308 | | |
240 | 309 | | |
241 | 310 | | |
242 | 311 | | |
| 312 | + | |
243 | 313 | | |
244 | 314 | | |
245 | 315 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 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 | + | |
19 | 49 | | |
20 | 50 | | |
21 | 51 | | |
| |||
335 | 365 | | |
336 | 366 | | |
337 | 367 | | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
344 | 373 | | |
345 | 374 | | |
346 | 375 | | |
| |||
458 | 487 | | |
459 | 488 | | |
460 | 489 | | |
461 | | - | |
462 | | - | |
463 | | - | |
464 | | - | |
465 | | - | |
466 | | - | |
467 | | - | |
468 | | - | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
469 | 514 | | |
470 | 515 | | |
471 | 516 | | |
| |||
1451 | 1496 | | |
1452 | 1497 | | |
1453 | 1498 | | |
| 1499 | + | |
| 1500 | + | |
| 1501 | + | |
| 1502 | + | |
| 1503 | + | |
| 1504 | + | |
| 1505 | + | |
| 1506 | + | |
| 1507 | + | |
| 1508 | + | |
| 1509 | + | |
| 1510 | + | |
| 1511 | + | |
| 1512 | + | |
| 1513 | + | |
| 1514 | + | |
| 1515 | + | |
| 1516 | + | |
| 1517 | + | |
| 1518 | + | |
| 1519 | + | |
| 1520 | + | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
| 1526 | + | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
| 1530 | + | |
| 1531 | + | |
| 1532 | + | |
| 1533 | + | |
| 1534 | + | |
| 1535 | + | |
| 1536 | + | |
| 1537 | + | |
| 1538 | + | |
| 1539 | + | |
| 1540 | + | |
| 1541 | + | |
| 1542 | + | |
| 1543 | + | |
| 1544 | + | |
| 1545 | + | |
| 1546 | + | |
| 1547 | + | |
| 1548 | + | |
| 1549 | + | |
| 1550 | + | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
| 1555 | + | |
| 1556 | + | |
| 1557 | + | |
| 1558 | + | |
| 1559 | + | |
| 1560 | + | |
1454 | 1561 | | |
0 commit comments