thirdshift 0.8.4

Turn a GitHub issue into a ready-for-review pull request, unattended
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
//! The Run notification: one email when a Run, a Spec run or an Architect run
//! ends, whatever its outcome, or when a Pickup run that took an issue does,
//! through the same checks and the same send as `email-test`.

use std::path::Path;
use std::time::{Duration, Instant};

use anyhow::Result;

use crate::config::EmailSettings;
use crate::email::Resend;
use crate::github;
use crate::host;
use crate::issue::{IssueUrl, Repo};
use crate::launch;
use crate::logs;
use crate::progress;
use crate::run_ending::Account;

/// What a Run, an Architect run or a Pickup run asks about its Run
/// notification, by its command or, without `email` or `no-email`, by the
/// User config.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NotificationAsk {
    /// Send one, to this address, else to `email.to`.
    Send(Option<String>),
    /// Send none.
    Skip,
}

/// A Run notification's checks, passed, and when they were made: what every
/// run that asks for one holds from before any work until it ends.
struct Checked {
    resend: Resend,
    started: Instant,
}

impl Checked {
    /// The checks of a Run notification sent to `to`, else to `email.to`,
    /// for a run starting now. Fails with the same checks as `email-test`: an
    /// address is known and a Resend API key is found.
    fn new(to: Option<String>, settings: &EmailSettings) -> Result<Self> {
        let started = Instant::now();
        let resend = Resend::new(to, settings)?;
        Ok(Checked { resend, started })
    }
}

/// What a Run notification is about, as a command starts.
pub enum About<'a> {
    /// A Run or a Spec run on this issue.
    Issue(&'a IssueUrl),
    /// An Architect run, from the Launch directory.
    ArchitectRun,
    /// A Pickup run, which is about the issue it takes, once it takes one.
    PickupRun,
}

/// What a Run notification's subject names.
enum Subject {
    /// The issue a Run or a Spec run is on, with its title if it is known:
    /// as GitHub gave it when the Run started, or as the Pickup run that
    /// took the issue listed it.
    Issue {
        issue: IssueUrl,
        title: Option<String>,
    },
    /// An Architect run, with the repository the Launch directory's `origin`
    /// names, if it names one.
    ArchitectRun(Option<Repo>),
    /// A Pickup run that has taken no issue yet, and so has nothing to tell.
    Pending,
}

/// A Run notification, checked and waiting for its command to end.
pub struct RunNotification {
    checked: Checked,
    subject: Subject,
}

impl RunNotification {
    /// The Run notification for a command about `about` starting now, sent
    /// to `to`, else to `email.to`. Fails, before any work, with the same
    /// checks as `email-test`: an address is known and a Resend API key is
    /// found. Once they pass, it reads a Run's issue title now, while someone
    /// may be watching, rather than after the Run, when a hung `gh` could
    /// keep the notification from ever going; and an Architect run's
    /// repository.
    pub fn new(to: Option<String>, settings: &EmailSettings, about: About) -> Result<Self> {
        let checked = Checked::new(to, settings)?;
        let subject = match about {
            About::Issue(issue) => Subject::Issue {
                issue: issue.clone(),
                // Left out of the subject if GitHub can't be asked; the Run's
                // own preflight reports why.
                title: github::issue_title(issue).ok(),
            },
            // Left out of the subject if origin names none; the Architect
            // run's own preflight reports why.
            About::ArchitectRun => Subject::ArchitectRun(launch::repo().ok()),
            About::PickupRun => Subject::Pending,
        };
        Ok(RunNotification { checked, subject })
    }

    /// Take note that the Pickup run took `issue`, titled `title`, as its
    /// search listed it: the notification is then the one the Spec run or
    /// Run it is dispatched as would send started by hand, which the Pickup
    /// run sends in its place.
    pub fn took(&mut self, issue: &IssueUrl, title: String) {
        self.subject = Subject::Issue {
            issue: issue.clone(),
            title: Some(title),
        };
    }

    /// Send the one notification for the command that ended as `account`
    /// tells. A Pickup run that took no issue has nothing to tell, and sends
    /// none, as a skipped pass, which has no account, does not either. A
    /// failed send is only a warning: it never changes the command's
    /// outcome.
    pub fn send(self, account: &Account) {
        let Some(subject) = subject_line(&self.subject, account.outcome) else {
            return;
        };
        let host = host::name();
        let command_log = logs::command_log_path();
        let text = body(
            account,
            command_log.as_deref(),
            host.as_deref().unwrap_or("unknown host"),
            self.checked.started.elapsed(),
        );
        if let Err(error) = self.checked.resend.send(&subject, &text) {
            progress::step(format_args!(
                "warning: could not send the Run notification: {error:#}"
            ));
        }
    }
}

