Compare commits

..

No commits in common. "master" and "0.1.0-prealpha.4" have entirely different histories.

15 changed files with 98 additions and 140 deletions

View File

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

2
.env Normal file
View File

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

1
.gitignore vendored
View File

@ -19,4 +19,3 @@ Cargo.lock
# Added by cargo
/target
.env

View File

@ -1,6 +1,6 @@
[package]
name = "autocrate"
version = "0.1.0-prealpha.5"
version = "0.1.0-prealpha.4"
edition = "2021"
publish = true
authors = ["Christoph J. Scherr <software@cscherr.de>"]
@ -28,8 +28,7 @@ clap-verbosity-flag = "2.1.2"
forgejo-api = "0.1.0"
futures = "0.3.30"
git2 = "0.18.1"
libpt = { version = "0.4.2", features = ["log"] }
octocrab = "0.38.0"
libpt = { version = "0.3.11", features = ["log"] }
reqwest = "0.11.24"
serde = { version = "1.0.195", features = ["derive"] }
serde_json = "1.0.116"

View File

@ -110,10 +110,10 @@ impl Cli {
}
};
if cli.meta {
Logger::build(None, Some(ll), true).expect("could not initialize Logger");
Logger::init(None, Some(ll), true).expect("could not initialize Logger");
} else {
// less verbose version
Logger::build_mini(Some(ll)).expect("could not initialize Logger");
Logger::init_mini(Some(ll)).expect("could not initialize Logger");
}
cli
}

View File

