Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
feat: `cibtool trigger` uses default branch by default
Merged liw opened 8 months ago

Previously main was used by default.

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

feat: cibtool event add defaults to default branch

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

feat: cibtool event add defaults to HEAD commit of chosen ref

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

4 files changed +77 -50 b84fffaf a13b5a98
modified ci-broker.md
@@ -700,7 +700,7 @@ Verify that `cib` can process a branch creation event.

~~~
when I run rm -f ci-broker.db
-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref main --commit HEAD --kind branch-created --id-file id.txt
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --kind branch-created --id-file id.txt
when I run ./env.sh cib --config broker.yaml queued
when I run cibtool --db ci-broker.db run list
then stdout has one line
@@ -710,7 +710,7 @@ Verify that `cib` can process a branch update event.

~~~
when I run rm -f ci-broker.db
-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref brancy --commit HEAD --base main --kind branch-updated --id-file id.txt
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref brancy --base main --kind branch-updated --id-file id.txt
when I run ./env.sh cib --config broker.yaml queued
when I run cibtool --db ci-broker.db run list
then stdout has one line
@@ -720,7 +720,7 @@ Verify that `cib` can process a branch deletion event.

~~~
when I run rm -f ci-broker.db
-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref main --commit HEAD --kind branch-deleted --id-file id.txt
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --kind branch-deleted --id-file id.txt
when I run ./env.sh cib --config broker.yaml queued
when I run cibtool --db ci-broker.db run list
then stdout has one line
@@ -730,7 +730,7 @@ Verify that `cib` can process a patch creation event.

~~~
when I run rm -f ci-broker.db
-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref main --commit HEAD --kind patch-created --patch-id-file patch-id.txt --id-file id.txt
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --kind patch-created --patch-id-file patch-id.txt --id-file id.txt
when I run ./env.sh cib --config broker.yaml queued
when I run cibtool --db ci-broker.db run list
then stdout has one line
@@ -740,7 +740,7 @@ Verify that `cib` can process a patch update event.

~~~
when I run rm -f ci-broker.db
-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref main --commit HEAD --kind patch-updated --patch-id-file patch-id.txt --id-file id.txt
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --kind patch-updated --patch-id-file patch-id.txt --id-file id.txt
when I run ./env.sh cib --config broker.yaml queued
when I run cibtool --db ci-broker.db run list
then stdout has one line
@@ -1020,8 +1020,8 @@ given a Radicle node, with CI configured with broker.yaml and adapter dummy.sh
given a Git repository xyzzy in the Radicle node
given a directory reports

-
when I run ./env.sh cibtool --db ci-broker.db event add --repo xyzzy --ref main --commit f42ea5cb9ce2dc7a9b87834ccee5b9bb3867db90 --kind branch-updated --base main
-
when I run ./env.sh cibtool --db ci-broker.db event add --repo xyzzy --ref main --commit HEAD --kind branch-updated --base main
+
when I run ./env.sh cibtool --db ci-broker.db event add --repo xyzzy --commit f42ea5cb9ce2dc7a9b87834ccee5b9bb3867db90 --kind branch-updated --base main
+
when I run ./env.sh cibtool --db ci-broker.db event add --repo xyzzy --kind branch-updated --base main

when I run ./env.sh cib --config broker.yaml queued
then stderr contains "f42ea5cb9ce2dc7a9b87834ccee5b9bb3867db90"
@@ -1224,8 +1224,8 @@ given a Git repository xyzzy in the Radicle node

given file config.yaml from filter-branchcreated.yaml

-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref main --commit HEAD --kind branch-created
-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --commit HEAD --kind branch-updated --base main
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --kind branch-created
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --kind branch-updated --base main

when I run ./env.sh cib --config config.yaml queued

@@ -1259,8 +1259,8 @@ given a Git repository xyzzy in the Radicle node

given file config.yaml from filter-branchupdated.yaml

-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref main --commit HEAD --kind branch-updated --base main
-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --commit HEAD --kind branch-created
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --kind branch-updated --base main
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --kind branch-created

when I run ./env.sh cib --config config.yaml queued

@@ -1294,8 +1294,8 @@ given a Git repository xyzzy in the Radicle node

given file config.yaml from filter-branchdeleted.yaml

-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref main --commit HEAD --kind branch-deleted
-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --commit HEAD --kind branch-created
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --kind branch-deleted
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --kind branch-created

when I run ./env.sh cib --config config.yaml queued

