From d02830b6560be3af798baea2de8691fcd72042b5 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sat, 24 Feb 2024 14:10:44 +0100 Subject: [PATCH] a lot more to do --- Cargo.toml | 1 + src/config/mod.rs | 2 +- src/error.rs | 12 ++++++++ src/serverapi/forgejo.rs | 62 ++++++++++++++++++++++++++++++++-------- src/serverapi/gitea.rs | 10 ++++--- src/serverapi/github.rs | 10 ++++--- src/serverapi/gitlab.rs | 10 ++++--- src/serverapi/mod.rs | 14 ++++++--- 8 files changed, 92 insertions(+), 29 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 23c7817..2cc02f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ clap = { version = "4.4.18", features = ["derive", "help"] } clap-verbosity-flag = "2.1.2" git2 = "0.18.1" libpt = { version = "0.3.11", features = ["log"] } +reqwest = "0.11.24" serde = { version = "1.0.195", features = ["derive"] } serde_yaml = "0.9.30" tempfile = "3.9.0" diff --git a/src/config/mod.rs b/src/config/mod.rs index 0e5e7b9..fce23ac 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -61,7 +61,7 @@ pub enum Pass { } impl Pass { /// Get the pass, extracting from the underlying source - fn get_pass(&self) -> Result { + pub fn get_pass(&self) -> Result { self.check()?; Ok(match self { Self::Text(pass) => pass.clone(), diff --git a/src/error.rs b/src/error.rs index e0d5b06..a80473f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -20,6 +20,18 @@ pub enum Error { SerdeYaml(#[from] serde_yaml::Error), #[error("Could not generate the changelog")] ChangelogError(#[from] ChangelogError), + #[error("Server Api error")] + ServerApiError(#[from] ServerApiError) +} + +#[derive(Error, Debug)] +pub enum ServerApiError { + #[error(transparent)] + ParseUrl(#[from] url::ParseError), + #[error(transparent)] + InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue), + #[error(transparent)] + ReqwestErr(#[from] reqwest::Error) } #[derive(Error, Debug)] diff --git a/src/serverapi/forgejo.rs b/src/serverapi/forgejo.rs index 011eeba..2c27327 100644 --- a/src/serverapi/forgejo.rs +++ b/src/serverapi/forgejo.rs @@ -1,11 +1,39 @@ -use async_trait::async_trait; - use super::ServerApi; use crate::{ - config::{packages::PackageType, ApiType, Config}, + config::{packages::PackageType, Api, ApiType, Config}, error::*, }; -pub struct Forgejo; +use async_trait::async_trait; +use reqwest::{ + header::{HeaderMap, HeaderValue}, Client, Method, Request, RequestBuilder, Url +}; + +pub struct Forgejo { + cfg: Api, + client: Client, +} + +impl Forgejo { + pub async fn build(api: &Api) -> Result { + let mut headers: HeaderMap = HeaderMap::new(); + // may be left empty if we only do reads from publically accessible urls + if api.auth.is_some() { + let _ = headers.insert( + "Authorization", + HeaderValue::from_str(api.auth.clone().unwrap().pass.get_pass()?.as_str()) + .map_err(ServerApiError::from)?, + ); + } + let client = super::client_builder() + .default_headers(headers) + .build() + .map_err(ServerApiError::from)?; + Ok(Self { + cfg: api.clone(), + client, + }) + } +} #[async_trait] impl ServerApi for Forgejo { @@ -13,7 +41,24 @@ impl ServerApi for Forgejo { todo!() } async fn push_release(&mut self) -> Result<()> { - todo!() + let raw_url = format!( + "{}/api/v1/repos/{user}/{repository}/releases", + self.cfg.endpoint + ); + let body = format!(r#" + {{ + "body": "{text}", + "draft": {draft}, + "name": "{name}", + "prerelease": {prerelease}, + "tag_name": "{tag}", + "target_commitish": "{commit_sig}" + }} + "#); + + let request = self.client.post().body(body).build()?; + let response = self.client.execute(request).await?; + Ok(()) } async fn push_release_artifact(&mut self) -> Result<()> { todo!() @@ -21,11 +66,4 @@ impl ServerApi for Forgejo { async fn push_pkg(&mut self, pkg_type: PackageType) -> Result<()> { todo!() } - -} - -impl Forgejo { - pub async fn build(cfg: &Config) -> Result { - todo!() - } } diff --git a/src/serverapi/gitea.rs b/src/serverapi/gitea.rs index 425fd74..44426e6 100644 --- a/src/serverapi/gitea.rs +++ b/src/serverapi/gitea.rs @@ -2,10 +2,12 @@ use async_trait::async_trait; use super::ServerApi; use crate::{ - config::{packages::PackageType, ApiType, Config}, + config::{packages::PackageType, Api, ApiType, Config}, error::*, }; -pub struct Gitea; +pub struct Gitea { + cfg: Api, +} #[async_trait] impl ServerApi for Gitea { @@ -24,7 +26,7 @@ impl ServerApi for Gitea { } impl Gitea { - pub async fn build(cfg: &Config) -> Result { - todo!() + pub async fn build(api: &Api) -> Result { + Ok(Self { cfg: api.clone() }) } } diff --git a/src/serverapi/github.rs b/src/serverapi/github.rs index 1456dff..a65f0fd 100644 --- a/src/serverapi/github.rs +++ b/src/serverapi/github.rs @@ -2,10 +2,12 @@ use async_trait::async_trait; use super::ServerApi; use crate::{ - config::{packages::PackageType, ApiType, Config}, + config::{packages::PackageType, Api, ApiType, Config}, error::*, }; -pub struct Github; +pub struct Github { + cfg: Api, +} #[async_trait] impl ServerApi for Github { @@ -24,7 +26,7 @@ impl ServerApi for Github { } impl Github { - pub async fn build(cfg: &Config) -> Result { - todo!() + pub async fn build(api: &Api) -> Result { + Ok(Self { cfg: api.clone() }) } } diff --git a/src/serverapi/gitlab.rs b/src/serverapi/gitlab.rs index fc330bd..be49f36 100644 --- a/src/serverapi/gitlab.rs +++ b/src/serverapi/gitlab.rs @@ -2,10 +2,12 @@ use async_trait::async_trait; use super::ServerApi; use crate::{ - config::{packages::PackageType, ApiType, Config}, + config::{packages::PackageType, Api, ApiType, Config}, error::*, }; -pub struct Gitlab; +pub struct Gitlab { + cfg: Api, +} #[async_trait] impl ServerApi for Gitlab { @@ -24,7 +26,7 @@ impl ServerApi for Gitlab { } impl Gitlab { - pub async fn build(cfg: &Config) -> Result { - todo!() + pub async fn build(api: &Api) -> Result { + Ok(Self { cfg: api.clone() }) } } diff --git a/src/serverapi/mod.rs b/src/serverapi/mod.rs index 3fb5bb0..15838f1 100644 --- a/src/serverapi/mod.rs +++ b/src/serverapi/mod.rs @@ -1,4 +1,5 @@ use async_trait::async_trait; +use reqwest::ClientBuilder; use crate::{ config::{packages::PackageType, ApiType, Config}, @@ -14,6 +15,7 @@ use gitea::*; use github::*; use gitlab::*; +pub static USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),); pub type ApiCollection = Vec>; // NOTE: in stable rust, traits can normally not contain async methods, @@ -27,21 +29,25 @@ pub trait ServerApi { async fn push_pkg(&mut self, pkg_type: PackageType) -> Result<()>; } +pub fn client_builder() -> ClientBuilder { + ClientBuilder::new().user_agent(USER_AGENT) +} + pub async fn init_servers(cfg: &Config) -> Result { let mut collection: ApiCollection = ApiCollection::new(); for api in &cfg.yaml.api { match api.1.server_type { ApiType::Gitea => { - collection.push(Box::new(Gitea::build(cfg).await?)); + collection.push(Box::new(Gitea::build(api.1).await?)); } ApiType::Gitlab => { - collection.push(Box::new(Gitlab::build(cfg).await?)); + collection.push(Box::new(Gitlab::build(api.1).await?)); } ApiType::Github => { - collection.push(Box::new(Github::build(cfg).await?)); + collection.push(Box::new(Github::build(api.1).await?)); } ApiType::Forgejo => { - collection.push(Box::new(Forgejo::build(cfg).await?)); + collection.push(Box::new(Forgejo::build(api.1).await?)); } } }