From 1bc34011c1a27632efb6c741d366177d01f34c9d Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Thu, 25 Apr 2024 15:44:55 +0200 Subject: [PATCH 1/4] 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 | 2 +- src/serverapi/forgejo.rs | 69 ++++++++++++---------------------------- 8 files changed, 55 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..fce1633 100644 --- a/src/release/mod.rs +++ b/src/release/mod.rs @@ -39,7 +39,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: crate::changelog::Changelog::build(cfg)?.to_string(), 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(()) From bf2b9c6d0875bd4828059e4c595f0eeefdaa9065 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Fri, 26 Apr 2024 08:34:13 +0200 Subject: [PATCH 2/4] bump libpt --- Cargo.toml | 2 +- src/config/cli.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 091e5b0..c3733db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,7 @@ 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"] } +libpt = { version = "0.4.2", features = ["log"] } reqwest = "0.11.24" serde = { version = "1.0.195", features = ["derive"] } serde_json = "1.0.116" diff --git a/src/config/cli.rs b/src/config/cli.rs index 6b26337..351c984 100644 --- a/src/config/cli.rs +++ b/src/config/cli.rs @@ -110,10 +110,10 @@ impl Cli { } }; if cli.meta { - Logger::init(None, Some(ll), true).expect("could not initialize Logger"); + Logger::build(None, Some(ll), true).expect("could not initialize Logger"); } else { // less verbose version - Logger::init_mini(Some(ll)).expect("could not initialize Logger"); + Logger::build_mini(Some(ll)).expect("could not initialize Logger"); } cli } From abe2e25071efdb5470e9f5ac840e073285f00bb7 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Fri, 26 Apr 2024 06:36:24 +0000 Subject: [PATCH 3/4] automatic cargo CI changes --- src/config/mod.rs | 3 +-- src/error.rs | 4 ++-- src/serverapi/forgejo.rs | 8 -------- 3 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 9e82373..dbede01 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -208,8 +208,7 @@ impl Version { panic!("{err:?}"); } } - - }, + } } } } diff --git a/src/error.rs b/src/error.rs index 44ed537..991d1e0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -33,7 +33,7 @@ pub enum ServerApiError { #[error(transparent)] ReqwestErr(#[from] reqwest::Error), #[error(transparent)] - ForgejoApiError(#[from] forgejo_api::ForgejoError) + ForgejoApiError(#[from] forgejo_api::ForgejoError), } #[derive(Error, Debug)] @@ -66,5 +66,5 @@ pub enum ConfigError { #[error("An endpoint was set for an ApiType that does not require one")] EndpointSetButNotNeeded, #[error("No endpoint was set for an ApiType that requires one")] - NoEndpointSet + NoEndpointSet, } diff --git a/src/serverapi/forgejo.rs b/src/serverapi/forgejo.rs index 722e84e..6dcc2c5 100644 --- a/src/serverapi/forgejo.rs +++ b/src/serverapi/forgejo.rs @@ -1,5 +1,3 @@ -use std::str::FromStr; - use crate::{ config::{Api, Config}, error::*, @@ -7,12 +5,6 @@ use crate::{ }; 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, From e66550f2effda74bc8518e10771d37fd47e607b2 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Fri, 26 Apr 2024 08:54:23 +0200 Subject: [PATCH 4/4] github should maybe work --- .autocrate.yaml | 14 +++++++------- .env | 2 -- .gitignore | 1 + Cargo.toml | 3 ++- src/error.rs | 2 ++ src/serverapi/forgejo.rs | 6 +++--- src/serverapi/github.rs | 37 ++++++++++++++++++++++++++----------- 7 files changed, 41 insertions(+), 24 deletions(-) delete mode 100644 .env diff --git a/.autocrate.yaml b/.autocrate.yaml index 427c875..f1cec2b 100644 --- a/.autocrate.yaml +++ b/.autocrate.yaml @@ -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/.env b/.env deleted file mode 100644 index b544a1e..0000000 --- a/.env +++ /dev/null @@ -1,2 +0,0 @@ -TOKEN_CSCHERR=test -TOKEN_GH=test diff --git a/.gitignore b/.gitignore index 193d30e..35ef9e9 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ Cargo.lock # Added by cargo /target +.env diff --git a/Cargo.toml b/Cargo.toml index c3733db..b2eb710 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "autocrate" -version = "0.1.0-prealpha.4" +version = "0.1.0-prealpha.5" edition = "2021" publish = true authors = ["Christoph J. Scherr "] @@ -29,6 +29,7 @@ forgejo-api = "0.1.0" futures = "0.3.30" git2 = "0.18.1" libpt = { version = "0.4.2", features = ["log"] } +octocrab = "0.38.0" reqwest = "0.11.24" serde = { version = "1.0.195", features = ["derive"] } serde_json = "1.0.116" diff --git a/src/error.rs b/src/error.rs index 991d1e0..6f80b38 100644 --- a/src/error.rs +++ b/src/error.rs @@ -34,6 +34,8 @@ pub enum ServerApiError { ReqwestErr(#[from] reqwest::Error), #[error(transparent)] ForgejoApiError(#[from] forgejo_api::ForgejoError), + #[error(transparent)] + GithubApiError(#[from] octocrab::Error) } #[derive(Error, Debug)] diff --git a/src/serverapi/forgejo.rs b/src/serverapi/forgejo.rs index 6dcc2c5..511f1b6 100644 --- a/src/serverapi/forgejo.rs +++ b/src/serverapi/forgejo.rs @@ -7,7 +7,7 @@ use async_trait::async_trait; use forgejo_api; pub struct Forgejo { - cfg: Api, + api: Api, api_wrapper: forgejo_api::Forgejo, } @@ -19,7 +19,7 @@ impl Forgejo { ) .map_err(ServerApiError::from)?; Ok(Self { - cfg: api.clone(), + api: api.clone(), api_wrapper, }) } @@ -53,6 +53,6 @@ impl ServerApi for Forgejo { todo!() } fn get_cfg(&self) -> &Api { - &self.cfg + &self.api } } diff --git a/src/serverapi/github.rs b/src/serverapi/github.rs index 468a983..596f1c8 100644 --- a/src/serverapi/github.rs +++ b/src/serverapi/github.rs @@ -1,4 +1,5 @@ use async_trait::async_trait; +use octocrab; use super::{PublishContext, ReleaseContext, ServerApi}; use crate::{ @@ -6,16 +7,36 @@ use crate::{ error::*, }; pub struct Github { - cfg: Api, + api: Api, +} + +impl Github { + pub async fn build(api: &Api) -> Result { + Ok(Self { + api: api.to_owned(), + }) + } } #[async_trait] impl ServerApi for Github { async fn init(&mut self, _cfg: &Config) -> Result<()> { - todo!() + Ok(()) } - async fn push_release(&mut self, _rc: ReleaseContext) -> Result<()> { - todo!() + async fn push_release(&mut self, rc: ReleaseContext) -> Result<()> { + let response = octocrab::instance() + .repos(rc.username, rc.repository) + .releases() + .create(&rc.tag) + .target_commitish(&rc.commit_sig) + .name(&rc.tag) + .body(&rc.text) + .draft(rc.draft) + .prerelease(rc.prerelease) + .send() + .await + .map_err(ServerApiError::from)?; + Ok(()) } async fn push_release_artifact(&mut self, _rc: ReleaseContext) -> Result<()> { todo!() @@ -24,12 +45,6 @@ impl ServerApi for Github { todo!() } fn get_cfg(&self) -> &Api { - &self.cfg - } -} - -impl Github { - pub async fn build(api: &Api) -> Result { - Ok(Self { cfg: api.clone() }) + &self.api } }