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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
//! `oakum ci`: GitHub writes for CI. `version-pr` opens or updates the version
//! PR. `pr-status` posts the contributor-PR comment and job summary.

use std::fmt::Write;
use std::path::{Path, PathBuf};

use clap::{Args, Subcommand};
use oakum::config::PrStatus;
use oakum::state::{CoverageOutcome, ReleaseState, RenderTarget};
use serde_json::Value;

use super::config::{load_config, LoadedConfig};
use super::git::{Git, Op};
use super::github::{self, FileAddition, FileChanges, FileDeletion, Look};
use super::release_state;
use super::render;
use super::repository;
use super::template::load_template_body;
use super::version::{self, VersionArgs, VersionWritePlan};
use super::CliError;
use super::{deliver_block, deliver_out, say_err, say_out};

pub(super) const VERSION_BRANCH: &str = "oakum/version-packages";
/// The default commit subject and the default title, which are one sentence:
/// where a repository squashes with `squash_merge_commit_title = PR_TITLE`, the
/// title is the commit a default-branch `cog check` reads. A configured
/// `commit-message` moves only the commit; `title` is its own key.
const VERSION_HEADLINE: &str = "chore(release): version packages";
const DEFAULT_TITLE: &str = VERSION_HEADLINE;
/// `pub(super)` so migrate's "equal to what `ci version-pr` writes anyway" skip
/// compares against this literal rather than a copy that could drift.
pub(super) const DEFAULT_COMMIT: &str = VERSION_HEADLINE;

#[derive(Debug, Args)]
pub(super) struct CiArgs {
    #[command(subcommand)]
    command: CiCommand,
}

#[derive(Debug, Subcommand)]
enum CiCommand {
    /// Create or update the version pull request.
    VersionPr(VersionArgs),
    /// Post the contributor-PR plan comment and job summary.
    PrStatus(PrStatusArgs),
}

#[derive(Debug, Args)]
struct PrStatusArgs {
    /// Git ref to scan from (exclusive). Same default as `check` / `status`.
    #[arg(long, value_name = "REF")]
    from: Option<String>,
    /// Write the sticky-comment body to DIR instead of posting it to GitHub.
    /// Escape hatch for fork PRs that use a trusted `workflow_run` job to post
    /// (ADR-0015); not the default path.
    #[arg(long, value_name = "DIR")]
    emit_comment: Option<PathBuf>,
}

pub(super) fn run(args: &CiArgs) -> Result<(), CliError> {
    match &args.command {
        CiCommand::VersionPr(args) => run_version_pr(args),
        CiCommand::PrStatus(args) => run_pr_status(args),
    }
}