@ -119,20 +119,6 @@ pub struct Api {
/// Name of the repository on the Git server, as git itself has no concept of repository name
pub repository: String,
}
impl std::fmt::Display for Api {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.server_type {
ApiType::Github => write!(
f,
"{}",
self.server_type
.default_endpoint()
.expect("no default endpoint set for github")
),
_ => write!(f, "{}", self.endpoint.clone().expect("no endpoint set")),
}
}
}
impl YamlConfigSection for Api {
fn check(&self) -> Result<()> {
self.server_type.check()?;
@ -222,7 +208,8 @@ impl Version {
panic!("{err:?}");
}
}
}
},
}
}
}
@ -273,10 +260,10 @@ impl YamlConfig {
}
}
#[derive(Clone)]
pub struct Config {
pub yaml: YamlConfig,
pub cli: Cli,
pub repo: git2::Repository,
pub path: PathBuf,
}
@ -333,6 +320,7 @@ impl Config {
Ok(Config {
yaml,
repo,
path,
cli: cli.clone(),
})

View File

@ -33,9 +33,7 @@ pub enum ServerApiError {
#[error(transparent)]
ReqwestErr(#[from] reqwest::Error),
#[error(transparent)]
ForgejoApiError(#[from] forgejo_api::ForgejoError),
#[error(transparent)]
GithubApiError(#[from] octocrab::Error),
ForgejoApiError(#[from] forgejo_api::ForgejoError)
}
#[derive(Error, Debug)]
@ -68,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
}

View File

@ -1,41 +1,26 @@
use std::process::Command;
use git2;
use libpt::log::error;
use crate::error::ConfigError;
use crate::{config::Config, error::Result};
pub(crate) fn get_repo() -> Result<git2::Repository> {
let repo = match git2::Repository::open_from_env() {
Ok(repo) => repo,
Err(_err) => {
let err = ConfigError::GitRepoNotFound.into();
error!("{err}");
return Err(err);
}
};
Ok(repo)
}
pub async fn tag<'repo>(
repo: &'repo mut git2::Repository,
cfg: &Config,
) -> Result<git2::Tag<'repo>> {
pub async fn tag(cfg: &Config) -> Result<git2::Tag> {
// TODO: error handling
// TODO: allow force
// TODO: allow setting a message
// TODO: maybe using git as cmd is fancier?
let target = repo
let target = cfg
.repo
.find_object(
repo.head().unwrap().target().unwrap(),
cfg.repo.head().unwrap().target().unwrap(),
Some(git2::ObjectType::Commit),
)
.unwrap();
let tagger = repo.signature().expect("could not get signature");
let tagger = cfg.repo.signature().expect("could not get signature");
let message = String::new();
let force = true;
let tag = repo
let tag = cfg
.repo
.tag(
&cfg.yaml.version.get_version(),
// "importantversion",
@ -45,7 +30,7 @@ pub async fn tag<'repo>(
force,
)
.unwrap();
let tag: git2::Tag = repo.find_tag(tag).unwrap();
let tag: git2::Tag = cfg.repo.find_tag(tag).unwrap();
Ok(tag)
}
@ -56,11 +41,12 @@ pub async fn push(_cfg: &Config) -> Result<()> {
Ok(())
}
pub async fn get_commit_sig<'repo>(repo: &'repo git2::Repository) -> Result<String> {
pub async fn get_commit_sig(cfg: &Config) -> Result<String> {
// TODO: error handling
// TODO: maybe using git as cmd is fancier?
let target = repo
.find_commit(repo.head().unwrap().target().unwrap())
let target = cfg
.repo
.find_commit(cfg.repo.head().unwrap().target().unwrap())
.unwrap();
Ok(target.id().to_string())
}

View File

@ -1,5 +1,3 @@
use std::error::Error as _;
use autocrate::{
changelog::*,
config::{
@ -11,37 +9,24 @@ use autocrate::{
release::release,
serverapi::ApiCollection,
};
use libpt::log::{debug, error};
#[tokio::main]
async fn main() -> Result<()> {
let cli = Cli::cli_parse();
let cfg = Config::load(&cli)?;
let status: Option<Error> = match cli.command {
match cli.command {
Commands::Changelog { .. } => {
let chlog = Changelog::build(&cfg);
if chlog.is_ok() {
println!("{}", chlog.unwrap());
None
} else {
Some(chlog.unwrap_err())
}
println!("{}", Changelog::build(&cfg)?);
}
Commands::Release { .. } => {
// TODO: check if repo is dirty and create a commit with a given option
let mut apis = ApiCollection::build(&cfg).await?;
match release(&cfg, &mut apis).await {
Ok(_) => None,
Err(err) => Some(err),
}
release(&cfg, &mut apis).await?;
}
Commands::Publish { .. } => {
// TODO: check if repo is dirty and create a commit with a given option
match publish(&cfg).await {
Ok(_) => None,
Err(err) => Some(err),
}
publish(&cfg).await?;
}
Commands::Version {} => {
// TODO: version bump
@ -54,9 +39,5 @@ async fn main() -> Result<()> {
todo!()
}
};
if let Some(err) = status {
error!("{err}");
debug!("{:#?}", err.source());
}
Ok(())
}

View File

@ -1,12 +1,12 @@
use crate::{
changelog,
config::Config,
error::*,
git::{self, get_commit_sig, push, tag},
git::{get_commit_sig, push, tag},
serverapi::ApiCollection,
};
use futures::{self, stream::FuturesUnordered, StreamExt};
use libpt::log::info;
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct ReleaseContext {
@ -21,10 +21,9 @@ 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 mut repo = git::get_repo()?;
let tag = tag(&mut repo, cfg).await?.name().unwrap().to_string();
let commit_sig = get_commit_sig(&repo).await?;
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
// remote, expecting that the repositories are somehow mirrored
// TODO: push to multiple remotes?
@ -36,17 +35,16 @@ pub async fn release(cfg: &Config, apis: &mut ApiCollection) -> Result<()> {
draft: true,
prerelease: true,
username: api
.get_inner()
.get_cfg()
.clone()
.auth
.expect("no auth but trying to publish")
.user,
repository: api.get_inner().repository.clone(),
text: crate::changelog::Changelog::build(cfg)?.to_string(),
repository: api.get_cfg().repository.clone(),
text: changelog.clone(),
tag: tag.clone(),
commit_sig: commit_sig.clone(),
};
info!("pushing release for {}", api.get_inner());
results.push(api.push_release(specific_rc));
}

View File

@ -1,3 +1,5 @@
use std::str::FromStr;
use crate::{
config::{Api, Config},
error::*,
@ -5,23 +7,27 @@ 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 {
api: Api,
cfg: Config,
cfg: Api,
api_wrapper: forgejo_api::Forgejo,
}
impl Forgejo {
pub async fn build(api: &Api, cfg: &Config) -> Result<Self> {
pub async fn build(api: &Api) -> Result<Self> {
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 {
api: api.clone(),
cfg: cfg.clone(),
cfg: api.clone(),
api_wrapper,
})
}
@ -29,6 +35,9 @@ impl Forgejo {
#[async_trait]
impl ServerApi for Forgejo {
async fn init(&mut self, _cfg: &Config) -> Result<()> {
Ok(())
}
async fn push_release(&mut self, rc: ReleaseContext) -> Result<()> {
let body: forgejo_api::structs::CreateReleaseOption =
forgejo_api::structs::CreateReleaseOption {
@ -51,7 +60,7 @@ impl ServerApi for Forgejo {
async fn push_pkg(&mut self, _pc: PublishContext) -> Result<()> {
todo!()
}
fn get_inner(&self) -> &Api {
&self.api
fn get_cfg(&self) -> &Api {
&self.cfg
}
}

View File

@ -6,11 +6,14 @@ use crate::{
error::*,
};
pub struct Gitea {
api: Api,
cfg: Api,
}
#[async_trait]
impl ServerApi for Gitea {
async fn init(&mut self, _cfg: &Config) -> Result<()> {
todo!()
}
async fn push_release(&mut self, _rc: ReleaseContext) -> Result<()> {
todo!()
}
@ -20,13 +23,13 @@ impl ServerApi for Gitea {
async fn push_pkg(&mut self, _pc: PublishContext) -> Result<()> {
todo!()
}
fn get_inner(&self) -> &Api {
&self.api
fn get_cfg(&self) -> &Api {
&self.cfg
}
}
impl Gitea {
pub async fn build(api: &Api, _cfg: &Config) -> Result<Self> {
Ok(Self { api: api.clone() })
pub async fn build(api: &Api) -> Result<Self> {
Ok(Self { cfg: api.clone() })
}
}

View File

@ -1,5 +1,4 @@
use async_trait::async_trait;
use octocrab;
use super::{PublishContext, ReleaseContext, ServerApi};
use crate::{
@ -7,33 +6,16 @@ use crate::{
error::*,
};
pub struct Github {
api: Api,
}
impl Github {
pub async fn build(api: &Api, _cfg: &Config) -> Result<Self> {
Ok(Self {
api: api.to_owned(),
})
}
cfg: Api,
}
#[async_trait]
impl ServerApi for Github {
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 init(&mut self, _cfg: &Config) -> Result<()> {
todo!()
}
async fn push_release(&mut self, _rc: ReleaseContext) -> Result<()> {
todo!()
}
async fn push_release_artifact(&mut self, _rc: ReleaseContext) -> Result<()> {
todo!()
@ -41,7 +23,13 @@ impl ServerApi for Github {
async fn push_pkg(&mut self, _pc: PublishContext) -> Result<()> {
todo!()
}
fn get_inner(&self) -> &Api {
&self.api
fn get_cfg(&self) -> &Api {
&self.cfg
}
}
impl Github {
pub async fn build(api: &Api) -> Result<Self> {
Ok(Self { cfg: api.clone() })
}
}

View File

@ -6,11 +6,14 @@ use crate::{
error::*,
};
pub struct Gitlab {
api: Api,
cfg: Api,
}
#[async_trait]
impl ServerApi for Gitlab {
async fn init(&mut self, _cfg: &Config) -> Result<()> {
todo!()
}
async fn push_release(&mut self, _rc: ReleaseContext) -> Result<()> {
todo!()
}
@ -20,13 +23,13 @@ impl ServerApi for Gitlab {
async fn push_pkg(&mut self, _pc: PublishContext) -> Result<()> {
todo!()
}
fn get_inner(&self) -> &Api {
&self.api
fn get_cfg(&self) -> &Api {
&self.cfg
}
}
impl Gitlab {
pub async fn build(api: &Api, _cfg: &Config) -> Result<Self> {
Ok(Self { api: api.clone() })
pub async fn build(api: &Api) -> Result<Self> {
Ok(Self { cfg: api.clone() })
}
}

View File

@ -26,10 +26,11 @@ pub static USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_P
// The `async_trait` crate can be used to work around this limitation.
#[async_trait]
pub trait ServerApi {
async fn init(&mut self, cfg: &Config) -> 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<()>;
fn get_inner(&self) -> &config::Api;
fn get_cfg(&self) -> &config::Api;
}
pub(crate) type ApiCollectionInner = Vec<Box<dyn ServerApi>>;
@ -44,19 +45,22 @@ impl ApiCollection {
for api in &cfg.yaml.api {
match api.1.server_type {
ApiType::Gitea => {
collection.push(Box::new(Gitea::build(api.1, cfg).await?));
collection.push(Box::new(Gitea::build(api.1).await?));
}
ApiType::Gitlab => {
collection.push(Box::new(Gitlab::build(api.1, cfg).await?));
collection.push(Box::new(Gitlab::build(api.1).await?));
}
ApiType::Github => {
collection.push(Box::new(Github::build(api.1, cfg).await?));
collection.push(Box::new(Github::build(api.1).await?));
}
ApiType::Forgejo => {
collection.push(Box::new(Forgejo::build(api.1, cfg).await?));
collection.push(Box::new(Forgejo::build(api.1).await?));
}
}
}
for api in collection.iter_mut() {
api.init(cfg).await?;
}
Ok(ApiCollection { collection })
}