ghtui 0.2.0

A keyboard-driven terminal client for GitHub, with a pull request diff viewer that goes further than GitHub's
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
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
//! Making a release, as GitHub's "Draft a new release" does, editing
//! one, and deleting one (then, asked again, its tag).
//!
//! The form is a page (`releases::NewRelease`, or `releases::EditRelease`
//! filled from the release): a tag (one GitHub has, or a new one GitHub
//! makes at a branch or commit when the release is published), a title
//! and notes written in the composer (the title on the first line),
//! GitHub's generated notes offered as a start, and whether it's a
//! pre-release or the latest. Saving asks whether to publish it or keep
//! it a draft, as GitHub's two buttons do; then the release's page (a
//! draft's: the releases) follows GitHub until it shows it. A release's
//! assets are [`assets`]'.

pub mod assets;

use std::borrow::Cow;

use crate::api::browse::{BranchInfo, Release, Results, TagInfo};
use crate::api::model::RepoId;
use crate::api::release::{Made, NewRelease, Notes, TagLook};
use crate::create::templates;
use crate::route::{Route, releases};
use crate::ui::form::Row;
use crate::ui::page::{Link, Page};
use crate::ui::pages::{FormRow, Mark, TextBox, WriteFormView};

use crate::data::DataKey;
use crate::follow::{Expectation, Shown};
use crate::forms::Answer;
use crate::forms::{Fields, filling, say};
use crate::keymap::Action;
use crate::nav::{Doable, row};
use crate::picker::entry;
use crate::picker::fuzzy_score;
use crate::state::{Cmd, State};
use crate::work::{self, Cx, Failure, Job};

/// A release, as written so far: a new one, or one being edited.
#[derive(Debug, Clone)]
pub struct Form {
    /// The title on the first line, then the notes.
    pub text: String,
    /// The text GitHub's notes filled in: while it's still that,
    /// generating again replaces it.
    pub filled: String,
    /// The tag picker has opened on it (a new release), or it's been
    /// filled from the release (an edit).
    pub started: bool,
    pub tag: Option<String>,
    /// What a new tag is made at: a branch or a commit (`None`: the
    /// default branch).
    pub target: Option<String>,
    pub prerelease: bool,
    pub latest: bool,
    /// The release this edits, if it does.
    pub editing: Option<Editing>,
    /// GitHub is writing the notes.
    pub generating: bool,
}

impl Fields for Form {
    fn text(&mut self, _: usize) -> Option<&mut String> {
        Some(&mut self.text)
    }

    fn title(&self, route: &Route, _: usize) -> String {
        match route {
            Route::Releases(releases::EditRelease { tag, .. }) => {
                format!("Editing {tag}: its title on the first line, then its notes")
            }
            _ => "New release: its title on the first line, then its notes".into(),
        }
    }

    fn save(&self, route: &Route) -> &'static str {
        match route {
            Route::Releases(releases::EditRelease { .. }) => "save…",
            _ => "publish…",
        }
    }
}

/// The release a form edits, as it was.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Editing {
    /// REST's ID for it.
    pub id: u64,
    pub tag: String,
    pub draft: bool,
}

impl Default for Form {
    fn default() -> Self {
        Self {
            text: String::new(),
            filled: String::new(),
            started: false,
            tag: None,
            target: None,
            prerelease: false,
            latest: true,
            editing: None,
            generating: false,
        }
    }
}

impl Form {
    /// A form editing `r`, with what it has.
    fn of(r: &Release, id: u64) -> Self {
        let text = templates::join(&r.name, r.notes.as_deref().unwrap_or_default());
        Self {
            text,
            started: true,
            tag: Some(r.tag.clone()),
            prerelease: r.prerelease,
            latest: r.latest,
            editing: Some(Editing {
                id,
                tag: r.tag.clone(),
                draft: r.draft,
            }),
            ..Self::default()
        }
    }

    /// What GitHub has of the tag, once it's said: an edit's own tag is
    /// its release's.
    fn tag_look<'a>(
        &self,
        state: &'a State,
        repo: &RepoId,
    ) -> Option<Result<&'a TagLook, &'a str>> {
        static OWN: TagLook = TagLook {
            exists: true,
            release: None,
        };
        if self.own_tag() {
            return Some(Ok(&OWN));
        }
        state.answer(&DataKey::TagLook(repo.clone(), self.tag.clone()?))
    }

    /// The tag is a new one: GitHub makes it at the target.
    fn new_tag(&self, state: &State, repo: &RepoId) -> bool {
        matches!(
            self.tag_look(state, repo),
            Some(Ok(TagLook { exists: false, .. }))
        )
    }

    /// The tag's release is the one this edits.
    fn own_tag(&self) -> bool {
        (self.editing.as_ref()).is_some_and(|e| self.tag.as_ref() == Some(&e.tag))
    }
}

