Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
fix: DefaultBranch event filter didn't work with namespaced refs
Merged liw opened 1 year ago

The CI event contains a namespaced ref. The default branch we get also. The CI broker filter configuration does not. Fix src/filter.rs::is_default_branch to compare only the un-namespaced branch names.

Signed-off-by: Lars Wirzenius liw@liw.fi

6 files changed +33 -25 cfac437f 2778011d
modified .radicle/ambient.yaml
@@ -32,13 +32,13 @@ plan:
      export HOME=/root
      export PATH="/root/.cargo/bin:$PATH"

-
      cargo doc --workspace
+
      cargo doc --workspace --no-deps

      # FIXME: We can't run upgrade tests from older versions because
      # Ambient only fetches dependencies for the current version.
      # Thus, we can't build the old versions. This is an Ambient
      # problem that we can't fix here. So we skip the upgrade tests.
-
      cargo test --workspace --no-fail-fast -- --skip upgrade
+
      cargo test --workspace --no-fail-fast -- --skip upgrade --test-threads 2

      subplot docgen ci-broker.subplot -o doc/ci-broker.html
      subplot docgen doc/userguide.subplot -o doc/userguide.html
deleted .radicle/native.yaml
@@ -1,13 +0,0 @@
-
shell: |
-
  cargo --version
-
  rustc --version
-

-
  cargo fmt --check
-
  cargo clippy --all-targets --workspace -- -Dwarnings
-
  cargo build --all-targets --workspace
-
  cargo doc --workspace
-
  cargo test --workspace --no-fail-fast
-

-
  subplot docgen ci-broker.subplot -o doc/ci-broker.html
-
  subplot docgen doc/userguide.subplot -o doc/userguide.html
-
  make -C doc publish
modified ci-broker.md
@@ -1220,6 +1220,7 @@ then stdout contains "main"
then stdout doesn't contain "oksa"
~~~

+

~~~{#filter-defaultbranch.yaml .file .json}
db: ci-broker.db
adapters:
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)?;