fix: make it work
This commit is contained in:
parent
e83362b7ee
commit
c36c6b2b9a
62
src/main.rs
62
src/main.rs
|
@ -2,13 +2,14 @@
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
use reqwest::StatusCode;
|
||||||
use warp::{Filter, Reply};
|
use warp::{Filter, Reply};
|
||||||
use warp::filters::BoxedFilter;
|
use warp::filters::BoxedFilter;
|
||||||
use crate::model::{
|
use crate::model::{
|
||||||
config::Config,
|
config::Config,
|
||||||
gitea::GiteaWebhook,
|
gitea::GiteaWebhook,
|
||||||
};
|
};
|
||||||
use crate::model::sourcehut::{BuildManifest, SubmitBuildPayload};
|
use crate::model::sourcehut::{BuildManifest, SubmitBuildPayload, SubmitBuildResponse};
|
||||||
|
|
||||||
pub mod model;
|
pub mod model;
|
||||||
|
|
||||||
|
@ -38,7 +39,7 @@ fn receive_webhook(config: Arc<Config>) -> BoxedFilter<(impl Reply, )> {
|
||||||
let config = Arc::clone(&config);
|
let config = Arc::clone(&config);
|
||||||
|
|
||||||
async move {
|
async move {
|
||||||
let res: Result<()> = try {
|
let res: Result<String> = try {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
|
|
||||||
// download the manifest
|
// download the manifest
|
||||||
|
@ -50,37 +51,42 @@ fn receive_webhook(config: Arc<Config>) -> BoxedFilter<(impl Reply, )> {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.clear()
|
.clear()
|
||||||
.extend(old_segments);
|
.extend(old_segments);
|
||||||
println!("{}", manifest_url);
|
let manifest_resp = client.get(manifest_url).send().await?;
|
||||||
let manifest_text = client.get(manifest_url).send().await?.text().await?;
|
if manifest_resp.status() != StatusCode::OK {
|
||||||
println!("{}", manifest_text);
|
"no manifest".to_string()
|
||||||
|
} else {
|
||||||
|
let manifest_text = manifest_resp.text().await?;
|
||||||
|
|
||||||
// deserialize the manifest
|
// deserialize the manifest
|
||||||
let mut manifest: BuildManifest = serde_yaml::from_str(&manifest_text)?;
|
let mut manifest: BuildManifest = serde_yaml::from_str(&manifest_text)?;
|
||||||
|
|
||||||
// add the source url
|
// add the source url
|
||||||
let source_url = format!("{}#{}", webhook.repository.clone_url, webhook.head_commit.id);
|
let source_url = format!("{}#{}", webhook.repository.clone_url, webhook.head_commit.id);
|
||||||
manifest.sources = Some(vec![source_url]);
|
manifest.sources = Some(vec![source_url]);
|
||||||
|
|
||||||
// submit the build
|
// submit the build
|
||||||
let payload = SubmitBuildPayload {
|
let payload = SubmitBuildPayload {
|
||||||
manifest: serde_yaml::to_string(&manifest)?,
|
manifest: serde_yaml::to_string(&manifest)?,
|
||||||
note: Some("Submitted via Gitea webhook".to_string()),
|
note: Some(format!("Submitted via Gitea webhook\n{}", webhook.head_commit.url)),
|
||||||
tags: None,
|
tags: None,
|
||||||
execute: None,
|
execute: None,
|
||||||
secrets: None,
|
secrets: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
client.post("https://builds.sr.ht/api/jobs")
|
let resp = client.post("https://builds.sr.ht/api/jobs")
|
||||||
.header("Authorization", format!("Bearer {}", config.sourcehut.personal_access_token))
|
.header("Authorization", format!("Bearer {}", config.sourcehut.personal_access_token))
|
||||||
.json(&payload)
|
.json(&payload)
|
||||||
.send()
|
.send()
|
||||||
.await?;
|
.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_err(|e| warp::reject::custom(Error(e)))
|
||||||
res
|
|
||||||
.map(|()| warp::reply())
|
|
||||||
.map_err(|e| warp::reject::custom(Error(e)))
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -13,8 +13,24 @@ pub struct BuildManifest {
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct SubmitBuildPayload {
|
pub struct SubmitBuildPayload {
|
||||||
pub manifest: String,
|
pub manifest: String,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub note: Option<String>,
|
pub note: Option<String>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub tags: Option<Vec<String>>,
|
pub tags: Option<Vec<String>>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub execute: Option<bool>,
|
pub execute: Option<bool>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub secrets: Option<bool>,
|
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,
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue