diff --git a/Cargo.lock b/Cargo.lock index dafe63a..015982e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2341,6 +2341,7 @@ dependencies = [ "schemars", "serde", "serde_json", + "serde_yaml", "sha2", "tokio", "toml", diff --git a/Cargo.toml b/Cargo.toml index 22099bf..5a6ced2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ reqwest = "0.12.5" schemars = "0.8.21" serde = { version = "1.0.204", features = ["derive"] } serde_json = { version = "1.0.120", default-features = false, features = ["std"] } +serde_yaml = "0.9.34" sha2 = "0.10.8" tokio = { version = "1.38.0", default-features = false, features = ["macros", "process", "rt-multi-thread", "signal"] } toml = { version = "0.8.19", default-features = false, features = ["display", "parse"] } diff --git a/src/discovery.rs b/src/discovery.rs index 7d0b76e..bb903af 100644 --- a/src/discovery.rs +++ b/src/discovery.rs @@ -1,6 +1,7 @@ use std::path::Path; use std::path::PathBuf; +use crate::crd_pipeline_run::PipelineRun; use crate::gitea_client::GiteaClient; use crate::gitea_client::Tree; use crate::gitea_client::TreeFileReference; @@ -31,7 +32,8 @@ pub(crate) async fn discover_matching_push_triggers>( repo_tree: &Tree, git_ref: RE, in_repo_config: &InRepoConfig, -) -> Result<(), Box> { +) -> Result, Box> { + let mut ret = Vec::new(); let ref_to_branch_regex = Regex::new(r"refs/heads/(?P.+)")?; let captures = ref_to_branch_regex .captures(git_ref.as_ref()) @@ -42,7 +44,6 @@ pub(crate) async fn discover_matching_push_triggers>( let push_triggers = in_repo_config.get_push_triggers_for_branch(branch)?; for trigger in push_triggers { let path_to_source = normalize_path(Path::new(".webhook_bridge").join(&trigger.source)); - debug!("Loading path {}", path_to_source.display()); let pipeline_template = repo_tree .files .iter() @@ -50,10 +51,17 @@ pub(crate) async fn discover_matching_push_triggers>( .next() .ok_or("Trigger source not found in remote repo.")?; let pipeline_contents = String::from_utf8(gitea.read_file(pipeline_template).await?)?; - debug!("Pipeline contents: {}", pipeline_contents); + debug!("Pipeline template contents: {}", pipeline_contents); + + let pipeline: PipelineRun = serde_yaml::from_str(&pipeline_contents)?; + ret.push(PipelineTemplate::new( + trigger.name.clone(), + trigger.clone_uri.clone(), + pipeline, + )); } - Ok(()) + Ok(ret) } fn normalize_path>(path: P) -> PathBuf { @@ -76,3 +84,24 @@ fn normalize_path>(path: P) -> PathBuf { ret } + +#[derive(Debug)] +pub(crate) struct PipelineTemplate { + pub(crate) name: String, + pub(crate) clone_uri: Option, + pub(crate) pipeline: PipelineRun, +} + +impl PipelineTemplate { + pub(crate) fn new, C: Into>>( + name: N, + clone_uri: C, + pipeline: PipelineRun, + ) -> PipelineTemplate { + PipelineTemplate { + name: name.into(), + clone_uri: clone_uri.into(), + pipeline, + } + } +} diff --git a/src/main.rs b/src/main.rs index 145ee95..590541d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -59,12 +59,13 @@ async fn main() -> Result<(), Box> { .get_tree( "talexander", "webhook_bridge", - "c32a8650f509b5e4bbf6df0c210a3a01ad405eb5", + "6d3b9e9db82d7857baa114d89efcb1bf470f448d", ) .await?; let in_repo_config = discover_webhook_bridge_config(&gitea, &repo_tree).await?; - - discover_matching_push_triggers(&gitea, &repo_tree, "refs/heads/main", &in_repo_config).await?; + let pipelines = + discover_matching_push_triggers(&gitea, &repo_tree, "refs/heads/main", &in_repo_config) + .await?; // let jobs: Api = Api::namespaced(kubernetes_client, "lighthouse"); // let jobs: Api = Api::default_namespaced(kubernetes_client);