@@ -1328,8 +1328,8 @@ given a Git repository xyzzy in the Radicle node

given file config.yaml from filter-allow.yaml

-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref main --commit HEAD --kind branch-deleted
-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --commit HEAD --kind branch-created
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --kind branch-deleted
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --kind branch-created

when I run ./env.sh cib --config config.yaml queued

@@ -1362,8 +1362,8 @@ given a Git repository xyzzy in the Radicle node

given file config.yaml from filter-deny.yaml

-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref main --commit HEAD --kind branch-deleted
-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --commit HEAD --kind branch-created
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --kind branch-deleted
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --kind branch-created

when I run ./env.sh cib --config config.yaml queued

@@ -1397,8 +1397,8 @@ given a Git repository xyzzy in the Radicle node

given file config.yaml from filter-and.yaml

-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref main --commit HEAD --kind branch-deleted
-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --commit HEAD --kind branch-created
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --kind branch-deleted
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --kind branch-created

when I run ./env.sh cib --config config.yaml queued

@@ -1434,8 +1434,8 @@ given a Git repository xyzzy in the Radicle node

given file config.yaml from filter-or.yaml

-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref main --commit HEAD --kind branch-deleted
-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --commit HEAD --kind branch-created
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --kind branch-deleted
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --kind branch-created

when I run ./env.sh cib --config config.yaml queued

@@ -1470,8 +1470,8 @@ given a Git repository xyzzy in the Radicle node

given file config.yaml from filter-not.yaml

-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref main --commit HEAD --kind branch-deleted
-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --commit HEAD --kind branch-created
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --kind branch-deleted
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --kind branch-created

when I run ./env.sh cib --config config.yaml queued

@@ -1507,8 +1507,8 @@ given a Git repository xyzzy in the Radicle node

given file config.yaml from filter-defaultbranch.yaml

-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref main --commit HEAD --kind branch-created
-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --commit HEAD --kind branch-created
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --kind branch-created
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref oksa --kind branch-created

when I run ./env.sh cib --config config.yaml queued

@@ -1542,13 +1542,13 @@ given a Radicle node, with CI configured with broker.yaml and adapter dummy.sh
given a Git repository xyzzy in the Radicle node

given file config.yaml from filter-hasfile-missing.yaml
-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref main --commit HEAD --kind branch-created
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --kind branch-created
when I run ./env.sh cib --config config.yaml queued
when I run cibtool --db ci-broker.db run list --json
then stdout doesn't contain "main"

given file config.yaml from filter-hasfile.yaml
-
when I run cibtool --db ci-broker.db event add --repo xyzzy --ref main --commit HEAD --kind branch-created
+
when I run cibtool --db ci-broker.db event add --repo xyzzy --kind branch-created
when I run ./env.sh cib --config config.yaml queued
when I run cibtool --db ci-broker.db run list --json
then stdout contains "main"
@@ -1778,7 +1778,7 @@ down.
given a Radicle node, with CI configured with broker.yaml and adapter dummy.sh
given a Git repository testy in the Radicle node

-
when I run ./env.sh cibtool --db ci-broker.db event add --repo testy --ref main --commit HEAD --kind branch-updated --base main
+
when I run ./env.sh cibtool --db ci-broker.db event add --repo testy --kind branch-updated --base main
when I run cibtool --db ci-broker.db event shutdown

given a directory reports
@@ -1901,7 +1901,7 @@ given a Git repository xyzzy in the Radicle node
when I run cibtool --db x.db event list
then stdout is empty

-
when I run ./env.sh cibtool --db x.db event add --repo xyzzy --ref main --commit HEAD --base HEAD --id-file id.txt --kind branch-updated
+
when I run ./env.sh cibtool --db x.db event add --repo xyzzy --base HEAD --id-file id.txt --kind branch-updated

when I run cibtool --db x.db event show --id-file id.txt
then stdout contains "rad:"
@@ -1930,12 +1930,12 @@ given a Git repository testy in the Radicle node
when I run cibtool --db x.db event list
then stdout is empty

