autocrate/src/serverapi/mod.rs

61 lines
1.9 KiB
Rust
Raw Normal View History

2024-02-16 20:00:55 +01:00
use async_trait::async_trait;
2024-02-24 14:10:44 +01:00
use reqwest::ClientBuilder;
2024-02-16 20:00:55 +01:00
use crate::{
2024-02-24 14:33:58 +01:00
config::{ApiType, Config},
2024-02-16 20:00:55 +01:00
error::*,
2024-02-24 14:33:58 +01:00
publish::PublishContext,
release::ReleaseContext,
2024-02-16 20:00:55 +01:00
};
2024-02-19 22:07:51 +01:00
pub mod forgejo;
2024-02-16 20:00:55 +01:00
pub mod gitea;
pub mod github;
pub mod gitlab;
2024-02-19 22:07:51 +01:00
use forgejo::*;
2024-02-16 20:00:55 +01:00
use gitea::*;
use github::*;
use gitlab::*;
2024-02-24 14:10:44 +01:00
pub static USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);
2024-02-16 20:00:55 +01:00
pub type ApiCollection = Vec<Box<dyn ServerApi>>;
// NOTE: in stable rust, traits can normally not contain async methods,
// see [here](https://stackoverflow.com/questions/65921581/how-can-i-define-an-async-method-in-a-trait).
// The `async_trait` crate can be used to work around this limitation.
#[async_trait]
pub trait ServerApi {
async fn init(&mut self, cfg: &Config) -> Result<()>;
2024-02-24 14:33:58 +01:00
async fn push_release(&mut self, rc: &ReleaseContext) -> Result<()>;
async fn push_release_artifact(&mut self, rc: &ReleaseContext) -> Result<()>;
async fn push_pkg(&mut self, pc: &PublishContext) -> Result<()>;
2024-02-16 20:00:55 +01:00
}
2024-02-24 14:10:44 +01:00
pub fn client_builder() -> ClientBuilder {
ClientBuilder::new().user_agent(USER_AGENT)
}
2024-02-16 20:00:55 +01:00
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 => {
2024-02-24 14:10:44 +01:00
collection.push(Box::new(Gitea::build(api.1).await?));
2024-02-16 20:00:55 +01:00
}
ApiType::Gitlab => {
2024-02-24 14:10:44 +01:00
collection.push(Box::new(Gitlab::build(api.1).await?));
2024-02-16 20:00:55 +01:00
}
ApiType::Github => {
2024-02-24 14:10:44 +01:00
collection.push(Box::new(Github::build(api.1).await?));
2024-02-16 20:00:55 +01:00
}
2024-02-19 22:07:51 +01:00
ApiType::Forgejo => {
2024-02-24 14:10:44 +01:00
collection.push(Box::new(Forgejo::build(api.1).await?));
2024-02-19 22:07:51 +01:00
}
2024-02-16 20:00:55 +01:00
}
}
2024-02-24 14:33:58 +01:00
for api in collection.iter_mut() {
2024-02-24 13:44:06 +00:00
api.init(cfg).await?;
2024-02-24 14:33:58 +01:00
}
2024-02-16 20:00:55 +01:00
Ok(collection)
}