a lot more to do

This commit is contained in:
Christoph J. Scherr 2024-02-24 14:10:44 +01:00
parent bd04eea9cc
commit d02830b656
Signed by: PlexSheep
GPG Key ID: 7CDD0B14851A08EF
8 changed files with 92 additions and 29 deletions

View File

@ -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"

View File

@ -61,7 +61,7 @@ pub enum Pass {
}
impl Pass {
/// Get the pass, extracting from the underlying source
fn get_pass(&self) -> Result<String> {
pub fn get_pass(&self) -> Result<String> {
self.check()?;
Ok(match self {
Self::Text(pass) => pass.clone(),

View File

@ -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)]

View File

@ -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<Self> {
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<Self> {
todo!()
}
}

View File

@ -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<Self> {
todo!()
pub async fn build(api: &Api) -> Result<Self> {
Ok(Self { cfg: api.clone() })
}
}

View File

@ -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<Self> {
todo!()
pub async fn build(api: &Api) -> Result<Self> {
Ok(Self { cfg: api.clone() })
}
}

View File

@ -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<Self> {
todo!()
pub async fn build(api: &Api) -> Result<Self> {
Ok(Self { cfg: api.clone() })
}
}

View File

@ -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<Box<dyn ServerApi>>;
// 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<ApiCollection> {
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?));
}
}
}