oakum 0.3.2

A polyglot release tool that derives dependent version bumps from the dependency graph
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
mod add;
mod changelog;
mod ci;
mod config;
mod coverage;
mod detect_tools;
mod fs;
mod generate;
mod git;
mod github;
mod handoff;
mod inherited;
mod init;
mod install_pin;
mod intent;
mod markdown;
mod migrate;
mod migrate_config;
mod migrate_gates;
mod migrate_output;
mod migrate_source_plan;
mod owned_files;
mod preconditions;
mod release;
mod release_state;
mod render;
mod repository;
mod status;
mod tag_shape;
mod tags;
mod template;
mod upgrade;
mod verdict;
mod version;
mod write_set;

use std::ffi::OsString;
use std::fmt;

use clap::{Parser, Subcommand};

/// Polyglot release tool: version math, changelogs, and graph-derived bumps.
#[derive(Debug, Parser)]
#[command(name = "oakum", version, about, long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Option<Commands>,
}

#[derive(Debug, Subcommand)]
enum Commands {
    /// Write one `.changeset/*.md` bump file.
    Add(add::AddArgs),
    /// Report drift and name the fix.
    Check(preconditions::CheckArgs),
    /// Derive one `.changeset/*.md` from commits on this branch.
    Generate(generate::GenerateArgs),
    /// Write oakum's three files and print the workflow to paste.
    Init(init::InitArgs),
    /// Transform another tool's bump files and config into oakum's, and print remaining steps.
    Migrate(migrate::MigrateArgs),
    /// Push tags and create GitHub releases for untagged manifest versions.
    Release(release::ReleaseArgs),
    /// Write planned package versions, inherited pins, lockfile rows, declared extra-files, changelogs, and — when bumping the Cargo member named `oakum` and config already declares `tool-version` — updates that pin.
    Version(version::VersionArgs),
    /// GitHub writes for CI.
    Ci(ci::CiArgs),
    /// Print plan bump-file inputs as JSON (hidden plumbing for tests).
    #[command(name = "plan-intent", hide = true)]
    PlanIntent(intent::PlanIntentArgs),
    /// Print foreign release-tool markers (hidden plumbing for tests).
    #[command(name = "detect-release-tools", hide = true)]
    DetectReleaseTools,
    /// Print the versioned release state: the `summary` render by default, or JSON.
    Status(status::StatusArgs),
    /// Print tags reachable from HEAD as `commit\\ttag`.
    #[command(name = "reachable-tags", hide = true)]
    ReachableTags,
    /// Tag-only readiness path (no coverage).
    #[command(name = "tag-drift", hide = true)]
    TagDrift,
    /// Migrate `.changeset/_config.toml` and `_schema.json` to this binary.
    Upgrade,
}

/// Says a line on stderr without panicking when the write is refused. A broken
/// pipe makes `eprintln!` panic, which replaces ADR-0034's exit code with 101
/// and loses the refusal — measured: a run owing exit 2 exited 101 and printed
/// nothing when its stderr reader had gone away. A *closed* stderr does not,
/// for the reason `git::env::say` records.
pub(crate) fn say_err(line: &str) {
    use std::io::Write;
    let _ = writeln!(std::io::stderr(), "{line}");
}

/// The same for stdout. A report is not worth a panic: `println!` aborts on a
/// refused write, so `oakum check | head -1` exited 101 with nothing on either
/// stream, discarding an outcome the run had already established.
pub(crate) fn say_out(line: &str) {
    use std::io::Write;
    let _ = writeln!(std::io::stdout(), "{line}");
}

/// A line that is the command's result (a path, a URL, a document) rather than
/// a report about it. `say_out` discards a refusal because a reader who went
/// away chose not to read; a result nobody received is a command that did not
/// finish. The caller maps the error with the side effect that already
/// happened, so exit 2 (unverified) and stderr name what exists.
/// A closed descriptor (`>&-`) is not a refusal: std reports `Ok` on EBADF
/// for stdout, so a caller who closed it gets exit 0, as for `ask`'s stderr.
pub(crate) fn deliver_out(line: &str) -> std::io::Result<()> {
    use std::io::Write;
    writeln!(std::io::stdout(), "{line}")
}

