local-ci 0.6.2

Run the GitHub Actions jobs a branch would trigger, locally, in an isolated worktree, and print a short report
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
//! The result of a run: its state, the short text report, and the JSON file.

use crate::exec::{tail, JobResult, Outcome, StepResult, UnitResult};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Skipped {
    pub workflow: String,
    pub reason: String,
}

#[derive(Debug, Serialize)]
pub struct RunResult {
    pub version: String,
    pub sha: String,
    pub base: String,
    pub changed_files: usize,
    /// `changes` (what the changes trigger) or `all` (paths filters ignored).
    pub mode: String,
    pub state: String,
    pub description: String,
    pub secs: f64,
    pub run_dir: PathBuf,
    pub setup: Vec<StepResult>,
    pub units: Vec<UnitResult>,
    pub skipped_workflows: Vec<Skipped>,
    pub not_covered: Vec<String>,
    pub warnings: Vec<String>,
}

/// What `local-ci status` needs back from a saved result.
#[derive(Debug, Deserialize)]
pub struct Saved {
    pub sha: String,
    pub state: String,
    pub description: String,
}

static DEFAULT_WORKFLOWS_DIR: std::sync::RwLock<Option<String>> = std::sync::RwLock::new(None);

/// The directory `short` leaves off: the first `workflows-dirs` entry of
/// `.local-ci.toml`. Set by `resolve`, before anything is printed.
pub fn set_default_workflows_dir(d: &str) {
    *DEFAULT_WORKFLOWS_DIR
        .write()
        .unwrap_or_else(|e| e.into_inner()) = Some(d.to_string());
}

/// How a workflow is named in output: the file name for the default directory
/// (`.github/workflows/`, or the first one `.local-ci.toml` names), the path
/// for any other, so equal file names stay apart.
pub fn short(f: &str) -> &str {
    let dir = DEFAULT_WORKFLOWS_DIR
        .read()
        .unwrap_or_else(|e| e.into_inner());
    short_in(
        dir.as_deref()
            .unwrap_or(crate::config::DEFAULT_WORKFLOWS_DIR),
        f,
    )
}

fn short_in<'a>(dir: &str, f: &'a str) -> &'a str {
    f.strip_prefix(dir)
        .and_then(|r| r.strip_prefix('/'))
        .unwrap_or(f)
}

pub fn tilde(p: &Path) -> String {
    let s = p.to_string_lossy().into_owned();
    match std::env::var("HOME") {
        Ok(h) if !h.is_empty() && s.starts_with(&h) => format!("~{}", &s[h.len()..]),
        _ => s,
    }
}

fn dur(secs: f64) -> String {
    let s = secs.round() as u64;
    if s >= 60 {
        format!("{}m{:02}s", s / 60, s % 60)
    } else {
        format!("{s}s")
    }
}

pub struct Counts {
    pub pass: usize,
    pub fail: usize,
    pub error: usize,
    pub skip: usize,
}

pub fn counts(units: &[UnitResult]) -> Counts {
    let mut c = Counts {
        pass: 0,
        fail: 0,
        error: 0,
        skip: 0,
    };
    for j in units.iter().flat_map(|u| &u.jobs) {
        match j.outcome {
            Outcome::Pass => c.pass += 1,
            Outcome::Fail => c.fail += 1,
            Outcome::Error(_) => c.error += 1,
            Outcome::Skip(_) => c.skip += 1,
        }
    }
    c
}

/// Jobs whose result was reused instead of run.
pub fn cached_jobs(units: &[UnitResult]) -> usize {
    units
        .iter()
        .filter(|u| u.cached.is_some())
        .map(|u| u.jobs.len())
        .sum()
}

/// success / failure / error, the same table the commit status uses.
pub fn state(setup: &[StepResult], units: &[UnitResult]) -> &'static str {
    let c = counts(units);
    if setup.iter().any(|s| s.outcome == Outcome::Fail) || c.fail > 0 {
        "failure"
    } else if setup.iter().any(|s| matches!(s.outcome, Outcome::Error(_))) || c.error > 0 {
        "error"
    } else {
        "success"
    }
}

/// Steps and jobs that were skipped because they cannot be reproduced here.
pub fn not_covered(units: &[UnitResult]) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    for j in units.iter().flat_map(|u| &u.jobs) {
        if let Outcome::Skip(r) = &j.outcome {
            if r.contains("not supported") {
                out.push(format!("{} › {}: {r}", short(&j.workflow), j.display));
            }
        }
        for s in &j.steps {
            if let Outcome::Skip(r) = &s.outcome {
                if r == "uses secrets"
                    || r.starts_with("action ")
                    || r == "installs system packages"
                    || r == "pushes, publishes or deploys"
                {
                    out.push(format!(
                        "{} › {} › {}: {r}",
                        short(&j.workflow),
                        j.display,
                        s.label
                    ));
                }
            }
        }
    }
    out
}

