release to forgejo works in an early state
cargo devel CI / cargo CI (push) Failing after 1m53s Details

This commit is contained in:
Christoph J. Scherr 2024-04-25 15:44:55 +02:00
parent 0e4d694b35
commit 1bc34011c1
8 changed files with 55 additions and 62 deletions

View File

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

View File

@ -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 <software@cscherr.de>"]
@ -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"

View File

@ -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<Option<String>> {
if !cfg.yaml.changelog.enable {
return Ok(None);

View File

@ -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:?}");
}
}
},
}
}
}

View File

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

View File

@ -22,8 +22,8 @@ pub async fn tag(cfg: &Config) -> Result<git2::Tag> {
let tag = cfg
.repo
.tag(
// &cfg.yaml.version.get_version(),
"importantversion",
&cfg.yaml.version.get_version(),
// "importantversion",
&target,
&tagger,
&message,

View File

@ -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(),
};

View File

@ -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<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)?,
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(())