use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use ts_rs::TS;
use crate::project::{double_option_string, ProjectId};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
pub struct Repo {
pub name: String,
pub source: RepoSource,
pub i_own_this: bool,
#[ts(type = "string")]
pub project_id: ProjectId,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
pub enum RepoSource {
Git {
url: String,
branch: Option<String>,
auth: Option<GitAuth>,
},
LocalPath {
#[ts(type = "string")]
path: PathBuf,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
pub enum GitAuth {
SshKey(#[ts(type = "string")] PathBuf),
TokenEnv(String),
GhApp(String),
}
impl GitAuth {
pub fn descriptor(&self) -> String {
match self {
GitAuth::SshKey(p) => format!("ssh-key:{}", p.display()),
GitAuth::TokenEnv(var) => format!("token-env:{var}"),
GitAuth::GhApp(id) => format!("gh-app:{id}"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, TS)]
pub struct RepoRecord {
pub id: String,
pub name: String,
pub project_id: String,
pub source_kind: String,
pub source_url_or_path: String,
pub branch: Option<String>,
pub auth_ref: Option<String>,
pub i_own_this: bool,
pub last_scan_run_id: Option<String>,
#[serde(default)]
#[ts(type = "number | null")]
pub last_scan_finished_at: Option<i64>,
#[ts(type = "number")]
pub created_at: i64,
#[ts(type = "number")]
pub updated_at: i64,
}
#[derive(Debug, Deserialize, TS)]
pub struct CreateRepoRequest {
pub name: String,
pub source_kind: String,
pub source_url_or_path: String,
#[serde(default)]
#[ts(optional)]
pub branch: Option<String>,
#[serde(default)]
#[ts(optional)]
pub auth_ref: Option<String>,
#[serde(default)]
pub i_own_this: bool,
}
#[derive(Debug, Deserialize, TS)]
pub struct PatchRepoRequest {
#[serde(default)]
#[ts(optional)]
pub source_kind: Option<String>,
#[serde(default)]
#[ts(optional)]
pub source_url_or_path: Option<String>,
#[serde(default, with = "double_option_string")]
#[ts(optional, type = "string | null")]
pub branch: Option<Option<String>>,
#[serde(default, with = "double_option_string")]
#[ts(optional, type = "string | null")]
pub auth_ref: Option<Option<String>>,
#[serde(default)]
#[ts(optional)]
pub i_own_this: Option<bool>,
}
#[derive(Debug, Deserialize, TS)]
pub struct TestRepoRequest {
pub source_kind: String,
pub source_url_or_path: String,
#[serde(default)]
#[ts(optional)]
pub branch: Option<String>,
}
#[derive(Debug, Serialize, TS)]
pub struct TestRepoResponse {
pub ok: bool,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[ts(optional)]
pub on_disk_git_remote: Option<String>,
}