-
when I run ./env.sh cibtool --db x.db event add --repo testy --ref main --commit HEAD --base HEAD --kind branch-updated
-
when I run ./env.sh cibtool --db x.db event add --repo testy --ref main --commit HEAD --base HEAD --kind branch-updated
-
when I run ./env.sh cibtool --db x.db event add --repo testy --ref main --commit HEAD --base HEAD --kind branch-updated
-
when I run ./env.sh cibtool --db x.db event add --repo testy --ref main --commit HEAD --base HEAD --kind branch-updated
-
when I run ./env.sh cibtool --db x.db event add --repo testy --ref main --commit HEAD --base HEAD --kind branch-updated
-
when I run ./env.sh cibtool --db x.db event add --repo testy --ref main --commit HEAD --base HEAD --kind branch-updated
+
when I run ./env.sh cibtool --db x.db event add --repo testy --base HEAD --kind branch-updated
+
when I run ./env.sh cibtool --db x.db event add --repo testy --base HEAD --kind branch-updated
+
when I run ./env.sh cibtool --db x.db event add --repo testy --base HEAD --kind branch-updated
+
when I run ./env.sh cibtool --db x.db event add --repo testy --base HEAD --kind branch-updated
+
when I run ./env.sh cibtool --db x.db event add --repo testy --base HEAD --kind branch-updated
+
when I run ./env.sh cibtool --db x.db event add --repo testy --base HEAD --kind branch-updated

when I run cibtool --db x.db event remove --all
when I run cibtool --db x.db event list
@@ -1977,7 +1977,7 @@ given a Radicle node, with CI configured with broker.yaml and adapter dummy.sh
given a Git repository xyzzy in the Radicle node

when I run ./env.sh env -C xyzzy git switch -c oksa
-
when I run ./env.sh cibtool --db x.db event add --repo xyzzy --ref oksa --commit HEAD --kind branch-created
+
when I run ./env.sh cibtool --db x.db event add --repo xyzzy --ref oksa --kind branch-created

when I run cibtool --db x.db event list --json
then stdout contains "BranchCreated"
@@ -1997,7 +1997,7 @@ given a Radicle node, with CI configured with broker.yaml and adapter dummy.sh
given a Git repository xyzzy in the Radicle node

when I run ./env.sh env -C xyzzy git switch -c oksa
-
when I run ./env.sh cibtool --db x.db event add --repo xyzzy --ref oksa --commit HEAD --kind branch-updated --base HEAD
+
when I run ./env.sh cibtool --db x.db event add --repo xyzzy --ref oksa --kind branch-updated --base HEAD

when I run cibtool --db x.db event list --json
then stdout contains "BranchUpdated"
@@ -2017,7 +2017,7 @@ given a Radicle node, with CI configured with broker.yaml and adapter dummy.sh
given a Git repository xyzzy in the Radicle node

when I run ./env.sh env -C xyzzy git switch -c oksa
-
when I run ./env.sh cibtool --db x.db event add --repo xyzzy --ref oksa --commit HEAD --kind branch-deleted
+
when I run ./env.sh cibtool --db x.db event add --repo xyzzy --ref oksa --kind branch-deleted

when I run cibtool --db x.db event list --json
then stdout contains "BranchDeleted"
@@ -2037,7 +2037,7 @@ given a Radicle node, with CI configured with broker.yaml and adapter dummy.sh
given a Git repository xyzzy in the Radicle node

when I run ./env.sh env -C xyzzy git switch -c oksa
-
when I run ./env.sh cibtool --db x.db event add --repo xyzzy --ref oksa --commit HEAD --kind patch-created --patch-id f863364f6774160607d90811b06a0e401c097466
+
when I run ./env.sh cibtool --db x.db event add --repo xyzzy --ref oksa --kind patch-created --patch-id f863364f6774160607d90811b06a0e401c097466

when I run cibtool --db x.db event list --json
then stdout contains "PatchCreated"
@@ -2057,7 +2057,7 @@ given a Radicle node, with CI configured with broker.yaml and adapter dummy.sh
given a Git repository xyzzy in the Radicle node

when I run ./env.sh env -C xyzzy git switch -c oksa
-
when I run ./env.sh cibtool --db x.db event add --repo xyzzy --ref oksa --commit HEAD --kind patch-updated --patch-id f863364f6774160607d90811b06a0e401c097466
+
when I run ./env.sh cibtool --db x.db event add --repo xyzzy --ref oksa --kind patch-updated --patch-id f863364f6774160607d90811b06a0e401c097466

when I run cibtool --db x.db event list --json
then stdout contains "PatchUpdated"
@@ -2295,11 +2295,11 @@ Note that we verify both lookup by name and by repository ID, and by
given a Radicle node, with CI configured with broker.yaml and adapter dummy.sh
given a Git repository testy in the Radicle node