/// GitHub's notes for a release of the tag.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Generate {
    pub repo: RepoId,
    pub tag: String,
    pub target: Option<String>,
}

/// Makes the release written on the form at `route`, or (with an ID)
/// changes that release to it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Save {
    pub route: Route,
    pub id: Option<u64>,
    pub new: NewRelease,
}

/// Deletes a release; its answer says whether its tag is on GitHub (a
/// draft's new tag never was).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Delete {
    pub repo: RepoId,
    pub tag: String,
    pub id: u64,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeleteTag {
    pub repo: RepoId,
    pub tag: String,
}

/// What a release's page, or the releases, show once GitHub has a change.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Expected {
    /// A published release's page.
    Release(RepoId, String),
    /// A release's page, as it was edited.
    Edited {
        repo: RepoId,
        tag: String,
        name: String,
        notes: String,
        prerelease: bool,
    },
    /// A release no longer among the releases.
    ReleaseGone(RepoId, String),
    /// A release's page with its asset `name` (or without it).
    Asset {
        repo: RepoId,
        tag: String,
        name: String,
        there: bool,
    },
    /// A draft among the releases.
    Draft(RepoId, String),
}

impl Expectation for Expected {
    fn waiting(&self) -> &'static str {
        match self {
            Expected::Release(..) | Expected::Draft(..) | Expected::Edited { .. } => {
                "Waiting for GitHub to show the release"
            }
            Expected::ReleaseGone(..) => "Waiting for GitHub to show the release gone",
            Expected::Asset { there: true, .. } => "Waiting for GitHub to show the asset",
            Expected::Asset { .. } => "Waiting for GitHub to show the asset gone",
        }
    }

    fn shown(&self, state: &State, _: &Route) -> Shown {
        let release = |repo: &RepoId, tag: &String| {
            state.picked::<Release>(&DataKey::Release(repo.clone(), tag.clone()))
        };
        let releases =
            |repo: &RepoId| state.picked::<Results<Release>>(&DataKey::Releases(repo.clone()));
        match self {
            Expected::Release(repo, tag) => release(repo, tag).is_some(),
            Expected::Edited {
                repo,
                tag,
                name,
                notes,
                prerelease,
            } => release(repo, tag).is_some_and(|r| {
                r.name == *name
                    && r.prerelease == *prerelease
                    && r.notes.as_deref().unwrap_or_default().trim() == notes.trim()
            }),
            Expected::ReleaseGone(repo, tag) => {
                releases(repo).is_some_and(|r| r.items.iter().all(|r| r.tag != *tag))
            }
            Expected::Asset {
                repo,
                tag,
                name,
                there,
            } => assets::shows(state, repo, tag, name, *there),
            Expected::Draft(repo, tag) => {
                releases(repo).is_some_and(|r| r.items.iter().any(|r| r.tag == *tag && r.draft))
            }
        }
        .into()
    }
}

/// The release form on screen: its page, its repository, and the form
/// (blank until it's started).
fn here(state: &State) -> Option<(Route, RepoId, Cow<'_, Form>)> {
    let route = state.route()?;
    let repo = form_repo(route)?;
    Some((route.clone(), repo.clone(), state.forms.shown(route)))
}

/// What releases add: making one wherever a repository is, and editing,
/// deleting and uploading to the one on screen.
pub const FEATURE: crate::features::Feature = crate::features::Feature {
    owns: &[Action::NewRelease, Action::UploadAsset],
    takes: |state, action| {
        matches!(action, Action::EditComment | Action::DeleteComment)
            && matches!(
                state.route(),
                Some(Route::Releases(releases::Release { .. }))
            )
    },
    act: |state, action| match action {
        Action::NewRelease => open(state),
        Action::UploadAsset => assets::open_upload(state),
        Action::EditComment => open_edit(state),
        _ => ask_delete(state),
    },
    offers: doables,
    ..crate::features::Feature::NONE
};

/// Why `repo` takes no release from you (nor changes to one), from
/// what's known.
pub(crate) fn refused(state: &State, repo: &RepoId) -> Option<String> {
    let o = state.overview(repo)?;
    if o.summary.archived {
        return Some(format!("{repo} is archived: it takes nothing new"));
    }
    (!o.can_write).then(|| format!("Releases take write access to {repo}"))
}

/// `,V`: the repository's new release, with what was written before.
#[must_use]
pub fn open(state: &mut State) -> Vec<Cmd> {
    let Some(repo) = state.route().and_then(Route::repo).cloned() else {
        return state.just_say("Open a repository first: a release is of one");
    };
    if let Some(why) = refused(state, &repo) {
        return state.just_say(why);
    }
    let route = Route::Releases(releases::NewRelease(repo));
    if state.route() == Some(&route) {
        return compose(state);
    }
    let mut cmds = state.push(route);
    cmds.extend(follow(state));
    cmds
}

