shipshape-core 0.11.0

Core library for shipshape: contract normalizer, repo-fact detection, audit scoring, release engine, and the versioned protocol DTOs.
Documentation
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! Read-only observation of GitHub Actions runs that own delegated publishes.
//!
//! The destination remains the final authority for delivery, but a GitHub-backed
//! delegated target first resolves the exact tag-triggered workflow run. This keeps
//! "still building" and "workflow failed" from being misreported as a missing
//! destination. Non-GitHub delegated adapters do not use this observer.

use std::fmt::Write as _;

use serde::Deserialize;

use crate::contract::schema::Adapter;
use crate::protocol::reconcile::{DelegatedJobFailure, DelegatedRun, DelegatedRunStatus};

use super::adapters::EffectCtx;

/// Observe the workflow run associated with `v{version}` for a GitHub-backed
/// delegated adapter. Returns `None` for adapters whose delegation is not owned by
/// GitHub Actions.
#[must_use]
pub fn observe_github_run(
    ctx: &EffectCtx<'_>,
    adapter: Adapter,
    version: &str,
) -> Option<DelegatedRun> {
    let workflow = match adapter {
        Adapter::CargoDist => ".github/workflows/release.yml".to_string(),
        Adapter::CargoPublishCi => match cargo_publish_workflow(ctx, version) {
            Ok(path) => path,
            Err(detail) => return Some(DelegatedRun::unknown(None, None, detail)),
        },
        _ => return None,
    };
    Some(observe_workflow(ctx, &workflow, version))
}

