implement automagic tagging, uploading and the like for release

This commit is contained in:
Cadey Ratio 2020-05-31 14:13:14 -04:00
parent c960a07ec2
commit 508a100268
4 changed files with 32 additions and 11 deletions

View File

@ -5,9 +5,18 @@ use std::path::PathBuf;
pub(crate) async fn run( pub(crate) async fn run(
common: Common, common: Common,
fname: PathBuf, fname: PathBuf,
tag: String, tag: Option<String>,
rm: ReleaseMeta, rm: ReleaseMeta,
) -> Result<()> { ) -> Result<()> {
let repo = git2::Repository::open(".")?;
let tag = tag.unwrap_or(version::read_version("VERSION".into())?);
let desc = changelog::read(fname.clone(), tag.clone())?;
if !git::has_tag(&repo, tag.clone())? {
git::tag_version(&repo, tag.clone(), desc.clone())?;
let _ = git::push_tags(&repo);
}
let desc = changelog::read(fname, tag.clone())?; let desc = changelog::read(fname, tag.clone())?;
let cli = client(&common)?; let cli = client(&common)?;
let cr = CreateRelease { let cr = CreateRelease {

View File

@ -1,9 +1,22 @@
use anyhow::Result; use anyhow::Result;
use git2::Repository; use git2::{Repository, Signature};
use std::path::PathBuf;
pub(crate) fn has_tag(repo_path: PathBuf, tag: String) -> Result<bool> { pub(crate) fn push_tags(repo: &Repository) -> Result<()> {
let repo = Repository::init(repo_path)?; let mut remote = repo.find_remote("origin")?;
remote.connect(git2::Direction::Push)?;
remote.push(&["refs/tags/*:refs/tags/*"], None)?;
Ok(())
}
pub(crate) fn tag_version(repo: &Repository, tag: String, desc: String) -> Result<()> {
let sig = &Signature::now("Gitea Release Tool", "gitea-release@tulpa.dev")?;
let obj = repo.revparse_single("HEAD")?;
repo.tag(&tag, &obj, &sig, &desc, false)?;
Ok(())
}
pub(crate) fn has_tag(repo: &Repository, tag: String) -> Result<bool> {
let tags = repo.tag_names(Some(&tag))?; let tags = repo.tag_names(Some(&tag))?;
for tag_obj in tags.iter() { for tag_obj in tags.iter() {
@ -40,7 +53,7 @@ mod tests {
let oid = index.write_tree()?; let oid = index.write_tree()?;
let tree = repo.find_tree(oid)?; let tree = repo.find_tree(oid)?;
let sig = Signature::now("Testificate", "test@ifica.te")?; let sig = &Signature::now("Gitea Release Tool", "gitea-release@tulpa.dev")?;
repo.commit( repo.commit(
Some("HEAD"), Some("HEAD"),
&sig, &sig,
@ -50,9 +63,8 @@ mod tests {
&[], &[],
)?; )?;
let obj = repo.revparse_single("HEAD")?; super::tag_version(&repo, TAG.into(), format!("version {}", TAG))?;
repo.tag(TAG, &obj, &sig, &format!("version {}", TAG), false)?; assert!(super::has_tag(&repo, TAG.into())?);
assert!(super::has_tag(dir.path().into(), TAG.into())?);
Ok(()) Ok(())
} }

View File

@ -106,7 +106,7 @@ pub(crate) enum Cmd {
#[structopt(short, long, default_value = "./CHANGELOG.md")] #[structopt(short, long, default_value = "./CHANGELOG.md")]
changelog: PathBuf, changelog: PathBuf,
/// The version tag to operate on /// The version tag to operate on
tag: String, tag: Option<String>,
#[structopt(flatten)] #[structopt(flatten)]
release_meta: ReleaseMeta, release_meta: ReleaseMeta,
}, },

View File

@ -1,7 +1,7 @@
use anyhow::Result; use anyhow::Result;
use std::{fs, path::PathBuf}; use std::{fs, path::PathBuf};
fn read_version(fname: PathBuf) -> Result<String> { pub(crate) fn read_version(fname: PathBuf) -> Result<String> {
let version = fs::read_to_string(fname)?; let version = fs::read_to_string(fname)?;
Ok(version.trim().into()) Ok(version.trim().into())
} }