/// The release on screen (its page's), as loaded.
pub(crate) fn shown(state: &State) -> Option<(RepoId, String, &Release)> {
    let Some(Route::Releases(releases::Release { repo, tag })) = state.route() else {
        return None;
    };
    let r = state.picked::<Release>(&DataKey::Release(repo.clone(), tag.clone()))?;
    Some((repo.clone(), tag.clone(), r))
}

/// Why the release on screen can't be changed, if it can't.
pub(crate) fn unchangeable(state: &State) -> Option<String> {
    let Some((repo, _, r)) = shown(state) else {
        return Some("The release hasn't loaded yet".into());
    };
    if let Some(why) = refused(state, &repo) {
        return Some(why);
    }
    r.id.is_none()
        .then(|| "GitHub didn't say the release's ID, which changing it takes".into())
}

/// `,e` on a release's page: the form editing it.
#[must_use]
pub fn open_edit(state: &mut State) -> Vec<Cmd> {
    if let Some(why) = unchangeable(state) {
        return state.just_say(why);
    }
    let Some((repo, tag, _)) = shown(state) else {
        return Vec::new();
    };
    let mut cmds = state.push(Route::Releases(releases::EditRelease { repo, tag }));
    cmds.extend(follow(state));
    cmds
}

/// Brings the form on screen up to date: fills an edit's from its
/// release, starts a new one by choosing its tag, and asks GitHub about
/// a tag it hasn't yet.
#[must_use]
fn follow(state: &mut State) -> Vec<Cmd> {
    let Some((route, repo, form)) = here(state) else {
        return Vec::new();
    };
    if let Route::Releases(releases::EditRelease { tag, .. }) = &route
        && !form.started
        && let Some(r) = state.picked::<Release>(&DataKey::Release(repo.clone(), tag.clone()))
        && let Some(id) = r.id
    {
        *state.forms.edit(&route) = Form::of(r, id);
    }
    let mut cmds = Vec::new();
    let loaded = state.overview(&repo).is_some();
    let form = state.forms.shown::<Form>(&route);
    let (started, tag) = (form.started, form.tag.clone().filter(|_| !form.own_tag()));
    if let Some(tag) = tag {
        cmds.extend(state.ensure(DataKey::TagLook(repo.clone(), tag), false));
    }
    let new = matches!(route, Route::Releases(releases::NewRelease(_)));
    if new && loaded && !started && state.overlay.is_none() && refused(state, &repo).is_none() {
        state.forms.edit::<Form>(&route).started = true;
        cmds.extend(state.open_picker(crate::picker::Kind::Tag(route)));
    }
    cmds
}

/// The composer, on the title and notes.
#[must_use]
fn compose(state: &mut State) -> Vec<Cmd> {
    let Some((route, ..)) = here(state) else {
        return Vec::new();
    };
    crate::forms::compose::<Form>(state, &route, 0, crate::forms::TITLE)
}

/// What the release form does.
pub const FORM: crate::forms::Kind = crate::forms::Kind {
    compose: |state, _| compose(state),
    row: on_row,
    fill,
    submit,
    follow,
    offers,
};

/// Enter on one of the form's rows.
#[must_use]
fn on_row(state: &mut State, link: &Row) -> Vec<Cmd> {
    let Some((route, repo, form)) = here(state) else {
        return Vec::new();
    };
    match link {
        Row::Tag => state.open_picker(crate::picker::Kind::Tag(route)),
        Row::Target if form.new_tag(state, &repo) => {
            state.open_picker(crate::picker::Kind::Target(route))
        }
        Row::Target => state.just_say("The tag is GitHub's already: it's where it is"),
        Row::Notes => generate(state),
        Row::Prerelease => {
            let form = state.forms.edit::<Form>(&route);
            form.prerelease = !form.prerelease;
            Vec::new()
        }
        Row::Latest if form.prerelease => {
            state.just_say("A pre-release is never the latest release")
        }
        Row::Latest => {
            let form = state.forms.edit::<Form>(&route);
            form.latest = !form.latest;
            Vec::new()
        }
        _ => Vec::new(),
    }
}

/// Asks GitHub to write the notes.
#[must_use]
fn generate(state: &mut State) -> Vec<Cmd> {
    let Some((route, repo, form)) = here(state) else {
        return Vec::new();
    };
    let Some(tag) = form.tag.clone() else {
        return state
            .just_say("Choose a tag first: GitHub writes the notes since the release before it");
    };
    if form.generating {
        return Vec::new();
    }
    let target = form.target.clone().filter(|_| form.new_tag(state, &repo));
    state.forms.edit::<Form>(&route).generating = true;
    state.info("Asking GitHub for release notes…");
    work::start(Generate { repo, tag, target })
}

