Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
fix: DefaultBranch event filter didn't work with namespaced refs
Lars Wirzenius committed 1 year ago
commit 94b3de7385692d6c08dd3c1f4ff0bf2b5d8fe299
parent cfac437
3 files changed +30 -10
added src/bin/default_branch.rs
@@ -0,0 +1,13 @@
+
//! The CI broker.
+

+
#![allow(clippy::result_large_err)]
+

+
use radicle::prelude::RepoId;
+

+
use radicle_ci_broker::filter::get_default_branch;
+

+
fn main() {
+
    let repo = RepoId::from_urn("rad:zwTxygwuz5LDGBq255RA2CbNGrz8").unwrap();
+
    let branch = get_default_branch(&repo).unwrap();
+
    println!("default branch is {branch}");
+
}
modified src/ci_event.rs
@@ -14,6 +14,8 @@ use radicle::{
    storage::RefUpdate,
};

+
use crate::logger;
+

#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub enum CiEvent {
@@ -506,12 +508,15 @@ mod test {
    }
}

-
fn namespaced_branch(refname: &str) -> Result<String, ParseError> {
+
pub fn namespaced_branch(refname: &str) -> Result<String, ParseError> {
+
    logger::debug2(format!("namespaced_branch: refname={refname:?}"));
    const PAT_BRANCH: &str = r"^refs/namespaces/[^/]+/refs/heads/(.+)$";
    let push_re = Regex::new(PAT_BRANCH).map_err(|e| ParseError::Regex(PAT_BRANCH, e))?;
    if let Some(push_captures) = push_re.captures(refname) {
        if let Some(branch) = push_captures.get(1) {
-
            return Ok(branch.as_str().to_string());
+
            let branch = branch.as_str().to_string();
+
            logger::debug2(format!("namespaced_branch: result={branch:?}"));
+
            return Ok(branch);
        }
    }
    Err(ParseError::NotBranch(refname.into()))
@@ -532,7 +537,7 @@ fn patch_id(refname: &str) -> Result<PatchId, ParseError> {
}

#[derive(Debug, thiserror::Error)]
-
enum ParseError {
+
pub enum ParseError {
    #[error("programming error: unacceptable regular expression {0:?}")]
    Regex(&'static str, regex::Error),

modified src/filter.rs
@@ -12,7 +12,7 @@ use radicle::{
use radicle_git_ext::Oid;

use crate::{
-
    ci_event::{CiEvent, CiEventV1},
+
    ci_event::{namespaced_branch, CiEvent, CiEventV1},
    config::TriggerConfig,
    logger,
};
@@ -203,15 +203,17 @@ impl EventFilter {
}

fn is_default_branch(repo_id: &RepoId, wanted: &str) -> bool {
-
    let res = get_default_branch(repo_id);
-
    if let Ok(actual) = res {
-
        actual == wanted
-
    } else {
-
        false
+
    if let Ok(default) = get_default_branch(repo_id) {
+
        if let Ok(default) = namespaced_branch(&default) {
+
            if let Ok(wanted) = namespaced_branch(wanted) {
+
                return default == wanted;
+
            }
+
        }
    }
+
    false
}

-
fn get_default_branch(repo_id: &RepoId) -> Result<String, Box<dyn std::error::Error>> {
+
pub fn get_default_branch(repo_id: &RepoId) -> Result<String, Box<dyn std::error::Error>> {
    let profile = Profile::load()?;
    let path = profile.storage.path().join(repo_id.canonical());
    let repo = Repository::open(path, *repo_id)?;