diff --git a/Cargo.lock b/Cargo.lock index 351c247..3ac0f05 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -86,12 +86,6 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" -[[package]] -name = "byte-unit" -version = "3.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55390dbbf21ce70683f3e926dace00a21da373e35e44a60cafd232e3e9bf2041" - [[package]] name = "byteorder" version = "1.3.4" @@ -134,16 +128,6 @@ dependencies = [ "vec_map", ] -[[package]] -name = "cli-table" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd782cbfda62468ed8f94f2c00496ff909ad4916f4411ab9ec7bdced5414a699" -dependencies = [ - "termcolor", - "unicode-width", -] - [[package]] name = "comrak" version = "0.7.0" @@ -324,15 +308,24 @@ dependencies = [ "url", ] +[[package]] +name = "gitea" +version = "0.1.0" +dependencies = [ + "reqwest", + "serde", + "serde_json", + "thiserror", +] + [[package]] name = "gitea-release" version = "0.3.2" dependencies = [ "anyhow", - "byte-unit", - "cli-table", "comrak", "git2", + "gitea", "http", "kankyo", "reqwest", @@ -1138,15 +1131,6 @@ dependencies = [ "winapi 0.3.8", ] -[[package]] -name = "termcolor" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" -dependencies = [ - "winapi-util", -] - [[package]] name = "textwrap" version = "0.11.0" @@ -1156,6 +1140,26 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "thiserror" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dfdd070ccd8ccb78f4ad66bf1982dc37f620ef696c6b5028fe2ed83dd3d0d08" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd80fc12f73063ac132ac92aceea36734f04a1d93c1240c6944e23a3b8841793" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "thread_local" version = "1.0.1" @@ -1476,15 +1480,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" -dependencies = [ - "winapi 0.3.8", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index f8c9744..ddb3eef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,8 +8,6 @@ edition = "2018" [dependencies] anyhow = "1.0" -byte-unit = "3" -cli-table = "0.3" comrak = "0.7" git2 = "0.13" http = "0.2" @@ -21,8 +19,15 @@ structopt = { version = "0.3", default-features = false } tokio = { version = "0.2", features = ["macros"] } url = "2" +gitea = { path = "./gitea" } + [dev-dependencies] tempfile = "3" [profile.release] lto = true + +[workspace] +members = [ + "./gitea" +] diff --git a/README.md b/README.md index 681c5ee..4826720 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,8 @@ To use this as a drone plugin, add the following to your `.drone.yml` under the ```yaml - name: auto-release - image: xena/gitea-release:0.3.2 + image: xena/gitea-release:latest + pull: always settings: auth_username: cadey changelog_path: ./CHANGELOG.md @@ -53,7 +54,8 @@ setting like this: ```yaml - name: auto-release - image: xena/gitea-release:0.3.2 + image: xena/gitea-release:latest + pull: always settings: auth_username: cadey default_branch: trunk diff --git a/docker.nix b/docker.nix index b4e47d0..2070e42 100644 --- a/docker.nix +++ b/docker.nix @@ -8,7 +8,7 @@ let dockerImage = pkg: pkgs.dockerTools.buildLayeredImage { name = "xena/gitea-release"; - tag = "${gitea-release.version}"; + tag = "latest"; contents = [ pkgs.cacert pkg ]; diff --git a/src/cmd/drone_plugin.rs b/src/cmd/drone_plugin.rs index 71b2152..a0dd6af 100644 --- a/src/cmd/drone_plugin.rs +++ b/src/cmd/drone_plugin.rs @@ -6,14 +6,17 @@ use url::Url; pub(crate) async fn run(env: DroneEnv) -> Result<()> { let common: Common = env.clone().into(); - let default_branch = match &env.default_branch { - None => { - let cli = crate::client(&common)?; - let repo = - crate::gitea::get_repo(&cli, &common.server, &common.owner, &common.repo).await?; - repo.default_branch + let default_branch = { + let common = common.clone(); + match &env.default_branch { + None => { + let cli = + gitea::Client::new(common.server, common.token, crate::APP_USER_AGENT.into())?; + let repo = cli.get_repo(common.owner, common.repo).await?; + repo.default_branch + } + Some(branch) => branch.to_string(), } - Some(branch) => branch.to_string(), }; if env.branch != default_branch { diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 131c02f..e518fa4 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -1,13 +1,8 @@ use std::path::PathBuf; use structopt::StructOpt; -pub(crate) mod delete; -pub(crate) mod download; pub(crate) mod drone_plugin; -pub(crate) mod edit; -pub(crate) mod info; pub(crate) mod release; -pub(crate) mod upload; #[derive(StructOpt, Debug, Clone)] pub(crate) struct Common { @@ -90,51 +85,11 @@ pub(crate) struct ReleaseMeta { #[derive(StructOpt, Debug)] #[structopt(about = "Gitea release assistant")] pub(crate) enum Cmd { - /// Delete a given release from Gitea - Delete { - #[structopt(flatten)] - common: Common, - /// The version tag to operate on - #[structopt(short, long)] - tag: String, - }, - /// Downloads release artifacts - Download { - #[structopt(flatten)] - common: Common, - /// File to download - fname: Option, - /// The version tag to operate on - #[structopt(short, long)] - tag: String, - }, /// Runs the release process as a drone plugin DronePlugin { #[structopt(flatten)] env: DroneEnv, }, - /// Edits a release's description, name and other flags - Edit { - #[structopt(flatten)] - common: Common, - /// Release description - #[structopt(short, long)] - description: Option, - #[structopt(flatten)] - release_meta: ReleaseMeta, - /// The version tag to operate on - tag: String, - }, - /// Gets release info - Info { - #[structopt(flatten)] - common: Common, - #[structopt(long, short)] - json: bool, - /// The version tag to operate on - #[structopt(short, long)] - tag: Option, - }, /// Create a new tag and release on Gitea Release { #[structopt(flatten)] @@ -147,14 +102,4 @@ pub(crate) enum Cmd { #[structopt(flatten)] release_meta: ReleaseMeta, }, - /// Uploads release artifacts to Gitea - Upload { - #[structopt(flatten)] - common: Common, - /// The version tag to operate on - #[structopt(short, long)] - tag: String, - /// The location of the file on the disk - fname: PathBuf, - }, } diff --git a/src/cmd/release.rs b/src/cmd/release.rs index 05d20bf..332d09b 100644 --- a/src/cmd/release.rs +++ b/src/cmd/release.rs @@ -1,4 +1,4 @@ -use crate::{gitea::*, *}; +use crate::{changelog, cmd::*, git, version}; use anyhow::Result; use std::path::PathBuf; @@ -22,8 +22,10 @@ pub(crate) async fn run( } let desc = changelog::read(fname, tag.clone())?; - let cli = client(&common)?; - let cr = CreateRelease { + + let cli = gitea::Client::new(common.server, common.token, crate::APP_USER_AGENT.into())?; + + let cr = gitea::CreateRelease { body: desc, draft: rm.draft, name: rm.name.or(Some(format!("Version {}", tag))).unwrap(), @@ -32,18 +34,7 @@ pub(crate) async fn run( target_commitish: "HEAD".into(), }; - let resp = cli - .post(&format!( - "{}/api/v1/repos/{}/{}/releases", - common.server, common.owner, common.repo - )) - .json(&cr) - .send() - .await?; - - if !resp.status().is_success() { - return Err(anyhow!("{:?} -> {}", resp.status(), resp.text().await?)); - } + let _ = cli.create_release(common.owner, common.repo, cr).await?; println!("Created release {}", tag); diff --git a/src/main.rs b/src/main.rs index d7bdaaa..26093d8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,29 +1,16 @@ -use anyhow::{anyhow, Result}; -use reqwest::{header, Client}; -use std::path::PathBuf; +use anyhow::Result; use structopt::StructOpt; mod changelog; mod cmd; mod git; -mod gitea; mod version; pub(crate) use cmd::*; // Name your user agent after your app? -static APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); - -pub(crate) fn client(c: &cmd::Common) -> Result { - let mut headers = header::HeaderMap::new(); - let auth = format!("token {}", &c.token); - let auth = auth.as_str(); - headers.insert(header::AUTHORIZATION, header::HeaderValue::from_str(auth)?); - Ok(Client::builder() - .user_agent(APP_USER_AGENT) - .default_headers(headers) - .build()?) -} +pub(crate) static APP_USER_AGENT: &str = + concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); #[tokio::main] async fn main() -> Result<()> { @@ -31,22 +18,12 @@ async fn main() -> Result<()> { let cmd = cmd::Cmd::from_args(); match cmd { - Cmd::Delete { common, tag } => cmd::delete::run(common, tag).await, - Cmd::Download { common, fname, tag } => cmd::download::run(common, fname, tag).await, Cmd::DronePlugin { env } => cmd::drone_plugin::run(env).await, - Cmd::Edit { - common, - description, - release_meta, - tag, - } => cmd::edit::run(common, description, release_meta, tag).await, - Cmd::Info { common, json, tag } => cmd::info::run(common, json, tag).await, Cmd::Release { common, changelog, tag, release_meta, } => cmd::release::run(common, changelog, tag, release_meta).await, - Cmd::Upload { common, fname, tag } => cmd::upload::run(common, fname, tag).await, } }