/// Whether `tag` can be a tag's name, as git's `check-ref-format` has it,
/// or why not.
fn tag_problem(tag: &str) -> Option<&'static str> {
    let bad = |c: char| c.is_whitespace() || c.is_control() || "~^:?*[\\".contains(c);
    if tag.is_empty() {
        Some("A tag needs a name")
    } else if tag.chars().any(bad) || tag.contains("..") || tag.contains("@{") {
        Some("A tag's name can't have spaces, .., or any of ~^:?*[\\")
    } else if tag.starts_with(['/', '.', '-'])
        || tag.ends_with(['/', '.'])
        || tag.ends_with(".lock")
    {
        Some("A tag's name can't start with / . or -, or end with / . or .lock")
    } else {
        None
    }
}

/// The repository a form's page is of.
fn form_repo(route: &Route) -> Option<&RepoId> {
    match route {
        Route::Releases(releases::NewRelease(repo))
        | Route::Releases(releases::EditRelease { repo, .. }) => Some(repo),
        _ => None,
    }
}

/// The tag picker's rows: a new tag typed, then the tags GitHub has.
pub fn tag_rows(state: &State, route: &Route, q: &str) -> crate::picker::Rows {
    let Some(repo) = form_repo(route) else {
        return Vec::new();
    };
    let tags = state.picked::<Results<TagInfo>>(&DataKey::Tags(repo.clone()));
    let mut rows = Vec::new();
    let known = tags.is_some_and(|t| t.items.iter().any(|t| t.name == q));
    if !q.is_empty() && !known {
        rows.push(match tag_problem(q) {
            Some(why) => entry(why, "", None),
            None => entry(
                format!("Create new tag: {q}"),
                "on publish",
                Some(filling(route, Row::Tag, q)),
            ),
        });
    }
    let Some(tags) = tags else {
        rows.push(entry("Loading the tags…", "", None));
        return rows;
    };
    let mut hits: Vec<(usize, &TagInfo)> = (tags.items.iter())
        .filter_map(|t| Some((fuzzy_score(q, &t.name)?, t)))
        .collect();
    hits.sort_by_key(|(score, _)| *score);
    for (_, t) in hits {
        let hint = t
            .date
            .as_deref()
            .map_or("tag", |d| d.get(..10).unwrap_or(d));
        rows.push(entry(
            t.name.clone(),
            hint,
            Some(filling(route, Row::Tag, &t.name)),
        ));
    }
    let more = tags.total.saturating_sub(tags.items.len() as u64);
    if more > 0 {
        let what = crate::ui::text::noun(more, "tag");
        rows.push(entry(
            format!("{more} older {what} on GitHub: type one's name"),
            "",
            None,
        ));
    }
    rows
}

/// The target picker's rows: a commit typed, then the branches.
pub fn target_rows(state: &State, route: &Route, q: &str) -> crate::picker::Rows {
    let Some(repo) = form_repo(route) else {
        return Vec::new();
    };
    let branches = state.picked::<Results<BranchInfo>>(&DataKey::Branches(repo.clone()));
    let mut rows = Vec::new();
    let known = branches.is_some_and(|b| b.items.iter().any(|b| b.name == q));
    if !q.is_empty() && !known {
        rows.push(entry(
            format!("Use {q}"),
            "a branch or a commit's SHA",
            Some(filling(route, Row::Target, q)),
        ));
    }
    let Some(branches) = branches else {
        rows.push(entry("Loading the branches…", "", None));
        return rows;
    };
    let mut hits: Vec<(usize, &BranchInfo)> = (branches.items.iter())
        .filter_map(|b| Some((fuzzy_score(q, &b.name)?, b)))
        .collect();
    hits.sort_by_key(|(score, b)| (!b.default, *score));
    for (_, b) in hits {
        let hint = if b.default {
            "default branch"
        } else {
            "branch"
        };
        rows.push(entry(
            b.name.clone(),
            hint,
            Some(filling(route, Row::Target, &b.name)),
        ));
    }
    rows
}

#[must_use]
/// A tag, or what a new tag is made at, chosen.
fn fill(state: &mut State, route: &Route, row: &Row, value: String) -> Vec<Cmd> {
    let form = state.forms.edit::<Form>(route);
    match row {
        Row::Tag => {
            form.tag = Some(value);
            let blank = form.text.trim().is_empty();
            let mut cmds = follow(state);
            // Then its title and notes, as on GitHub.
            if blank && state.overlay.is_none() {
                cmds.extend(compose(state));
            }
            cmds
        }
        Row::Target => {
            form.target = Some(value);
            Vec::new()
        }
        _ => Vec::new(),
    }
}

