Skip to content

Commit d0b96cb

Browse files
committed
xtask-stale-label: check stale paths in autolabel defintions
1 parent 0ad289b commit d0b96cb

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

.cargo/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
[alias]
22
unpublished = "run --package xtask-unpublished --"
3+
stale-label = "run --package xtask-stale-label --"

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/xtask-stale-label/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "xtask-stale-label"
3+
version = "0.0.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[dependencies]
8+
toml_edit = "0.19"

crates/xtask-stale-label/src/main.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//! ```text
2+
//! NAME
3+
//! stale-label
4+
//!
5+
//! SYNOPSIS
6+
//! stale-label [<FILE>]
7+
//!
8+
//! DESCRIPTION
9+
//! Detect stale paths in autolabel definitions in triagebot.toml.
10+
//! Probably autofix them in the future.
11+
//! ```
12+
13+
use std::fmt::Write as _;
14+
use std::path::Path;
15+
use std::path::PathBuf;
16+
use std::process;
17+
use toml_edit::Document;
18+
19+
fn main() {
20+
let pkg_root = std::env!("CARGO_MANIFEST_DIR");
21+
let ws_root = PathBuf::from(format!("{pkg_root}/../.."));
22+
let triagebot_toml = format!("{pkg_root}/../../triagebot.toml");
23+
let path = std::env::args_os().nth(1).unwrap_or(triagebot_toml.into());
24+
let path = Path::new(&path).canonicalize().unwrap_or(path.into());
25+
26+
eprintln!("Checking file {path:?}\n");
27+
28+
let mut failed = 0;
29+
let mut passed = 0;
30+
31+
let toml = std::fs::read_to_string(path).expect("read from file");
32+
let doc = toml.parse::<Document>().expect("a toml");
33+
let autolabel = doc["autolabel"].as_table().expect("a toml table");
34+
35+
for (label, value) in autolabel.iter() {
36+
let Some(trigger_files) = value.get("trigger_files") else {
37+
continue
38+
};
39+
let trigger_files = trigger_files.as_array().expect("an array");
40+
let missing_files: Vec<_> = trigger_files
41+
.iter()
42+
// Hey TOML content is strict UTF-8.
43+
.map(|v| v.as_str().unwrap())
44+
.filter(|f| {
45+
// triagebot checks with `starts_with` only.
46+
// See https://github.com/rust-lang/triagebot/blob/0e4b48ca86ffede9cc70fb1611e658e4d013bce2/src/handlers/autolabel.rs#L45
47+
let path = ws_root.join(f);
48+
if path.exists() {
49+
return false;
50+
}
51+
let Some(mut read_dir) = path.parent().and_then(|p| p.read_dir().ok()) else {
52+
return true;
53+
};
54+
!read_dir.any(|e| e.unwrap().path().to_str().unwrap().starts_with(f))
55+
})
56+
.collect();
57+
58+
failed += missing_files.len();
59+
passed += trigger_files.len() - missing_files.len();
60+
61+
if missing_files.is_empty() {
62+
continue;
63+
}
64+
65+
let mut msg = String::new();
66+
writeln!(
67+
&mut msg,
68+
"missing files defined in `autolabel.{label}.trigger_files`:"
69+
)
70+
.unwrap();
71+
for f in missing_files.iter() {
72+
writeln!(&mut msg, "\t {f}").unwrap();
73+
}
74+
eprintln!("{msg}");
75+
}
76+
77+
let result = if failed == 0 { "ok" } else { "FAILED" };
78+
eprintln!("test result: {result}. {passed} passed; {failed} failed;");
79+
80+
process::exit(failed as i32);
81+
}

0 commit comments

Comments
 (0)