/// [`deliver_out`] for a pre-formatted block, written as-is: it carries its own
/// newlines, and a body that ends without one must stay that way.
pub(crate) fn deliver_block(text: &str) -> std::io::Result<()> {
    use std::io::Write;
    let mut stdout = std::io::stdout();
    stdout.write_all(text.as_bytes())?;
    // Stdout is line-buffered: a body with no newline would sit until the
    // exit flush, whose refusal nobody reads.
    stdout.flush()
}

/// A prompt on stderr, no newline, flushed so it reaches the terminal before
/// the read that follows. A refused write propagates: a prompt that cannot be
/// shown cannot be answered. A *closed* stderr does not — std reports `Ok` on
/// EBADF, as `say_err` records — so the wizard then runs on an empty read.
pub(crate) fn ask(prompt: &str) -> std::io::Result<()> {
    use std::io::Write;
    let mut stderr = std::io::stderr();
    stderr.write_all(prompt.as_bytes())?;
    stderr.flush()
}

pub fn run() -> Result<(), CliError> {
    run_with(std::env::args_os())
}

fn run_with<I, T>(args: I) -> Result<(), CliError>
where
    I: IntoIterator<Item = T>,
    T: Into<OsString> + Clone,
{
    let cli = Cli::parse_from(args);
    match cli.command {
        None => {
            say_out("oakum");
            Ok(())
        }
        Some(Commands::Add(args)) => add::run(args).map_err(CliError::from_boxed),
        Some(Commands::Check(args)) => preconditions::run(&args),
        Some(Commands::TagDrift) => preconditions::run_tags_only(),
        Some(Commands::Generate(args)) => generate::run(&args).map_err(CliError::from_boxed),
        Some(Commands::Init(args)) => init::run(&args).map_err(CliError::from_boxed),
        Some(Commands::Migrate(args)) => migrate::run(&args).map_err(CliError::from_boxed),
        Some(Commands::PlanIntent(args)) => intent::run(&args).map_err(CliError::from_boxed),
        Some(Commands::DetectReleaseTools) => detect_tools::run(),
        Some(Commands::Status(args)) => status::run(&args).map_err(CliError::from_boxed),
        Some(Commands::ReachableTags) => tags::run(),
        Some(Commands::Upgrade) => upgrade::run().map_err(CliError::from_boxed),
        Some(Commands::Version(args)) => version::run(&args).map_err(CliError::from_boxed),
        Some(Commands::Release(args)) => release::run(&args),
        Some(Commands::Ci(args)) => ci::run(&args),
    }
}

/// The one implementation lives in the library, which builds the same sentence
/// for a render that failed on an undefined variable.
pub(super) use oakum::template::quoted;

/// Asserts the schema description for a template surface names every variable
/// that surface actually renders with. The lists are written by hand where a
/// reader configuring the key will find them; this is what stops them rotting
/// away from the contexts, which is how `tag-format` came to document nothing
/// about `package` at all.
#[cfg(test)]
pub(crate) fn schema_names_every_variable(surface: &str, context: impl serde::Serialize) {
    let schema = oakum::config::schema();
    let described = schema["properties"][surface]["description"]
        .as_str()
        .unwrap_or_else(|| panic!("`{surface}` has a schema description"))
        .to_owned();
    let rendered = serde_json::to_value(context).expect("the context serializes");
    let keys = rendered
        .as_object()
        .unwrap_or_else(|| panic!("`{surface}` renders with a map context"));
    assert!(!keys.is_empty(), "`{surface}` renders with no variables");
    for key in keys.keys() {
        assert!(
            described.contains(&format!("`{key}`")),
            "`{surface}` receives `{key}`, which its schema description does not name: {described}"
        );
    }
}

/// What a failure is, once. [`CliError::class`] is the only thing that decides
/// it, and the exit code and the stderr token are both read off it rather than
/// filed separately against the same variant.
///
/// Ordered by which one a run carrying several refusals should carry out:
/// a finding outranks a look that did not happen, so `Error` is declared first.
/// ADR-0034 exists so a caller can tell the two apart, and an ordering held
/// anywhere but on this type is a second place for them to disagree.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum Outcome {
    Error,
    Unverified,
}