/// The default branch, for what a new tag is made at by default.
fn default_branch(state: &State, repo: &RepoId) -> String {
    (state.overview(repo))
        .and_then(|o| o.default_branch.clone())
        .unwrap_or_else(|| "the default branch".into())
}

/// Why the release can't be saved yet, if it can't.
fn problem(state: &State, repo: &RepoId, form: &Form) -> Option<String> {
    if let Some(why) = refused(state, repo) {
        return Some(why);
    }
    let tag = form.tag.as_deref()?;
    match form.tag_look(state, repo) {
        None => Some(format!("Still looking for {tag} on GitHub")),
        Some(Err(why)) => Some(format!("Couldn't look for {tag} on GitHub: {why}")),
        // Its own release, being edited.
        Some(Ok(_)) if form.own_tag() => None,
        Some(Ok(TagLook {
            release: Some((true, _)),
            ..
        })) => Some(format!(
            "{tag} has a draft release already: publish or delete that one"
        )),
        Some(Ok(TagLook {
            release: Some(_), ..
        })) => Some(format!("{tag} has a release already")),
        Some(Ok(_)) => None,
    }
}

/// `,s`, ctrl-s, or its button: asks whether to publish it or keep a
/// draft.
#[must_use]
fn submit(state: &mut State) -> Vec<Cmd> {
    let Some((route, repo, form)) = here(state) else {
        return Vec::new();
    };
    let Some(tag) = form.tag.clone() else {
        return say(state, "Choose a tag first (Enter on Tag)".into());
    };
    if let Some(why) = problem(state, &repo, &form) {
        return say(state, why);
    }
    let (title, notes) = templates::split(&form.text);
    let mut facts = vec![(
        if form.new_tag(state, &repo) {
            let at = form
                .target
                .clone()
                .unwrap_or_else(|| default_branch(state, &repo));
            format!("A new tag, {tag}, made at {at}")
        } else {
            format!("Tag {tag}, as GitHub has it")
        },
        false,
    )];
    if let Some(was) = form.editing.as_ref().filter(|e| e.tag != tag) {
        facts.push((format!("Its tag was {}: that tag stays", was.tag), true));
    }
    facts.push((
        if title.is_empty() {
            format!("Titled {tag}: it has no title of its own")
        } else {
            format!("“{title}”")
        },
        false,
    ));
    if notes.is_empty() {
        facts.push(("It has no notes".into(), true));
    }
    facts.push((
        match (form.prerelease, form.latest) {
            (true, _) => "A pre-release".to_owned(),
            (false, true) => "It becomes the latest release".to_owned(),
            (false, false) => "It doesn't become the latest release".to_owned(),
        },
        false,
    ));
    let (question, choices) = match &form.editing {
        None => (
            format!("Publish {tag} in {repo}?"),
            vec![
                ("Publish release".into(), Answer::Release { draft: false }),
                ("Save draft".into(), Answer::Release { draft: true }),
            ],
        ),
        Some(e) if e.draft => (
            format!("Save the draft of {tag}?"),
            vec![
                ("Publish release".into(), Answer::Release { draft: false }),
                ("Save draft".into(), Answer::Release { draft: true }),
            ],
        ),
        Some(_) => (
            format!("Update {tag}?"),
            vec![
                ("Update release".into(), Answer::Release { draft: false }),
                (
                    "Make it a draft again".into(),
                    Answer::Release { draft: true },
                ),
            ],
        ),
    };
    crate::forms::ask(state, route, question, facts, choices);
    Vec::new()
}

/// Saves the release: published, or a draft.
#[must_use]
pub fn create(state: &mut State, route: &Route, draft: bool) -> Vec<Cmd> {
    let Some(repo) = form_repo(route).cloned() else {
        return Vec::new();
    };
    let Some(form) = state.forms.get::<Form>(route) else {
        return Vec::new();
    };
    let Some(tag) = form.tag.clone() else {
        return Vec::new();
    };
    let (name, notes) = templates::split(&form.text);
    let new = NewRelease {
        target: form.target.clone().filter(|_| form.new_tag(state, &repo)),
        repo,
        tag,
        name,
        notes,
        draft,
        prerelease: form.prerelease,
        latest: form.latest,
    };
    let id = form.editing.as_ref().map(|e| e.id);
    crate::forms::form_sending(state, route);
    work::start(Save {
        route: route.clone(),
        id,
        new,
    })
}

