Skip to content

Commit 0ccf3ee

Browse files
Add tests for file extraction from diffs
1 parent fbe982c commit 0ccf3ee

File tree

2 files changed

+99
-30
lines changed

2 files changed

+99
-30
lines changed

src/github.rs

+85
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,21 @@ pub struct CommitBase {
779779
sha: String,
780780
}
781781

782+
pub fn files_changed(diff: &str) -> Vec<&str> {
783+
let mut files = Vec::new();
784+
for line in diff.lines() {
785+
// mostly copied from highfive
786+
if line.starts_with("diff --git ") {
787+
files.push(
788+
line[line.find(" b/").unwrap()..]
789+
.strip_prefix(" b/")
790+
.unwrap(),
791+
);
792+
}
793+
}
794+
files
795+
}
796+
782797
impl IssuesEvent {
783798
/// Returns the diff in this event, for Open and Synchronize events for now.
784799
pub async fn diff_between(&self, client: &GithubClient) -> anyhow::Result<Option<String>> {
@@ -1331,3 +1346,73 @@ pub trait IssuesQuery {
13311346
client: &'a GithubClient,
13321347
) -> anyhow::Result<Vec<crate::actions::IssueDecorator>>;
13331348
}
1349+
1350+
#[cfg(test)]
1351+
mod tests {
1352+
use super::*;
1353+
1354+
#[test]
1355+
fn extract_one_file() {
1356+
let input = r##"\
1357+
diff --git a/triagebot.toml b/triagebot.toml
1358+
index fb9cee43b2d..b484c25ea51 100644
1359+
--- a/triagebot.toml
1360+
+++ b/triagebot.toml
1361+
@@ -114,6 +114,15 @@ trigger_files = [
1362+
"src/tools/rustdoc-themes",
1363+
]
1364+
+[autolabel."T-compiler"]
1365+
+trigger_files = [
1366+
+ # Source code
1367+
+ "compiler",
1368+
+
1369+
+ # Tests
1370+
+ "src/test/ui",
1371+
+]
1372+
+
1373+
[notify-zulip."I-prioritize"]
1374+
zulip_stream = 245100 # #t-compiler/wg-prioritization/alerts
1375+
topic = "#{number} {title}"
1376+
"##;
1377+
assert_eq!(files_changed(input), vec!["triagebot.toml".to_string()]);
1378+
}
1379+
1380+
#[test]
1381+
fn extract_several_files() {
1382+
let input = r##"\
1383+
diff --git a/library/stdarch b/library/stdarch
1384+
index b70ae88ef2a..cfba59fccd9 160000
1385+
--- a/library/stdarch
1386+
+++ b/library/stdarch
1387+
@@ -1 +1 @@
1388+
-Subproject commit b70ae88ef2a6c83acad0a1e83d5bd78f9655fd05
1389+
+Subproject commit cfba59fccd90b3b52a614120834320f764ab08d1
1390+
diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs
1391+
index 1fe4aa9023e..f0330f1e424 100644
1392+
--- a/src/librustdoc/clean/types.rs
1393+
+++ b/src/librustdoc/clean/types.rs
1394+
@@ -2322,3 +2322,4 @@ impl SubstParam {
1395+
if let Self::Lifetime(lt) = self { Some(lt) } else { None }
1396+
}
1397+
}
1398+
+
1399+
diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs
1400+
index c58310947d2..3b0854d4a9b 100644
1401+
--- a/src/librustdoc/core.rs
1402+
+++ b/src/librustdoc/core.rs
1403+
@@ -591,3 +591,4 @@ fn from(idx: u32) -> Self {
1404+
ImplTraitParam::ParamIndex(idx)
1405+
}
1406+
}
1407+
+
1408+
"##;
1409+
assert_eq!(
1410+
files_changed(input),
1411+
vec![
1412+
"library/stdarch".to_string(),
1413+
"src/librustdoc/clean/types.rs".to_string(),
1414+
"src/librustdoc/core.rs".to_string(),
1415+
]
1416+
)
1417+
}
1418+
}

src/handlers/autolabel.rs

+14-30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
config::AutolabelConfig,
3-
github::{IssuesAction, IssuesEvent, Label},
3+
github::{files_changed, IssuesAction, IssuesEvent, Label},
44
handlers::Context,
55
};
66
use tracing as log;
@@ -23,38 +23,22 @@ pub(super) async fn parse_input(
2323
})
2424
.unwrap_or_default()
2525
{
26-
let mut files = Vec::new();
27-
for line in diff.lines() {
28-
// mostly copied from highfive
29-
if line.starts_with("diff --git ") {
30-
files.push(
31-
line[line.find(" b/").unwrap()..]
32-
.strip_prefix(" b/")
33-
.unwrap(),
34-
);
35-
}
36-
}
26+
let files = files_changed(&diff);
3727
let mut autolabels = Vec::new();
38-
for trigger_file in files {
39-
if trigger_file.is_empty() {
40-
// TODO: when would this be true?
41-
continue;
42-
}
43-
for (label, cfg) in config.labels.iter() {
44-
if cfg
45-
.trigger_files
46-
.iter()
47-
.any(|f| trigger_file.starts_with(f))
48-
{
49-
autolabels.push(Label {
50-
name: label.to_owned(),
51-
});
52-
}
53-
}
54-
if !autolabels.is_empty() {
55-
return Ok(Some(AutolabelInput { labels: autolabels }));
28+
for (label, cfg) in config.labels.iter() {
29+
if cfg
30+
.trigger_files
31+
.iter()
32+
.any(|f| files.iter().any(|diff_file| diff_file.starts_with(f)))
33+
{
34+
autolabels.push(Label {
35+
name: label.to_owned(),
36+
});
5637
}
5738
}
39+
if !autolabels.is_empty() {
40+
return Ok(Some(AutolabelInput { labels: autolabels }));
41+
}
5842
}
5943
}
6044

0 commit comments

Comments
 (0)