Add Makefile for running CI jobs locally.

This commit is contained in:
Tom Alexander 2024-09-28 22:32:20 -04:00
parent efe37f020a
commit c20927b726
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
9 changed files with 85 additions and 69 deletions

2
.dockerignore Normal file
View File

@ -0,0 +1,2 @@
**/.git
target/

35
Makefile Normal file
View File

@ -0,0 +1,35 @@
SHELL := bash
.ONESHELL:
.SHELLFLAGS := -eu -o pipefail -c
.DELETE_ON_ERROR:
MAKEFLAGS += --warn-undefined-variables
MAKEFLAGS += --no-builtin-rules
OS:=$(shell uname -s)
ifeq ($(origin .RECIPEPREFIX), undefined)
$(error This Make does not support .RECIPEPREFIX. Please use GNU Make 4.0 or later)
endif
.RECIPEPREFIX = >
.PHONY: help
help:
> @grep -h "##" $(MAKEFILE_LIST) | grep -v grep | sed -E 's/^([^:]*): *## */\1: /'
.PHONY: test
test: ## Run the rust tests
> $(MAKE) -C docker/webhook_bridge_development build
> docker run --rm -i -t --mount type=tmpfs,destination=/tmp -v "$(shell readlink -f .):/source" --workdir=/source --env CARGO_TARGET_DIR=/target -v "webhook-bridge-cargo-registry:/usr/local/cargo/registry" -v "webhook-bridge-rust-target:/target" webhook-bridge-development cargo test
.PHONY: clippy
clippy: ## Run static analysis of the code.
> $(MAKE) -C docker/webhook_bridge_development build
> docker run --rm -i -t --mount type=tmpfs,destination=/tmp -v "$(shell readlink -f .):/source" --workdir=/source --env CARGO_TARGET_DIR=/target -v "webhook-bridge-cargo-registry:/usr/local/cargo/registry" -v "webhook-bridge-rust-target:/target" webhook-bridge-development cargo clippy --no-deps --all-targets --all-features -- -D warnings
.PHONY: format
format: ## Auto-format source files.
> $(MAKE) -C docker/webhook_bridge_development build
> docker run --rm -i -t --mount type=tmpfs,destination=/tmp -v "$(shell readlink -f .):/source" --workdir=/source --env CARGO_TARGET_DIR=/target -v "webhook-bridge-cargo-registry:/usr/local/cargo/registry" -v "webhook-bridge-rust-target:/target" webhook-bridge-development cargo fmt
.PHONY: clean
clean:
> $(MAKE) -C docker/webhook_bridge_development clean

View File

@ -1,5 +1,5 @@
FROM rustlang/rust:nightly-alpine3.20 AS builder
#openssl-dev
RUN apk add --no-cache musl-dev pkgconfig libressl3.8-libssl libressl-dev
RUN cargo install --locked --no-default-features --features ci-autoclean cargo-cache
RUN rustup component add rustfmt

View File

@ -21,6 +21,7 @@ help:
build: ## Build the docker image.
> docker build --tag $(IMAGE_NAME) --target=$(TARGET) --file Dockerfile .
> docker volume create webhook-bridge-cargo-registry
> docker volume create webhook-bridge-rust-target
.PHONY: shell
shell: ## Launch an interactive shell inside the docker image with the source repository mounted at /source.
@ -31,3 +32,4 @@ shell: build
clean:
> docker rmi $(IMAGE_NAME)
> docker volume rm webhook-bridge-cargo-registry
> docker volume rm webhook-bridge-rust-target

View File