fn run_pr_status(args: &PrStatusArgs) -> Result<(), CliError> {
    let repo = repository::discover().map_err(CliError::from_boxed)?;
    let config = load_config(&repo).map_err(CliError::from_boxed)?;
    if config.is_default() {
        say_err(super::config::DEFAULTS_NOTE);
    }
    let channels = config.pr_status();
    let emit = args.emit_comment.as_deref();
    // Emit mode never touches GitHub on this run — including stale-comment
    // cleanup — so a fork's untrusted job cannot write with a read-only token.
    if channels == PrStatus::None {
        if emit.is_some() {
            return Err(CliError::new(
                "pr-status=none refuses --emit-comment; set pr-status to comment, summary, or both, or drop the flag",
            ));
        }
        say_err(
            "pr-status is set to `none`, so no coverage comment and no job summary were written.",
        );
        clear_stale_comment(&repo);
        return Ok(());
    }
    // The version PR already carries the release plan in its body; bump files
    // were consumed to produce it. Coverage comments are for contributor PRs.
    if on_version_packages_branch() {
        say_err(&format!(
            "this is the version pull request, which carries the plan in its body, so {} written.",
            nothing_was(channels)
        ));
        if let Some(dir) = emit {
            clear_emitted_comment(dir)?;
        } else if matches!(channels, PrStatus::Comment | PrStatus::Both) {
            clear_stale_comment(&repo);
        }
        return Ok(());
    }
    let state = pr_status_state(&repo, &config, args.from.as_deref())?;
    let want_comment = matches!(channels, PrStatus::Comment | PrStatus::Both);
    let want_summary = matches!(channels, PrStatus::Summary | PrStatus::Both);
    let Some(comment) = render::render_comment(&state) else {
        // Without this line an empty plan, a post nobody could see and a
        // swallowed failure are one silence from the workflow's side.
        say_err(&format!(
            "nothing to post: no package these commits touch plans a release or lacks covering intent, so {} written.",
            nothing_was(channels)
        ));
        if let Some(dir) = emit {
            // Same lifecycle as clear_stale_comment: a reused artifact dir must
            // not upload yesterday's plan when this run has nothing to say.
            clear_emitted_comment(dir)?;
        } else if want_comment {
            clear_stale_comment(&repo);
        }
        return Ok(());
    };
    let summary = render::render_summary(&state);
    if want_summary {
        write_step_summary(&summary)?;
    }
    if let Some(dir) = emit {
        return emit_comment_file(dir, &comment);
    }
    if !want_comment {
        return Ok(());
    }
    match post_pr_comment(&repo, &comment) {
        Ok(()) => Ok(()),
        Err(err) if github_forbidden(&err) => {
            degrade_to_summary(
                "comment requested but this run has no write permission (fork pull request); wrote the plan to the job summary instead.",
                want_summary,
                &summary,
            )
        }
        Err(err) if missing_comment_token(&err) => {
            degrade_to_summary(
                "comment requested but GITHUB_TOKEN is unset; wrote the plan to the job summary instead.",
                want_summary,
                &summary,
            )
        }
        Err(err) if missing_pull_number(&err) => {
            degrade_to_summary(
                "comment requested but no pull request number could be read from GITHUB_EVENT_PATH or GITHUB_REF; wrote the plan to the job summary instead.",
                want_summary,
                &summary,
            )
        }
        Err(err) => degrade_to_summary(
            &format!(
                "comment requested but GitHub did not accept the comment ({err}); wrote the plan to the job summary instead."
            ),
            want_summary,
            &summary,
        ),
    }
}

/// What a run that posts nothing skipped, named by the channels it was asked
/// for. Naming the comment alone told an operator who had configured `summary`
/// about a channel they never wanted, while the summary they did want went
/// unwritten and unmentioned.
const fn nothing_was(channels: PrStatus) -> &'static str {
    match channels {
        PrStatus::Comment => "no coverage comment was",
        PrStatus::Summary => "no job summary was",
        PrStatus::Both => "no coverage comment and no job summary were",
        // Unreachable: every caller sits below the `PrStatus::None` early
        // return, which has already said its own piece.
        PrStatus::None => "nothing was",
    }
}

fn degrade_to_summary(
    message: &str,
    summary_already_written: bool,
    summary: &str,
) -> Result<(), CliError> {
    say_err(message);
    if !summary_already_written {
        write_step_summary(summary)?;
    }
    Ok(())
}

/// Stable name so a trusted `workflow_run` job can find the artifact without
/// parsing the untrusted job's logs.
const EMITTED_COMMENT_FILE: &str = "oakum-pr-comment.md";

fn emit_comment_file(dir: &Path, comment: &str) -> Result<(), CliError> {
    std::fs::create_dir_all(dir).map_err(|err| {
        CliError::new(format!(
            "failed to create --emit-comment directory {}: {err}",
            dir.display()
        ))
    })?;
    let path = dir.join(EMITTED_COMMENT_FILE);
    let mut body = comment.to_owned();
    if !body.ends_with('\n') {
        body.push('\n');
    }
    std::fs::write(&path, body).map_err(|err| {
        CliError::new(format!(
            "failed to write --emit-comment file {}: {err}",
            path.display()
        ))
    })?;
    Ok(())
}

fn clear_emitted_comment(dir: &Path) -> Result<(), CliError> {
    let path = dir.join(EMITTED_COMMENT_FILE);
    match std::fs::remove_file(&path) {
        Ok(()) => Ok(()),
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(()),
        Err(err) => Err(CliError::new(format!(
            "failed to remove stale --emit-comment file {}: {err}",
            path.display()
        ))),
    }
}

