tidy/
triagebot.rs

1//! Tidy check to ensure paths mentioned in triagebot.toml exist in the project.
2
3use std::path::Path;
4
5use toml::Value;
6
7use crate::diagnostics::DiagCtx;
8
9pub fn check(path: &Path, diag_ctx: DiagCtx) {
10    let mut check = diag_ctx.start_check("triagebot");
11    let triagebot_path = path.join("triagebot.toml");
12
13    // This check is mostly to catch broken path filters *within* `triagebot.toml`, and not enforce
14    // the existence of `triagebot.toml` itself (which is more obvious), as distribution tarballs
15    // will not include non-essential bits like `triagebot.toml`.
16    if !triagebot_path.exists() {
17        return;
18    }
19
20    let contents = std::fs::read_to_string(&triagebot_path).unwrap();
21    let config: Value = toml::from_str(&contents).unwrap();
22
23    // Check [mentions."*"] sections, i.e. [mentions."compiler/rustc_const_eval/src/"]
24    if let Some(Value::Table(mentions)) = config.get("mentions") {
25        for (entry_key, entry_val) in mentions.iter() {
26            // If the type is set to something other than "filename", then this is not a path.
27            if entry_val.get("type").is_some_and(|t| t.as_str().unwrap_or_default() != "filename") {
28                continue;
29            }
30            let path_str = entry_key;
31            // Remove quotes from the path
32            let clean_path = path_str.trim_matches('"');
33            let full_path = path.join(clean_path);
34
35            if !full_path.exists() {
36                check.error(format!(
37                    "triagebot.toml [mentions.*] contains path '{clean_path}' which doesn't exist"
38                ));
39            }
40        }
41    } else {
42        check.error(
43            "triagebot.toml missing [mentions.*] section, this wrong for rust-lang/rust repo.",
44        );
45    }
46
47    // Check [assign.owners] sections, i.e.
48    // [assign.owners]
49    // "/.github/workflows" = ["infra-ci"]
50    if let Some(Value::Table(assign)) = config.get("assign") {
51        if let Some(Value::Table(owners)) = assign.get("owners") {
52            for path_str in owners.keys() {
53                // Remove quotes and leading slash from the path
54                let clean_path = path_str.trim_matches('"').trim_start_matches('/');
55                let full_path = path.join(clean_path);
56
57                if !full_path.exists() {
58                    check.error(format!(
59                        "triagebot.toml [assign.owners] contains path '{clean_path}' which doesn't exist"
60                    ));
61                }
62            }
63        } else {
64            check.error(
65                "triagebot.toml missing [assign.owners] section, this wrong for rust-lang/rust repo."
66            );
67        }
68    }
69
70    // Verify that trigger_files in [autolabel."*"] exist in the project, i.e.
71    // [autolabel."A-rustdoc-search"]
72    // trigger_files = [
73    //    "src/librustdoc/html/static/js/search.js",
74    //    "tests/rustdoc-js",
75    //    "tests/rustdoc-js-std",
76    // ]
77    if let Some(Value::Table(autolabels)) = config.get("autolabel") {
78        for (label, content) in autolabels {
79            if let Some(trigger_files) = content.get("trigger_files").and_then(|v| v.as_array()) {
80                for file in trigger_files {
81                    if let Some(file_str) = file.as_str() {
82                        let full_path = path.join(file_str);
83
84                        // Handle both file and directory paths
85                        if !full_path.exists() {
86                            check.error(format!(
87                                "triagebot.toml [autolabel.{label}] contains trigger_files path '{file_str}' which doesn't exist",
88                            ));
89                        }
90                    }
91                }
92            }
93        }
94    }
95}