pub fn warnings(units: &[UnitResult]) -> Vec<String> {
    let mut out: Vec<String> = units
        .iter()
        .flat_map(|u| &u.jobs)
        .flat_map(|j| &j.steps)
        .filter_map(|s| {
            s.note
                .as_ref()
                .filter(|n| n.starts_with("wants "))
                .map(|n| format!("{}: {n}", s.label))
        })
        .collect();
    out.sort();
    out.dedup();
    out
}

pub fn description(
    state: &str,
    units: &[UnitResult],
    skipped_only: bool,
    not_covered: usize,
) -> String {
    if skipped_only {
        return "no workflow triggered by these changes".into();
    }
    let c = counts(units);
    let mut wfs: Vec<&str> = units.iter().map(|u| short(&u.workflow)).collect();
    wfs.dedup();
    let mut d = format!(
        "{state}: pass {} / fail {} / error {} / skip {}",
        c.pass, c.fail, c.error, c.skip
    );
    if not_covered > 0 {
        d.push_str(&format!(", {not_covered} not covered"));
    }
    let cached = cached_jobs(units);
    if cached > 0 {
        d.push_str(&format!(", {cached} cached"));
    }
    let list = wfs.join(",");
    if d.len() + list.len() + 3 <= 140 {
        d = format!("{list}: {d}");
    }
    d.chars().take(140).collect()
}

fn failed_step(j: &JobResult) -> Option<&StepResult> {
    j.steps
        .iter()
        .find(|s| s.outcome == Outcome::Fail && s.note.is_none())
        .or_else(|| {
            j.steps
                .iter()
                .find(|s| matches!(s.outcome, Outcome::Error(_)))
        })
}

/// "paths not matched: a.yml, b.yml; only schedule: c.yml"
pub fn group_skipped(skipped: &[Skipped]) -> String {
    let mut groups: Vec<(&str, Vec<&str>)> = Vec::new();
    for sk in skipped {
        match groups.iter_mut().find(|(reason, _)| *reason == sk.reason) {
            Some((_, files)) => files.push(short(&sk.workflow)),
            None => groups.push((&sk.reason, vec![short(&sk.workflow)])),
        }
    }
    groups
        .iter()
        .map(|(reason, files)| format!("{reason}: {}", files.join(", ")))
        .collect::<Vec<_>>()
        .join("; ")
}