fn pr_status_state(
    repo: &repository::Repository,
    config: &LoadedConfig,
    from: Option<&str>,
) -> Result<ReleaseState, CliError> {
    // Required, not reported: a comment is a claim about the pull request, and
    // one built on a look that did not happen would be worse than none.
    release_state::release_state(
        repo,
        config,
        from,
        RenderTarget::Comment,
        release_state::CoverageMode::Required,
    )
}

fn write_step_summary(text: &str) -> Result<(), CliError> {
    if let Ok(path) = std::env::var("GITHUB_STEP_SUMMARY") {
        let path = path.trim();
        if !path.is_empty() {
            use std::io::Write as _;
            let mut file = std::fs::OpenOptions::new()
                .create(true)
                .append(true)
                .open(path)
                .map_err(|err| {
                    CliError::new(format!("failed to write GITHUB_STEP_SUMMARY: {err}"))
                })?;
            file.write_all(text.as_bytes()).map_err(|err| {
                CliError::new(format!("failed to write GITHUB_STEP_SUMMARY: {err}"))
            })?;
            if !text.ends_with('\n') {
                file.write_all(b"\n").map_err(|err| {
                    CliError::new(format!("failed to write GITHUB_STEP_SUMMARY: {err}"))
                })?;
            }
            return Ok(());
        }
    }
    deliver_block(text).map_err(|err| CliError::undelivered("the plan", &err))?;
    Ok(())
}

fn post_pr_comment(repo: &repository::Repository, body: &str) -> Result<(), CliError> {
    let token = actions_token().ok_or(CliError::MissingActionsToken)?;
    let number = pull_number().ok_or(CliError::MissingPullNumber)?;
    let git = Git::at_repository(repo).map_err(CliError::from_boxed)?;
    let (owner, name) = repository_slug(&git)?;
    let client = github::Client::new(token).map_err(CliError::from)?;
    client
        .upsert_plan_comment(&owner, &name, number, render::PR_PLAN_MARKER, body)
        .map_err(CliError::from)?;
    Ok(())
}

fn actions_token() -> Option<String> {
    match std::env::var("GITHUB_TOKEN") {
        Ok(token) if !token.is_empty() => Some(token),
        _ => None,
    }
}

fn pull_number() -> Option<u64> {
    if let Ok(path) = std::env::var("GITHUB_EVENT_PATH") {
        if let Ok(bytes) = std::fs::read(path) {
            if let Ok(value) = serde_json::from_slice::<Value>(&bytes) {
                if let Some(number) = pull_number_from_event(&value) {
                    return Some(number);
                }
            }
        }
    }
    pull_number_from_ref(std::env::var("GITHUB_REF").ok().as_deref())
}

/// True when this Actions run is the bot's own version-packages PR. A fork or a
/// collaborator can use the branch name, so the payload must also say the head
/// repository is this one and that both the author and the sender are bots: the
/// four terms the scaffolded workflow tests. Anything short of that is "not the
/// version PR", because a wrong answer that way costs a redundant coverage
/// comment, and the other way costs a contributor's unreported coverage gap.
fn on_version_packages_branch() -> bool {
    let head = std::env::var("GITHUB_HEAD_REF").ok();
    if head.as_deref().is_some_and(|head| head != VERSION_BRANCH) {
        return false;
    }
    let Ok(path) = std::env::var("GITHUB_EVENT_PATH") else {
        if head.is_some() {
            say_err("GITHUB_EVENT_PATH is not set, so the version pull request cannot be recognised; treating this run as a contributor pull request");
        }
        return false;
    };
    // Still "not the version PR"; the line only makes that cost diagnosable.
    let value = match std::fs::read(&path)
        .map_err(|err| err.to_string())
        .and_then(|bytes| serde_json::from_slice::<Value>(&bytes).map_err(|err| err.to_string()))
    {
        Ok(value) => value,
        Err(err) => {
            say_err(&format!(
                "event payload at {path} could not be read ({err}); treating this run as a contributor pull request"
            ));
            return false;
        }
    };
    let text = |pointer: &str| value.pointer(pointer).and_then(Value::as_str);
    if text("/pull_request/head/ref") != Some(VERSION_BRANCH) {
        return false;
    }
    let Ok(this_repo) = std::env::var("GITHUB_REPOSITORY") else {
        say_err("GITHUB_REPOSITORY is not set, so the version pull request cannot be recognised; treating this run as a contributor pull request");
        return false;
    };
    text("/pull_request/head/repo/full_name") == Some(this_repo.as_str())
        && text("/pull_request/user/type") == Some("Bot")
        && text("/sender/type") == Some("Bot")
}