/// The subject of a notification about `subject` for a command whose
/// outcome is `outcome`: none about a Pickup run that took no issue.
fn subject_line(subject: &Subject, outcome: &str) -> Option<String> {
    match subject {
        Subject::Issue { issue, title } => Some(issue_subject(issue, title.as_deref(), outcome)),
        Subject::ArchitectRun(repo) => Some(architect_subject(repo.as_ref(), outcome)),
        Subject::Pending => None,
    }
}

/// `[thirdshift] <owner>/<repo>#<n> <title>: <outcome>`, without the title
/// if it isn't known.
fn issue_subject(issue: &IssueUrl, title: Option<&str>, outcome: &str) -> String {
    let title = title.map(|title| format!(" {title}")).unwrap_or_default();
    format!(
        "[thirdshift] {}#{}{title}: {outcome}",
        issue.repo_slug(),
        issue.number
    )
}

/// `[thirdshift] <owner>/<repo> Architect run: <outcome>`, without the
/// repository if it isn't known.
fn architect_subject(repo: Option<&Repo>, outcome: &str) -> String {
    let repo = repo
        .map(|repo| format!(" {}", repo.slug()))
        .unwrap_or_default();
    format!("[thirdshift]{repo} Architect run: {outcome}")
}

/// The plain-text body of the notification for a command that ended as
/// `account` tells, naming `command_log`, if the command keeps one, and
/// `host`, for a command that `took` so long. An Architect run's starts with
/// how its review ended, and the outcome of the run it dispatched, if any.
/// The cause is left out of an interrupted command's, whose outcome says so.
fn body(account: &Account, command_log: Option<&Path>, host: &str, took: Duration) -> String {
    let mut text = String::new();
    if let Some(review) = &account.review {
        text += &format!("Review:       {}\n", review.line);
        if review.dispatched.is_some() {
            text += &format!("Dispatched:   {}\n", account.outcome);
        }
    }
    if let Some(pr_url) = account.pr_url {
        text += &format!("Pull request: {pr_url}\n");
    }
    if let Err(cause) = &account.ended
        && !account.interrupted
    {
        text += &format!("Cause:        {}\n", cause.full());
    }
    for line in account.advice {
        text += &format!("{:<14}{}\n", format!("{}:", line.label), line.value);
    }
    if let Some(base_fix) = account.base_fix {
        text += &format!("Base fix:     {base_fix}\n");
    }
    if let Some(log) = account.log {
        text += &format!("Session log:  {}\n", log.display());
    }
    if let Some(log) = command_log {
        text += &format!("Command log:  {}\n", log.display());
    }
    text += &format!("Host:         {host}\n");
    text += &format!("Took:         {}\n", self::took(took));
    if !account.ticket_lines.is_empty() {
        text += "\nTickets:\n";
        for line in account.ticket_lines {
            text += &format!("{line}\n");
        }
    }
    text
}

