use clap::Subcommand;
use gitflow_core::{
CliOutput,
release::{CreateReleaseArgs, ReleaseProvider},
};
use gitflow_gitcode::GitCodeReleaseProvider;
use gitflow_github::GitHubReleaseProvider;
use gitflow_gitlab::GitLabReleaseProvider;
use crate::OutputFormat;
#[derive(Debug, Subcommand)]
pub enum ReleaseCommand {
Create {
#[arg(long)]
tag_name: String,
#[arg(long)]
name: Option<String>,
#[arg(long)]
body: Option<String>,
#[arg(long = "body-file")]
body_file: Option<String>,
#[arg(long, default_value_t = false)]
draft: bool,
#[arg(long, default_value_t = false)]
prerelease: bool,
#[arg(long)]
target_commitish: Option<String>,
},
List {
#[arg(long)]
limit: Option<u32>,
},
View {
tag: String,
},
Edit {
tag: String,
#[arg(long)]
name: Option<String>,
#[arg(long)]
body: Option<String>,
#[arg(long = "body-file")]
body_file: Option<String>,
},
Upload {
tag: String,
#[arg(long)]
file: String,
},
Download {
tag: String,
#[arg(long)]
pattern: Option<String>,
#[arg(long)]
dir: Option<String>,
},
Delete {
tag: String,
#[arg(long, short, default_value_t = false)]
yes: bool,
},
}
#[allow(
clippy::too_many_lines,
reason = "Command dispatch: each match arm maps to one operation"
)]
pub async fn handle(
command: ReleaseCommand,
platform: &str,
repo: &str,
output_format: OutputFormat,
) -> miette::Result<()> {
let provider: Box<dyn ReleaseProvider> = match platform {
"github" => Box::new(GitHubReleaseProvider::new(repo)),
"gitlab" => Box::new(GitLabReleaseProvider::new(repo)),
"gitcode" => Box::new(GitCodeReleaseProvider::new(repo)),
other => {
return Err(miette::miette!(
"Platform '{other}' not yet supported for release commands"
));
}
};
match command {
ReleaseCommand::Create {
tag_name,
name,
body,
body_file,
draft,
prerelease,
target_commitish,
} => {
let resolved_body = resolve_body(body, body_file)?;
let args = CreateReleaseArgs {
tag_name,
name,
body: resolved_body,
draft,
prerelease,
target_commitish,
};
let release = provider
.create(args)
.await
.map_err(|e| miette::miette!("Failed to create release: {e}"))?;
let output = CliOutput::success(release, platform, "release create");
print_output(&output, &output_format)?;
}
ReleaseCommand::List { .. } => {
let releases = provider
.list()
.await
.map_err(|e| miette::miette!("Failed to list releases: {e}"))?;
let output = CliOutput::success(releases, platform, "release list");
print_output(&output, &output_format)?;
}
ReleaseCommand::View { tag } => {
let release = provider
.view(&tag)
.await
.map_err(|e| miette::miette!("Failed to view release '{tag}': {e}"))?;
let output = CliOutput::success(release, platform, "release view");
print_output(&output, &output_format)?;
}
ReleaseCommand::Edit {
tag,
name,
body,
body_file,
} => {
let resolved_body = resolve_body(body, body_file)?;
let args = CreateReleaseArgs {
tag_name: tag.clone(),
name,
body: resolved_body,
draft: false,
prerelease: false,
target_commitish: None,
};
let release = provider
.edit(&tag, args)
.await
.map_err(|e| miette::miette!("Failed to edit release '{tag}': {e}"))?;
let output = CliOutput::success(release, platform, "release edit");
print_output(&output, &output_format)?;
}
ReleaseCommand::Upload { tag, file } => {
let asset_name = std::path::Path::new(&file)
.file_name()
.map_or_else(|| file.clone(), |n| n.to_string_lossy().to_string());
provider
.upload_asset(&tag, &file, &asset_name)
.await
.map_err(|e| miette::miette!("Failed to upload asset to release '{tag}': {e}"))?;
let result = serde_json::json!({
"tag_name": tag,
"asset_file": asset_name,
});
let output = CliOutput::success(result, platform, "release upload");
print_output(&output, &output_format)?;
}
ReleaseCommand::Download { tag, pattern, dir } => {
let asset_name = pattern.unwrap_or_else(|| "*".into());
let dest = dir.unwrap_or_else(|| ".".into());
provider
.download_asset(&tag, &asset_name, &dest)
.await
.map_err(|e| {
miette::miette!("Failed to download asset from release '{tag}': {e}")
})?;
let result = serde_json::json!({
"tag_name": tag,
"pattern": asset_name,
"dest": dest,
});
let output = CliOutput::success(result, platform, "release download");
print_output(&output, &output_format)?;
}
ReleaseCommand::Delete { tag, .. } => {
provider
.delete(&tag)
.await
.map_err(|e| miette::miette!("Failed to delete release '{tag}': {e}"))?;
let result = serde_json::json!({
"tag_name": tag,
"deleted": true,
});
let output = CliOutput::success(result, platform, "release delete");
print_output(&output, &output_format)?;
}
}
Ok(())
}
#[allow(
clippy::disallowed_methods,
reason = "Sync file read; called from async handler but file body input is small and local"
)]
fn resolve_body(body: Option<String>, body_file: Option<String>) -> miette::Result<Option<String>> {
if body.is_some() && body_file.is_some() {
return Err(miette::miette!(
"Cannot specify both --body and --body-file"
));
}
if let Some(path) = body_file {
let content = std::fs::read_to_string(&path)
.map_err(|e| miette::miette!("Failed to read body file '{path}': {e}"))?;
return Ok(Some(content));
}
Ok(body)
}
fn print_output<T: serde::Serialize>(value: &T, format: &OutputFormat) -> miette::Result<()> {
crate::commands::output::print_output(value, format)
}
#[cfg(test)]
#[allow(
clippy::panic,
reason = "Test code: panic is acceptable for assertion failures"
)]
mod tests {
use super::*;
#[test]
fn test_should_resolve_body_with_body_only() {
let result = resolve_body(Some("release notes".into()), None);
assert!(result.is_ok());
assert_eq!(
result.expect("already checked"),
Some("release notes".into())
);
}
#[test]
fn test_should_resolve_body_with_none() {
let result = resolve_body(None, None);
assert!(result.is_ok());
assert_eq!(result.expect("already checked"), None);
}
#[test]
fn test_should_resolve_body_from_file() {
let dir = std::env::temp_dir();
let path = dir.join("gitflow_release_body.md");
std::fs::write(&path, "release body content").expect("write temp file");
let result = resolve_body(None, Some(path.to_string_lossy().into_owned()));
let _ = std::fs::remove_file(&path);
assert!(result.is_ok());
assert_eq!(
result.expect("already checked"),
Some("release body content".into())
);
}
#[test]
fn test_should_error_on_missing_body_file() {
let result = resolve_body(None, Some("/nonexistent/path/body.md".into()));
assert!(result.is_err());
let err = result.unwrap_err().to_string();
assert!(err.contains("Failed to read body file"));
}
#[test]
fn test_should_reject_both_body_and_body_file() {
let result = resolve_body(Some("hello".into()), Some("/tmp/body.md".into()));
assert!(result.is_err());
let err = result.unwrap_err().to_string();
assert!(err.contains("Cannot specify both"));
}
#[test]
fn test_should_print_json_output() {
let value = serde_json::json!({"tag_name": "v1.0.0", "name": "v1.0.0"});
let result = print_output(&value, &OutputFormat::Json);
assert!(result.is_ok());
}
#[test]
fn test_should_accept_text_output() {
let value = serde_json::json!({"number": 1});
let result = print_output(&value, &OutputFormat::Text);
assert!(result.is_ok());
}
#[test]
fn test_should_parse_release_create() {
use clap::Parser;
let cli = crate::Cli::try_parse_from([
"gitflow",
"release",
"create",
"--tag-name",
"v1.0.0",
"--name",
"v1.0.0",
"--body",
"Initial release",
])
.expect("parse");
match cli.command {
crate::Commands::Release(ReleaseCommand::Create {
tag_name,
name,
body,
body_file,
draft,
prerelease,
target_commitish,
}) => {
assert_eq!(tag_name, "v1.0.0");
assert_eq!(name, Some("v1.0.0".into()));
assert_eq!(body, Some("Initial release".into()));
assert!(body_file.is_none());
assert!(!draft);
assert!(!prerelease);
assert!(target_commitish.is_none());
}
_ => panic!("Expected ReleaseCommand::Create"),
}
}
#[test]
fn test_should_parse_release_list() {
use clap::Parser;
let cli = crate::Cli::try_parse_from(["gitflow", "release", "list", "--limit", "10"])
.expect("parse");
match cli.command {
crate::Commands::Release(ReleaseCommand::List { limit }) => {
assert_eq!(limit, Some(10));
}
_ => panic!("Expected ReleaseCommand::List"),
}
}
#[test]
fn test_should_parse_release_view() {
use clap::Parser;
let cli =
crate::Cli::try_parse_from(["gitflow", "release", "view", "v1.0.0"]).expect("parse");
match cli.command {
crate::Commands::Release(ReleaseCommand::View { tag }) => {
assert_eq!(tag, "v1.0.0");
}
_ => panic!("Expected ReleaseCommand::View"),
}
}
#[test]
fn test_should_parse_release_edit() {
use clap::Parser;
let cli = crate::Cli::try_parse_from([
"gitflow",
"release",
"edit",
"v1.0.0",
"--name",
"v1.0.1",
"--body",
"Updated notes",
])
.expect("parse");
match cli.command {
crate::Commands::Release(ReleaseCommand::Edit {
tag,
name,
body,
body_file,
}) => {
assert_eq!(tag, "v1.0.0");
assert_eq!(name, Some("v1.0.1".into()));
assert_eq!(body, Some("Updated notes".into()));
assert!(body_file.is_none());
}
_ => panic!("Expected ReleaseCommand::Edit"),
}
}
#[test]
fn test_should_parse_release_upload() {
use clap::Parser;
let cli = crate::Cli::try_parse_from([
"gitflow",
"release",
"upload",
"v1.0.0",
"--file",
"./artifact.tar.gz",
])
.expect("parse");
match cli.command {
crate::Commands::Release(ReleaseCommand::Upload { tag, file }) => {
assert_eq!(tag, "v1.0.0");
assert_eq!(file, "./artifact.tar.gz");
}
_ => panic!("Expected ReleaseCommand::Upload"),
}
}
#[test]
fn test_should_parse_release_download() {
use clap::Parser;
let cli = crate::Cli::try_parse_from([
"gitflow",
"release",
"download",
"v1.0.0",
"--pattern",
"*.tar.gz",
"--dir",
"./downloads",
])
.expect("parse");
match cli.command {
crate::Commands::Release(ReleaseCommand::Download { tag, pattern, dir }) => {
assert_eq!(tag, "v1.0.0");
assert_eq!(pattern, Some("*.tar.gz".into()));
assert_eq!(dir, Some("./downloads".into()));
}
_ => panic!("Expected ReleaseCommand::Download"),
}
}
#[test]
fn test_should_parse_release_delete() {
use clap::Parser;
let cli = crate::Cli::try_parse_from(["gitflow", "release", "delete", "v1.0.0", "--yes"])
.expect("parse");
match cli.command {
crate::Commands::Release(ReleaseCommand::Delete { tag, yes }) => {
assert_eq!(tag, "v1.0.0");
assert!(yes);
}
_ => panic!("Expected ReleaseCommand::Delete"),
}
}
#[test]
fn test_should_parse_release_create_with_draft_and_prerelease() {
use clap::Parser;
let cli = crate::Cli::try_parse_from([
"gitflow",
"release",
"create",
"--tag-name",
"v2.0.0-beta",
"--draft",
"--prerelease",
"--target-commitish",
"abc123",
])
.expect("parse");
match cli.command {
crate::Commands::Release(ReleaseCommand::Create {
tag_name,
draft,
prerelease,
target_commitish,
..
}) => {
assert_eq!(tag_name, "v2.0.0-beta");
assert!(draft);
assert!(prerelease);
assert_eq!(target_commitish, Some("abc123".into()));
}
_ => panic!("Expected ReleaseCommand::Create"),
}
}
#[test]
fn test_should_parse_release_list_without_limit() {
use clap::Parser;
let cli = crate::Cli::try_parse_from(["gitflow", "release", "list"]).expect("parse");
match cli.command {
crate::Commands::Release(ReleaseCommand::List { limit }) => {
assert!(limit.is_none());
}
_ => panic!("Expected ReleaseCommand::List"),
}
}
#[test]
fn test_should_parse_release_download_without_pattern() {
use clap::Parser;
let cli = crate::Cli::try_parse_from(["gitflow", "release", "download", "v1.0.0"])
.expect("parse");
match cli.command {
crate::Commands::Release(ReleaseCommand::Download { tag, pattern, dir }) => {
assert_eq!(tag, "v1.0.0");
assert!(pattern.is_none());
assert!(dir.is_none());
}
_ => panic!("Expected ReleaseCommand::Download"),
}
}
#[test]
fn test_should_print_toon_output() {
let value = serde_json::json!({"tag_name": "v1.0.0"});
let result = print_output(&value, &OutputFormat::Toon);
assert!(result.is_ok());
}
#[test]
fn test_should_print_auto_output() {
let value = serde_json::json!({"tag_name": "v1.0.0"});
let result = print_output(&value, &OutputFormat::Auto);
assert!(result.is_ok());
}
}