gitea-webhook-builds-sr-ht/src/main.rs

94 lines
3.3 KiB
Rust

#![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 {}