/// `,D` on a release's page: deleting the asset selected, or the release,
/// after asking.
#[must_use]
pub fn ask_delete(state: &mut State) -> Vec<Cmd> {
    if let Some(why) = unchangeable(state) {
        return state.just_say(why);
    }
    if let Some(name) = assets::selected(state) {
        return assets::ask_delete(state, &name);
    }
    let Some((repo, tag, r)) = shown(state) else {
        return Vec::new();
    };
    let mut facts = Vec::new();
    let n = r.assets.total();
    if n > 0 {
        let what = crate::ui::text::noun(n, "asset");
        facts.push((format!("Its {n} {what} go with it"), true));
    }
    facts.push((
        format!("Its tag, {tag}, stays: you're asked about it next"),
        false,
    ));
    let title = format!("Delete the release {}?", r.name);
    let route = Route::Releases(releases::Release { repo, tag });
    let choices = vec![("Delete release".into(), Answer::DeleteRelease)];
    crate::forms::ask(state, route, title, facts, choices);
    Vec::new()
}

/// The release on `route`'s page was asked to go: deletes it.
#[must_use]
pub fn delete(state: &mut State, route: &Route) -> Vec<Cmd> {
    let Route::Releases(releases::Release { repo, tag }) = route else {
        return Vec::new();
    };
    let key = DataKey::Release(repo.clone(), tag.clone());
    let Some(id) = state.picked::<Release>(&key).and_then(|r| r.id) else {
        return Vec::new();
    };
    crate::forms::sending(state);
    work::start(Delete {
        repo: repo.clone(),
        tag: tag.clone(),
        id,
    })
}

/// Deletes the tag of the release deleted from `route`, as asked.
#[must_use]
pub fn delete_tag(state: &mut State, route: &Route) -> Vec<Cmd> {
    let Route::Releases(releases::Release { repo, tag }) = route else {
        return Vec::new();
    };
    crate::forms::sending(state);
    work::start(DeleteTag {
        repo: repo.clone(),
        tag: tag.clone(),
    })
}

impl Job for Generate {
    type Out = Notes;
    const WRITES: bool = false;

    async fn run(&self, cx: &Cx) -> Result<Notes, Failure> {
        let notes = cx
            .gh
            .release_notes(&self.repo, &self.tag, self.target.as_deref());
        Ok(notes.await?)
    }

    fn done(self, state: &mut State, result: Result<Notes, Failure>) -> Vec<Cmd> {
        let Generate { repo, tag, .. } = self;
        let Some(route) = state
            .route()
            .cloned()
            .filter(|r| form_repo(r) == Some(&repo))
        else {
            return Vec::new();
        };
        let Some(form) = state.forms.get_mut::<Form>(&route) else {
            return Vec::new();
        };
        form.generating = false;
        match result {
            Ok(notes) => {
                // GitHub's title and notes in place of what's there,
                // unless you've written your own: then below them.
                let (title, _) = templates::split(&form.text);
                if form.text.trim().is_empty() || form.text == form.filled {
                    let title = if title.is_empty() { notes.name } else { title };
                    form.text = templates::join(&title, &notes.body);
                    form.filled = form.text.clone();
                } else {
                    form.text = format!("{}\n\n{}", form.text.trim_end(), notes.body);
                }
                let write = state.first_key(Action::Comment);
                state.info(format!(
                    "GitHub wrote the notes for {tag}: {write} edits them"
                ));
            }
            Err(why) => state.error(format!("GitHub didn't write the notes: {why}")),
        }
        Vec::new()
    }
}

impl Job for DeleteTag {
    type Out = ();

    async fn run(&self, cx: &Cx) -> Result<(), Failure> {
        Ok(cx.gh.delete_tag(&self.repo, &self.tag).await?)
    }

    fn done(self, state: &mut State, result: Result<(), Failure>) -> Vec<Cmd> {
        let DeleteTag { repo, tag } = self;
        let route = Route::Releases(releases::Release {
            repo: repo.clone(),
            tag: tag.clone(),
        });
        match result {
            Ok(()) => {
                crate::forms::close_sender(state, &route);
                state.info(format!("Deleted the tag {tag} on GitHub"));
                // The tags list, if it's shown, no longer has it.
                state.ensure(DataKey::Tags(repo), true)
            }
            Err(why) => say(state, format!("GitHub didn't delete the tag: {why}")),
        }
    }
}

impl Job for Save {
    type Out = Made;

    async fn run(&self, cx: &Cx) -> Result<Made, Failure> {
        let gh = &cx.gh;
        Ok(match self.id {
            Some(id) => gh.update_release(id, &self.new).await?,
            None => gh.create_release(&self.new).await?,
        })
    }

