From 378b269fa0441f55980206a35bf8efd7bf8c7fa8 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Thu, 25 Apr 2024 15:44:55 +0200 Subject: [PATCH] release to forgejo works in an early state --- .autocrate.yaml | 18 +++++------ Cargo.toml | 4 ++- src/changelog/mod.rs | 1 + src/config/mod.rs | 17 +++++++++- src/error.rs | 2 ++ src/git/mod.rs | 4 +-- src/release/mod.rs | 4 ++- src/serverapi/forgejo.rs | 69 ++++++++++++---------------------------- 8 files changed, 57 insertions(+), 62 deletions(-) diff --git a/.autocrate.yaml b/.autocrate.yaml index f479d9c..427c875 100644 --- a/.autocrate.yaml +++ b/.autocrate.yaml @@ -1,5 +1,5 @@ -version: - !text "echo foo" +version: + !cargo changelog: enable: true git-log: true @@ -13,13 +13,13 @@ uses: - cscherr api: - github: - type: github - repository: autocrate - auth: - user: PlexSheep - pass: - !env TOKEN_GH + # github: + # type: github + # repository: autocrate + # auth: + # user: PlexSheep + # pass: + # !env TOKEN_GH cscherr: type: forgejo endpoint: https://git.cscherr.de diff --git a/Cargo.toml b/Cargo.toml index 7d4a7c4..091e5b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "autocrate" -version = "0.1.0-prealpha.3" +version = "0.1.0-prealpha.4" edition = "2021" publish = true authors = ["Christoph J. Scherr "] @@ -25,11 +25,13 @@ async-trait = "0.1.77" # cargo = "0.76.0" clap = { version = "4.4.18", features = ["derive", "help"] } clap-verbosity-flag = "2.1.2" +forgejo-api = "0.1.0" futures = "0.3.30" git2 = "0.18.1" libpt = { version = "0.3.11", features = ["log"] } reqwest = "0.11.24" serde = { version = "1.0.195", features = ["derive"] } +serde_json = "1.0.116" serde_yaml = "0.9.30" tempfile = "3.9.0" thiserror = "1.0.56" diff --git a/src/changelog/mod.rs b/src/changelog/mod.rs index 1b914ba..cce0dd8 100644 --- a/src/changelog/mod.rs +++ b/src/changelog/mod.rs @@ -17,6 +17,7 @@ impl Changelog { Ok(Changelog { git_log }) } + // TODO: use libgit2 instead of the cli interface fn make_git_log(cfg: &Config) -> Result> { if !cfg.yaml.changelog.enable { return Ok(None); diff --git a/src/config/mod.rs b/src/config/mod.rs index b9d6dd6..9e82373 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -194,7 +194,22 @@ impl Version { } } } - Self::Cargo => todo!(), + Self::Cargo => { + match Command::new("/bin/bash") + .arg("-c") + .arg(r#"cat Cargo.toml | rg '^\s*version\s*=\s*"([^"]*)"\s*$' -or '$1'"#) + .output() + { + Ok(output) => { + // TODO: check status + String::from_utf8(output.stdout).unwrap().trim().to_owned() + } + Err(err) => { + panic!("{err:?}"); + } + } + + }, } } } diff --git a/src/error.rs b/src/error.rs index 985b588..44ed537 100644 --- a/src/error.rs +++ b/src/error.rs @@ -32,6 +32,8 @@ pub enum ServerApiError { InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue), #[error(transparent)] ReqwestErr(#[from] reqwest::Error), + #[error(transparent)] + ForgejoApiError(#[from] forgejo_api::ForgejoError) } #[derive(Error, Debug)] diff --git a/src/git/mod.rs b/src/git/mod.rs index 6201cbe..bac89fc 100644 --- a/src/git/mod.rs +++ b/src/git/mod.rs @@ -22,8 +22,8 @@ pub async fn tag(cfg: &Config) -> Result { let tag = cfg .repo .tag( - // &cfg.yaml.version.get_version(), - "importantversion", + &cfg.yaml.version.get_version(), + // "importantversion", &target, &tagger, &message, diff --git a/src/release/mod.rs b/src/release/mod.rs index 18e8abe..c341373 100644 --- a/src/release/mod.rs +++ b/src/release/mod.rs @@ -1,4 +1,5 @@ use crate::{ + changelog, config::Config, error::*, git::{get_commit_sig, push, tag}, @@ -20,6 +21,7 @@ pub struct ReleaseContext { pub async fn release(cfg: &Config, apis: &mut ApiCollection) -> Result<()> { // TODO: Error handling + let changelog = crate::changelog::Changelog::build(cfg)?.to_string(); let tag = tag(cfg).await?.name().unwrap().to_string(); let commit_sig = get_commit_sig(cfg).await?; push(cfg).await?; // we assume that we only need to push the current branch to the singular @@ -39,7 +41,7 @@ pub async fn release(cfg: &Config, apis: &mut ApiCollection) -> Result<()> { .expect("no auth but trying to publish") .user, repository: api.get_cfg().repository.clone(), - text: String::from("TODO: ADD TEXT VARIABLE SOMEHOW"), + text: changelog.clone(), tag: tag.clone(), commit_sig: commit_sig.clone(), }; diff --git a/src/serverapi/forgejo.rs b/src/serverapi/forgejo.rs index 2dfaa94..722e84e 100644 --- a/src/serverapi/forgejo.rs +++ b/src/serverapi/forgejo.rs @@ -6,35 +6,29 @@ use crate::{ serverapi::{PublishContext, ReleaseContext, ServerApi}, }; use async_trait::async_trait; +use forgejo_api; +use libpt::log::debug; use reqwest::{ header::{HeaderMap, HeaderValue}, Client, Url, }; +use serde_json; pub struct Forgejo { cfg: Api, - client: Client, + api_wrapper: forgejo_api::Forgejo, } 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)?, - HeaderValue::from_str("hardcoded").map_err(ServerApiError::from)?, - ); - } - let client = super::client_builder() - .default_headers(headers) - .build() - .map_err(ServerApiError::from)?; + let api_wrapper: forgejo_api::Forgejo = forgejo_api::Forgejo::new( + forgejo_api::Auth::Token(&api.auth.clone().unwrap().pass.get_pass()?), + api.endpoint.clone().unwrap(), + ) + .map_err(ServerApiError::from)?; Ok(Self { cfg: api.clone(), - client, + api_wrapper, }) } } @@ -45,38 +39,17 @@ impl ServerApi for Forgejo { Ok(()) } async fn push_release(&mut self, rc: ReleaseContext) -> Result<()> { - let url: Url = match &self.cfg.endpoint { - Some(url) => url.clone(), - None => self - .cfg - .server_type - .default_endpoint() - .expect("no default endpoint for this api type"), - }; - url.join("/api/v1/repos/{}/{}/releases"); - let body = format!( - r#" - {{ - "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(url) - .body(body) - .build() - .map_err(ServerApiError::from)?; - let _response = self - .client - .execute(request) + let body: forgejo_api::structs::CreateReleaseOption = + forgejo_api::structs::CreateReleaseOption { + body: Some(rc.text), + draft: Some(rc.draft), + name: Some(rc.tag.clone()), + prerelease: Some(rc.prerelease), + tag_name: rc.tag, + target_commitish: Some(rc.commit_sig), + }; + self.api_wrapper + .repo_create_release(&rc.username, &rc.repository, body) .await .map_err(ServerApiError::from)?; Ok(())