fix: make it work

This commit is contained in:
Anna 2021-11-30 19:35:41 -05:00
parent e83362b7ee
commit c36c6b2b9a
Signed by: anna
GPG Key ID: 0B391D8F06FCD9E0
2 changed files with 50 additions and 28 deletions

View File

@ -2,13 +2,14 @@
use std::sync::Arc;
use anyhow::{Context, Result};
use reqwest::StatusCode;
use warp::{Filter, Reply};
use warp::filters::BoxedFilter;
use crate::model::{
config::Config,
gitea::GiteaWebhook,
};
use crate::model::sourcehut::{BuildManifest, SubmitBuildPayload};
use crate::model::sourcehut::{BuildManifest, SubmitBuildPayload, SubmitBuildResponse};
pub mod model;
@ -38,7 +39,7 @@ fn receive_webhook(config: Arc<Config>) -> BoxedFilter<(impl Reply, )> {
let config = Arc::clone(&config);
async move {
let res: Result<()> = try {
let res: Result<String> = try {
let client = reqwest::Client::new();
// download the manifest
@ -50,37 +51,42 @@ fn receive_webhook(config: Arc<Config>) -> BoxedFilter<(impl Reply, )> {
.unwrap()
.clear()
.extend(old_segments);
println!("{}", manifest_url);
let manifest_text = client.get(manifest_url).send().await?.text().await?;
println!("{}", manifest_text);
let manifest_resp = client.get(manifest_url).send().await?;
if manifest_resp.status() != StatusCode::OK {
"no manifest".to_string()
} else {
let manifest_text = manifest_resp.text().await?;
// deserialize the manifest
let mut manifest: BuildManifest = serde_yaml::from_str(&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]);
// 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,
};
// submit the build
let payload = SubmitBuildPayload {
manifest: serde_yaml::to_string(&manifest)?,
note: Some(format!("Submitted via Gitea webhook\n{}", webhook.head_commit.url)),
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?;
let resp = client.post("https://builds.sr.ht/api/jobs")
.header("Authorization", format!("Bearer {}", config.sourcehut.personal_access_token))
.json(&payload)
.send()
.await?;
let resp: SubmitBuildResponse = resp.json().await?;
let link = format!("https://builds.sr.ht/{}/job/{}", resp.owner.canonical_name, resp.id);
println!("{}", link);
link
}
};
// reqwest::post("https://builds.sr.ht/api/jobs")
// .json(&payload);
res
.map(|()| warp::reply())
.map_err(|e| warp::reject::custom(Error(e)))
res.map_err(|e| warp::reject::custom(Error(e)))
}
});

View File

@ -13,8 +13,24 @@ pub struct BuildManifest {
#[derive(Debug, Serialize)]
pub struct SubmitBuildPayload {
pub manifest: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub note: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub execute: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub secrets: Option<bool>,
}
#[derive(Debug, Deserialize)]
pub struct SubmitBuildResponse {
pub id: u32,
pub owner: BuildOwner,
}
#[derive(Debug, Deserialize)]
pub struct BuildOwner {
pub canonical_name: String,
pub name: String,
}