diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..8560f47 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,9 @@ +kind: pipeline +name: tools + +steps: + - name: rust tests + image: "rust:1" + pull: always + commands: + - cargo test diff --git a/Cargo.lock b/Cargo.lock index 08bdde6..2853be6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -339,6 +339,7 @@ dependencies = [ "serde", "serde_json", "structopt", + "tempfile", "tokio", ] diff --git a/Cargo.toml b/Cargo.toml index 2ab2c4a..d3d4ca3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,5 +20,8 @@ serde = { version = "1.0", features = ["derive"] } structopt = { version = "0.3", default-features = false } tokio = { version = "0.2", features = ["macros"] } +[dev-dependencies] +tempfile = "3" + [profile.release] lto = true diff --git a/README.md b/README.md index 54ed4d9..bef1eb0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # gitea-release +[![built with nix](https://builtwithnix.org/badge.svg)](https://builtwithnix.org) +[![Build Status](https://drone.tulpa.dev/api/badges/cadey/gitea-release/status.svg)](https://drone.tulpa.dev/cadey/gitea-release) + A small command line tool to automate releases for [Gitea](https://gitea.io) repositories that reads from CHANGELOG and VERSION files. This is a clone of [github-release](https://github.com/github-release/github-release), but more diff --git a/src/git.rs b/src/git.rs new file mode 100644 index 0000000..ac8f7f2 --- /dev/null +++ b/src/git.rs @@ -0,0 +1,59 @@ +use anyhow::Result; +use git2::Repository; +use std::path::PathBuf; + +pub(crate) fn has_tag(repo_path: PathBuf, tag: String) -> Result { + let repo = Repository::init(repo_path)?; + let tags = repo.tag_names(Some(&tag))?; + + for tag_obj in tags.iter() { + if tag_obj.is_none() { + continue; + } + + let tag_name = tag_obj.unwrap(); + if tag == tag_name.to_string() { + return Ok(true); + } + } + + Ok(false) +} + +#[cfg(test)] +mod tests { + use anyhow::Result; + use git2::*; + use std::{fs::File, io::Write, path::Path}; + use tempfile::tempdir; + + #[test] + fn has_tag() -> Result<()> { + const TAG: &'static str = "0.1.0"; + let dir = tempdir()?; + let repo = Repository::init(&dir)?; + let mut fout = File::create(&dir.path().join("VERSION"))?; + write!(fout, "{}", TAG)?; + drop(fout); + let mut index = repo.index()?; + index.add_path(Path::new("VERSION"))?; + let oid = index.write_tree()?; + let tree = repo.find_tree(oid)?; + + let sig = Signature::now("Testificate", "test@ifica.te")?; + repo.commit( + Some("HEAD"), + &sig, + &sig, + "test commit please ignore", + &tree, + &[], + )?; + + let obj = repo.revparse_single("HEAD")?; + repo.tag(TAG, &obj, &sig, &format!("version {}", TAG), false)?; + assert!(super::has_tag(dir.path().into(), TAG.into())?); + + Ok(()) + } +} diff --git a/src/main.rs b/src/main.rs index 439ec64..7d141ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use structopt::StructOpt; mod changelog; mod cmd; +mod git; mod gitea; mod version;