Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
feat: make Tag event filter take a regular expression
Lars Wirzenius committed 1 year ago
commit a7d02c18c4e3282fd84238eb99d111bc904b5f2f
parent b783ef6
3 files changed +32 -9
modified ci-broker.md
@@ -943,7 +943,7 @@ adapters:
triggers:
  - adapter: default
    filters:
-
      - !Tag "v1.0"
+
      - !Tag "v\\d+(\\.\\d+)"
~~~

## Filter predicate `Branch`
modified src/filter.rs
@@ -1,10 +1,11 @@
use std::path::{Path, PathBuf};

+
use regex::Regex;
use serde::{Deserialize, Serialize};

use radicle::{
    cob::patch::PatchId,
-
    git::{raw::ObjectType, BranchName, Oid, RefString},
+
    git::{raw::ObjectType, BranchName, Oid},
    node::NodeId,
    prelude::{Profile, RepoId},
    storage::git::Repository,
@@ -55,8 +56,8 @@ pub enum EventFilter {
    /// Event is for a specific branch.
    Branch(BranchName),

-
    /// Event is for a specific tag.
-
    Tag(RefString),
+
    /// Event is for a tag whose name matches a regular expression.
+
    Tag(String),

    /// Event if for the default branch for the repository.
    DefaultBranch,
@@ -155,11 +156,24 @@ impl EventFilter {
                    format!("wanted={wanted} actual={actual:?}"),
                )
            }
-
            Self::Tag(wanted) => {
-
                let actual = event.tag();
-
                let allowed = Some(wanted) == actual;
-
                Decision::string("Tag", allowed, format!("wanted={wanted} actual={actual:?}"))
-
            }
+
            Self::Tag(wanted) => match Regex::new(wanted) {
+
                Ok(re) => {
+
                    let actual = event.tag();
+
                    let allowed = match &actual {
+
                        Some(actual) => re.is_match_at(actual.as_str(), 0),
+
                        _ => false,
+
                    };
+
                    Decision::string(
+
                        "Tag",
+
                        allowed,
+
                        format!("wanted={wanted:?} actual={actual:?}"),
+
                    )
+
                }
+
                Err(err) => {
+
                    logger::queueproc_filter_regex_error(wanted, err);
+
                    Decision::new("Tag", false, "regex syntax error")
+
                }
+
            },
            Self::DefaultBranch => {
                let repo = event.repository();
                let actual = event.branch();
modified src/logger.rs
@@ -444,6 +444,15 @@ pub fn queueproc_queue_length(len: usize) {
    );
}

+
pub fn queueproc_filter_regex_error(pattern: &str, err: regex::Error) {
+
    warn!(
+
        msg_id = ?Id::QueueProcFilterDecision,
+
        kind = %Kind::FilterDecision,
+
        ?pattern,
+
        ?err,
+
        "regular expression syntax error");
+
}
+

pub fn queueproc_filter_decision(event: &CiEvent, filter: &EventFilter, allowed: bool) {
    info!(
        msg_id = ?Id::QueueProcFilterDecision,