fn pull_number_from_event(value: &Value) -> Option<u64> {
    if let Some(number) = value
        .pointer("/pull_request/number")
        .and_then(Value::as_u64)
    {
        return Some(number);
    }
    if value
        .pointer("/issue/pull_request")
        .is_some_and(|value| !value.is_null())
    {
        return value.pointer("/issue/number").and_then(Value::as_u64);
    }
    None
}

fn pull_number_from_ref(value: Option<&str>) -> Option<u64> {
    let value = value?.trim();
    let mut parts = value.split('/');
    if parts.next()? != "refs" || parts.next()? != "pull" {
        return None;
    }
    parts.next()?.parse().ok()
}

fn clear_stale_comment(repo: &repository::Repository) {
    match delete_pr_comment(repo) {
        Ok(()) => {}
        // Expected states, not failures — but the caller has just said what this
        // run did not write, and a reader takes that for "nothing was left
        // behind". One line covers all three: which of them it was changes
        // nothing a reader would do about it.
        Err(err)
            if github_forbidden(&err)
                || missing_comment_token(&err)
                || missing_pull_number(&err) =>
        {
            say_err(
                "a leftover plan comment could not be removed with this run's token, so an earlier plan may still be visible on the pull request",
            );
        }
        Err(err) => {
            say_err(&format!("could not remove a leftover plan comment ({err})"));
        }
    }
}

fn delete_pr_comment(repo: &repository::Repository) -> Result<(), CliError> {
    let token = actions_token().ok_or(CliError::MissingActionsToken)?;
    let number = pull_number().ok_or(CliError::MissingPullNumber)?;
    let git = Git::at_repository(repo).map_err(CliError::from_boxed)?;
    let (owner, name) = repository_slug(&git)?;
    let client = github::Client::new(token).map_err(CliError::from)?;
    client
        .delete_plan_comments(&owner, &name, number, render::PR_PLAN_MARKER)
        .map_err(CliError::from)?;
    Ok(())
}

fn github_forbidden(err: &CliError) -> bool {
    matches!(err, CliError::Forbidden { .. })
}

fn missing_comment_token(err: &CliError) -> bool {
    matches!(err, CliError::MissingActionsToken)
}

fn missing_pull_number(err: &CliError) -> bool {
    matches!(err, CliError::MissingPullNumber)
}

