reqfile 0.3.0

Check that a codebase meets its requirements
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
//! The Reqfile.yaml format, parsed into precise types.

use globset::{GlobBuilder, GlobSet, GlobSetBuilder};

use super::error::ConfigError;
use super::paths;
use super::yaml::{self, Node, Value};

pub const FILE_NAME: &str = "Reqfile.yaml";
const VERSION: i64 = 1;
const DEFAULT_TIMEOUT_SECS: u64 = 60;

/// A file whose name looks like a Reqfile but is not exactly `Reqfile.yaml`.
pub fn is_near_miss(path: &str) -> bool {
    let name = paths::file_name(path);
    name != FILE_NAME && matches!(name.to_lowercase().as_str(), "reqfile.yaml" | "reqfile.yml")
}

/// YAML with a top-level Reqfile key, so a near-miss name holding it is a
/// misnamed Reqfile rather than another tool's file, such as a CI workflow
/// named `reqfile.yml`.
pub fn looks_like_reqfile(text: &str) -> bool {
    match yaml::parse("", text).map(|node| node.value) {
        Ok(Value::Map(entries)) => entries.iter().any(|(key, _)| {
            key.name == "reqfile" || Kind::ALL.iter().any(|k| k.as_str() == key.name)
        }),
        _ => false,
    }
}

#[derive(Debug)]
pub struct Reqfile {
    pub path: String,
    pub dir: String,
    /// Ids are unique within a Reqfile.
    pub blocks: Vec<Block>,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Kind {
    Product,
    Code,
    Process,
}

impl Kind {
    pub const ALL: [Kind; 3] = [Kind::Product, Kind::Code, Kind::Process];

    pub fn as_str(self) -> &'static str {
        match self {
            Kind::Product => "product",
            Kind::Code => "code",
            Kind::Process => "process",
        }
    }

    /// A product requirement describes its own repository's product, so
    /// only code and process requirements can be taken by reference.
    pub fn importable(self) -> bool {
        self != Kind::Product
    }
}

/// One entry of a Reqfile: a definition, or a use block taking its
/// requirement by reference from another folder or repository.
#[derive(Debug, Clone)]
pub struct Block {
    pub id: String,
    pub kind: Kind,
    pub line: usize,
    /// Required in a definition; in a use block, replaces the definition's.
    pub why: Option<String>,
    pub who: Option<String>,
    /// Required in a definition; in a use block, replaces the definition's
    /// checks entirely.
    pub checks: Option<Checks>,
    pub origin: Origin,
}

#[derive(Debug, Clone)]
pub enum Origin {
    Definition {
        must: String,
        reference: Option<String>,
    },
    Use(Location),
}

/// The checks of a block, with their YAML as written, which identifies them.
#[derive(Debug, Clone)]
pub struct Checks {
    pub list: Vec<Check>,
    pub source: String,
}

/// Where a use block takes its requirement from.
#[derive(Debug, Clone, PartialEq)]
pub enum Location {
    /// A folder, relative to the folder of the Reqfile, as written.
    Local(String),
    /// A repository `owner/repo`, pinned to a ref.
    Git { repo: String, reference: GitRef },
}

