Skip to content

Commit c8d980c

Browse files
committed
Auto merge of #12051 - weihanglo:stale-label, r=epage
chore: new xtask to check stale paths in autolabel defintions
2 parents a285008 + 36653ab commit c8d980c

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-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 --"

.github/workflows/main.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ jobs:
3636
# TODO: check every members
3737
- run: cargo clippy -p cargo --lib --no-deps -- -D warnings
3838

39+
stale-label:
40+
runs-on: ubuntu-latest
41+
steps:
42+
- uses: actions/checkout@v3
43+
- run: rustup update stable && rustup default stable
44+
- run: cargo stale-label
45+
3946
# Ensure Cargo.lock is up-to-date
4047
lockfile:
4148
runs-on: ubuntu-latest

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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
//! ```text
2+
//! NAME
3+
//! stale-label
4+
//!
5+
//! SYNOPSIS
6+
//! stale-label
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::PathBuf;
15+
use std::process;
16+
use toml_edit::Document;
17+
18+
fn main() {
19+
let pkg_root = std::env!("CARGO_MANIFEST_DIR");
20+
let ws_root = PathBuf::from(format!("{pkg_root}/../.."));
21+
let path = {
22+
let path = ws_root.join("triagebot.toml");
23+
path.canonicalize().unwrap_or(path)
24+
};
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| {
55+
e.unwrap()
56+
.path()
57+
.strip_prefix(&ws_root)
58+
.unwrap()
59+
.to_str()
60+
.unwrap()
61+
.starts_with(f)
62+
})
63+
})
64+
.collect();
65+
66+
failed += missing_files.len();
67+
passed += trigger_files.len() - missing_files.len();
68+
69+
if missing_files.is_empty() {
70+
continue;
71+
}
72+
73+
let mut msg = String::new();
74+
writeln!(
75+
&mut msg,
76+
"missing files defined in `autolabel.{label}.trigger_files`:"
77+
)
78+
.unwrap();
79+
for f in missing_files.iter() {
80+
writeln!(&mut msg, "\t {f}").unwrap();
81+
}
82+
eprintln!("{msg}");
83+
}
84+
85+
let result = if failed == 0 { "ok" } else { "FAILED" };
86+
eprintln!("test result: {result}. {passed} passed; {failed} failed;");
87+
88+
if failed > 0 {
89+
process::exit(1);
90+
}
91+
}

0 commit comments

Comments
 (0)