    /// GitHub saved the release: its page opens in place of the form's,
    /// and follows GitHub until it shows it.
    fn done(self, state: &mut State, result: Result<Made, Failure>) -> Vec<Cmd> {
        let route = &self.route;
        let Some(repo) = form_repo(route).cloned() else {
            return Vec::new();
        };
        match result {
            Ok(made) => {
                let form: Form = crate::forms::done(state, route);
                let tag = made.tag_name;
                let editing = form.editing.is_some();
                let (page, what, said) = if made.draft {
                    let said = format!("Saved a draft release of {tag}");
                    (
                        Route::Releases(releases::Releases(repo.clone())),
                        Expected::Draft(repo, tag),
                        said,
                    )
                } else {
                    let (name, notes) = templates::split(&form.text);
                    let what = if editing {
                        Expected::Edited {
                            repo: repo.clone(),
                            tag: tag.clone(),
                            name: if name.is_empty() { tag.clone() } else { name },
                            notes,
                            prerelease: form.prerelease,
                        }
                    } else {
                        Expected::Release(repo.clone(), tag.clone())
                    };
                    let said = if editing {
                        format!("Updated {tag}")
                    } else {
                        format!("Published {tag}")
                    };
                    (Route::Releases(releases::Release { repo, tag }), what, said)
                };
                state.info(said);
                if state.route() != Some(route) {
                    return Vec::new();
                }
                crate::follow::expect(state, page.clone(), what);
                state.replace(page, true, None)
            }
            Err(why) => {
                let why = format!("GitHub didn't save the release: {why}");
                crate::forms::failed(state, route, why)
            }
        }
    }
}

impl Job for Delete {
    type Out = bool;

    async fn run(&self, cx: &Cx) -> Result<bool, Failure> {
        let (gh, repo) = (&cx.gh, &self.repo);
        gh.delete_release(repo, self.id).await?;
        // Whether there's a tag to ask about.
        Ok(gh.tag_look(repo, &self.tag).await.is_ok_and(|l| l.exists))
    }

    /// GitHub deleted the release: the releases follow until it's gone, and
    /// deleting its tag too is asked, apart.
    fn done(self, state: &mut State, result: Result<bool, Failure>) -> Vec<Cmd> {
        let Delete { repo, tag, .. } = &self;
        let route = Route::Releases(releases::Release {
            repo: repo.clone(),
            tag: tag.to_owned(),
        });
        let tagged = match result {
            Ok(tagged) => tagged,
            Err(why) => {
                return say(state, format!("GitHub didn't delete the release: {why}"));
            }
        };
        crate::forms::close_sender(state, &route);
        state.info(format!("Deleted the release of {tag}"));
        let mut cmds = Vec::new();
        if state.route() == Some(&route) {
            let list = Route::Releases(releases::Releases(repo.clone()));
            let gone = Expected::ReleaseGone(repo.clone(), tag.to_owned());
            crate::follow::expect(state, list.clone(), gone);
            cmds = state.replace(list, true, None);
        }
        if !tagged {
            return cmds;
        }
        crate::forms::ask(
            state,
            route,
            format!("Delete the tag {tag} as well?"),
            vec![
                (
                    format!(
                        "The release is deleted; the tag {tag} stays on GitHub unless you delete it"
                    ),
                    false,
                ),
                ("A deleted tag can't be brought back from here".into(), true),
            ],
            vec![
                (format!("Delete the tag {tag}"), Answer::DeleteTag),
                ("Keep the tag".into(), Answer::KeepTag),
            ],
        );
        cmds
    }
}

/// The form's rows in the menu: writing it, and saving it.
fn offers(state: &State, out: &mut Vec<Doable>) {
    if let Some((_, repo, form)) = here(state) {
        let why = match &form.tag {
            None => Some("choose a tag first".to_owned()),
            Some(_) => problem(state, &repo, &form),
        };
        let publish = if form.editing.is_some() {
            "Save the release"
        } else {
            "Publish the release, or save a draft"
        };
        let write = Some("Write the title and notes");
        crate::forms::offer(state, write, (publish, "publish"), why, out);
    }
}

/// The menu's rows: making a release from a repository's pages, and
/// changing the release on screen.
fn doables(state: &State, out: &mut Vec<Doable>) {
    let Some(route) = state.route().filter(|_| here(state).is_none()) else {
        return;
    };
    let Some(repo) = route.repo() else {
        return;
    };
    if matches!(route, Route::Releases(releases::Release { .. })) {
        let why = unchangeable(state);
        out.push(row(
            "Edit",
            Action::EditComment,
            "Edit the release",
            "edit",
            why.clone(),
        ));
        let delete = if assets::selected(state).is_some() {
            "Delete the asset"
        } else {
            "Delete the release"
        };
        out.push(row(
            "Edit",
            Action::DeleteComment,
            delete,
            "delete",
            why.clone(),
        ));
        out.push(row(
            "Change",
            Action::UploadAsset,
            "Upload an asset",
            "upload",
            why,
        ));
    }
    out.push(row(
        "New",
        Action::NewRelease,
        "New release",
        "",
        refused(state, repo),
    ));
}

