Compare commits

...

4 Commits

Author SHA1 Message Date
Tom Alexander e3ee5ebf77
Support multiple types of requests. 2024-07-14 18:37:52 -04:00
Tom Alexander eb0c993e03
Full request schema. 2024-07-14 17:04:08 -04:00
Tom Alexander 14373c21dd
Initial schema for webhook. 2024-07-14 16:38:07 -04:00
Tom Alexander ab5db8aded
Add a webhook endpoint. 2024-07-14 16:13:06 -04:00
5 changed files with 451 additions and 0 deletions

1
Cargo.lock generated
View File

@ -831,6 +831,7 @@ version = "0.0.1"
dependencies = [
"axum",
"serde",
"serde_json",
"tokio",
"tower-http",
"tracing",

View File

@ -21,6 +21,8 @@ include = [
# default form, http1, json, matched-path, original-uri, query, tokio, tower-log, tracing
axum = { version = "0.7.5", default-features = false, features = ["tokio", "http1", "http2", "json"] }
serde = { version = "1.0.204", features = ["derive"] }
# default std
serde_json = { version = "1.0.120", default-features = false, features = ["std"] }
tokio = { version = "1.38.0", default-features = false, features = ["macros", "process", "rt", "rt-multi-thread"] }
tower-http = { version = "0.5.2", default-features = false, features = ["trace"] }
# default attributes, std, tracing-attributes

View File

@ -1,5 +1,7 @@
#![forbid(unsafe_code)]
use axum::http::StatusCode;
use axum::routing::get;
use axum::routing::post;
use axum::Json;
use axum::Router;
use serde::Serialize;
@ -7,6 +9,10 @@ use tower_http::trace::TraceLayer;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use self::webhook::hook;
mod webhook;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::registry()
@ -19,6 +25,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.init();
let app = Router::new()
.route("/health", get(health))
.route("/hook", post(hook))
.layer(TraceLayer::new_for_http());
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await?;

217
src/webhook.rs Normal file
View File

@ -0,0 +1,217 @@
use axum::async_trait;
use axum::extract::FromRequest;
use axum::extract::Request;
use axum::http::HeaderMap;
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::response::Response;
use axum::Json;
use axum::RequestExt;
use serde::Deserialize;
use serde::Serialize;
use serde_json::Value;
use tracing::debug;
pub(crate) async fn hook(
headers: HeaderMap,
payload: HookRequest,
) -> (StatusCode, Json<HookResponse>) {
debug!("REQ: {:?}", payload);
match payload {
HookRequest::Push(payload) => (
StatusCode::OK,
Json(HookResponse {
ok: true,
message: None,
}),
),
HookRequest::Unrecognized(payload) => (
StatusCode::BAD_REQUEST,
Json(HookResponse {
ok: false,
message: Some(format!("unrecognized input: {payload}")),
}),
),
}
}
#[derive(Debug)]
pub(crate) enum HookRequest {
Push(HookPush),
Unrecognized(String),
}
#[async_trait]
impl<S> FromRequest<S> for HookRequest
where
S: Send + Sync,
{
type Rejection = Response;
async fn from_request(req: Request, _state: &S) -> Result<Self, Self::Rejection> {
let event_type = req
.headers()
.get("X-Gitea-Event-Type")
.ok_or(StatusCode::UNSUPPORTED_MEDIA_TYPE.into_response())?;
let event_type = event_type
.to_str()
.map_err(|_| StatusCode::UNSUPPORTED_MEDIA_TYPE.into_response())?;
match event_type {
"push" => {
let Json(payload): Json<HookPush> =
req.extract().await.map_err(IntoResponse::into_response)?;
Ok(HookRequest::Push(payload))
}
_ => {
let body: String = req.extract().await.map_err(IntoResponse::into_response)?;
Ok(HookRequest::Unrecognized(body))
}
}
}
}
#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub(crate) struct HookPush {
#[serde(rename = "ref")]
ref_field: String,
before: String,
compare_url: String,
commits: Vec<HookCommit>,
total_commits: u64,
head_commit: HookCommit,
repository: HookRepository,
pusher: HookUser,
sender: HookUser,
}
#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub(crate) struct HookUser {
id: u64,
login: String,
login_name: String,
full_name: String,
email: String,
avatar_url: String,
language: String,
is_admin: bool,
last_login: String, // TODO: parse to datetime
created: String, // TODO: parse to datetime
restricted: bool,
active: bool,
prohibit_login: bool,
location: String,
website: String,
description: String,
visibility: String,
followers_count: u64,
following_count: u64,
starred_repos_count: u64,
username: String,
}
#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub(crate) struct HookRepository {
id: u64,
owner: HookUser,
name: String,
full_name: String,
description: String,
empty: bool,
private: bool,
fork: bool,
template: bool,
parent: Value, // Was null in test hook
mirror: bool,
size: u64,
language: String,
languages_url: String,
html_url: String,
url: String,
link: String,
ssh_url: String,
clone_url: String,
original_url: String,
website: String,
stars_count: u64,
forks_count: u64,
watchers_count: u64,
open_issues_count: u64,
open_pr_counter: u64,
release_counter: u64,
default_branch: String,
archived: bool,
created_at: String, // TODO: parse to datetime
updated_at: String, // TODO: parse to datetime
archived_at: String, // TODO: parse to datetime
permissions: HookRepositoryPermissions,
has_issues: bool,
internal_tracker: HookRepositoryInternalTracker,
has_wiki: bool,
has_pull_requests: bool,
has_projects: bool,
has_releases: bool,
has_packages: bool,
has_actions: bool,
ignore_whitespace_conflicts: bool,
allow_merge_commits: bool,
allow_rebase: bool,
allow_rebase_explicit: bool,
allow_squash_merge: bool,
allow_rebase_update: bool,
default_delete_branch_after_merge: bool,
default_merge_style: String,
default_allow_maintainer_edit: bool,
avatar_url: String,
internal: bool,
mirror_interval: String,
mirror_updated: String, // TODO: parse to datetime
repo_transfer: Value, // Was null in test hook
}
#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub(crate) struct HookRepositoryPermissions {
admin: bool,
push: bool,
pull: bool,
}
#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub(crate) struct HookRepositoryInternalTracker {
enable_time_tracker: bool,
allow_only_contributors_to_track_time: bool,
enable_issue_dependencies: bool,
}
#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub(crate) struct HookCommit {
id: String,
message: String,
url: String,
author: HookGitUser,
committer: HookGitUser,
verification: Value, // Was null in test hook
timestamp: String, // TODO: parse to datetime
added: Vec<String>,
removed: Vec<String>,
modified: Vec<String>,
}
#[allow(dead_code)]
#[derive(Debug, Deserialize)]
pub(crate) struct HookGitUser {
name: String,
email: String,
username: String,
}
#[derive(Debug, Serialize)]
pub(crate) struct HookResponse {
ok: bool,
message: Option<String>,
}

