Radish alpha
r
Radicle CI broker
Radicle
Git (anonymous pull)
Log in to clone via SSH
Change the way that the broker perceives a patch
Michalis Zampetakis committed 1 year ago
commit 8adca82fe3f5745310a8f9c57842662351ad7f47
parent 3a0ef23cd50d0458463e54282ed2ca958ecce072
4 files changed +98 -12
modified Cargo.lock
@@ -1643,6 +1643,7 @@ dependencies = [
 "radicle",
 "radicle-git-ext",
 "radicle-surf",
+
 "regex",
 "serde",
 "serde_json",
 "serde_yaml 0.9.34+deprecated",
modified Cargo.toml
@@ -26,6 +26,7 @@ subplotlib = "0.9.0"
thiserror = "1.0.50"
time = { version = "0.3.34", features = ["formatting", "macros"] }
uuid = { version = "1.7.0", features = ["v4"] }
+
regex = "1.10.4"

[dependencies.radicle]
version = "0.9.0"
modified src/event.rs
@@ -27,12 +27,6 @@
//! let e = Filters::try_from(filters).unwrap();
//! ```

-
use std::{
-
    fs::read,
-
    path::{Path, PathBuf},
-
    time,
-
};
-

use log::{debug, info, trace};
use radicle::{
    node::{Event, Handle, NodeId},
@@ -41,7 +35,13 @@ use radicle::{
    Profile,
};
use radicle_git_ext::{ref_format::RefString, Oid};
+
use regex::Regex;
use serde::{Deserialize, Serialize};
+
use std::{
+
    fs::read,
+
    path::{Path, PathBuf},
+
    time,
+
};

/// Source of events from the local Radicle node.
///
@@ -222,6 +222,9 @@ pub enum EventFilter {
    /// Event concerns changed refs on any Radicle patch branch.
    AnyPatchRef,

+
    /// Event concerns changed refs on any Radicle branch.
+
    AnyPushRef,
+

    /// Event concerns changed refs on the branch of the specified Radicle patch.
    PatchRef(String),

@@ -394,6 +397,7 @@ impl BrokerEvent {
            EventFilter::AnyPatch => is_patch_update(name).is_some(),
            EventFilter::Patch(wanted) => is_patch_update(name) == Some(wanted),
            EventFilter::AnyPatchRef => is_patch_ref(name).is_some(),
+
            EventFilter::AnyPushRef => is_push_ref(name).is_some(),
            EventFilter::PatchRef(wanted) => is_patch_ref(name) == Some(wanted),
            EventFilter::And(conds) => conds.iter().all(|cond| self.is_allowed(cond)),
            EventFilter::Or(conds) => conds.iter().any(|cond| self.is_allowed(cond)),
@@ -432,7 +436,7 @@ impl BrokerEvent {

    pub fn patch_id(&self) -> Option<Oid> {
        if let Some(name) = self.name() {
-
            let suffix = is_patch_update(name);
+
            let suffix = is_patch_ref(name);
            if let Some(suffix_str) = suffix {
                return suffix_str.parse().ok();
            }
@@ -448,7 +452,6 @@ pub fn is_patch_update(name: &str) -> Option<&str> {
            return Some(suffix);
        }
    }
-

    None
}

@@ -459,6 +462,27 @@ pub fn is_patch_ref(name: &str) -> Option<&str> {
            return Some(suffix);
        }
    }
+
    None
+
}
+

+
/// Extract the suffix from the RefString in case of a push event.
+
/// Typically, this is the branch name of the push event.
+
///
+
/// # Example
+
/// ```Rust
+
/// let push_ref =  is_push_ref("refs/namespaces/NID/refs/heads/BRANCH")
+
/// println!("{}", push_ref.to_string()); // expects `BRANCH`
+
/// is_push_ref("refs/namespaces/NID/refs/rad/sigrefs")
+
/// println!("{}", push_ref.to_string()); // expects None
+
/// ```
+
fn is_push_ref(name: &str) -> Option<&str> {
+
    let re = Regex::new(r"^refs/namespaces/[^/]+/refs/heads/([^/]+)$").unwrap();
+

+
    if let Some(captures) = re.captures(name) {
+
        if let Some(branch) = captures.get(1) {
+
            return Some(branch.as_str());
+
        }
+
    }

    None
}
@@ -475,7 +499,7 @@ pub fn push_branch(name: &str) -> String {

#[cfg(test)]
mod test {
-
    use super::{is_patch_ref, is_patch_update, push_branch};
+
    use super::{is_patch_ref, is_patch_update, is_push_ref, push_branch};

    #[test]
    fn branch_is_not_patch() {
@@ -518,6 +542,66 @@ mod test {
    }

    #[test]
+
    fn patch_is_not_push_update_no_refs() {
+
        assert_eq!(
+
            is_push_ref(
+
                "nonrefs/namespaces/z6MkuhvCnrcow7vzkyQzkuFixzpTa42iC2Cfa4DA8HRLCmys/refs/heads/main"
+
            ),
+
            None
+
        );
+
    }
+

+
    #[test]
+
    fn patch_is_not_push_update_no_namespaces() {
+
        assert_eq!(
+
            is_push_ref(
+
                "refs/namespacesfoo/z6MkuhvCnrcow7vzkyQzkuFixzpTa42iC2Cfa4DA8HRLCmys/refs/heads/main"
+
            ),
+
            None
+
        );
+
    }
+

+
    #[test]
+
    fn patch_is_not_push_update_no_nid() {
+
        assert_eq!(
+
            is_push_ref(
+
                "refs/namespaces/z6MkuhvCnrcow7/vzkyQzkuFixzpTa42iC2Cfa4DA8HRLCmys/refs/heads/main"
+
            ),
+
            None
+
        );
+
    }
+

+
    #[test]
+
    fn patch_is_not_push_update_no_branch() {
+
        assert_eq!(
+
            is_push_ref(
+
                "refs/namespaces/z6MkuhvCnrcow7vzkyQzkuFixzpTa42iC2Cfa4DA8HRLCmys/refs/heads/"
+
            ),
+
            None
+
        );
+
    }
+

+
    #[test]
+
    fn patch_is_not_push_update() {
+
        assert_eq!(
+
            is_push_ref(
+
                "refs/namespaces/z6MkuhvCnrcow7vzkyQzkuFixzpTa42iC2Cfa4DA8HRLCmys/refs/cobs/xyz.radicle.patch/bbb54a2c9314a528a4fff9d6c2aae874ed098433"
+
            ),
+
            None
+
        );
+
    }
+

+
    #[test]
+
    fn branch_is_push_update() {
+
        assert_eq!(
+
            is_push_ref(
+
                "refs/namespaces/z6MkuhvCnrcow7vzkyQzkuFixzpTa42iC2Cfa4DA8HRLCmys/refs/heads/main"
+
            ),
+
            Some("main")
+
        );
+
    }
+

+
    #[test]
    fn patch_update() {
        assert_eq!(
            is_patch_update(
modified src/msg.rs
@@ -32,7 +32,7 @@ use radicle::{
    Profile,
};

-
use crate::event::{is_patch_update, push_branch, BrokerEvent};
+
use crate::event::{is_patch_ref, push_branch, BrokerEvent};

// This gets put into every [`Request`] message so the adapter can
// detect its getting a message it knows how to handle.
@@ -138,7 +138,7 @@ impl<'a> RequestBuilder<'a> {
            } => (rid, name, oid, old),
        };
        debug!("build_trigger: unpacked event");
-
        let is_patch = is_patch_update(name).is_some();
+
        let is_patch = is_patch_ref(name).is_some();
        let repository = profile.storage.repository(*rid)?;
        debug!("build_trigger: got repository");
        let storage = &profile.storage;
@@ -847,7 +847,7 @@ pub mod tests {
        let be = BrokerEvent::RefChanged {
            rid: project.id,
            name: RefString::try_from(
-
                "refs/namespaces/$nid/refs/cobs/xyz.radicle.patch/$patchId"
+
                "refs/namespaces/$nid/refs/heads/patches/$patchId"
                    .replace("$nid", &profile.id().to_string())
                    .replace("$patchId", &patch_cob.id.to_string()),
            )?,