Parse the pipeline templates.

This commit is contained in:
Tom Alexander 2024-09-28 19:19:38 -04:00
parent 6d3b9e9db8
commit d5902e3e7f
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 39 additions and 7 deletions

1
Cargo.lock generated
View File

@ -2341,6 +2341,7 @@ dependencies = [
"schemars", "schemars",
"serde", "serde",
"serde_json", "serde_json",
"serde_yaml",
"sha2", "sha2",
"tokio", "tokio",
"toml", "toml",

View File

@ -29,6 +29,7 @@ reqwest = "0.12.5"
schemars = "0.8.21" schemars = "0.8.21"
serde = { version = "1.0.204", features = ["derive"] } serde = { version = "1.0.204", features = ["derive"] }
serde_json = { version = "1.0.120", default-features = false, features = ["std"] } serde_json = { version = "1.0.120", default-features = false, features = ["std"] }
serde_yaml = "0.9.34"
sha2 = "0.10.8" sha2 = "0.10.8"
tokio = { version = "1.38.0", default-features = false, features = ["macros", "process", "rt-multi-thread", "signal"] } 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"] } toml = { version = "0.8.19", default-features = false, features = ["display", "parse"] }

View File

@ -1,6 +1,7 @@
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use crate::crd_pipeline_run::PipelineRun;
use crate::gitea_client::GiteaClient; use crate::gitea_client::GiteaClient;
use crate::gitea_client::Tree; use crate::gitea_client::Tree;
use crate::gitea_client::TreeFileReference; use crate::gitea_client::TreeFileReference;
@ -31,7 +32,8 @@ pub(crate) async fn discover_matching_push_triggers<RE: AsRef<str>>(
repo_tree: &Tree, repo_tree: &Tree,
git_ref: RE, git_ref: RE,
in_repo_config: &InRepoConfig, in_repo_config: &InRepoConfig,
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<Vec<PipelineTemplate>, Box<dyn std::error::Error>> {
let mut ret = Vec::new();
let ref_to_branch_regex = Regex::new(r"refs/heads/(?P<branch>.+)")?; let ref_to_branch_regex = Regex::new(r"refs/heads/(?P<branch>.+)")?;
let captures = ref_to_branch_regex let captures = ref_to_branch_regex
.captures(git_ref.as_ref()) .captures(git_ref.as_ref())
@ -42,7 +44,6 @@ pub(crate) async fn discover_matching_push_triggers<RE: AsRef<str>>(
let push_triggers = in_repo_config.get_push_triggers_for_branch(branch)?; let push_triggers = in_repo_config.get_push_triggers_for_branch(branch)?;
for trigger in push_triggers { for trigger in push_triggers {
let path_to_source = normalize_path(Path::new(".webhook_bridge").join(&trigger.source)); 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 let pipeline_template = repo_tree
.files .files
.iter() .iter()
@ -50,10 +51,17 @@ pub(crate) async fn discover_matching_push_triggers<RE: AsRef<str>>(
.next() .next()
.ok_or("Trigger source not found in remote repo.")?; .ok_or("Trigger source not found in remote repo.")?;
let pipeline_contents = String::from_utf8(gitea.read_file(pipeline_template).await?)?; 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<P: AsRef<Path>>(path: P) -> PathBuf { fn normalize_path<P: AsRef<Path>>(path: P) -> PathBuf {
@ -76,3 +84,24 @@ fn normalize_path<P: AsRef<Path>>(path: P) -> PathBuf {
ret ret
} }
#[derive(Debug)]
pub(crate) struct PipelineTemplate {
pub(crate) name: String,
pub(crate) clone_uri: Option<String>,
pub(crate) pipeline: PipelineRun,
}
impl PipelineTemplate {
pub(crate) fn new<N: Into<String>, C: Into<Option<String>>>(
name: N,
clone_uri: C,
pipeline: PipelineRun,
) -> PipelineTemplate {
PipelineTemplate {
name: name.into(),
clone_uri: clone_uri.into(),
pipeline,
}
}
}

View File

@ -59,12 +59,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.get_tree( .get_tree(
"talexander", "talexander",
"webhook_bridge", "webhook_bridge",
"c32a8650f509b5e4bbf6df0c210a3a01ad405eb5", "6d3b9e9db82d7857baa114d89efcb1bf470f448d",
) )
.await?; .await?;
let in_repo_config = discover_webhook_bridge_config(&gitea, &repo_tree).await?; let in_repo_config = discover_webhook_bridge_config(&gitea, &repo_tree).await?;
let pipelines =
discover_matching_push_triggers(&gitea, &repo_tree, "refs/heads/main", &in_repo_config).await?; discover_matching_push_triggers(&gitea, &repo_tree, "refs/heads/main", &in_repo_config)
.await?;
// let jobs: Api<PipelineRun> = Api::namespaced(kubernetes_client, "lighthouse"); // let jobs: Api<PipelineRun> = Api::namespaced(kubernetes_client, "lighthouse");
// let jobs: Api<PipelineRun> = Api::default_namespaced(kubernetes_client); // let jobs: Api<PipelineRun> = Api::default_namespaced(kubernetes_client);