fn cargo_publish_workflow(ctx: &EffectCtx<'_>, version: &str) -> Result<String, String> {
    let output = ctx
        .runner
        .run(
            "git",
            &[
                "grep",
                "-l",
                "-e",
                "cargo publish",
                &format!("v{version}"),
                "--",
                ".github/workflows/*.yml",
                ".github/workflows/*.yaml",
            ],
            ctx.repo_root,
        )
        .map_err(|error| format!("could not inspect tag-triggered workflows: {error}"))?;
    if output.status != Some(0) && output.status != Some(1) {
        return Err(format!(
            "could not inspect tag-triggered workflows: {}",
            output.stderr.trim()
        ));
    }
    let mut candidates: Vec<String> = output
        .stdout
        .lines()
        .map(str::trim)
        .filter(|line| !line.is_empty())
        .map(str::to_string)
        .collect();
    candidates.sort();
    candidates.dedup();
    match candidates.as_slice() {
        [only] => Ok(only.clone()),
        [] => Err(
            "no tracked GitHub Actions workflow containing `cargo publish` could be resolved for the cargo-publish-ci target"
                .to_string(),
        ),
        many => Err(format!(
            "more than one GitHub Actions workflow contains `cargo publish`; cannot identify the delegated owner: {}",
            many.join(", ")
        )),
    }
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct RunRow {
    database_id: u64,
    status: String,
    #[serde(default)]
    conclusion: Option<String>,
    head_branch: String,
    head_sha: String,
    url: String,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct RunView {
    conclusion: String,
    url: String,
    jobs: Vec<JobRow>,
}

#[derive(Deserialize)]
struct JobRow {
    name: String,
    status: String,
    conclusion: String,
}

#[allow(clippy::too_many_lines)] // one linear fail-closed query/classification pipeline
fn observe_workflow(ctx: &EffectCtx<'_>, workflow: &str, version: &str) -> DelegatedRun {
    let tag = format!("v{version}");
    let expected_sha = match ctx
        .runner
        .run("git", &["rev-list", "-n", "1", &tag], ctx.repo_root)
    {
        Ok(output) if output.status == Some(0) && !output.stdout.trim().is_empty() => {
            output.stdout.trim().to_string()
        }
        Ok(output) => {
            return DelegatedRun::unknown(
                Some(workflow.to_string()),
                None,
                format!(
                    "could not resolve commit for tag `{tag}`: {}",
                    output.stderr.trim()
                ),
            )
        }
        Err(error) => {
            return DelegatedRun::unknown(
                Some(workflow.to_string()),
                None,
                format!("could not resolve commit for tag `{tag}`: {error}"),
            )
        }
    };
    let workflow_id = workflow.rsplit('/').next().unwrap_or(workflow);
    let output = match ctx.runner.run(
        "gh",
        &[
            "run",
            "list",
            "--workflow",
            workflow_id,
            "--branch",
            &tag,
            "--event",
            "push",
            "--json",
            "databaseId,status,conclusion,headBranch,headSha,url",
            "--limit",
            "20",
        ],
        ctx.repo_root,
    ) {
        Ok(output) => output,
        Err(error) => {
            return DelegatedRun::unknown(
                Some(workflow.to_string()),
                None,
                format!("could not query GitHub Actions runs for `{workflow}`: {error}"),
            )
        }
    };
    if output.status != Some(0) {
        return DelegatedRun::unknown(
            Some(workflow.to_string()),
            None,
            format!(
                "could not query GitHub Actions runs for `{workflow}`: {}",
                output.stderr.trim()
            ),
        );
    }
    let rows: Vec<RunRow> = match serde_json::from_str(&output.stdout) {
        Ok(rows) => rows,
        Err(error) => {
            return DelegatedRun::unknown(
                Some(workflow.to_string()),
                None,
                format!("GitHub Actions returned an unreadable run list for `{workflow}`: {error}"),
            )
        }
    };
    let mut matching = rows
        .into_iter()
        .filter(|run| run.head_branch == tag && run.head_sha == expected_sha);
    let Some(run) = matching.next() else {
        return DelegatedRun {
            provider: "github-actions".to_string(),
            workflow: Some(workflow.to_string()),
            run_id: None,
            url: None,
            status: DelegatedRunStatus::Pending,
            conclusion: None,
            failed_jobs: Vec::new(),
            detail: Some(format!(
                "the `{workflow}` run for tag `{tag}` at commit `{expected_sha}` is not visible yet"
            )),
        };
    };
    if matching.next().is_some() {
        return DelegatedRun::unknown(
            Some(workflow.to_string()),
            None,
            format!(
                "multiple `{workflow}` runs match tag `{tag}` at commit `{expected_sha}`; refusing to guess which run owns this release"
            ),
        );
    }
    classify_run(ctx, workflow, run)
}

#[allow(clippy::too_many_lines)] // keeps terminal run + job evidence assembled in one place
fn classify_run(ctx: &EffectCtx<'_>, workflow: &str, run: RunRow) -> DelegatedRun {
    if matches!(
        run.status.as_str(),
        "queued" | "in_progress" | "waiting" | "requested" | "pending"
    ) {
        return DelegatedRun {
            provider: "github-actions".to_string(),
            workflow: Some(workflow.to_string()),
            run_id: Some(run.database_id),
            url: Some(run.url),
            status: DelegatedRunStatus::Pending,
            conclusion: None,
            failed_jobs: Vec::new(),
            detail: Some(format!("the delegated workflow run is {}", run.status)),
        };
    }
    if run.status != "completed" {
        return DelegatedRun::unknown(
            Some(workflow.to_string()),
            Some(run.database_id),
            format!(
                "GitHub Actions returned unrecognized run status `{}`",
                run.status
            ),
        );
    }
    if run.conclusion.as_deref() == Some("success") {
        return DelegatedRun {
            provider: "github-actions".to_string(),
            workflow: Some(workflow.to_string()),
            run_id: Some(run.database_id),
            url: Some(run.url),
            status: DelegatedRunStatus::Success,
            conclusion: run.conclusion,
            failed_jobs: Vec::new(),
            detail: None,
        };
    }
    let Some(list_conclusion) = run.conclusion.clone() else {
        return DelegatedRun::unknown(
            Some(workflow.to_string()),
            Some(run.database_id),
            "GitHub Actions reported a completed run without a conclusion".to_string(),
        );
    };
    let id = run.database_id.to_string();
    let view = ctx.runner.run(
        "gh",
        &["run", "view", &id, "--json", "status,conclusion,url,jobs"],
        ctx.repo_root,
    );
    let (conclusion, url, failed_jobs, extra) = match view {
        Ok(output) if output.status == Some(0) => {
            match serde_json::from_str::<RunView>(&output.stdout) {
                Ok(view) => {
                    if view.conclusion == "success" {
                        return DelegatedRun {
                            provider: "github-actions".to_string(),
                            workflow: Some(workflow.to_string()),
                            run_id: Some(run.database_id),
                            url: Some(view.url),
                            status: DelegatedRunStatus::Success,
                            conclusion: Some(view.conclusion),
                            failed_jobs: Vec::new(),
                            detail: None,
                        };
                    }
                    let jobs = view
                        .jobs
                        .into_iter()
                        .filter(|job| {
                            job.status == "completed"
                                && !matches!(
                                    job.conclusion.as_str(),
                                    "success" | "skipped" | "neutral"
                                )
                        })
                        .map(|job| DelegatedJobFailure {
                            name: job.name,
                            conclusion: job.conclusion,
                        })
                        .collect();
                    (view.conclusion, Some(view.url), jobs, None)
                }
                Err(error) => (
                    list_conclusion.clone(),
                    Some(run.url.clone()),
                    Vec::new(),
                    Some(format!("could not parse the failed run's jobs: {error}")),
                ),
            }
        }
        Ok(output) => (
            list_conclusion.clone(),
            Some(run.url.clone()),
            Vec::new(),
            Some(format!(
                "could not inspect the failed run's jobs: {}",
                output.stderr.trim()
            )),
        ),
        Err(error) => (
            list_conclusion,
            Some(run.url.clone()),
            Vec::new(),
            Some(format!("could not inspect the failed run's jobs: {error}")),
        ),
    };
    let jobs = if failed_jobs.is_empty() {
        "no failed job detail was available".to_string()
    } else {
        failed_jobs
            .iter()
            .map(|job| format!("`{}` ({})", job.name, job.conclusion))
            .collect::<Vec<_>>()
            .join(", ")
    };
    let mut detail = format!(
        "delegated workflow run {} ended `{conclusion}`; failed/cancelled job(s): {jobs}",
        run.database_id
    );
    if let Some(extra) = extra {
        let _ = write!(detail, "; {extra}");
    }
    DelegatedRun {
        provider: "github-actions".to_string(),
        workflow: Some(workflow.to_string()),
        run_id: Some(run.database_id),
        url,
        status: DelegatedRunStatus::Failed,
        conclusion: Some(conclusion),
        failed_jobs,
        detail: Some(detail),
    }
}

#[cfg(test)]
mod tests {
    use std::cell::RefCell;
    use std::io;
    use std::path::Path;

    use super::*;
    use crate::ports::{Clock, CommandOutput, CommandRunner, RegistryQuery};
    use crate::release::adapters::EMPTY_ARTIFACTS;

    struct ClockFake;
    impl Clock for ClockFake {
        fn now_unix(&self) -> u64 {
            0
        }
    }

    struct RegistryFake;
    impl RegistryQuery for RegistryFake {
        fn published_versions(&self, _ecosystem: &str, _package: &str) -> io::Result<Vec<String>> {
            Ok(Vec::new())
        }
    }

    struct RunnerFake {
        run_list: String,
        run_view: Option<String>,
        calls: RefCell<Vec<String>>,
    }
    impl CommandRunner for RunnerFake {
        fn run(&self, program: &str, args: &[&str], _cwd: &Path) -> io::Result<CommandOutput> {
            self.calls
                .borrow_mut()
                .push(format!("{program} {}", args.join(" ")));
            let stdout = if program == "git" && args.starts_with(&["rev-list"]) {
                "abc123\n".to_string()
            } else if program == "git" && args.starts_with(&["grep"]) {
                ".github/workflows/publish-crates.yml\n".to_string()
            } else if program == "gh" && args.starts_with(&["run", "list"]) {
                self.run_list.clone()
            } else if program == "gh" && args.starts_with(&["run", "view"]) {
                self.run_view.clone().unwrap_or_default()
            } else {
                String::new()
            };
            Ok(CommandOutput {
                status: Some(0),
                stdout,
                stderr: String::new(),
            })
        }
    }

    fn ctx<'a>(
        runner: &'a RunnerFake,
        clock: &'a ClockFake,
        registry: &'a RegistryFake,
    ) -> EffectCtx<'a> {
        EffectCtx {
            runner,
            clock,
            registry,
            repo_root: Path::new("/repo"),
            artifacts: &EMPTY_ARTIFACTS,
        }
    }

    #[test]
    fn in_progress_run_is_pending_not_missing() {
        let runner = RunnerFake {
            run_list: r#"[{"databaseId":77,"status":"in_progress","conclusion":"","headBranch":"v1.0.0","headSha":"abc123","url":"https://example/run/77"}]"#.to_string(),
            run_view: None,
            calls: RefCell::new(Vec::new()),
        };
        let (clock, registry) = (ClockFake, RegistryFake);
        let run = observe_github_run(
            &ctx(&runner, &clock, &registry),
            Adapter::CargoDist,
            "1.0.0",
        )
        .unwrap();
        assert_eq!(run.status, DelegatedRunStatus::Pending);
        assert_eq!(run.run_id, Some(77));
        assert!(!runner
            .calls
            .borrow()
            .iter()
            .any(|call| call.starts_with("gh release")));
    }

    #[test]
    fn cancelled_run_reports_the_terminal_job_cause() {
        let runner = RunnerFake {
            run_list: r#"[{"databaseId":88,"status":"completed","conclusion":"cancelled","headBranch":"v1.0.0","headSha":"abc123","url":"https://example/run/88"}]"#.to_string(),
            run_view: Some(r#"{"status":"completed","conclusion":"cancelled","url":"https://example/run/88","jobs":[{"name":"build (aarch64-unknown-linux-musl)","status":"completed","conclusion":"cancelled"},{"name":"host","status":"completed","conclusion":"skipped"}]}"#.to_string()),
            calls: RefCell::new(Vec::new()),
        };
        let (clock, registry) = (ClockFake, RegistryFake);
        let run = observe_github_run(
            &ctx(&runner, &clock, &registry),
            Adapter::CargoDist,
            "1.0.0",
        )
        .unwrap();
        assert_eq!(run.status, DelegatedRunStatus::Failed);
        assert_eq!(run.conclusion.as_deref(), Some("cancelled"));
        assert_eq!(
            run.failed_jobs[0].name,
            "build (aarch64-unknown-linux-musl)"
        );
        assert!(run.detail.unwrap().contains("cancelled"));
    }

    #[test]
    fn successful_run_is_distinct_and_allows_destination_observation_to_follow() {
        let runner = RunnerFake {
            run_list: r#"[{"databaseId":99,"status":"completed","conclusion":"success","headBranch":"v1.0.0","headSha":"abc123","url":"https://example/run/99"}]"#.to_string(),
            run_view: None,
            calls: RefCell::new(Vec::new()),
        };
        let (clock, registry) = (ClockFake, RegistryFake);
        let run = observe_github_run(
            &ctx(&runner, &clock, &registry),
            Adapter::CargoDist,
            "1.0.0",
        )
        .unwrap();
        assert_eq!(run.status, DelegatedRunStatus::Success);
        assert_eq!(run.run_id, Some(99));
    }

    #[test]
    fn non_github_delegated_adapters_have_no_github_run_state() {
        struct NoCommands;
        impl CommandRunner for NoCommands {
            fn run(&self, program: &str, args: &[&str], _cwd: &Path) -> io::Result<CommandOutput> {
                panic!("non-GitHub adapter invoked {program} {args:?}")
            }
        }
        let (runner, clock, registry) = (NoCommands, ClockFake, RegistryFake);
        let context = EffectCtx {
            runner: &runner,
            clock: &clock,
            registry: &registry,
            repo_root: Path::new("/repo"),
            artifacts: &EMPTY_ARTIFACTS,
        };
        assert!(observe_github_run(&context, Adapter::ReleasePlease, "1.0.0").is_none());
        assert!(observe_github_run(&context, Adapter::GhActionPypiPublish, "1.0.0").is_none());
    }

    #[test]
    fn command_failure_is_unknown_not_missing_or_pending() {
        struct Broken;
        impl CommandRunner for Broken {
            fn run(&self, program: &str, args: &[&str], _cwd: &Path) -> io::Result<CommandOutput> {
                if program == "git" && args.starts_with(&["rev-list"]) {
                    return Ok(CommandOutput {
                        status: Some(0),
                        stdout: "abc123\n".into(),
                        stderr: String::new(),
                    });
                }
                Err(io::Error::new(io::ErrorKind::TimedOut, "offline"))
            }
        }
        let (broken, clock, registry) = (Broken, ClockFake, RegistryFake);
        let context = EffectCtx {
            runner: &broken,
            clock: &clock,
            registry: &registry,
            repo_root: Path::new("/repo"),
            artifacts: &EMPTY_ARTIFACTS,
        };
        let run = observe_github_run(&context, Adapter::CargoDist, "1.0.0").unwrap();
        assert_eq!(run.status, DelegatedRunStatus::Unknown);
    }
}