/// The form's page.
pub fn build(state: &State, page: &mut Page, route: &Route, repo: &RepoId) {
    let form = state.forms.shown::<Form>(route);
    let form = form.as_ref();
    let composing = crate::forms::composing(state, route, 0);
    let mut rows = vec![FormRow {
        name: "Tag",
        value: form
            .tag
            .as_ref()
            .map_or_else(|| "Choose a tag ▾".into(), |t| format!("{t} ▾")),
        unset: form.tag.is_none(),
        chips: Vec::new(),
        link: Some(Row::Tag),
    }];
    let mut status = Vec::new();
    if let Some(tag) = &form.tag {
        let (text, mark) = match form.tag_look(state, repo) {
            None => (format!("Looking for {tag} on GitHub…"), Mark::Waiting),
            Some(Err(why)) => (format!("Couldn't look for {tag}: {why}"), Mark::Problem),
            Some(Ok(_)) if form.own_tag() => (format!("{tag} is this release's tag"), Mark::Fine),
            Some(Ok(TagLook {
                release: Some(_), ..
            })) => (
                problem(state, repo, form).unwrap_or_default(),
                Mark::Problem,
            ),
            Some(Ok(TagLook { exists: true, .. })) => {
                (format!("{tag} is on GitHub already"), Mark::Fine)
            }
            Some(Ok(_)) => (
                format!("{tag} is a new tag: GitHub makes it when the release is published"),
                Mark::Fine,
            ),
        };
        status.push((text, mark, None));
    }
    if form.new_tag(state, repo) {
        let target = (form.target.clone())
            .unwrap_or_else(|| format!("{} (default branch)", default_branch(state, repo)));
        rows.push(FormRow {
            name: "Target",
            value: format!("{target} ▾"),
            unset: false,
            chips: Vec::new(),
            link: Some(Row::Target),
        });
    }
    let check = |on: bool| if on { "☑" } else { "☐" };
    let notes = if form.generating {
        "GitHub is writing them…"
    } else {
        "From the pull requests since the last release"
    };
    let after = vec![
        FormRow {
            name: "Notes",
            value: format!("Generate release notes · {notes}"),
            unset: false,
            chips: Vec::new(),
            link: Some(Row::Notes),
        },
        FormRow {
            name: "Pre-release",
            value: format!("{} Set as a pre-release", check(form.prerelease)),
            unset: false,
            chips: Vec::new(),
            link: Some(Row::Prerelease),
        },
        FormRow {
            name: "Latest",
            value: if form.prerelease {
                "☐ A pre-release isn't the latest".to_owned()
            } else {
                format!("{} Set as the latest release", check(form.latest))
            },
            unset: form.prerelease,
            chips: Vec::new(),
            link: Some(Row::Latest),
        },
    ];
    let (title, notes) = templates::split(&form.text);
    let write = state.first_key(Action::Comment);
    let save_key = state.first_key(Action::Create);
    let why_not = match &form.tag {
        None => Some("Choose a tag first".to_owned()),
        Some(_) => problem(state, repo, form),
    };
    let (title, notes) = if composing {
        (String::new(), String::new())
    } else {
        (title, notes)
    };
    let (heading, button) = match &form.editing {
        Some(e) => (
            format!("Editing {} in {repo}", e.tag),
            if e.draft {
                "Save the draft…"
            } else {
                "Update release…"
            },
        ),
        None => (format!("New release in {repo}"), "Publish release…"),
    };
    let view = WriteFormView {
        heading,
        base: Some(crate::ui::markdown::LinkBase::new(repo, "HEAD", "")),
        rows,
        status,
        boxes: vec![TextBox {
            title,
            untitled: "Release title (the tag's name, if none)",
            body: &notes,
            markdown: true,
            empty: "No notes yet.",
            hint: "The first line is the title; then the notes, in Markdown (ctrl-e for $EDITOR)",
            link: Link::Form(crate::ui::form::Row::Text(0)),
            actions: Vec::new(),
        }],
        after,
        write: &write,
        button: button.into(),
        save_key: &save_key,
        why_not,
        error: state.forms.sent(route).error.as_deref(),
    };
    crate::ui::pages::write_form(page, &view);
}

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

    #[test]
    fn tag_names_follow_gits_rules() {
        assert_eq!(tag_problem("v1.2.0"), None);
        assert_eq!(tag_problem("release/2026-10"), None);
        assert!(tag_problem("v1 2").is_some());
        assert!(tag_problem("v1..2").is_some());
        assert!(tag_problem("v1.lock").is_some());
        assert!(tag_problem("-v1").is_some());
        assert!(tag_problem("").is_some());
    }
}