From 89c24b63122c8b91c8730f2fd24d7ca38c619fea Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 10 Oct 2026 20:43:01 +0300 Subject: [PATCH] feat: filter route tags with inclusion regexes --- README.md | 9 +++-- config.example.toml | 6 ++- k8s/configmap.yaml | 4 +- src/config.rs | 4 +- src/routing.rs | 9 +++-- src/tests.rs | 91 +++++++++++++++++++++++++++++++++++++-------- 6 files changed, 96 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 13ca1d8..47c3cb2 100644 --- a/README.md +++ b/README.md @@ -18,15 +18,18 @@ TOML defines named `sources`, named `destinations`, and `[[routes]]`. Unknown fi A source has an explicit repository allowlist. Unauthorized repository events return 403. A ping is source-authenticated but need not contain a repository, which permits organization pings. Other events need an allowed `repository.full_name`. Non-release events and release actions other than `published` return 204 without creating jobs. -Routes compare `repository.full_name` exactly and case-sensitively. A configured `tag_prefix` uses a case-sensitive prefix match. **Omit `tag_prefix` to match all repository tags, except excluded tags.** Prereleases match only routes with `include_prereleases = true`. All matching routes contribute destinations; each destination receives one message even when routes overlap. An unmatched published release returns 202 with no jobs. +Routes compare `repository.full_name` exactly and case-sensitively. Prereleases match only routes with `include_prereleases = true`. All matching routes contribute destinations; each destination receives one message even when routes overlap. An unmatched published release returns 202 with no jobs. -To exclude tags, add an `exclude_tags` list to a route: +Use `include_tags` to select tags and `exclude_tags` to reject tags for a route: ```toml +include_tags = ["^actix-web-lab-.*$", "^lab-v.*$"] exclude_tags = ["^actix-web-lab-derive-.*$"] ``` -Patterns are case-insensitive regular expressions. A match anywhere in the tag excludes it; use `^` and `$` to match the complete tag. For example, the pattern above excludes both `actix-web-lab-derive-v0.1.0` and `ACTIX-WEB-LAB-DERIVE-v0.1.0`. See the [regex syntax](https://docs.rs/regex/latest/regex/#syntax) for more options. If any pattern matches, that route contributes no destinations, even if `tag_prefix` matches. Other routes can still announce the release. Omit `exclude_tags` or use `[]` to exclude no tags. Invalid regular expressions fail startup. Restart the service after changing the configuration. +Both lists contain case-insensitive regular expressions. A pattern can match anywhere in the tag; use `^` and `$` to match the complete tag. See the [regex syntax](https://docs.rs/regex/latest/regex/#syntax) for more options. A tag must match at least one `include_tags` pattern. **Omit `include_tags` or use `[]` to allow all repository tags before exclusions.** If any `exclude_tags` pattern matches, that route contributes no destinations, even if an inclusion pattern matches. Other routes can still announce the release. Omit `exclude_tags` or use `[]` to exclude no tags. The example above allows `actix-web-lab-v0.27.0` and `LAB-v0.27.0`, but excludes both `actix-web-lab-derive-v0.1.0` and `ACTIX-WEB-LAB-DERIVE-v0.1.0`. Invalid regular expressions fail startup. Restart the service after changing the configuration. + +Replace an existing `tag_prefix = "web-"` with `include_tags = ["^web-"]`. The `tag_prefix` field is no longer accepted. To add the third, fourth, or later repository, add its real `owner/name` to a source allowlist and add a route. Use a new source and secret if the hook needs a different trust boundary. Add named destinations to send to more channels, then list those destination names in routes. The example intentionally includes only the two known repositories. diff --git a/config.example.toml b/config.example.toml index ce284b0..3defad9 100644 --- a/config.example.toml +++ b/config.example.toml @@ -19,14 +19,16 @@ webhook = { file = "/run/secrets/discord_webhook_url" } [[routes]] name = "actix-web" repository = "actix/actix-web" -tag_prefix = "web-" # Optional. Omit this line to match all tags before exclusions. +# Optional. Match any of these case-insensitive regexes. +include_tags = ["^web-"] # Omit or use [] to match all tags before exclusions. include_prereleases = false destinations = ["web"] [[routes]] name = "actix-web-lab" repository = "robjtede/actix-web-lab" -tag_prefix = "lab-" # Optional. Omit this line to match all tags before exclusions. +# Optional. Match any of these case-insensitive regexes. +include_tags = ["^lab-"] # Omit or use [] to match all tags before exclusions. # Optional. Exclude tags that match any of these case-insensitive regexes. # exclude_tags = ["^actix-web-lab-derive-.*$"] include_prereleases = false diff --git a/k8s/configmap.yaml b/k8s/configmap.yaml index 21f707b..bf9dc7b 100644 --- a/k8s/configmap.yaml +++ b/k8s/configmap.yaml @@ -21,14 +21,14 @@ data: [[routes]] name = "actix-web" repository = "actix/actix-web" - tag_prefix = "web-" + include_tags = ["^web-"] include_prereleases = false destinations = ["web"] [[routes]] name = "actix-web-lab" repository = "robjtede/actix-web-lab" - tag_prefix = "lab-" + include_tags = ["^lab-"] # exclude_tags = ["^actix-web-lab-derive-.*$"] include_prereleases = false destinations = ["web"] diff --git a/src/config.rs b/src/config.rs index ba9ccac..2830426 100644 --- a/src/config.rs +++ b/src/config.rs @@ -64,7 +64,9 @@ pub struct Destination { pub struct Route { pub name: String, pub repository: String, - pub tag_prefix: Option, + #[serde(default, deserialize_with = "deserialize_tag_patterns")] + pub include_tags: Vec, + #[serde(default, deserialize_with = "deserialize_tag_patterns")] pub exclude_tags: Vec, #[serde(default)] diff --git a/src/routing.rs b/src/routing.rs index 37be49b..455202b 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -11,10 +11,11 @@ pub fn destinations( for route in &config.routes { if route.repository == repository && (!release.prerelease || route.include_prereleases) - && route - .tag_prefix - .as_ref() - .is_none_or(|prefix| release.tag_name.starts_with(prefix)) + && (route.include_tags.is_empty() + || route + .include_tags + .iter() + .any(|pattern| pattern.is_match(&release.tag_name))) && !route .exclude_tags .iter() diff --git a/src/tests.rs b/src/tests.rs index c77a526..3b6e857 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -47,7 +47,7 @@ fn config() -> Config { routes: vec![Route { name: "web".into(), repository: "actix/actix-web".into(), - tag_prefix: Some("web-".into()), + include_tags: vec![regex::Regex::new("(?i)^web-").unwrap()], exclude_tags: Vec::new(), include_prereleases: false, destinations: vec!["web".into()], @@ -186,17 +186,23 @@ async fn incoming_filters_authorizes_limits_and_deduplicates() { } #[actix_web::test] -async fn incoming_excludes_case_insensitive_regexes_before_queueing_announcements() { +async fn incoming_applies_case_insensitive_tag_regexes_before_queueing_announcements() { for (tag, expected_job) in [ ("actix-web-lab-derive-v0.1.0", false), ("actix-web-lab-derive-", false), ("actix-web-lab-v0.27.0-internal", false), ("actix-web-lab-v0.27.0-TEST", false), ("actix-web-lab-v0.27.0", true), + ("ACTIX-WEB-LAB-v0.27.0", true), ("actix-web-lab-Derive-v0.1.0", false), ("actix-web-lab-test-v0.1.0", true), ("actix-web-lab-v0.27.0-internal-more", true), ("other-actix-web-lab-v0.27.0", false), + ("lab-v0.27.0", true), + ("LAB-v0.27.0", true), + ("lab-v0.27.0-internal", false), + ("other-lab-v0.27.0", false), + ("web-v4.0.0", false), ] { let mut config = config(); config.sources.get_mut("org").unwrap().repositories = vec!["robjtede/actix-web-lab".into()]; @@ -205,7 +211,7 @@ async fn incoming_excludes_case_insensitive_regexes_before_queueing_announcement r#" name = "lab" repository = "robjtede/actix-web-lab" - tag_prefix = "actix-web-lab-" + include_tags = ["^actix-web-lab-", "^lab-v[0-9]+[.][0-9]+[.][0-9]+$"] exclude_tags = ["^actix-web-lab-derive-.*$", "-(internal|test)$"] destinations = ["web"] "#, @@ -246,14 +252,15 @@ async fn incoming_excludes_case_insensitive_regexes_before_queueing_announcement } #[actix_web::test] -async fn routes_apply_tag_exclusions_per_route() { +async fn routes_apply_tag_filters_per_route() { let mut config = config(); - config.routes[0].tag_prefix = None; + config.routes[0].include_tags.clear(); config.routes.push( toml::from_str( r#" name = "filtered" repository = "actix/actix-web" + include_tags = ["lab", "^http-v"] exclude_tags = ["^actix-web-lab-derive-.*$"] destinations = ["web", "another"] "#, @@ -262,12 +269,61 @@ async fn routes_apply_tag_exclusions_per_route() { ); let mut release: github::Release = serde_json::from_value(event()["release"].clone()).unwrap(); - release.tag_name = "ACTIX-WEB-LAB-DERIVE-v0.1.0".into(); - assert_eq!( - routing::destinations(&config, "actix/actix-web", &release), - BTreeMap::from([("web".into(), vec!["web".into()])]) - ); + for (tag, expected_filtered) in [ + ("ACTIX-WEB-LAB-DERIVE-v0.1.0", false), + ("ACTIX-WEB-LAB-v0.27.0", true), + ("HTTP-v3.0.0", true), + ("web-v4.0.0", false), + ] { + release.tag_name = tag.into(); + + let expected = if expected_filtered { + BTreeMap::from([ + ("web".into(), vec!["web".into(), "filtered".into()]), + ("another".into(), vec!["filtered".into()]), + ]) + } else { + BTreeMap::from([("web".into(), vec!["web".into()])]) + }; + + assert_eq!( + routing::destinations(&config, "actix/actix-web", &release), + expected, + "{tag}" + ); + } +} + +#[actix_web::test] +async fn routes_allow_all_tags_before_exclusions_when_inclusion_is_omitted_or_empty() { + let mut release: github::Release = serde_json::from_value(event()["release"].clone()).unwrap(); + + for inclusion in ["", "include_tags = []"] { + let mut config = config(); + config.routes = vec![ + toml::from_str(&format!( + r#" + name = "all" + repository = "actix/actix-web" + {inclusion} + exclude_tags = ["^private-"] + destinations = ["web"] + "#, + )) + .unwrap(), + ]; + + for (tag, expected_match) in [("unmatched-v1.0.0", true), ("private-v1.0.0", false)] { + release.tag_name = tag.into(); + + assert_eq!( + routing::destinations(&config, "actix/actix-web", &release).contains_key("web"), + expected_match, + "{inclusion}: {tag}" + ); + } + } } #[actix_web::test] @@ -276,7 +332,7 @@ async fn routes_union_destinations_and_include_prereleases_per_route() { config.routes.push(Route { name: "all".into(), repository: "actix/actix-web".into(), - tag_prefix: None, + include_tags: Vec::new(), exclude_tags: Vec::new(), include_prereleases: true, destinations: vec!["web".into(), "another".into()], @@ -470,12 +526,14 @@ async fn config_validates_references_and_redacts_secrets() { let loaded = Config::load(&path).unwrap(); assert_eq!(loaded.sources["org"].key, "test-secret"); assert_eq!(loaded.bind, "0.0.0.0:8080"); - assert_eq!(loaded.routes[0].tag_prefix, None); + assert!(loaded.routes[0].include_tags.is_empty()); assert!(loaded.routes[0].exclude_tags.is_empty()); - for patterns in ["[]", r#"["^web-derive-.*$", "-(internal|test)$"]"#] { - std::fs::write(&path, format!("{input}\nexclude_tags = {patterns}\n")).unwrap(); - Config::load(&path).unwrap(); + for field in ["include_tags", "exclude_tags"] { + for patterns in ["[]", r#"["^web-derive-.*$", "-(internal|test)$"]"#] { + std::fs::write(&path, format!("{input}\n{field} = {patterns}\n")).unwrap(); + Config::load(&path).unwrap(); + } } for invalid in [ @@ -486,6 +544,9 @@ async fn config_validates_references_and_redacts_secrets() { "repository = \"other/repo\"", ), format!("unknown = \"private-token\"\n{input}"), + format!("{input}\ntag_prefix = \"web-\"\n"), + format!("{input}\ninclude_tags = [\"web-[\"]\n"), + format!("{input}\ninclude_tags = [\"private-token(\"]\n"), format!("{input}\nexclude_tags = [\"web-[\"]\n"), format!("{input}\nexclude_tags = [\"private-token(\"]\n"), ] {