fn run_version_pr(args: &VersionArgs) -> Result<(), CliError> {
    let prepared = version::plan_writes(args).map_err(CliError::from_boxed)?;
    if !prepared.needs_github() {
        say_out("nothing to version");
        return Ok(());
    }
    let client =
        github::Client::new(github::token().ok_or_else(|| {
            CliError::new("`oakum ci version-pr` needs GITHUB_TOKEN or GH_TOKEN")
        })?)?;
    let git = Git::at_repository(&prepared.repo).map_err(CliError::from_boxed)?;
    let (owner, name) = repository_slug(&git)?;
    let default_branch = client.default_branch(&owner, &name)?;
    let base_oid = match client.branch_head(&owner, &name, &default_branch)? {
        Look::Found(oid) => oid,
        Look::Empty => {
            return Err(CliError::new(format!(
                "default branch `{default_branch}` has no head"
            )));
        }
    };
    let head = local_head(&git)?;
    if head != base_oid {
        return Err(CliError::new(format!(
            "checkout HEAD `{head}` is not `{default_branch}` at `{base_oid}`"
        )));
    }
    let additions = github_additions(&prepared)?;
    let deletions = github_deletions(&prepared)?;
    let headline = commit_headline(&prepared)?;
    let title = pr_title(&prepared)?;
    let body = pr_body(&prepared);
    let existing = version_pull(&client, &owner, &name)?;
    client.replace_branch_commit(
        &owner,
        &name,
        VERSION_BRANCH,
        &base_oid,
        &headline,
        FileChanges {
            additions: &additions,
            deletions: &deletions,
        },
    )?;
    let opened = if let Some(pull) = existing {
        client.update_pull(&owner, &name, pull.number, &title, &body)?
    } else {
        client.create_pull(
            &owner,
            &name,
            VERSION_BRANCH,
            &default_branch,
            &title,
            &body,
        )?
    };
    deliver_out(&opened.html_url).map_err(|err| {
        CliError::undelivered(
            format!(
                "version pull request {} is open, but its URL",
                opened.html_url
            ),
            &err,
        )
    })?;
    // stderr: stdout is the URL a caller captures, and a diagnostic that
    // lands there turns `URL=$(oakum ci version-pr)` into two lines.
    say_err(&author_note(opened.author.as_ref()));
    Ok(())
}

/// Says who the token opened the pull request as, where that was measured,
/// rather than leaving it to be inferred from a workflow that did or did not
/// fire. A personal token opens it as a person and every check then runs on the
/// version pull request, whose only other symptom is a longer bill.
fn author_note(author: Option<&github::PullAuthor>) -> String {
    let Some(author) = author else {
        return String::from(
            "version pull request author: not reported by GitHub; \
             whether the scaffolded `oakum check --strict` skip applies is unverified",
        );
    };
    // Only a named non-bot settles it: that term alone defeats the skip. A bot
    // does not, because the skip also tests the sender of the event this push
    // raises, which no run can observe from here.
    let consequence = match author.kind.as_deref() {
        Some("Bot") => "the scaffolded `oakum check --strict` skip also tests the event sender, \
                        which this run cannot observe",
        Some(_) => {
            "the scaffolded `oakum check --strict` skip does not apply, so it runs on this pull request"
        }
        None => "GitHub reported no author type, so whether the scaffolded \
                 `oakum check --strict` skip applies is unverified",
    };
    format!(
        "version pull request author: {} ({}); {consequence}",
        author.login.as_deref().unwrap_or("login not reported"),
        author.kind.as_deref().unwrap_or("type not reported")
    )
}

fn local_head(git: &Git) -> Result<String, CliError> {
    let sha = git.text(Op::Head).map_err(|err| {
        CliError::new(format!(
            "`oakum ci version-pr` needs a git HEAD to compare with the default branch ({err})"
        ))
    })?;
    if sha.is_empty() {
        return Err(CliError::new("git HEAD is empty"));
    }
    Ok(sha)
}

fn version_pull(
    client: &github::Client,
    owner: &str,
    name: &str,
) -> Result<Option<github::PullRequest>, CliError> {
    match client.open_pulls_for_head(owner, name, VERSION_BRANCH)? {
        Look::Found(pulls) if pulls.len() > 1 => Err(CliError::new(format!(
            "multiple open version pull requests on `{VERSION_BRANCH}` ({})",
            pulls.len()
        ))),
        Look::Found(pulls) if pulls.len() == 1 => {
            Ok(Some(pulls.into_iter().next().expect("one pull")))
        }
        Look::Found(_) | Look::Empty => {
            match client.pulls_for_head(owner, name, VERSION_BRANCH, "closed")? {
                Look::Found(pulls) => {
                    let unmerged: Vec<_> = pulls.into_iter().filter(|pull| !pull.merged).collect();
                    match unmerged.len() {
                        0 => Ok(None),
                        1 => Ok(Some(unmerged.into_iter().next().expect("one pull"))),
                        count => Err(CliError::new(format!(
                            "multiple closed unmerged version pull requests on `{VERSION_BRANCH}` ({count})"
                        ))),
                    }
                }
                Look::Empty => Ok(None),
            }
        }
    }
}