@ -1,13 +1,13 @@
{
"ref": "refs/heads/main",
"before": "b8444344c4821e87a894cd195f8bec39cd501f68",
"after": "470031251cc4f7d70227140f7edde09b53c372c4",
"compare_url": "https://code.fizz.buzz/talexander/webhook_bridge/compare/b8444344c4821e87a894cd195f8bec39cd501f68...470031251cc4f7d70227140f7edde09b53c372c4",
"before": "d3b6cb93897a8f807ee4e31de3a1a231416f8f5e",
"after": "efe37f020a949de2b5258593641dcc4b22273a66",
"compare_url": "https://code.fizz.buzz/talexander/webhook_bridge/compare/d3b6cb93897a8f807ee4e31de3a1a231416f8f5e...efe37f020a949de2b5258593641dcc4b22273a66",
"commits": [
{
"id": "470031251cc4f7d70227140f7edde09b53c372c4",
"message": "Remove prettier.\n",
"url": "https://code.fizz.buzz/talexander/webhook_bridge/commit/470031251cc4f7d70227140f7edde09b53c372c4",
"id": "efe37f020a949de2b5258593641dcc4b22273a66",
"message": "Generate names for pipeline runs.\n",
"url": "https://code.fizz.buzz/talexander/webhook_bridge/commit/efe37f020a949de2b5258593641dcc4b22273a66",
"author": {
"name": "Tom Alexander",
"email": "tom@fizz.buzz",
@ -19,52 +19,22 @@
"username": ""
},
"verification": null,
"timestamp": "2024-09-28T21:46:25-04:00",
"timestamp": "2024-09-28T21:50:50-04:00",
"added": [],
"removed": [],
"modified": [
".webhook_bridge/pipeline-format.yaml",
"docker/webhook_bridge_development/Dockerfile"
]
},
{
"id": "ed1e1c08d08eb271fa7af35c22b4c9bfbc0d6224",
"message": "Create PipelineRun in response to webhook triggers.\n",
"url": "https://code.fizz.buzz/talexander/webhook_bridge/commit/ed1e1c08d08eb271fa7af35c22b4c9bfbc0d6224",
"author": {
"name": "Tom Alexander",
"email": "tom@fizz.buzz",
"username": ""
},
"committer": {
"name": "Tom Alexander",
"email": "tom@fizz.buzz",
"username": ""
},
"verification": null,
"timestamp": "2024-09-28T19:43:26-04:00",
"added": [
"docker/webhook_bridge_development/Dockerfile",
"example_webhook_payload.json",
"src/kubernetes.rs"
],
"removed": [
"example_pipeline_run.json",
"example_pipeline_run.yaml"
],
"modified": [
"src/crd_pipeline_run.rs",
"src/gitea_client/mod.rs",
"src/hook_push.rs",
"src/in_repo_config.rs",
"src/kubernetes.rs",
"src/main.rs"
]
}
],
"total_commits": 2,
"total_commits": 1,
"head_commit": {
"id": "470031251cc4f7d70227140f7edde09b53c372c4",
"message": "Remove prettier.\n",
"url": "https://code.fizz.buzz/talexander/webhook_bridge/commit/470031251cc4f7d70227140f7edde09b53c372c4",
"id": "efe37f020a949de2b5258593641dcc4b22273a66",
"message": "Generate names for pipeline runs.\n",
"url": "https://code.fizz.buzz/talexander/webhook_bridge/commit/efe37f020a949de2b5258593641dcc4b22273a66",
"author": {
"name": "Tom Alexander",
"email": "tom@fizz.buzz",
@ -76,12 +46,14 @@
"username": ""
},
"verification": null,
"timestamp": "2024-09-28T21:46:25-04:00",
"timestamp": "2024-09-28T21:50:50-04:00",
"added": [],
"removed": [],
"modified": [
".webhook_bridge/pipeline-format.yaml",
"docker/webhook_bridge_development/Dockerfile"
"docker/webhook_bridge_development/Dockerfile",
"example_webhook_payload.json",
"src/kubernetes.rs",
"src/main.rs"
]
},
"repository": {
@ -118,7 +90,7 @@
"template": false,
"parent": null,
"mirror": false,
"size": 293,
"size": 315,
"language": "",
"languages_url": "https://code.fizz.buzz/api/v1/repos/talexander/webhook_bridge/languages",
"html_url": "https://code.fizz.buzz/talexander/webhook_bridge",
@ -137,7 +109,7 @@
"default_branch": "main",
"archived": false,
"created_at": "2024-07-14T18:48:52Z",
"updated_at": "2024-09-29T00:34:06Z",
"updated_at": "2024-09-29T02:41:59Z",
"archived_at": "1970-01-01T00:00:00Z",
"permissions": {
"admin": true,

View File

@ -5,33 +5,33 @@ use crate::crd_pipeline_run::PipelineRun;
use crate::gitea_client::GiteaClient;
use crate::gitea_client::Tree;
use crate::gitea_client::TreeFileReference;
use crate::in_repo_config::InRepoConfig;
use crate::remote_config::RemoteConfig;
use regex::Regex;
use tracing::debug;
pub(crate) async fn discover_webhook_bridge_config(
gitea: &GiteaClient,
repo_tree: &Tree,
) -> Result<InRepoConfig, Box<dyn std::error::Error>> {
let in_repo_config_reference = repo_tree
) -> Result<RemoteConfig, Box<dyn std::error::Error>> {
let remote_config_reference = repo_tree
.files
.iter()
.filter(|file_reference| file_reference.path == ".webhook_bridge/webhook_bridge.toml")
.next()
.ok_or("File not found in remote repo: .webhook_bridge/webhook_bridge.toml.")?;
let in_repo_config_contents =
String::from_utf8(gitea.read_file(in_repo_config_reference).await?)?;
let parsed_in_repo_config = InRepoConfig::from_str(in_repo_config_contents)?;
let remote_config_contents =
String::from_utf8(gitea.read_file(remote_config_reference).await?)?;
let parsed_remote_config = RemoteConfig::from_str(remote_config_contents)?;
Ok(parsed_in_repo_config)
Ok(parsed_remote_config)
}
pub(crate) async fn discover_matching_push_triggers<RE: AsRef<str>>(
gitea: &GiteaClient,
repo_tree: &Tree,
git_ref: RE,
in_repo_config: &InRepoConfig,
remote_config: &RemoteConfig,
) -> 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>.+)")?;
@ -41,7 +41,7 @@ pub(crate) async fn discover_matching_push_triggers<RE: AsRef<str>>(
let branch = &captures["branch"];
debug!("Detected branch from push as {:?}", branch);
let push_triggers = in_repo_config.get_push_triggers_for_branch(branch)?;
let push_triggers = remote_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));
let pipeline_template = repo_tree

View File

@ -24,7 +24,12 @@ pub(crate) async fn run_pipelines(
tracing::debug!("Using crd: {}", serde_json::to_string(&PipelineRun::crd())?);
for mut pipeline in pipelines {
debug!("Kicking off {}", pipeline.name);
info!(
"Kicking off {} for repo {}/{}",
pipeline.name,
hook.get_repo_owner()?,
hook.get_repo_name()?,
);
if pipeline.pipeline.spec.params.is_none() {
pipeline.pipeline.spec.params = Some(Vec::new());
}

View File

@ -25,8 +25,8 @@ use self::discovery::discover_webhook_bridge_config;
use self::gitea_client::GiteaClient;
use self::hook_push::HookPush;
use self::hook_push::PipelineParamters;
use self::in_repo_config::InRepoConfig;
use self::kubernetes::run_pipelines;
use self::remote_config::RemoteConfig;
use self::webhook::hook;
use self::webhook::verify_signature;
use kube::CustomResourceExt;
@ -35,8 +35,8 @@ mod crd_pipeline_run;
mod discovery;
mod gitea_client;
mod hook_push;
mod in_repo_config;
mod kubernetes;
mod remote_config;
mod webhook;
const EXAMPLE_WEBHOOK_PAYLOAD: &'static str = include_str!("../example_webhook_payload.json");
@ -68,9 +68,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
webhook_payload.get_pull_base_sha()?,
)
.await?;
let in_repo_config = discover_webhook_bridge_config(&gitea, &repo_tree).await?;
let remote_config = discover_webhook_bridge_config(&gitea, &repo_tree).await?;
let pipelines =
discover_matching_push_triggers(&gitea, &repo_tree, "refs/heads/main", &in_repo_config)
discover_matching_push_triggers(&gitea, &repo_tree, "refs/heads/main", &remote_config)
.await?;
run_pipelines(webhook_payload, pipelines, kubernetes_client).await?;

View File

@ -5,7 +5,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) struct RemoteConfig {
pub(crate) version: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
@ -29,16 +29,16 @@ pub(crate) struct TriggerPush {
pub(crate) skip_branches: Vec<String>,
}
impl InRepoConfig {
impl RemoteConfig {
pub(crate) fn from_str<S: AsRef<str>>(
contents: S,
) -> Result<InRepoConfig, Box<dyn std::error::Error>> {
let parsed_in_repo_config: InRepoConfig = toml::from_str(contents.as_ref())?;
) -> Result<RemoteConfig, Box<dyn std::error::Error>> {
let parsed_remote_config: RemoteConfig = toml::from_str(contents.as_ref())?;
assert!(
parsed_in_repo_config.version == "0.0.1",
parsed_remote_config.version == "0.0.1",
"We only support version 0.0.1 currently."
);
Ok(parsed_in_repo_config)
Ok(parsed_remote_config)
}
pub(crate) fn get_push_triggers_for_branch<B: AsRef<str>>(