pub fn print(r: &RunResult, tail_lines: usize) {
    println!(
        "local-ci {} · {} vs {} · {} file{} changed · {} · logs {}",
        r.version,
        &r.sha[..7],
        r.base,
        r.changed_files,
        if r.changed_files == 1 { "" } else { "s" },
        if r.mode == "all" {
            "all checks"
        } else {
            "triggered by the changes"
        },
        tilde(&r.run_dir)
    );
    let setup_bad: Vec<&StepResult> = r
        .setup
        .iter()
        .filter(|s| s.outcome != Outcome::Pass)
        .collect();
    if !r.setup.is_empty() {
        let secs: f64 = r.setup.iter().map(|s| s.secs).sum();
        if setup_bad.is_empty() {
            println!(
                "setup  ok  {} step{} {}",
                r.setup.len(),
                if r.setup.len() == 1 { "" } else { "s" },
                dur(secs)
            );
        } else {
            println!("setup  FAILED at {}", setup_bad[0].label);
        }
    }
    let mut skipped_jobs = Vec::new();
    for (u, j) in r
        .units
        .iter()
        .flat_map(|u| u.jobs.iter().map(move |j| (u, j)))
    {
        let name = format!("{} › {}", short(&j.workflow), j.display);
        match &j.outcome {
            Outcome::Pass => match &u.cached {
                Some(from) => println!("PASS   {name}  cached ({from})"),
                None => println!("PASS   {name}  {}", dur(j.secs)),
            },
            Outcome::Fail => {
                let s = failed_step(j);
                let what = s
                    .map(|s| format!(" › {} (exit {})", s.label, s.exit.unwrap_or(-1)))
                    .unwrap_or_default();
                println!("FAIL   {name}{what}  {}", dur(j.secs));
            }
            Outcome::Error(e) => println!("ERROR  {name}  {}", e.lines().next().unwrap_or("")),
            Outcome::Skip(reason) => skipped_jobs.push(format!("{} ({reason})", j.display)),
        }
    }
    if !skipped_jobs.is_empty() {
        println!("SKIP   {}", skipped_jobs.join(", "));
    }
    if !r.skipped_workflows.is_empty() {
        println!("not triggered ({})", group_skipped(&r.skipped_workflows));
    }
    if !r.not_covered.is_empty() {
        println!("not covered: {}", r.not_covered.join("; "));
    }
    if !r.warnings.is_empty() {
        println!("warnings: {}", r.warnings.join("; "));
    }
    // details of what went wrong
    let mut details: Vec<(String, &StepResult)> = setup_bad
        .iter()
        .map(|s| ("setup".to_string(), *s))
        .collect();
    for j in r.units.iter().flat_map(|u| &u.jobs) {
        if matches!(j.outcome, Outcome::Fail | Outcome::Error(_)) {
            if let Some(s) = failed_step(j) {
                details.push((format!("{} › {}", short(&j.workflow), j.display), s));
            } else if let Outcome::Error(e) = &j.outcome {
                if e.lines().count() > 1 {
                    println!(
                        "\n── ERROR {} › {}\n{}",
                        short(&j.workflow),
                        j.display,
                        indent(e.lines().skip(1).map(String::from).collect())
                    );
                }
            }
        }
    }
    for (where_, s) in details {
        let what = match &s.outcome {
            Outcome::Error(e) => format!("error: {e}"),
            _ => format!("exit {}", s.exit.unwrap_or(-1)),
        };
        println!("\n── {where_} › {} ({what})", s.label);
        if let Some(log) = &s.log {
            let t = tail(log, tail_lines);
            if t.is_empty() {
                println!("   (no output)");
            } else {
                println!("{}", indent(t));
            }
            println!("   log: {}", tilde(log));
        }
    }
    let c = counts(&r.units);
    let cached = cached_jobs(&r.units);
    println!(
        "\nresult: {} · pass {} / fail {} / error {} / skip {}{} · {}",
        r.state,
        c.pass,
        c.fail,
        c.error,
        c.skip,
        if cached > 0 {
            format!(" ({cached} cached)")
        } else {
            String::new()
        },
        dur(r.secs)
    );
}

fn indent(lines: Vec<String>) -> String {
    lines
        .iter()
        .map(|l| format!("   {l}"))
        .collect::<Vec<_>>()
        .join("\n")
}

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

    fn job(outcome: Outcome) -> JobResult {
        JobResult {
            workflow: ".github/workflows/ci.yml".into(),
            job: "j".into(),
            display: "j".into(),
            outcome,
            secs: 1.0,
            steps: vec![],
        }
    }

    #[test]
    fn states() {
        let u = |o: Vec<Outcome>| {
            vec![UnitResult {
                workflow: "ci.yml".into(),
                jobs: o.into_iter().map(job).collect(),
                waited_secs: 0.0,
                copy_secs: None,
                cached: None,
            }]
        };
        assert_eq!(
            state(
                &[],
                &u(vec![Outcome::Pass, Outcome::Skip("if: false".into())])
            ),
            "success"
        );
        assert_eq!(
            state(
                &[],
                &u(vec![Outcome::Pass, Outcome::Error("docker".into())])
            ),
            "error"
        );
        assert_eq!(
            state(
                &[],
                &u(vec![Outcome::Fail, Outcome::Error("docker".into())])
            ),
            "failure"
        );
        let d = description("success", &u(vec![Outcome::Pass]), false, 2);
        assert_eq!(
            d,
            "ci.yml: success: pass 1 / fail 0 / error 0 / skip 0, 2 not covered"
        );
        assert!(d.len() <= 140);
    }

    #[test]
    fn durations() {
        assert_eq!(dur(5.2), "5s");
        assert_eq!(dur(125.0), "2m05s");
    }

    #[test]
    fn workflow_names_leave_off_the_default_dir() {
        assert_eq!(
            short_in(".github/workflows", ".github/workflows/ci.yml"),
            "ci.yml"
        );
        assert_eq!(short_in(".ci/workflows", ".ci/workflows/ci.yml"), "ci.yml");
        assert_eq!(
            short_in(".ci/workflows", ".github/workflows/ci.yml"),
            ".github/workflows/ci.yml"
        );
        assert_eq!(
            short_in(".ci/workflows", "ci/local/m.yml"),
            "ci/local/m.yml"
        );
        // a sibling that only shares the prefix keeps its path
        assert_eq!(
            short_in(".ci/workflows", ".ci/workflows-old/ci.yml"),
            ".ci/workflows-old/ci.yml"
        );
    }
}