fix config parser
cargo devel CI / cargo CI (push) Failing after 1m41s Details

This commit is contained in:
Christoph J. Scherr 2024-04-25 14:33:48 +02:00
parent 926f72b87e
commit 0e4d694b35
6 changed files with 51 additions and 24 deletions

View File

@ -1,4 +1,5 @@
---
version:
!text "echo foo"
changelog:
enable: true
git-log: true
@ -14,12 +15,11 @@ uses:
api:
github:
type: github
endpoint: https://github.com
repository: autocrate
auth:
user: PlexSheep
pass:
env: TOKEN_GH
!env TOKEN_GH
cscherr:
type: forgejo
endpoint: https://git.cscherr.de
@ -27,4 +27,4 @@ api:
auth:
user: PlexSheep
pass:
env: TOKEN_CSCHERR
!env TOKEN_CSCHERR

2
.env Normal file
View File

@ -0,0 +1,2 @@
TOKEN_CSCHERR=test
TOKEN_GH=test

View File

@ -35,7 +35,6 @@ impl Changelog {
return Err(ChangelogError::GitBadStatus(out.status, buf).into());
}
dbg!(&buf);
Ok(Some(buf))
}

View File

@ -1,10 +1,11 @@
use std::str::FromStr;
use std::{
collections::HashMap, fmt::Debug, fs::File, io::BufReader, path::PathBuf, process::Command,
};
use git2;
use libpt::log::{debug, error, trace};
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use url::Url;
use crate::error::*;
@ -17,7 +18,7 @@ pub trait YamlConfigSection: Debug + Clone + for<'a> Deserialize<'a> {
fn check(&self) -> Result<()>;
}
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Changelog {
pub enable: bool,
#[serde(alias = "git-log")]
@ -29,7 +30,7 @@ impl YamlConfigSection for Changelog {
}
}
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct UseCargo {
pub publish: bool,
pub registries: Vec<String>,
@ -40,7 +41,7 @@ impl YamlConfigSection for UseCargo {
}
}
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Uses {
cargo: UseCargo,
}
@ -51,7 +52,7 @@ impl YamlConfigSection for Uses {
}
}
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Pass {
/// pass specified as plainext
@ -94,7 +95,7 @@ impl YamlConfigSection for Pass {
Ok(())
}
}
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct ApiAuth {
pub user: String,
pub pass: Pass,
@ -106,11 +107,12 @@ impl YamlConfigSection for ApiAuth {
}
}
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Api {
#[serde(alias = "type")]
pub server_type: ApiType,
pub endpoint: Url,
/// May be left empty if the [ApiType] is [Github](ApiType::Github).
pub endpoint: Option<Url>,
/// May be left empty if the Api does not need auth or the auth is part of the
/// [endpoint](Api::endpoint) [Url].
pub auth: Option<ApiAuth>,
@ -120,9 +122,16 @@ pub struct Api {
impl YamlConfigSection for Api {
fn check(&self) -> Result<()> {
self.server_type.check()?;
match self.endpoint.socket_addrs(|| None) {
Ok(_) => (),
Err(err) => return Err(err.into()),
if self.server_type != ApiType::Github {
if self.auth.is_none() {
return Err(ConfigError::NoEndpointSet.into());
}
match self.endpoint.clone().unwrap().socket_addrs(|| None) {
Ok(_) => (),
Err(err) => return Err(err.into()),
}
} else if let Some(_url) = &self.endpoint {
return Err(ConfigError::EndpointSetButNotNeeded.into());
}
if self.auth.is_some() {
self.auth.clone().unwrap().check()?;
@ -131,7 +140,7 @@ impl YamlConfigSection for Api {
}
}
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
pub enum ApiType {
#[serde(alias = "gitea")]
Gitea,
@ -142,13 +151,22 @@ pub enum ApiType {
#[serde(alias = "forgejo")]
Forgejo,
}
impl ApiType {
pub fn default_endpoint(&self) -> Option<Url> {
match self {
Self::Github => Some(Url::from_str("https://github.com").unwrap()),
_ => None,
}
}
}
impl YamlConfigSection for ApiType {
fn check(&self) -> Result<()> {
Ok(())
}
}
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Version {
Text(String),
@ -198,7 +216,7 @@ impl YamlConfigSection for Version {
}
}
#[derive(Debug, Clone, Deserialize)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct YamlConfig {
pub changelog: Changelog,
pub uses: Uses,

View File

@ -61,4 +61,8 @@ pub enum ConfigError {
EnvNotSet(String),
#[error("Bad value for environment variable: {0}")]
BadEnv(#[from] VarError),
#[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
}

View File

@ -45,11 +45,15 @@ impl ServerApi for Forgejo {
Ok(())
}
async fn push_release(&mut self, rc: ReleaseContext) -> Result<()> {
let raw_url = format!(
"{}/api/v1/repos/{}/{}/releases",
self.cfg.endpoint, rc.username, rc.repository
);
let url = Url::parse(&raw_url).map_err(ServerApiError::from)?;
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#"
{{