coodev-runner 0.1.42

A simple runner for coodev
Documentation
use crate::{
  actions::Actions,
  runner::{GithubAuthorization, Runner, RunnerInner},
};
use parking_lot::Mutex;
use std::{collections::HashMap, path::PathBuf, sync::Arc};
use tokio::sync::Semaphore;

use super::{MessageSender, PluginManager};

#[derive(Debug, Clone, Default)]
pub struct RunnerBuilder {
  github_authorization: Option<GithubAuthorization>,
  // optional
  working_dir: Option<PathBuf>,
  // parallel size, default 10
  parallel_size: Option<usize>,
}

impl RunnerBuilder {
  pub fn github_authorization(&mut self, github_authorization: GithubAuthorization) -> &mut Self {
    self.github_authorization = Some(github_authorization);

    self
  }

  pub fn working_dir(&mut self, working_dir: impl Into<PathBuf>) -> &mut Self {
    self.working_dir = Some(working_dir.into());

    self
  }

  pub fn parallel_size(&mut self, parallel_size: usize) -> &mut Self {
    self.parallel_size = Some(parallel_size);

    self
  }

  pub fn build(&self) -> anyhow::Result<Runner> {
    let github_authorization = self.github_authorization.clone().ok_or(
      anyhow::anyhow!("Github authorization is required. Please set github authorization by Runner::builder().github_authorization()"),
    )?;
    let parallel_size = self.parallel_size.unwrap_or(10);

    let plugin_manager = PluginManager::new();
    let runner = Runner {
      github_authorization,
      parallel_semaphore: Arc::new(Semaphore::new(parallel_size)),
      working_dir: self.working_dir.clone(),
      inner: Arc::new(Mutex::new(RunnerInner {
        actions: Actions::new(),
        secrets: HashMap::new(),
        volumes: HashMap::new(),
        workflow_message_sender: MessageSender::new(),
        plugin_manager,
      })),
    };

    Ok(runner)
  }
}