impl Location {
    pub fn describe(&self) -> String {
        match self {
            Location::Local(path) => path.clone(),
            Location::Git { repo, reference } => format!("{repo}@{}", reference.as_str()),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum GitRef {
    /// A full 40-character commit id.
    Commit(String),
    /// A tag, resolved through `refs/tags/<name>`.
    Tag(String),
}

impl GitRef {
    pub fn as_str(&self) -> &str {
        match self {
            GitRef::Commit(id) | GitRef::Tag(id) => id,
        }
    }
}

/// Parses `use`: `./folder`, `../folder` or `owner/repo@ref`.
pub fn parse_location(text: &str) -> Result<Location, String> {
    if text == "." || text == ".." || text.starts_with("./") || text.starts_with("../") {
        return Ok(Location::Local(text.to_string()));
    }
    let invalid = || {
        format!(
            "`use: {text}` must be a folder relative to this Reqfile (./std) or a repository pinned to a tag or commit (owner/repo@v1.0.0)"
        )
    };
    let (repo, reference) = text.split_once('@').ok_or_else(invalid)?;
    let name = |part: &str| {
        !part.is_empty()
            && part
                .chars()
                .all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.'))
    };
    match repo.split_once('/') {
        Some((owner, name_part)) if name(owner) && name(name_part) => {}
        _ => return Err(invalid()),
    }
    Ok(Location::Git {
        repo: repo.to_string(),
        reference: parse_ref(reference)?,
    })
}

/// A full commit id or a tag name; `HEAD` and short commit ids are rejected
/// here, and a name that is not a tag (a branch) when it is resolved.
fn parse_ref(text: &str) -> Result<GitRef, String> {
    let hex = !text.is_empty() && text.chars().all(|c| c.is_ascii_hexdigit());
    if text.len() == 40 && hex {
        Ok(GitRef::Commit(text.to_ascii_lowercase()))
    } else if text.is_empty() || text == "HEAD" || text.starts_with("refs/") {
        Err(format!(
            "`{text}` is not a tag or a full commit id; pin requirements to a tag (v1.0.0) or a 40-character commit id"
        ))
    } else if hex && text.len() >= 7 && text.chars().any(|c| c.is_ascii_digit()) {
        Err(format!(
            "`{text}` looks like a short commit id; use the full 40-character id so the pin cannot become ambiguous"
        ))
    } else {
        Ok(GitRef::Tag(text.to_string()))
    }
}

#[derive(Debug, Clone)]
pub enum Check {
    Command(CommandCheck),
    Decision(DecisionCheck),
}

#[derive(Debug, Clone)]
pub struct CommandCheck {
    pub run: String,
    /// Declared quick enough for `reqfile check --fast`, such as in an edit hook.
    pub fast: bool,
    pub fix_hint: String,
    pub files: Option<FileGlob>,
    pub pass_files: bool,
    pub format: OutputFormat,
    pub violation_codes: Vec<i32>,
    pub timeout_secs: u64,
    /// How results carrying a probability of violation are judged.
    pub thresholds: Thresholds,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OutputFormat {
    Exit,
    Sarif,
    Junit,
}

/// A probability of violation above `violation_above` is a violation, below
/// `pass_below` a pass, and anything between an uncertain finding.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Thresholds {
    pub violation_above: f64,
    pub pass_below: f64,
}

impl Default for Thresholds {
    fn default() -> Self {
        Self {
            violation_above: 0.8,
            pass_below: 0.2,
        }
    }
}

#[derive(Debug, Clone)]
pub struct DecisionCheck {
    pub blocking: bool,
    pub line: usize,
}

/// Globs relative to the folder of their Reqfile; a path matches if any glob does.
#[derive(Debug, Clone)]
pub struct FileGlob {
    set: GlobSet,
}

impl FileGlob {
    pub fn new(pattern: &str) -> Result<Self, String> {
        Self::any(&[pattern])
    }

    pub fn any(patterns: &[&str]) -> Result<Self, String> {
        let mut set = GlobSetBuilder::new();
        for pattern in patterns {
            set.add(
                GlobBuilder::new(pattern)
                    .literal_separator(true)
                    .build()
                    .map_err(|e| e.to_string())?,
            );
        }
        Ok(Self {
            set: set.build().map_err(|e| e.to_string())?,
        })
    }

    pub fn is_match(&self, relative_path: &str) -> bool {
        self.set.is_match(relative_path)
    }
}

pub fn parse(path: &str, text: &str) -> Result<Reqfile, ConfigError> {
    let root = yaml::parse(path, text)?;
    const NO_VERSION: &str = "a Reqfile needs a `reqfile: 1` version key";
    if matches!(root.value, Value::Null) {
        return Err(ConfigError::at(path, 1, NO_VERSION));
    }
    let mut fields = root.fields(
        path,
        "the Reqfile",
        &["reqfile", "product", "code", "process"],
    )?;
    let file_line = fields.line;
    let version = fields
        .optional("reqfile")
        .ok_or_else(|| ConfigError::at(path, file_line, NO_VERSION))?;
    let line = version.line;
    let version = version.integer(path, "reqfile")?;
    if version != VERSION {
        return Err(ConfigError::at(
            path,
            line,
            format!(
                "unsupported Reqfile version {version}; this reqfile supports version {VERSION}"
            ),
        ));
    }
    let mut blocks: Vec<Block> = Vec::new();
    for kind in Kind::ALL {
        if let Some(list) = fields.optional(kind.as_str()) {
            for node in list.list(path, kind.as_str())? {
                let block = block(path, kind, node)?;
                if let Some(first) = blocks.iter().find(|b| b.id == block.id) {
                    return Err(ConfigError::at(
                        path,
                        block.line,
                        format!(
                            "requirement id {} appears twice in this Reqfile (first at line {})",
                            block.id, first.line
                        ),
                    ));
                }
                blocks.push(block);
            }
        }
    }
    Ok(Reqfile {
        path: path.to_string(),
        dir: paths::parent(path).to_string(),
        blocks,
    })
}

/// A block with `use` is a use block; any other block is a definition.
fn block(path: &str, kind: Kind, node: Node) -> Result<Block, ConfigError> {
    let line = node.line;
    let mut fields = node.fields(
        path,
        "a requirement",
        &["id", "use", "ref", "must", "why", "who", "checks"],
    )?;
    let id = fields.required("id")?;
    let id_line = id.line;
    let id = id.text(path, "id")?;
    if !is_screaming_snake_case(&id) {
        return Err(ConfigError::at(
            path,
            id_line,
            format!("requirement id `{id}` must be SCREAMING_SNAKE_CASE"),
        ));
    }
    fields.what = format!("requirement {id}");
    let location = match fields.optional("use") {
        None => None,
        Some(node) => {
            let use_line = node.line;
            if !kind.importable() {
                return Err(ConfigError::at(
                    path,
                    use_line,
                    format!(
                        "requirement {id} is a product requirement, which describes this repository's own product; only code and process requirements can be taken with `use`"
                    ),
                ));
            }
            let text = node.text(path, "use")?;
            let location = parse_location(&text).map_err(|e| ConfigError::at(path, use_line, e))?;
            if let Location::Git {
                repo,
                reference: GitRef::Tag(tag),
            } = &location
            {
                return Err(ConfigError::at(
                    path,
                    use_line,
                    format!(
                        "`use: {text}` is pinned to a tag, which can move; a Reqfile pins a repository to a full commit id. `reqfile add {repo}@{tag}` writes the line pinned to the commit {tag} points to"
                    ),
                ));
            }
            Some(location)
        }
    };
    let origin = match location {
        Some(location) => {
            for key in ["must", "ref"] {
                if let Some(node) = fields.optional(key) {
                    return Err(ConfigError::at(
                        path,
                        node.line,
                        format!(
                            "requirement {id} is a use block, which takes `{key}` from its definition; remove `{key}`, or remove `use` to define the requirement here"
                        ),
                    ));
                }
            }
            Origin::Use(location)
        }
        None => {
            let must = fields.required("must")?.text(path, "must")?;
            let reference = fields
                .optional("ref")
                .map(|r| r.text(path, "ref"))
                .transpose()?;
            Origin::Definition { must, reference }
        }
    };
    let definition = matches!(origin, Origin::Definition { .. });
    let why = match fields.optional("why") {
        Some(node) => Some(node.text(path, "why")?),
        None if definition => return Err(fields.missing("why")),
        None => None,
    };
    let who = fields
        .optional("who")
        .map(|n| n.text(path, "who"))
        .transpose()?;
    let checks = match fields.optional("checks") {
        Some(node) => Some(checks(path, &id, node)?),
        None if definition => return Err(fields.missing("checks")),
        None => None,
    };
    Ok(Block {
        id,
        kind,
        line,
        why,
        who,
        checks,
        origin,
    })
}

fn checks(path: &str, id: &str, checks_node: Node) -> Result<Checks, ConfigError> {
    let checks_line = checks_node.line;
    let source = checks_node.clone().into_json().to_string();
    let mut checks = Vec::new();
    for check_node in checks_node.list(path, "checks")? {
        let check = check(path, check_node)?;
        if let (Check::Decision(new), true) = (
            &check,
            checks.iter().any(|c| matches!(c, Check::Decision(_))),
        ) {
            return Err(ConfigError::at(
                path,
                new.line,
                format!("requirement {id} declares more than one decision check"),
            ));
        }
        checks.push(check);
    }
    if checks.is_empty() {
        return Err(ConfigError::at(
            path,
            checks_line,
            format!(
                "requirement {id} declares no checks; add at least one `command` or `decision` check"
            ),
        ));
    }
    Ok(Checks {
        list: checks,
        source,
    })
}

fn is_screaming_snake_case(id: &str) -> bool {
    id.starts_with(|c: char| c.is_ascii_uppercase())
        && !id.ends_with('_')
        && !id.contains("__")
        && id
            .chars()
            .all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == '_')
}

const NOT_A_CHECK: &str = "a check is either `decision` or a `command` mapping";

fn check(path: &str, node: Node) -> Result<Check, ConfigError> {
    let line = node.line;
    match node.value {
        Value::Str(name) if name == "decision" => Ok(Check::Decision(DecisionCheck {
            blocking: false,
            line,
        })),
        Value::Map(mut entries) if entries.len() == 1 => {
            let (key, body) = entries.remove(0);
            match key.name.as_str() {
                "command" => command(path, body).map(Check::Command),
                "decision" => decision(path, line, body).map(Check::Decision),
                other => Err(ConfigError::at(
                    path,
                    key.line,
                    format!("unknown check `{other}`; {NOT_A_CHECK}"),
                )),
            }
        }
        Value::Str(other) => Err(ConfigError::at(
            path,
            line,
            format!("unknown check `{other}`; {NOT_A_CHECK}"),
        )),
        _ => Err(ConfigError::at(path, line, NOT_A_CHECK)),
    }
}

fn decision(path: &str, line: usize, body: Node) -> Result<DecisionCheck, ConfigError> {
    if matches!(body.value, Value::Null) {
        return Ok(DecisionCheck {
            blocking: false,
            line,
        });
    }
    let mut fields = body.fields(path, "a decision check", &["mode"])?;
    let blocking = match fields.optional("mode") {
        None => false,
        Some(mode) => {
            let mode_line = mode.line;
            match mode.text(path, "mode")?.as_str() {
                "advisory" => false,
                "blocking" => true,
                other => {
                    return Err(ConfigError::at(
                        path,
                        mode_line,
                        format!(
                            "unknown decision mode `{other}`; expected `advisory` or `blocking`"
                        ),
                    ));
                }
            }
        }
    };
    Ok(DecisionCheck { blocking, line })
}

fn command(path: &str, body: Node) -> Result<CommandCheck, ConfigError> {
    let mut fields = body.fields(
        path,
        "a command check",
        &[
            "run",
            "fix_hint",
            "files",
            "pass_files",
            "format",
            "violation_codes",
            "timeout",
            "fast",
            "thresholds",
        ],
    )?;
    let run = fields.required("run")?.text(path, "run")?;
    let fix_hint = fields.required("fix_hint")?.text(path, "fix_hint")?;
    let files =
        match fields.optional("files") {
            None => None,
            Some(node) => {
                let line = node.line;
                let pattern = node.text(path, "files")?;
                Some(FileGlob::new(&pattern).map_err(|e| {
                    ConfigError::at(path, line, format!("invalid `files` glob: {e}"))
                })?)
            }
        };
    let pass_files = match fields.optional("pass_files") {
        None => false,
        Some(node) => {
            let line = node.line;
            let pass = node.boolean(path, "pass_files")?;
            if pass && files.is_none() {
                return Err(ConfigError::at(
                    path,
                    line,
                    "`pass_files` needs a `files` glob",
                ));
            }
            pass
        }
    };
    let fast = match fields.optional("fast") {
        None => false,
        Some(node) => node.boolean(path, "fast")?,
    };
    let format = match fields.optional("format") {
        None => OutputFormat::Exit,
        Some(node) => {
            let line = node.line;
            match node.text(path, "format")?.as_str() {
                "exit" => OutputFormat::Exit,
                "sarif" => OutputFormat::Sarif,
                "junit" => OutputFormat::Junit,
                other => {
                    return Err(ConfigError::at(
                        path,
                        line,
                        format!("unknown format `{other}`; expected `exit`, `sarif` or `junit`"),
                    ));
                }
            }
        }
    };
    let violation_codes = match fields.optional("violation_codes") {
        None => vec![1],
        Some(node) => {
            let line = node.line;
            let codes = node
                .list(path, "violation_codes")?
                .into_iter()
                .map(|code| code.integer(path, "violation_codes"))
                .collect::<Result<Vec<_>, _>>()?;
            if codes.is_empty() || codes.iter().any(|c| !(1..=255).contains(c)) {
                return Err(ConfigError::at(
                    path,
                    line,
                    "`violation_codes` must list exit codes between 1 and 255",
                ));
            }
            codes.into_iter().map(|c| c as i32).collect()
        }
    };
    let timeout_secs = match fields.optional("timeout") {
        None => DEFAULT_TIMEOUT_SECS,
        Some(node) => {
            let line = node.line;
            match node.integer(path, "timeout")? {
                secs if secs > 0 => secs as u64,
                _ => {
                    return Err(ConfigError::at(
                        path,
                        line,
                        "`timeout` must be a positive number of seconds",
                    ));
                }
            }
        }
    };
    let thresholds = match fields.optional("thresholds") {
        None => Thresholds::default(),
        Some(node) => {
            let line = node.line;
            let mut t = node.fields(path, "thresholds", &["violation_above", "pass_below"])?;
            let defaults = Thresholds::default();
            let violation_above = match t.optional("violation_above") {
                None => defaults.violation_above,
                Some(n) => n.number(path, "violation_above")?,
            };
            let pass_below = match t.optional("pass_below") {
                None => defaults.pass_below,
                Some(n) => n.number(path, "pass_below")?,
            };
            if !(0.0..=1.0).contains(&pass_below)
                || !(0.0..=1.0).contains(&violation_above)
                || pass_below > violation_above
            {
                return Err(ConfigError::at(
                    path,
                    line,
                    "thresholds must satisfy 0 <= pass_below <= violation_above <= 1",
                ));
            }
            Thresholds {
                violation_above,
                pass_below,
            }
        }
    };
    Ok(CommandCheck {
        run,
        fast,
        fix_hint,
        files,
        pass_files,
        format,
        violation_codes,
        timeout_secs,
        thresholds,
    })
}