224
test_webhook.bash Executable file
View File

@ -0,0 +1,224 @@
#!/usr/bin/env bash
#
set -euo pipefail
IFS=$'\n\t'
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
function main() {
local payload
payload=$(cat <<EOF
{
"ref": "refs/heads/main",
"before": "25c06cbffd1cd2b372e790732bc8566e575a7f01",
"after": "8fb5a83e8672cf9c317087b70cef329cb604eeed",
"compare_url": "https://code.fizz.buzz/talexander/webhook_bridge/compare/25c06cbffd1cd2b372e790732bc8566e575a7f01...8fb5a83e8672cf9c317087b70cef329cb604eeed",
"commits": [
{
"id": "8fb5a83e8672cf9c317087b70cef329cb604eeed",
"message": "Access log.\n",
"url": "https://code.fizz.buzz/talexander/webhook_bridge/commit/8fb5a83e8672cf9c317087b70cef329cb604eeed",
"author": {
"name": "Tom Alexander",
"email": "tom@fizz.buzz",
"username": ""
},
"committer": {
"name": "Tom Alexander",
"email": "tom@fizz.buzz",
"username": ""
},
"verification": null,
"timestamp": "2024-07-14T15:50:13-04:00",
"added": [],
"removed": [],
"modified": [
"Cargo.lock",
"Cargo.toml",
"src/main.rs"
]
}
],
"total_commits": 1,
"head_commit": {
"id": "8fb5a83e8672cf9c317087b70cef329cb604eeed",
"message": "Access log.\n",
"url": "https://code.fizz.buzz/talexander/webhook_bridge/commit/8fb5a83e8672cf9c317087b70cef329cb604eeed",
"author": {
"name": "Tom Alexander",
"email": "tom@fizz.buzz",
"username": ""
},
"committer": {
"name": "Tom Alexander",
"email": "tom@fizz.buzz",
"username": ""
},
"verification": null,
"timestamp": "2024-07-14T15:50:13-04:00",
"added": [],
"removed": [],
"modified": [
"Cargo.lock",
"Cargo.toml",
"src/main.rs"
]
},
"repository": {
"id": 21,
"owner": {
"id": 1,
"login": "talexander",
"login_name": "",
"full_name": "",
"email": "gitea@local.domain",
"avatar_url": "https://code.fizz.buzz/avatars/9d402a89b5a0786f83c1b8c5486fc7ff3d083a54fe20e55c0a776a1932c30289",
"language": "",
"is_admin": false,
"last_login": "0001-01-01T00:00:00Z",
"created": "2023-07-05T22:03:28Z",
"restricted": false,
"active": false,
"prohibit_login": false,
"location": "",
"website": "",
"description": "",
"visibility": "public",
"followers_count": 0,
"following_count": 0,
"starred_repos_count": 0,
"username": "talexander"
},
"name": "webhook_bridge",
"full_name": "talexander/webhook_bridge",
"description": "A server that receives webhooks from gitea and fires off Tekton jobs in response.",
"empty": false,
"private": false,
"fork": false,
"template": false,
"parent": null,
"mirror": false,
"size": 45,
"language": "",
"languages_url": "https://code.fizz.buzz/api/v1/repos/talexander/webhook_bridge/languages",
"html_url": "https://code.fizz.buzz/talexander/webhook_bridge",
"url": "https://code.fizz.buzz/api/v1/repos/talexander/webhook_bridge",
"link": "",
"ssh_url": "git@code.fizz.buzz:talexander/webhook_bridge.git",
"clone_url": "https://code.fizz.buzz/talexander/webhook_bridge.git",
"original_url": "",
"website": "",
"stars_count": 0,
"forks_count": 0,
"watchers_count": 1,
"open_issues_count": 0,
"open_pr_counter": 0,
"release_counter": 0,
"default_branch": "main",
"archived": false,
"created_at": "2024-07-14T18:48:52Z",
"updated_at": "2024-07-14T19:43:45Z",
"archived_at": "1970-01-01T00:00:00Z",
"permissions": {
"admin": true,
"push": true,
"pull": true
},
"has_issues": true,
"internal_tracker": {
"enable_time_tracker": true,
"allow_only_contributors_to_track_time": true,
"enable_issue_dependencies": true
},
"has_wiki": true,
"has_pull_requests": true,
"has_projects": true,
"has_releases": true,
"has_packages": true,
"has_actions": false,
"ignore_whitespace_conflicts": false,
"allow_merge_commits": true,
"allow_rebase": true,
"allow_rebase_explicit": true,
"allow_squash_merge": true,
"allow_rebase_update": true,
"default_delete_branch_after_merge": false,
"default_merge_style": "merge",
"default_allow_maintainer_edit": false,
"avatar_url": "",
"internal": false,
"mirror_interval": "",
"mirror_updated": "0001-01-01T00:00:00Z",
"repo_transfer": null
},
"pusher": {
"id": 1,
"login": "talexander",
"login_name": "",
"full_name": "",
"email": "talexander@noreply.code.fizz.buzz",
"avatar_url": "https://code.fizz.buzz/avatars/9d402a89b5a0786f83c1b8c5486fc7ff3d083a54fe20e55c0a776a1932c30289",
"language": "",
"is_admin": false,
"last_login": "0001-01-01T00:00:00Z",
"created": "2023-07-05T22:03:28Z",
"restricted": false,
"active": false,
"prohibit_login": false,
"location": "",
"website": "",
"description": "",
"visibility": "public",
"followers_count": 0,
"following_count": 0,
"starred_repos_count": 0,
"username": "talexander"
},
"sender": {
"id": 1,
"login": "talexander",
"login_name": "",
"full_name": "",
"email": "talexander@noreply.code.fizz.buzz",
"avatar_url": "https://code.fizz.buzz/avatars/9d402a89b5a0786f83c1b8c5486fc7ff3d083a54fe20e55c0a776a1932c30289",
"language": "",
"is_admin": false,
"last_login": "0001-01-01T00:00:00Z",
"created": "2023-07-05T22:03:28Z",
"restricted": false,
"active": false,
"prohibit_login": false,
"location": "",
"website": "",
"description": "",
"visibility": "public",
"followers_count": 0,
"following_count": 0,
"starred_repos_count": 0,
"username": "talexander"
}
}
EOF
)
curl -v \
--http2-prior-knowledge \
-X POST \
-H 'Content-Type: application/json' \
-H 'X-GitHub-Delivery: 2187f277-1104-4011-845b-a88a5a98731f' \
-H 'X-GitHub-Event: push' \
-H 'X-GitHub-Event-Type: push' \
-H 'X-Gitea-Delivery: 2187f277-1104-4011-845b-a88a5a98731f' \
-H 'X-Gitea-Event: push' \
-H 'X-Gitea-Event-Type: push' \
-H 'X-Gitea-Signature: 5f5ca80269a5cebd41c3ef2de547b86c40b35418fbefc9f59459602435d2d9ea' \
-H 'X-Gogs-Delivery: 2187f277-1104-4011-845b-a88a5a98731f' \
-H 'X-Gogs-Event: push' \
-H 'X-Gogs-Event-Type: push' \
-H 'X-Gogs-Signature: 5f5ca80269a5cebd41c3ef2de547b86c40b35418fbefc9f59459602435d2d9ea' \
-H 'X-Hub-Signature: sha1=6e13417e9a9ce53fc08a684635350aa2c60ce7bc' \
-H 'X-Hub-Signature-256: sha256=5f5ca80269a5cebd41c3ef2de547b86c40b35418fbefc9f59459602435d2d9ea' \
-d @- \
'http://127.0.0.1:8080/hook' <<<"$payload"
}
main "${@}"