/// `duration` to the second, as in `1h 2m 3s`, `2m 3s` or `3s`.
fn took(duration: Duration) -> String {
    let seconds = duration.as_secs();
    let (hours, minutes, seconds) = (seconds / 3600, seconds / 60 % 60, seconds % 60);
    if hours > 0 {
        format!("{hours}h {minutes}m {seconds}s")
    } else if minutes > 0 {
        format!("{minutes}m {seconds}s")
    } else {
        format!("{seconds}s")
    }
}

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

    use super::*;
    use crate::base_fix::Advice;
    use crate::run_ending::{Cause, Review};

    const PR: &str = "https://github.com/acme/widgets/pull/1";
    const PLAN: &str = "https://github.com/acme/widgets/issues/8";
    const LOG: &str = "/logs/x.jsonl";

    fn issue() -> IssueUrl {
        IssueUrl::parse("https://github.com/acme/widgets/issues/123").unwrap()
    }

    fn about_issue() -> Subject {
        Subject::Issue {
            issue: issue(),
            title: Some("Add export button".to_string()),
        }
    }

    fn about_architect_run() -> Subject {
        Subject::ArchitectRun(Repo::of_origin("https://github.com/acme/widgets.git"))
    }

    /// A merged Run's account.
    fn merged() -> Account<'static> {
        Account {
            outcome: "merged",
            ended: Ok(format!("PR {PR} is merged")),
            interrupted: false,
            pr_url: Some(PR),
            advice: &[],
            base_fix: None,
            log: Some(Path::new(LOG)),
            ticket_lines: &[],
            review: None,
            urls: vec![PR],
        }
    }

    /// The account of a Run that failed as `cause`, with nothing else to
    /// tell.
    fn failed(cause: &str) -> Account<'static> {
        Account {
            outcome: "failed",
            ended: Err(Cause::of(&anyhow!("{cause}"))),
            interrupted: false,
            pr_url: None,
            advice: &[],
            base_fix: None,
            log: None,
            ticket_lines: &[],
            review: None,
            urls: Vec::new(),
        }
    }

    /// The subject and body of the notification about `subject` for a
    /// command that ended as `account` tells, if it sends one.
    fn told(subject: &Subject, account: &Account) -> Option<(String, String)> {
        subject_line(subject, account.outcome).map(|subject| (subject, body_of(account)))
    }

    /// The body for a command that ended as `account` tells, run on
    /// `droplet-1` for 4s with no Command log.
    fn body_of(account: &Account) -> String {
        body(account, None, "droplet-1", Duration::from_secs(4))
    }

    #[test]
    fn a_runs_notification_tells_how_the_run_ended() {
        assert_eq!(
            told(&about_issue(), &merged()),
            Some((
                "[thirdshift] acme/widgets#123 Add export button: merged".to_string(),
                format!(
                    "Pull request: {PR}\n\
                     Session log:  {LOG}\n\
                     Host:         droplet-1\n\
                     Took:         4s\n"
                )
            ))
        );
        let base_fix = format!("{PLAN} not merged");
        let account = Account {
            base_fix: Some(&base_fix),
            log: Some(Path::new(LOG)),
            ..failed("claude exited 1")
        };
        assert_eq!(
            told(&about_issue(), &account),
            Some((
                "[thirdshift] acme/widgets#123 Add export button: failed".to_string(),
                format!(
                    "Cause:        claude exited 1\n\
                     Base fix:     {PLAN} not merged\n\
                     Session log:  {LOG}\n\
                     Host:         droplet-1\n\
                     Took:         4s\n"
                )
            ))
        );
    }

    #[test]
    fn an_interrupted_runs_notification_says_so_and_leaves_out_the_cause() {
        let account = Account {
            outcome: "interrupted",
            interrupted: true,
            ..failed("interrupted")
        };
        assert_eq!(
            told(&about_issue(), &account),
            Some((
                "[thirdshift] acme/widgets#123 Add export button: interrupted".to_string(),
                "Host:         droplet-1\n\
                 Took:         4s\n"
                    .to_string()
            ))
        );
    }

    #[test]
    fn an_architect_runs_notification_with_its_plan_dispatched_tells_how_both_ended() {
        let account = Account {
            review: Some(Review {
                line: format!("plan published: {PLAN}"),
                dispatched: Some(PLAN),
            }),
            ..merged()
        };
        assert_eq!(
            told(&about_architect_run(), &account),
            Some((
                "[thirdshift] acme/widgets Architect run: merged".to_string(),
                format!(
                    "Review:       plan published: {PLAN}\n\
                     Dispatched:   merged\n\
                     Pull request: {PR}\n\
                     Session log:  {LOG}\n\
                     Host:         droplet-1\n\
                     Took:         4s\n"
                )
            ))
        );
    }

    #[test]
    fn an_architect_runs_notification_with_nothing_dispatched_tells_how_its_review_ended() {
        let account = Account {
            outcome: "idea filed",
            ended: Ok(format!(
                "no Strong candidate: the Architecture review filed the idea {PLAN}"
            )),
            pr_url: None,
            log: None,
            review: Some(Review {
                line: format!("idea filed: {PLAN}"),
                dispatched: None,
            }),
            urls: vec![PLAN],
            ..merged()
        };
        assert_eq!(
            told(&about_architect_run(), &account),
            Some((
                "[thirdshift] acme/widgets Architect run: idea filed".to_string(),
                format!(
                    "Review:       idea filed: {PLAN}\n\
                     Host:         droplet-1\n\
                     Took:         4s\n"
                )
            ))
        );
    }

    #[test]
    fn an_architect_runs_notification_tells_how_its_review_failed() {
        let account = Account {
            outcome: "review failed",
            log: Some(Path::new(LOG)),
            review: Some(Review {
                line: "failed".to_string(),
                dispatched: None,
            }),
            ..failed("claude exited 1")
        };
        assert_eq!(
            told(&Subject::ArchitectRun(None), &account),
            Some((
                "[thirdshift] Architect run: review failed".to_string(),
                format!(
                    "Review:       failed\n\
                     Cause:        claude exited 1\n\
                     Session log:  {LOG}\n\
                     Host:         droplet-1\n\
                     Took:         4s\n"
                )
            ))
        );
    }

    #[test]
    fn a_pickup_run_that_took_nothing_has_no_notification() {
        assert_eq!(told(&Subject::Pending, &merged()), None);
    }

    #[test]
    fn the_subject_names_the_issue_its_title_if_known_and_the_outcome() {
        assert_eq!(
            issue_subject(&issue(), Some("Add export button"), "ready for review"),
            "[thirdshift] acme/widgets#123 Add export button: ready for review"
        );
        assert_eq!(
            issue_subject(&issue(), None, "failed"),
            "[thirdshift] acme/widgets#123: failed"
        );
    }

    #[test]
    fn the_body_leaves_out_what_the_run_does_not_have() {
        assert_eq!(
            body_of(&failed("origin mismatch")),
            "Cause:        origin mismatch\n\
             Host:         droplet-1\n\
             Took:         4s\n"
        );
        let account = Account {
            log: Some(Path::new("/home/me/.thirdshift/logs/sessions/x.jsonl")),
            ..merged()
        };
        assert_eq!(
            body(
                &account,
                Some(Path::new("/home/me/.thirdshift/logs/commands/issue/x.log")),
                "droplet-1",
                Duration::from_secs(4)
            ),
            "Pull request: https://github.com/acme/widgets/pull/1\n\
             Session log:  /home/me/.thirdshift/logs/sessions/x.jsonl\n\
             Command log:  /home/me/.thirdshift/logs/commands/issue/x.log\n\
             Host:         droplet-1\n\
             Took:         4s\n"
        );
    }

    #[test]
    fn the_body_reports_the_base_fix_after_the_cause() {
        let account = Account {
            pr_url: Some(PR),
            base_fix: Some("https://github.com/acme/widgets/issues/8 merged"),
            ..failed("claude exited 1")
        };
        assert_eq!(
            body_of(&account),
            "Pull request: https://github.com/acme/widgets/pull/1\n\
             Cause:        claude exited 1\n\
             Base fix:     https://github.com/acme/widgets/issues/8 merged\n\
             Host:         droplet-1\n\
             Took:         4s\n"
        );
    }

    #[test]
    fn the_body_gives_the_advice_after_the_cause_laid_out_like_its_other_lines() {
        let advice = [
            Advice {
                label: "Base check",
                value: "test: https://ci.example/main/test".to_string(),
            },
            Advice {
                label: "Or set",
                value: "base.fix = true".to_string(),
            },
        ];
        let account = Account {
            pr_url: Some(PR),
            advice: &advice,
            log: Some(Path::new("/home/me/.thirdshift/logs/x.jsonl")),
            ..failed("CI red on test, which also fails on main at 362b9ca; fix main first")
        };
        assert_eq!(
            body_of(&account),
            "Pull request: https://github.com/acme/widgets/pull/1\n\
             Cause:        CI red on test, which also fails on main at 362b9ca; fix main first\n\
             Base check:   test: https://ci.example/main/test\n\
             Or set:       base.fix = true\n\
             Session log:  /home/me/.thirdshift/logs/x.jsonl\n\
             Host:         droplet-1\n\
             Took:         4s\n"
        );
    }

    #[test]
    fn a_spec_runs_body_ends_with_a_line_per_ticket() {
        let tickets = [
            "#21 failed: claude exited 1".to_string(),
            "#22 blocked by #21".to_string(),
        ];
        let account = Account {
            ticket_lines: &tickets,
            ..failed("Tickets not done: #21, #22")
        };
        assert_eq!(
            body_of(&account),
            "Cause:        Tickets not done: #21, #22\n\
             Host:         droplet-1\n\
             Took:         4s\n\
             \n\
             Tickets:\n\
             #21 failed: claude exited 1\n\
             #22 blocked by #21\n"
        );
    }

    #[test]
    fn an_architect_runs_subject_names_the_repository_if_known_and_the_outcome() {
        let repo = Repo::of_origin("https://github.com/acme/widgets.git").unwrap();
        assert_eq!(
            architect_subject(Some(&repo), "plan published"),
            "[thirdshift] acme/widgets Architect run: plan published"
        );
        assert_eq!(
            architect_subject(None, "review failed"),
            "[thirdshift] Architect run: review failed"
        );
    }

    #[test]
    fn took_is_to_the_second() {
        for (seconds, text) in [
            (0, "0s"),
            (59, "59s"),
            (60, "1m 0s"),
            (3599, "59m 59s"),
            (3723, "1h 2m 3s"),
        ] {
            assert_eq!(took(Duration::from_secs(seconds)), text);
        }
    }
}