From 45ba3f6b97d69e99dc622fb60cef39922391e90e Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sat, 24 Feb 2024 14:33:58 +0100 Subject: [PATCH] contexts for api actions --- src/publish/mod.rs | 3 +++ src/release/mod.rs | 18 ++++++++++------- src/serverapi/forgejo.rs | 43 ++++++++++++++++++++++++---------------- src/serverapi/gitea.rs | 8 ++++---- src/serverapi/github.rs | 8 ++++---- src/serverapi/gitlab.rs | 8 ++++---- src/serverapi/mod.rs | 13 ++++++++---- 7 files changed, 61 insertions(+), 40 deletions(-) diff --git a/src/publish/mod.rs b/src/publish/mod.rs index 6016027..5146fb8 100644 --- a/src/publish/mod.rs +++ b/src/publish/mod.rs @@ -1,4 +1,7 @@ use crate::{config::Config, error::*}; + +pub struct PublishContext; + pub async fn publish(_cfg: &Config) -> Result<()> { todo!() } diff --git a/src/release/mod.rs b/src/release/mod.rs index 2aac8c4..2482340 100644 --- a/src/release/mod.rs +++ b/src/release/mod.rs @@ -1,18 +1,22 @@ use crate::{config::Config, error::*, serverapi::ApiCollection}; +pub struct ReleaseContext { + pub draft: bool, + pub prerelease: bool, + pub username: String, + pub repository: String, + pub text: String, + pub tag: String, + pub commit_sig: String +} + pub async fn release(cfg: &Config, apis: &mut ApiCollection) -> Result<()> { // TODO: git tag // TODO: push to each server // TODO: release to each server tag(cfg).await?; - for api in apis.iter_mut() { - api.push_release().await?; - } - - for api in apis.iter_mut() { - api.push_release().await?; - } + todo!(); // TODO: check that the release is made // TODO: generate artifacts diff --git a/src/serverapi/forgejo.rs b/src/serverapi/forgejo.rs index 2c27327..94c6188 100644 --- a/src/serverapi/forgejo.rs +++ b/src/serverapi/forgejo.rs @@ -1,11 +1,12 @@ -use super::ServerApi; use crate::{ config::{packages::PackageType, Api, ApiType, Config}, error::*, + serverapi::{PublishContext, ReleaseContext, ServerApi}, }; use async_trait::async_trait; use reqwest::{ - header::{HeaderMap, HeaderValue}, Client, Method, Request, RequestBuilder, Url + header::{HeaderMap, HeaderValue}, + Client, Method, Request, RequestBuilder, Url, }; pub struct Forgejo { @@ -40,30 +41,38 @@ impl ServerApi for Forgejo { async fn init(&mut self, cfg: &Config) -> Result<()> { todo!() } - async fn push_release(&mut self) -> Result<()> { + async fn push_release(&mut self, rc: &ReleaseContext) -> Result<()> { let raw_url = format!( - "{}/api/v1/repos/{user}/{repository}/releases", - self.cfg.endpoint + "{}/api/v1/repos/{}/{}/releases", + self.cfg.endpoint, rc.username, rc.repository ); - let body = format!(r#" + let url = Url::parse(&raw_url).map_err(ServerApiError::from)?; + let body = format!( + r#" {{ - "body": "{text}", - "draft": {draft}, - "name": "{name}", - "prerelease": {prerelease}, - "tag_name": "{tag}", - "target_commitish": "{commit_sig}" + "body": "{}", + "draft": {}, + "name": "{}", + "prerelease": {}, + "tag_name": "{}", + "target_commitish": "{}" }} - "#); + "#, + rc.text, rc.draft, rc.tag, rc.prerelease, rc.tag, rc.commit_sig + ); - let request = self.client.post().body(body).build()?; - let response = self.client.execute(request).await?; + let request = self + .client + .post(url) + .body(body) + .build().map_err(ServerApiError::from)?; + let response = self.client.execute(request).await.map_err(ServerApiError::from)?; Ok(()) } - async fn push_release_artifact(&mut self) -> Result<()> { + async fn push_release_artifact(&mut self, rc: &ReleaseContext) -> Result<()> { todo!() } - async fn push_pkg(&mut self, pkg_type: PackageType) -> Result<()> { + async fn push_pkg(&mut self, pc: &PublishContext) -> Result<()> { todo!() } } diff --git a/src/serverapi/gitea.rs b/src/serverapi/gitea.rs index 44426e6..e222545 100644 --- a/src/serverapi/gitea.rs +++ b/src/serverapi/gitea.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; -use super::ServerApi; +use super::{PublishContext, ReleaseContext, ServerApi}; use crate::{ config::{packages::PackageType, Api, ApiType, Config}, error::*, @@ -14,13 +14,13 @@ impl ServerApi for Gitea { async fn init(&mut self, cfg: &Config) -> Result<()> { todo!() } - async fn push_release(&mut self) -> Result<()> { + async fn push_release(&mut self, rc: &ReleaseContext) -> Result<()> { todo!() } - async fn push_release_artifact(&mut self) -> Result<()> { + async fn push_release_artifact(&mut self, rc: &ReleaseContext) -> Result<()> { todo!() } - async fn push_pkg(&mut self, pkg_type: PackageType) -> Result<()> { + async fn push_pkg(&mut self, pc: &PublishContext) -> Result<()> { todo!() } } diff --git a/src/serverapi/github.rs b/src/serverapi/github.rs index a65f0fd..2fd61fa 100644 --- a/src/serverapi/github.rs +++ b/src/serverapi/github.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; -use super::ServerApi; +use super::{PublishContext, ReleaseContext, ServerApi}; use crate::{ config::{packages::PackageType, Api, ApiType, Config}, error::*, @@ -14,13 +14,13 @@ impl ServerApi for Github { async fn init(&mut self, cfg: &Config) -> Result<()> { todo!() } - async fn push_release(&mut self) -> Result<()> { + async fn push_release(&mut self, rc: &ReleaseContext) -> Result<()> { todo!() } - async fn push_release_artifact(&mut self) -> Result<()> { + async fn push_release_artifact(&mut self, rc: &ReleaseContext) -> Result<()> { todo!() } - async fn push_pkg(&mut self, pkg_type: PackageType) -> Result<()> { + async fn push_pkg(&mut self, pc: &PublishContext) -> Result<()> { todo!() } } diff --git a/src/serverapi/gitlab.rs b/src/serverapi/gitlab.rs index be49f36..8b39979 100644 --- a/src/serverapi/gitlab.rs +++ b/src/serverapi/gitlab.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; -use super::ServerApi; +use super::{PublishContext, ReleaseContext, ServerApi}; use crate::{ config::{packages::PackageType, Api, ApiType, Config}, error::*, @@ -14,13 +14,13 @@ impl ServerApi for Gitlab { async fn init(&mut self, cfg: &Config) -> Result<()> { todo!() } - async fn push_release(&mut self) -> Result<()> { + async fn push_release(&mut self, rc: &ReleaseContext) -> Result<()> { todo!() } - async fn push_release_artifact(&mut self) -> Result<()> { + async fn push_release_artifact(&mut self, rc: &ReleaseContext) -> Result<()> { todo!() } - async fn push_pkg(&mut self, pkg_type: PackageType) -> Result<()> { + async fn push_pkg(&mut self, pc: &PublishContext) -> Result<()> { todo!() } } diff --git a/src/serverapi/mod.rs b/src/serverapi/mod.rs index 15838f1..615659c 100644 --- a/src/serverapi/mod.rs +++ b/src/serverapi/mod.rs @@ -2,8 +2,10 @@ use async_trait::async_trait; use reqwest::ClientBuilder; use crate::{ - config::{packages::PackageType, ApiType, Config}, + config::{ApiType, Config}, error::*, + publish::PublishContext, + release::ReleaseContext, }; pub mod forgejo; @@ -24,9 +26,9 @@ pub type ApiCollection = Vec>; #[async_trait] pub trait ServerApi { async fn init(&mut self, cfg: &Config) -> Result<()>; - async fn push_release(&mut self) -> Result<()>; - async fn push_release_artifact(&mut self) -> Result<()>; - async fn push_pkg(&mut self, pkg_type: PackageType) -> Result<()>; + 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<()>; } pub fn client_builder() -> ClientBuilder { @@ -51,5 +53,8 @@ pub async fn init_servers(cfg: &Config) -> Result { } } } + for api in collection.iter_mut() { + api.init(&cfg).await?; + } Ok(collection) }