Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
refactor: broker execute helper for clarity, DRY
Archived liw opened 1 year ago

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

1 file changed +54 -53 47f77639 305bc3f3
modified src/broker.rs
@@ -76,76 +76,77 @@ impl Broker {
    ) -> Result<Run, BrokerError> {
        let broker_run_id = RunId::default();
        let span = span!(Level::TRACE, "execute_ci_run", %broker_run_id,).entered();
-
        let run =
-
            span.in_scope(|| self.execute_helper(broker_run_id, trigger, run_notification))?;
+
        let rid = trigger.repo();
+
        let adapter = self.adapter(&rid).ok_or(BrokerError::NoAdapter(rid))?;
+
        let run = span
+
            .in_scope(|| self.execute_helper(adapter, broker_run_id, trigger, run_notification))?;
        Ok(run)
    }

    fn execute_helper(
        &self,
+
        adapter: &Adapter,
        broker_run_id: RunId,
        trigger: &Request,
        run_notification: &NotificationSender,
    ) -> Result<Run, BrokerError> {
        logger::broker_start_run(trigger);
-
        let run = match trigger {
+
        let (common, whence) = match &trigger {
            Request::Trigger {
                common,
-
                push,
-
                patch,
-
            } => {
-
                let rid = &common.repository.id;
-
                if let Some(adapter) = self.adapter(rid) {
-
                    let whence = if let Some(PushEvent {
+
                push:
+
                    Some(PushEvent {
                        pusher,
-
                        before: _,
                        after,
                        branch,
-
                        commits: _,
-
                    }) = push
-
                    {
-
                        let who = pusher.to_string();
-
                        Whence::branch(branch, *after, Some(who.as_str()))
-
                    } else if let Some(PatchEvent { action: _, patch }) = patch {
-
                        let revision = patch
-
                            .revisions
-
                            .last()
-
                            .ok_or(BrokerError::NoRevisions)?
-
                            .clone();
-
                        let who = patch.author.to_string();
-
                        Whence::patch(patch.id, patch.after, revision, Some(who.as_str()))
-
                    } else {
-
                        panic!("neither push not patch event");
-
                    };
-

-
                    let mut run = RunBuilder::default()
-
                        .broker_run_id(broker_run_id)
-
                        .repo_id(*rid)
-
                        .repo_name(&common.repository.name)
-
                        .whence(whence)
-
                        .timestamp(now()?)
-
                        .build();
-
                    self.db.push_run(&run)?;
-

-
                    // We run the adapter, but if that fails, we just
-
                    // log the error. The `Run` value records the
-
                    // result of the run.
-
                    if let Err(e) = adapter.run(
-
                        trigger,
-
                        &mut run,
-
                        &self.db,
-
                        run_notification,
-
                        self.max_run_time,
-
                    ) {
-
                        logger::error("failed to run adapter or it failed to run CI", &e);
-
                    }
-

-
                    run
-
                } else {
-
                    return Err(BrokerError::NoAdapter(*rid));
-
                }
+
                        ..
+
                    }),
+
                patch: None,
+
            } => {
+
                let who = pusher.to_string();
+
                (common, Whence::branch(branch, *after, Some(who.as_str())))
+
            }
+
            Request::Trigger {
+
                common,
+
                push: None,
+
                patch: Some(PatchEvent { patch, .. }),
+
            } => {
+
                let revision = patch
+
                    .revisions
+
                    .last()
+
                    .ok_or(BrokerError::NoRevisions)?
+
                    .clone();
+
                let who = patch.author.to_string();
+
                (
+
                    common,
+
                    Whence::patch(patch.id, patch.after, revision, Some(who.as_str())),
+
                )
            }
+
            _ => panic!("neither a push nor a patch event"),
        };
+

+
        let mut run = RunBuilder::default()
+
            .broker_run_id(broker_run_id)
+
            .repo_id(common.repository.id)
+
            .repo_name(&common.repository.name)
+
            .whence(whence)
+
            .timestamp(now()?)
+
            .build();
+
        self.db.push_run(&run)?;
+

+
        // We run the adapter, but if that fails, we just
+
        // log the error. The `Run` value records the
+
        // result of the run.
+
        if let Err(e) = adapter.run(
+
            trigger,
+
            &mut run,
+
            &self.db,
+
            run_notification,
+
            self.max_run_time,
+
        ) {
+
            logger::error("failed to run adapter or it failed to run CI", &e);
+
        }
+

        logger::broker_end_run(&run);

        self.db.update_run(&run)?;