impl Outcome {
    fn code(self) -> i32 {
        match self {
            Self::Unverified => 2,
            Self::Error => 1,
        }
    }

    fn token(self) -> &'static str {
        match self {
            Self::Unverified => "unverified",
            Self::Error => "error",
        }
    }
}

/// Distinct variants so check outcomes stay distinguishable, and
/// [`Self::exit_code`] carries that distinction out to the shell.
///
/// `Clone` so a failure can be cached and handed to more than one caller.
#[derive(Clone, Debug)]
pub(crate) enum CliError {
    Unverified { detail: String },
    TagDrift { count: usize },
    Uncovered { count: usize },
    Forbidden { path: String },
    MissingActionsToken,
    MissingPullNumber,
    Other(String),
}

impl CliError {
    pub(crate) fn new(message: impl Into<String>) -> Self {
        Self::Other(message.into())
    }

    pub(crate) fn unverified(detail: impl Into<String>) -> Self {
        Self::Unverified {
            detail: detail.into(),
        }
    }

    /// A new detail under this verdict's class. A wrapper that adds what
    /// landed must not reclassify why the run stopped.
    pub(crate) fn recast(&self, detail: impl Into<String>) -> Self {
        match self.class() {
            Outcome::Unverified => Self::unverified(detail),
            Outcome::Error => Self::new(detail),
        }
    }

    /// A result nobody received. `what` names the side effect that already
    /// happened and the thing that did not arrive ("wrote x, but its path"),
    /// so the shell learns both from one line.
    pub(crate) fn undelivered(what: impl std::fmt::Display, err: &std::io::Error) -> Self {
        Self::unverified(format!("{what} could not be delivered to stdout: {err}"))
    }

    pub(crate) fn tag_drift(count: usize) -> Self {
        Self::TagDrift { count }
    }

    pub(crate) fn uncovered(count: usize) -> Self {
        Self::Uncovered { count }
    }

    pub(crate) fn from_boxed(err: Box<dyn std::error::Error>) -> Self {
        match err.downcast::<Self>() {
            Ok(typed) => *typed,
            Err(other) => Self::Other(other.to_string()),
        }
    }

    /// The one place a variant is classified. Both channels a caller can read —
    /// the number and the word — come from here, so a variant cannot be filed
    /// one way for the shell and another way for stderr. Two exhaustive matches
    /// made the compiler demand each variant be filed twice without being able
    /// to compare the filings, and the test that was supposed to catch the
    /// difference counted table rows rather than the variants in them: an
    /// eighth row naming any variant satisfied it (measured).
    pub(crate) fn class(&self) -> Outcome {
        match self {
            Self::Unverified { .. } => Outcome::Unverified,
            Self::TagDrift { .. }
            | Self::Uncovered { .. }
            | Self::Forbidden { .. }
            | Self::MissingActionsToken
            | Self::MissingPullNumber
            | Self::Other(_) => Outcome::Error,
        }
    }

    /// The three outcomes `AGENTS.md` requires, as the only channel a caller
    /// that is not reading stderr can see: `0` ok, `2` unverified, `1` error.
    ///
    /// Every exit stays non-zero, so a shell testing success is unaffected;
    /// what changes is that "we could not look" no longer arrives wearing the
    /// same number as "this failed".
    pub(crate) fn exit_code(&self) -> i32 {
        self.class().code()
    }

    /// The token stderr leads with. Printing a fixed `error: ` put two outcome
    /// tokens on one line — `error: unverified: …` — saying different things
    /// about one result.
    pub(crate) fn outcome(&self) -> &'static str {
        self.class().token()
    }

    /// The message without a leading `unverified: ` outcome token.
    pub(crate) fn detail(&self) -> String {
        let message = self.to_string();
        message
            .strip_prefix("unverified: ")
            .map(str::to_owned)
            .unwrap_or(message)
    }
}