pub(super) fn repository_slug(git: &Git) -> Result<(String, String), CliError> {
    repository_slug_from(git, "origin")
}

pub(super) fn repository_slug_from(git: &Git, remote: &str) -> Result<(String, String), CliError> {
    if let Ok(value) = std::env::var("GITHUB_REPOSITORY") {
        let value = value.trim();
        if !value.is_empty() {
            return parse_slug(value).ok_or_else(|| {
                CliError::new(format!("GITHUB_REPOSITORY `{value}` is not owner/repo"))
            });
        }
    }
    let url = git.text(Op::RemoteUrl { remote }).map_err(|err| {
        CliError::new(format!(
            "needs GITHUB_REPOSITORY or a git `{remote}` remote ({err})"
        ))
    })?;
    parse_github_origin(&url).ok_or_else(|| {
        CliError::new(format!(
            "git `{remote}` `{url}` is not a github.com owner/repo URL"
        ))
    })
}

fn parse_slug(value: &str) -> Option<(String, String)> {
    let (owner, name) = value.split_once('/')?;
    if owner.is_empty() || name.is_empty() || name.contains('/') {
        return None;
    }
    Some((owner.to_owned(), name.to_owned()))
}

fn parse_github_origin(url: &str) -> Option<(String, String)> {
    let rest = url
        .strip_prefix("git@github.com:")
        .or_else(|| url.strip_prefix("https://github.com/"))
        .or_else(|| url.strip_prefix("http://github.com/"))
        .or_else(|| url.strip_prefix("ssh://git@github.com/"))
        .or_else(|| url.strip_prefix("git://github.com/"))?;
    let rest = rest.trim_end_matches('/').trim_end_matches(".git");
    parse_slug(rest)
}

fn github_path(path: &Path) -> Result<String, CliError> {
    let raw = path
        .to_str()
        .ok_or_else(|| CliError::new("a version write path is not valid UTF-8"))?;
    github::git_path(raw, "write").map_err(CliError::from)
}

fn github_additions(prepared: &VersionWritePlan) -> Result<Vec<FileAddition>, CliError> {
    let mut additions = Vec::new();
    for write in &prepared.writes {
        if write.original() == write.next() {
            continue;
        }
        additions.push(FileAddition::from_text(
            github_path(write.path())?,
            write.next(),
        ));
    }
    Ok(additions)
}

fn github_deletions(prepared: &VersionWritePlan) -> Result<Vec<FileDeletion>, CliError> {
    prepared
        .deletes
        .iter()
        .map(|delete| FileDeletion::new(github_path(delete.path())?).map_err(CliError::from))
        .collect()
}

fn commit_headline(prepared: &VersionWritePlan) -> Result<String, CliError> {
    render_pref(
        &prepared.repo,
        "commit-message",
        prepared.commit_message.as_ref(),
        DEFAULT_COMMIT,
        prepared,
    )
}

fn pr_title(prepared: &VersionWritePlan) -> Result<String, CliError> {
    render_pref(
        &prepared.repo,
        "title",
        prepared.title.as_ref(),
        DEFAULT_TITLE,
        prepared,
    )
}

fn render_pref(
    repo: &repository::Repository,
    name: &str,
    source: Option<&oakum::template::TemplateSource>,
    default: &str,
    prepared: &VersionWritePlan,
) -> Result<String, CliError> {
    let Some(source) = source else {
        return Ok(default.to_owned());
    };
    let body = load_template_body(repo.dir(), repo.path(), source).map_err(CliError::from_boxed)?;
    let state = ReleaseState::from_plan(
        &prepared.plan,
        CoverageOutcome::NotAsked,
        RenderTarget::Status,
    );
    let rendered = oakum::template::render(name, &body, &state)
        .map_err(|err| CliError::new(err.to_string()))?;
    let rendered = rendered.trim();
    if rendered.is_empty() {
        return Err(CliError::new(format!(
            "{name} template rendered an empty string"
        )));
    }
    Ok(rendered.to_owned())
}

