chore: initial commit
This commit is contained in:
commit
e83362b7ee
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
/config.toml
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,18 @@
|
|||
[package]
|
||||
name = "gitea-webhook-builds-sr-ht"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1"
|
||||
reqwest = { version = "0.11", features = ["json"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
serde_yaml = "0.8"
|
||||
serde-value = "0.7"
|
||||
toml = "0.5"
|
||||
tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "macros", "fs"] }
|
||||
url = "2"
|
||||
warp = "0.3"
|
|
@ -0,0 +1,93 @@
|
|||
#![feature(try_blocks)]
|
||||
|
||||
use std::sync::Arc;
|
||||
use anyhow::{Context, Result};
|
||||
use warp::{Filter, Reply};
|
||||
use warp::filters::BoxedFilter;
|
||||
use crate::model::{
|
||||
config::Config,
|
||||
gitea::GiteaWebhook,
|
||||
};
|
||||
use crate::model::sourcehut::{BuildManifest, SubmitBuildPayload};
|
||||
|
||||
pub mod model;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
let config_text = tokio::fs::read_to_string("config.toml")
|
||||
.await
|
||||
.context("could not read config")?;
|
||||
let config: Config = toml::from_str(&config_text).context("could not deserialise config")?;
|
||||
let config = Arc::new(config);
|
||||
|
||||
warp::serve(receive_webhook(Arc::clone(&config)))
|
||||
.run(([0, 0, 0, 0], 12645))
|
||||
.await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn receive_webhook(config: Arc<Config>) -> BoxedFilter<(impl Reply, )> {
|
||||
let path = config.web.path.clone();
|
||||
|
||||
let route = warp::path("receive")
|
||||
.and(warp::path(path))
|
||||
.and(warp::path::end())
|
||||
.and(warp::body::json())
|
||||
.and_then(move |webhook: GiteaWebhook| {
|
||||
let config = Arc::clone(&config);
|
||||
|
||||
async move {
|
||||
let res: Result<()> = try {
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
// download the manifest
|
||||
let mut manifest_url = url::Url::parse(&webhook.head_commit.url)?;
|
||||
let mut old_segments = manifest_url.path_segments().unwrap().map(ToString::to_string).collect::<Vec<_>>();
|
||||
old_segments.insert(old_segments.len() - 2, "raw".to_string());
|
||||
old_segments.push(".build.yml".to_string());
|
||||
manifest_url.path_segments_mut()
|
||||
.unwrap()
|
||||
.clear()
|
||||
.extend(old_segments);
|
||||
println!("{}", manifest_url);
|
||||
let manifest_text = client.get(manifest_url).send().await?.text().await?;
|
||||
println!("{}", manifest_text);
|
||||
|
||||
// deserialize the manifest
|
||||
let mut manifest: BuildManifest = serde_yaml::from_str(&manifest_text)?;
|
||||
|
||||
// add the source url
|
||||
let source_url = format!("{}#{}", webhook.repository.clone_url, webhook.head_commit.id);
|
||||
manifest.sources = Some(vec![source_url]);
|
||||
|
||||
// submit the build
|
||||
let payload = SubmitBuildPayload {
|
||||
manifest: serde_yaml::to_string(&manifest)?,
|
||||
note: Some("Submitted via Gitea webhook".to_string()),
|
||||
tags: None,
|
||||
execute: None,
|
||||
secrets: None,
|
||||
};
|
||||
|
||||
client.post("https://builds.sr.ht/api/jobs")
|
||||
.header("Authorization", format!("Bearer {}", config.sourcehut.personal_access_token))
|
||||
.json(&payload)
|
||||
.send()
|
||||
.await?;
|
||||
};
|
||||
// reqwest::post("https://builds.sr.ht/api/jobs")
|
||||
// .json(&payload);
|
||||
res
|
||||
.map(|()| warp::reply())
|
||||
.map_err(|e| warp::reject::custom(Error(e)))
|
||||
}
|
||||
});
|
||||
|
||||
warp::post().and(route).boxed()
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Error(anyhow::Error);
|
||||
|
||||
impl warp::reject::Reject for Error {}
|
|
@ -0,0 +1,3 @@
|
|||
pub mod config;
|
||||
pub mod gitea;
|
||||
pub mod sourcehut;
|
|
@ -0,0 +1,17 @@
|
|||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
pub web: Web,
|
||||
pub sourcehut: Sourcehut,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Web {
|
||||
pub path: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Sourcehut {
|
||||
pub personal_access_token: String,
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct GiteaWebhook {
|
||||
#[serde(rename = "ref")]
|
||||
pub ref_: String,
|
||||
pub before: String,
|
||||
pub after: String,
|
||||
pub compare_url: String,
|
||||
pub commits: Vec<GiteaCommit>,
|
||||
pub head_commit: GiteaCommit,
|
||||
pub repository: GiteaRepository,
|
||||
pub pusher: GiteaUser,
|
||||
pub sender: GiteaUser,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct GiteaCommit {
|
||||
pub id: String,
|
||||
pub message: String,
|
||||
pub url: String,
|
||||
pub author: GitUser,
|
||||
pub committer: GitUser,
|
||||
pub timestamp: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct GitUser {
|
||||
pub name: String,
|
||||
pub email: String,
|
||||
pub username: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct GiteaRepository {
|
||||
pub id: u32,
|
||||
pub owner: GiteaUser,
|
||||
pub name: String,
|
||||
pub full_name: String,
|
||||
pub description: String,
|
||||
pub private: bool,
|
||||
pub fork: bool,
|
||||
pub html_url: String,
|
||||
pub ssh_url: String,
|
||||
pub clone_url: String,
|
||||
pub website: String,
|
||||
pub stars_count: u32,
|
||||
pub forks_count: u32,
|
||||
pub watchers_count: u32,
|
||||
pub open_issues_count: u32,
|
||||
pub default_branch: String,
|
||||
pub created_at: String,
|
||||
pub updated_at: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct GiteaUser {
|
||||
pub id: u32,
|
||||
pub login: String,
|
||||
pub full_name: String,
|
||||
pub email: String,
|
||||
pub avatar_url: String,
|
||||
pub username: String,
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
use std::collections::HashMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_value::Value;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct BuildManifest {
|
||||
#[serde(default)]
|
||||
pub sources: Option<Vec<String>>,
|
||||
#[serde(flatten)]
|
||||
pub other: HashMap<String, Value>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct SubmitBuildPayload {
|
||||
pub manifest: String,
|
||||
pub note: Option<String>,
|
||||
pub tags: Option<Vec<String>>,
|
||||
pub execute: Option<bool>,
|
||||
pub secrets: Option<bool>,
|
||||
}
|
Loading…
Reference in New Issue