impl fmt::Display for CliError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Unverified { detail } => f.write_str(detail),
            Self::TagDrift { count } => {
                write!(f, "{count} package(s) bumped without a tag")
            }
            Self::Uncovered { count } => {
                write!(f, "{count} package(s) changed with no covering intent")
            }
            Self::Forbidden { path } => write!(f, "GitHub {path} returned 403"),
            Self::MissingActionsToken => {
                write!(
                    f,
                    "`oakum ci pr-status` needs GITHUB_TOKEN to post a comment"
                )
            }
            Self::MissingPullNumber => write!(
                f,
                "`oakum ci pr-status` needs a pull request number (GITHUB_EVENT_PATH or GITHUB_REF)"
            ),
            Self::Other(message) => f.write_str(message),
        }
    }
}

impl std::error::Error for CliError {}

impl From<github::Error> for CliError {
    fn from(err: github::Error) -> Self {
        match err {
            github::Error::Unverified { detail } => Self::Unverified { detail },
            github::Error::Forbidden { path } => Self::Forbidden { path },
            unauthorized @ github::Error::Unauthorized { .. } => {
                Self::Other(unauthorized.to_string())
            }
            github::Error::Other(message) => Self::Other(message),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::CliError;

    #[test]
    fn detail_strips_a_leading_unverified_token() {
        let nested = CliError::unverified("unverified: no remotes");
        assert_eq!(nested.detail(), "no remotes");
        let plain = CliError::new("boom");
        assert_eq!(plain.detail(), "boom");
    }

    #[test]
    fn check_outcomes_are_distinct_variants() {
        let unverified = CliError::unverified("unverified: no remotes");
        let drift = CliError::tag_drift(1);
        let uncovered = CliError::uncovered(2);
        assert!(matches!(unverified, CliError::Unverified { .. }));
        assert!(matches!(drift, CliError::TagDrift { count: 1 }));
        assert!(matches!(uncovered, CliError::Uncovered { count: 2 }));
        assert_ne!(
            std::mem::discriminant(&unverified),
            std::mem::discriminant(&drift)
        );
    }

    /// How many variants `CliError` declares. [`exit_codes`] states one row for
    /// each, so a variant cannot reach the table without stating its code, and
    /// `every_variant_states_an_exit_code` ties the count back to the enum.
    const EXIT_CODED: usize = 7;

    /// Every variant beside the code it must answer. An exhaustive `match` in
    /// [`CliError::exit_code`] forces a new variant to state *a* code; this
    /// table is what checks *which*.
    fn exit_codes() -> [(CliError, i32); EXIT_CODED] {
        [
            (CliError::unverified("unverified: no remotes"), 2),
            (CliError::tag_drift(1), 1),
            (CliError::uncovered(2), 1),
            (
                CliError::Forbidden {
                    path: String::from("/comments"),
                },
                1,
            ),
            (CliError::MissingActionsToken, 1),
            (CliError::MissingPullNumber, 1),
            (CliError::new("boom"), 1),
        ]
    }

    /// The table must name every variant, not merely have as many rows as there
    /// are variants. Measured before this: an eighth row duplicating an
    /// existing variant satisfied the count, and a ninth variant filed into the
    /// wrong class shipped with the whole suite green.
    #[test]
    fn the_exit_code_table_names_every_variant_once() {
        let named: std::collections::HashSet<_> = exit_codes()
            .iter()
            .map(|(err, _)| std::mem::discriminant(err))
            .collect();
        assert_eq!(
            named.len(),
            EXIT_CODED,
            "every row must name a different variant"
        );
    }

    #[test]
    fn only_unverified_exits_two() {
        for (err, expected) in exit_codes() {
            assert_eq!(err.exit_code(), expected, "{err}");
            // The "only" in this test's name: a second variant claiming 2 in
            // both the arm and the table would otherwise pass, the table being
            // a second declaration by the same hand.
            assert_eq!(
                expected == 2,
                matches!(err, CliError::Unverified { .. }),
                "only `Unverified` exits 2: {err}"
            );
        }
    }

    /// The two channels a caller reads — the number and the word — are keyed
    /// off one variant, so a variant cannot exit 2 while stderr calls it an
    /// error. Without this, `outcome` is a second hand-maintained copy of
    /// `exit_code`'s arms, free to drift exactly as the table above is.
    #[test]
    fn the_printed_token_agrees_with_the_exit_code() {
        for (err, expected) in exit_codes() {
            assert_eq!(
                err.outcome() == "unverified",
                expected == 2,
                "`{}` beside exit {expected}: {err}",
                err.outcome()
            );
        }
    }

    /// main prints `<outcome>: <detail>`; a detail that still carries its own
    /// `unverified: ` would put the token on the line twice, which is the
    /// defect this pairing replaced.
    #[test]
    fn the_printed_line_carries_one_outcome_token() {
        let line = {
            let err = CliError::unverified("unverified: no remotes");
            format!("{}: {}", err.outcome(), err.detail())
        };
        assert_eq!(line, "unverified: no remotes");
        assert_eq!(line.matches("unverified").count(), 1, "{line}");
    }

    /// The exhaustive `match` catches a variant that states no code; nothing
    /// catches one dropped into the wrong arm, which is the mistake that would
    /// ship silently. Measured: a variant added to the `=> 2` arm left the whole
    /// bin suite green.
    #[test]
    fn every_variant_states_an_exit_code() {
        let source = include_str!("mod.rs");
        // Two pieces, so the needle does not match this line as well.
        let opener = concat!("pub(crate) ", "enum CliError {");
        let body = source
            .split_once(opener)
            .expect("the CliError enum")
            .1
            .split_once("\n}\n")
            .expect("the end of the CliError enum")
            .0;
        let declared = body
            .lines()
            .filter(|line| {
                let trimmed = line.trim_start();
                line.len() - trimmed.len() == 4
                    && trimmed.starts_with(|c: char| c.is_ascii_uppercase())
            })
            .count();
        assert_eq!(
            declared,
            exit_codes().len(),
            "`CliError` declares {declared} variants and the exit-code table lists {}",
            exit_codes().len()
        );
    }

    #[test]
    fn from_boxed_keeps_unverified() {
        let boxed: Box<dyn std::error::Error> =
            Box::new(CliError::unverified("unverified: no remotes"));
        assert!(matches!(
            CliError::from_boxed(boxed),
            CliError::Unverified { .. }
        ));
    }

    #[test]
    fn from_boxed_does_not_treat_io_as_unverified() {
        let boxed: Box<dyn std::error::Error> = Box::new(std::io::Error::other("boom"));
        assert!(matches!(CliError::from_boxed(boxed), CliError::Other(_)));
    }

    #[test]
    fn github_unverified_maps_to_cli_unverified() {
        let err = CliError::from(super::github::Error::Unverified {
            detail: String::from("unverified: GitHub /graphql returned 502"),
        });
        assert!(matches!(err, CliError::Unverified { .. }));
    }

    #[test]
    fn github_forbidden_maps_to_cli_forbidden() {
        let err = CliError::from(super::github::Error::Forbidden {
            path: String::from("/repos/oakoss/oakum/issues/4/comments"),
        });
        assert!(matches!(err, CliError::Forbidden { .. }));
        assert_eq!(
            err.to_string(),
            "GitHub /repos/oakoss/oakum/issues/4/comments returned 403"
        );
    }

    #[test]
    fn missing_comment_preconditions_are_distinct_variants() {
        assert!(matches!(
            CliError::MissingActionsToken,
            CliError::MissingActionsToken
        ));
        assert!(matches!(
            CliError::MissingPullNumber,
            CliError::MissingPullNumber
        ));
        assert_ne!(
            std::mem::discriminant(&CliError::MissingActionsToken),
            std::mem::discriminant(&CliError::MissingPullNumber)
        );
        assert_eq!(
            CliError::MissingActionsToken.to_string(),
            "`oakum ci pr-status` needs GITHUB_TOKEN to post a comment"
        );
    }

    #[test]
    fn tag_drift_and_uncovered_display_match_the_check_summaries() {
        assert_eq!(
            CliError::tag_drift(1).to_string(),
            "1 package(s) bumped without a tag"
        );
        assert_eq!(
            CliError::uncovered(2).to_string(),
            "2 package(s) changed with no covering intent"
        );
    }
}