fn pr_body(prepared: &VersionWritePlan) -> String {
    let state = ReleaseState::from_plan(
        &prepared.plan,
        CoverageOutcome::NotAsked,
        RenderTarget::Status,
    );
    let mut body = render::render_summary(&state);
    if !body.ends_with('\n') {
        body.push('\n');
    }
    let _ = write!(body, "\nGenerated by oakum {}.\n", prepared.tool_version);
    body
}

#[cfg(test)]
mod tests {
    use super::{
        commit_headline, github_path, parse_github_origin, parse_slug, pr_body, pr_title,
        pull_number_from_event, pull_number_from_ref, VersionWritePlan, VERSION_HEADLINE,
    };
    use crate::cli::repository;
    use crate::cli::write_set::{PlannedDelete, PlannedWrite};
    use oakum::plan::Plan;
    use oakum::state::{CoverageOutcome, ReleaseState, RenderTarget};
    use serde_json::json;
    use std::path::PathBuf;

    fn stub_plan(writes: Vec<PlannedWrite>, deletes: Vec<PlannedDelete>) -> VersionWritePlan {
        VersionWritePlan {
            repo: repository::discover().expect("test checkout is a git repository"),
            writes,
            deletes,
            plan: Plan::default(),
            tool_version: String::from("0.0.0"),
            title: None,
            commit_message: None,
        }
    }

    #[test]
    fn an_unconfigured_title_is_the_commit_headline_and_is_conventional() {
        let plan = stub_plan(Vec::new(), Vec::new());
        let title = pr_title(&plan).expect("title");

        assert_eq!(title, commit_headline(&plan).expect("headline"));
        // `cog verify` refuses a type outside the conventional set; parsing
        // alone accepts any.
        let parsed = git_conventional::Commit::parse(&title)
            .expect("the title a squash merge lands parses as a conventional commit");
        assert_eq!(parsed.type_(), git_conventional::Type::CHORE);
    }

    /// Each key moves its own surface and nothing else. The configured message is
    /// itself conventional on purpose: a re-coupling would most plausibly mirror
    /// `commit-message` into the title only where it already parses, and a
    /// non-conventional fixture walks straight past that.
    #[test]
    fn a_configured_message_and_title_stay_on_their_own_surfaces() {
        let mut message_only = stub_plan(Vec::new(), Vec::new());
        message_only.commit_message = Some(oakum::template::TemplateSource::Inline(String::from(
            "chore(release): v9.9.9",
        )));
        assert_eq!(
            commit_headline(&message_only).expect("headline"),
            "chore(release): v9.9.9"
        );
        assert_eq!(pr_title(&message_only).expect("title"), VERSION_HEADLINE);

        let mut title_only = stub_plan(Vec::new(), Vec::new());
        title_only.title = Some(oakum::template::TemplateSource::Inline(String::from(
            "Version Packages",
        )));
        assert_eq!(pr_title(&title_only).expect("title"), "Version Packages");
        assert_eq!(
            commit_headline(&title_only).expect("headline"),
            VERSION_HEADLINE
        );
    }

    #[test]
    fn slug_rejects_extra_segments() {
        assert!(parse_slug("oakoss/oakum/extra").is_none());
        assert_eq!(
            parse_slug("oakoss/oakum"),
            Some((String::from("oakoss"), String::from("oakum")))
        );
    }

    #[test]
    fn origin_urls_resolve_owner_and_repo() {
        for url in [
            "git@github.com:oakoss/oakum.git",
            "https://github.com/oakoss/oakum.git",
            "https://github.com/oakoss/oakum",
            "ssh://git@github.com/oakoss/oakum.git",
            "git://github.com/oakoss/oakum.git",
        ] {
            assert_eq!(
                parse_github_origin(url),
                Some((String::from("oakoss"), String::from("oakum"))),
                "{url}"
            );
        }
        assert!(parse_github_origin("git@gitlab.com:oakoss/oakum.git").is_none());
    }

