use super::github::GitHubRunnerClient;
use super::gitlab::GitLabRunnerClient;
use crate::error::{Result, ToriiError};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Runner {
pub id: String,
pub description: String,
pub status: String,
pub paused: bool,
pub ip_address: String,
pub os: String,
pub tags: Vec<String>,
pub version: String,
pub runner_type: String,
pub web_url: String,
}
#[allow(dead_code)]
pub trait RunnerClient: Send {
fn list(&self, owner: &str, repo: &str) -> Result<Vec<Runner>>;
fn show(&self, owner: &str, repo: &str, id: &str) -> Result<Runner>;
fn remove(&self, owner: &str, repo: &str, id: &str) -> Result<()>;
fn reset_token(&self, owner: &str, repo: &str, id: &str) -> Result<String>;
fn pause(&self, owner: &str, repo: &str, id: &str) -> Result<()>;
fn resume(&self, owner: &str, repo: &str, id: &str) -> Result<()>;
fn registration_token(&self, owner: &str, repo: &str) -> Result<RegistrationToken>;
}
#[derive(Debug, Clone)]
pub struct RegistrationToken {
pub token: String,
pub register_url: String,
pub expires_in_seconds: Option<u64>,
}
pub(crate) fn set_paused(
c: &GitLabRunnerClient,
_owner: &str,
_repo: &str,
id: &str,
paused: bool,
) -> Result<()> {
let url = format!("{}/runners/{}", c.base_url, id);
let req = c
.client()
.put(&url)
.header("Authorization", c.auth())
.json(&serde_json::json!({ "paused": paused }));
crate::http::send_empty(req, &format!("GitLab set runner.paused={}", paused))
}
pub fn get_runner_client(platform: &str) -> Result<Box<dyn RunnerClient>> {
match platform {
"gitlab" => Ok(Box::new(GitLabRunnerClient::new()?)),
"github" => Ok(Box::new(GitHubRunnerClient::new()?)),
other => Err(ToriiError::Unsupported(format!(
"Runners surface not implemented for `{}` yet. \
Supported: github, gitlab.",
other
))),
}
}