Create PipelineRun in response to webhook triggers.
This commit is contained in:
@@ -14,11 +14,32 @@ use serde_json::Value;
|
||||
plural = "pipelineruns"
|
||||
)]
|
||||
#[kube(namespaced)]
|
||||
pub struct PipelineRunSpec {
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct PipelineRunSpec {
|
||||
/// Contents of the Pipeline
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub pipelineSpec: Option<Value>,
|
||||
pub(crate) pipelineSpec: Option<Value>,
|
||||
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub timeouts: Option<Value>,
|
||||
pub(crate) timeouts: Option<Value>,
|
||||
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(crate) taskRunTemplate: Option<Value>,
|
||||
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(crate) workspaces: Option<Value>,
|
||||
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(crate) params: Option<Vec<PipelineParam>>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, JsonSchema)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct PipelineParam {
|
||||
/// Contents of the Pipeline
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(crate) name: Option<String>,
|
||||
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub(crate) value: Option<Value>,
|
||||
}
|
||||
|
||||
@@ -97,6 +97,7 @@ impl GiteaClient {
|
||||
|
||||
/// A single API response for GetTree containing only one page.
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct ResponseGetTree {
|
||||
sha: String,
|
||||
url: String,
|
||||
@@ -107,6 +108,7 @@ struct ResponseGetTree {
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct ResponseObjectReference {
|
||||
path: String,
|
||||
mode: String,
|
||||
@@ -144,6 +146,7 @@ impl TreeFileReference {
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct ResponseReadFile {
|
||||
content: String,
|
||||
encoding: String,
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use regex::Regex;
|
||||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct HookPush {
|
||||
#[serde(rename = "ref")]
|
||||
ref_field: String,
|
||||
before: String,
|
||||
after: String,
|
||||
compare_url: String,
|
||||
commits: Vec<HookCommit>,
|
||||
total_commits: u64,
|
||||
@@ -18,6 +23,7 @@ pub(crate) struct HookPush {
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct HookUser {
|
||||
id: u64,
|
||||
login: String,
|
||||
@@ -44,6 +50,7 @@ pub(crate) struct HookUser {
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct HookRepository {
|
||||
id: u64,
|
||||
owner: HookUser,
|
||||
@@ -104,6 +111,7 @@ pub(crate) struct HookRepository {
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct HookRepositoryPermissions {
|
||||
admin: bool,
|
||||
push: bool,
|
||||
@@ -112,6 +120,7 @@ pub(crate) struct HookRepositoryPermissions {
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct HookRepositoryInternalTracker {
|
||||
enable_time_tracker: bool,
|
||||
allow_only_contributors_to_track_time: bool,
|
||||
@@ -120,6 +129,7 @@ pub(crate) struct HookRepositoryInternalTracker {
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct HookCommit {
|
||||
id: String,
|
||||
message: String,
|
||||
@@ -135,8 +145,48 @@ pub(crate) struct HookCommit {
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct HookGitUser {
|
||||
name: String,
|
||||
email: String,
|
||||
username: String,
|
||||
}
|
||||
|
||||
pub(crate) trait PipelineParamters {
|
||||
fn get_pull_base_ref(&self) -> Result<Cow<str>, Box<dyn std::error::Error>>;
|
||||
|
||||
fn get_pull_base_sha(&self) -> Result<Cow<str>, Box<dyn std::error::Error>>;
|
||||
|
||||
fn get_repo_url(&self) -> Result<Cow<str>, Box<dyn std::error::Error>>;
|
||||
|
||||
fn get_repo_name(&self) -> Result<Cow<str>, Box<dyn std::error::Error>>;
|
||||
|
||||
fn get_repo_owner(&self) -> Result<Cow<str>, Box<dyn std::error::Error>>;
|
||||
}
|
||||
|
||||
impl PipelineParamters for HookPush {
|
||||
fn get_pull_base_ref(&self) -> Result<Cow<str>, Box<dyn std::error::Error>> {
|
||||
let ref_to_branch_regex = Regex::new(r"refs/heads/(?P<branch>.+)")?;
|
||||
let captures = ref_to_branch_regex
|
||||
.captures(self.ref_field.as_str())
|
||||
.ok_or("Could not find branch name.")?;
|
||||
let branch = &captures["branch"];
|
||||
Ok(Cow::Owned(branch.to_owned()))
|
||||
}
|
||||
|
||||
fn get_pull_base_sha(&self) -> Result<Cow<str>, Box<dyn std::error::Error>> {
|
||||
Ok(Cow::Borrowed(self.after.as_str()))
|
||||
}
|
||||
|
||||
fn get_repo_url(&self) -> Result<Cow<str>, Box<dyn std::error::Error>> {
|
||||
Ok(Cow::Borrowed(self.repository.clone_url.as_str()))
|
||||
}
|
||||
|
||||
fn get_repo_name(&self) -> Result<Cow<str>, Box<dyn std::error::Error>> {
|
||||
Ok(Cow::Borrowed(self.repository.name.as_str()))
|
||||
}
|
||||
|
||||
fn get_repo_owner(&self) -> Result<Cow<str>, Box<dyn std::error::Error>> {
|
||||
Ok(Cow::Borrowed(self.repository.owner.username.as_str()))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ use serde::Serialize;
|
||||
|
||||
/// The webhook_bridge.toml file that lives inside repos that have their CI triggered by webhook_bridge.
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct InRepoConfig {
|
||||
pub(crate) version: String,
|
||||
|
||||
@@ -13,6 +14,7 @@ pub(crate) struct InRepoConfig {
|
||||
|
||||
/// A config for a job that is triggered by a push to a git repo.
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct TriggerPush {
|
||||
pub(crate) name: String,
|
||||
pub(crate) source: String,
|
||||
|
||||
69
src/kubernetes.rs
Normal file
69
src/kubernetes.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use kube::api::PostParams;
|
||||
use kube::Api;
|
||||
use kube::CustomResourceExt;
|
||||
use tracing::debug;
|
||||
|
||||
use crate::crd_pipeline_run::PipelineParam;
|
||||
use crate::crd_pipeline_run::PipelineRun;
|
||||
use crate::discovery::PipelineTemplate;
|
||||
use crate::hook_push::HookPush;
|
||||
use crate::hook_push::PipelineParamters;
|
||||
|
||||
pub(crate) async fn run_pipelines(
|
||||
hook: HookPush,
|
||||
pipelines: Vec<PipelineTemplate>,
|
||||
kubernetes_client: kube::Client,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let jobs: Api<PipelineRun> = Api::namespaced(kubernetes_client, "lighthouse");
|
||||
// let jobs: Api<PipelineRun> = Api::default_namespaced(kubernetes_client);
|
||||
tracing::info!("Using crd: {}", serde_json::to_string(&PipelineRun::crd())?);
|
||||
|
||||
for mut pipeline in pipelines {
|
||||
debug!("Kicking off {}", pipeline.name);
|
||||
if pipeline.pipeline.spec.params.is_none() {
|
||||
pipeline.pipeline.spec.params = Some(Vec::new());
|
||||
}
|
||||
if let Some(param_list) = pipeline.pipeline.spec.params.as_mut() {
|
||||
param_list.push(PipelineParam {
|
||||
name: Some("JOB_NAME".to_owned()),
|
||||
value: Some(serde_json::Value::String(pipeline.name)),
|
||||
});
|
||||
param_list.push(PipelineParam {
|
||||
name: Some("REPO_OWNER".to_owned()),
|
||||
value: Some(serde_json::Value::String(
|
||||
hook.get_repo_owner()?.into_owned(),
|
||||
)),
|
||||
});
|
||||
param_list.push(PipelineParam {
|
||||
name: Some("REPO_NAME".to_owned()),
|
||||
value: Some(serde_json::Value::String(
|
||||
hook.get_repo_name()?.into_owned(),
|
||||
)),
|
||||
});
|
||||
let hook_repo_url = hook.get_repo_url()?;
|
||||
param_list.push(PipelineParam {
|
||||
name: Some("REPO_URL".to_owned()),
|
||||
value: pipeline
|
||||
.clone_uri
|
||||
.map(|uri| serde_json::Value::String(uri))
|
||||
.or_else(|| Some(serde_json::Value::String(hook_repo_url.into_owned()))),
|
||||
});
|
||||
param_list.push(PipelineParam {
|
||||
name: Some("PULL_BASE_SHA".to_owned()),
|
||||
value: Some(serde_json::Value::String(
|
||||
hook.get_pull_base_sha()?.into_owned(),
|
||||
)),
|
||||
});
|
||||
param_list.push(PipelineParam {
|
||||
name: Some("PULL_BASE_REF".to_owned()),
|
||||
value: Some(serde_json::Value::String(
|
||||
hook.get_pull_base_ref()?.into_owned(),
|
||||
)),
|
||||
});
|
||||
}
|
||||
let pp = PostParams::default();
|
||||
let created_run = jobs.create(&pp, &pipeline.pipeline).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
16
src/main.rs
16
src/main.rs
@@ -22,7 +22,9 @@ use self::crd_pipeline_run::PipelineRun;
|
||||
use self::discovery::discover_matching_push_triggers;
|
||||
use self::discovery::discover_webhook_bridge_config;
|
||||
use self::gitea_client::GiteaClient;
|
||||
use self::hook_push::HookPush;
|
||||
use self::in_repo_config::InRepoConfig;
|
||||
use self::kubernetes::run_pipelines;
|
||||
use self::webhook::hook;
|
||||
use self::webhook::verify_signature;
|
||||
use kube::CustomResourceExt;
|
||||
@@ -32,9 +34,10 @@ mod discovery;
|
||||
mod gitea_client;
|
||||
mod hook_push;
|
||||
mod in_repo_config;
|
||||
mod kubernetes;
|
||||
mod webhook;
|
||||
|
||||
const EXAMPLE_PIPELINE_RUN: &'static str = include_str!("../example_pipeline_run.json");
|
||||
const EXAMPLE_WEBHOOK_PAYLOAD: &'static str = include_str!("../example_webhook_payload.json");
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
@@ -59,7 +62,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.get_tree(
|
||||
"talexander",
|
||||
"webhook_bridge",
|
||||
"6d3b9e9db82d7857baa114d89efcb1bf470f448d",
|
||||
"b8444344c4821e87a894cd195f8bec39cd501f68",
|
||||
)
|
||||
.await?;
|
||||
let in_repo_config = discover_webhook_bridge_config(&gitea, &repo_tree).await?;
|
||||
@@ -67,14 +70,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
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::default_namespaced(kubernetes_client);
|
||||
// tracing::info!("Using crd: {}", serde_json::to_string(&PipelineRun::crd())?);
|
||||
let webhook_payload: HookPush = serde_json::from_str(EXAMPLE_WEBHOOK_PAYLOAD)?;
|
||||
|
||||
// let test_run: PipelineRun = serde_json::from_str(EXAMPLE_PIPELINE_RUN)?;
|
||||
|
||||
// let pp = PostParams::default();
|
||||
// let created_run = jobs.create(&pp, &test_run).await?;
|
||||
run_pipelines(webhook_payload, pipelines, kubernetes_client).await?;
|
||||
|
||||
// let app = Router::new()
|
||||
// .route("/hook", post(hook))
|
||||
|
||||
Reference in New Issue
Block a user