    #[test]
    fn author_note_settles_only_what_the_author_decides() {
        fn note(login: Option<&str>, kind: Option<&str>) -> String {
            super::author_note(Some(&super::github::PullAuthor {
                login: login.map(str::to_owned),
                kind: kind.map(str::to_owned),
            }))
        }
        assert!(super::author_note(None).contains("not reported by GitHub"));
        assert!(super::author_note(None).contains("unverified"));
        // A person defeats the skip on its own; a bot leaves the sender open.
        let person = note(Some("jbabin91"), Some("User"));
        assert!(person.contains("jbabin91 (User)"), "{person}");
        assert!(person.contains("does not apply"), "{person}");
        let bot = note(Some("oakoss[bot]"), Some("Bot"));
        assert!(bot.contains("oakoss[bot] (Bot)"), "{bot}");
        assert!(bot.contains("also tests the event sender"), "{bot}");
        assert!(!bot.contains("does not apply"), "{bot}");
        let no_kind = note(Some("oakum-bot"), None);
        assert!(
            no_kind.contains("oakum-bot (type not reported)"),
            "{no_kind}"
        );
        assert!(no_kind.contains("unverified"), "{no_kind}");
        assert!(!no_kind.contains("does not apply"), "{no_kind}");
        let no_login = note(None, Some("Bot"));
        assert!(no_login.contains("login not reported (Bot)"), "{no_login}");
    }

    #[test]
    fn pr_body_stamps_the_tool_version() {
        let prepared = stub_plan(Vec::new(), Vec::new());
        let body = pr_body(&prepared);
        assert!(body.contains("No packages planned."), "{body}");
        assert!(body.contains("Generated by oakum 0.0.0.\n"), "{body}");
    }

    #[test]
    fn needs_github_is_true_for_a_write_without_deletes() {
        let prepared = stub_plan(
            vec![PlannedWrite::new(
                PathBuf::from("Cargo.toml"),
                "0.1.0",
                "0.1.1",
            )],
            Vec::new(),
        );
        assert!(prepared.needs_github());
    }

    #[test]
    fn needs_github_is_true_for_a_delete_without_writes() {
        let prepared = stub_plan(
            Vec::new(),
            vec![PlannedDelete::new(
                PathBuf::from(".changeset/one.md"),
                "---\n",
            )],
        );
        assert!(prepared.needs_github());
    }

    #[test]
    fn github_path_normalizes_backslashes() {
        assert_eq!(
            github_path(std::path::Path::new("foo\\bar.md")).expect("path"),
            "foo/bar.md"
        );
    }

    #[test]
    fn pull_number_parses_actions_ref() {
        assert_eq!(pull_number_from_ref(Some("refs/pull/12/merge")), Some(12));
        assert_eq!(pull_number_from_ref(Some("refs/heads/main")), None);
        assert_eq!(pull_number_from_ref(None), None);
    }

    #[test]
    fn pull_number_from_event_accepts_pr_shapes_only() {
        assert_eq!(
            pull_number_from_event(&json!({"pull_request":{"number":4}})),
            Some(4)
        );
        assert_eq!(
            pull_number_from_event(&json!({"issue":{"number":4,"pull_request":{}}})),
            Some(4)
        );
        assert_eq!(pull_number_from_event(&json!({"issue":{"number":4}})), None);
        assert_eq!(
            pull_number_from_event(&json!({"issue":{"number":4,"pull_request":null}})),
            None
        );
    }

    /// `commit-message` and `title` render with the `ReleaseState` document,
    /// so a field added there is a variable those surfaces gain. Their schema
    /// descriptions name the set, and this is what fails when it drifts.
    #[test]
    fn the_schema_names_every_variable_the_state_surfaces_render_with() {
        for surface in ["commit-message", "title"] {
            crate::cli::schema_names_every_variable(
                surface,
                ReleaseState::from_plan(
                    &oakum::plan::Plan::default(),
                    CoverageOutcome::NotAsked,
                    RenderTarget::Status,
                ),
            );
        }
    }
}