-
when I try to run ./env.sh cibtool --db x.db event add --repo missing --ref main --commit HEAD --base c0ffee --kind branch-updated
+
when I try to run ./env.sh cibtool --db x.db event add --repo missing --base c0ffee --kind branch-updated
then command fails
then stderr contains "missing"

-
when I try to run ./env.sh cibtool --db x.db event add --repo rad:z3byzFpcfbMJBp4tKYyuuTZiP8WUB --ref main --commit HEAD --base c0ffee --kind branch-updated
+
when I try to run ./env.sh cibtool --db x.db event add --repo rad:z3byzFpcfbMJBp4tKYyuuTZiP8WUB --base c0ffee --kind branch-updated
then command fails
then stderr contains "rad:z3byzFpcfbMJBp4tKYyuuTZiP8WUB"

modified src/bin/cibtool.rs
@@ -274,6 +274,9 @@ enum CibToolError {
    #[error("failed to open git repository in node storage: {0}")]
    RepoOpen(RepoId, #[source] radicle::storage::RepositoryError),

+
    #[error("failed to get project information for {0}")]
+
    GetProject(RepoId, #[source] radicle::storage::RepositoryError),
+

    #[error("failed to parse git ref as a commit id: {0}")]
    RevParse(String, #[source] radicle::git::raw::Error),

modified src/bin/cibtoolcmd/event.rs
@@ -77,14 +77,15 @@ pub struct AddEvent {
    #[clap(long)]
    repo: String,

-
    /// Set the name of the ref the event refers to.
+
    /// Set the name of the ref the event refers to. Default is the
+
    /// default branch.
    #[clap(long, alias = "ref")]
-
    name: String,
+
    name: Option<String>,

    /// Set the commit the event refers to. Can be the SHA1 commit id,
    /// or a symbolic Git revision, as understood by `git rev-parse`.
    /// For example, `HEAD`.
-
    #[clap(long)]
+
    #[clap(long, default_value = "HEAD")]
    commit: String,

    /// Type of event to create.
@@ -181,8 +182,20 @@ impl Leaf for AddEvent {
            self.lookup_commit(rid, &self.commit)?
        };

-
        let branch_name = branch_ref(&ref_string(&self.name).map_err(CibToolError::RefError)?)
-
            .map_err(CibToolError::RefError)?;
+
        let branch_name = if let Some(name) = &self.name {
+
            branch_ref(&ref_string(name).map_err(CibToolError::RefError)?)
+
                .map_err(CibToolError::RefError)?
+
        } else {
+
            let profile = util::load_profile()?;
+
            let repo = profile
+
                .storage
+
                .repository(rid)
+
                .map_err(|err| CibToolError::RepoOpen(rid, err))?;
+
            let project = repo
+
                .project()
+
                .map_err(|err| CibToolError::GetProject(rid, err))?;
+
            project.default_branch().clone()
+
        };

        let event = match &self.kind {
            EventKind::BranchCreated => {
modified src/bin/cibtoolcmd/trigger.rs
@@ -16,7 +16,7 @@ pub struct TriggerCmd {

    /// Set the name of the ref the event refers to.
    #[clap(long = "ref", aliases = ["name"], default_value = "main")]
-
    name: String,
+
    name: Option<String>,

    /// Set the commit the event refers to. Can be the SHA1 commit id,
    /// or a symbolic Git revision, as understood by `git rev-parse`.
@@ -49,8 +49,19 @@ impl Leaf for TriggerCmd {
        let oid = util::oid_from_cli_arg(&profile, rid, &self.commit)?;

        let base = util::lookup_commit(&profile, rid, &format!("{oid}^")).unwrap_or(oid);
-
        let branch_name = branch_ref(&ref_string(&self.name).map_err(CibToolError::RefError)?)
-
            .map_err(CibToolError::RefError)?;
+
        let branch_name = if let Some(name) = &self.name {
+
            branch_ref(&ref_string(name).map_err(CibToolError::RefError)?)
+
                .map_err(CibToolError::RefError)?
+
        } else {
+
            let repo = profile
+
                .storage
+
                .repository(rid)
+
                .map_err(|err| CibToolError::RepoOpen(rid, err))?;
+
            let project = repo
+
                .project()
+
                .map_err(|err| CibToolError::GetProject(rid, err))?;
+
            project.default_branch().clone()
+
        };
        let event = CiEvent::branch_updated(nid, rid, &branch_name, oid, base)
            .map_err(CibToolError::CiEvent)?;