use clap::Subcommand;
use gitflow_cli_core::{CliOutput, pipeline::PipelineProvider};
use gitflow_cli_gitcode::GitCodePipelineProvider;
use gitflow_cli_github::GitHubPipelineProvider;
use gitflow_cli_gitlab::GitLabPipelineProvider;
use crate::OutputFormat;
#[derive(Debug, Subcommand)]
pub enum PipelineCommand {
Status {
#[arg(long, default_value = "main")]
branch: String,
},
Logs {
#[arg(long)]
pipeline_id: u64,
},
Jobs {
#[arg(long)]
pipeline_id: u64,
},
Report {
#[arg(long, default_value = "main")]
branch: String,
#[arg(long, default_value = "30")]
days: u32,
},
}
pub async fn handle(
command: PipelineCommand,
platform: &str,
repo: &str,
output_format: OutputFormat,
) -> miette::Result<()> {
let provider: Box<dyn PipelineProvider> = match platform {
"github" => Box::new(GitHubPipelineProvider::new(repo)),
"gitlab" => Box::new(GitLabPipelineProvider::new(repo)),
"gitcode" => Box::new(GitCodePipelineProvider::new(repo)),
other => {
return Err(miette::miette!(
"Platform '{other}' not yet supported for pipeline commands"
));
}
};
match command {
PipelineCommand::Status { branch } => {
let result = provider.status(&branch).await.map_err(|e| {
miette::miette!("Failed to get pipeline status for '{branch}': {e}")
})?;
let output = CliOutput::success(result, platform, "pipeline status");
print_output(&output, &output_format)?;
}
PipelineCommand::Logs { pipeline_id } => {
let result = provider.logs(pipeline_id).await.map_err(|e| {
miette::miette!("Failed to get logs for pipeline {pipeline_id}: {e}")
})?;
let result_value = serde_json::json!({
"pipeline_id": pipeline_id,
"logs": result,
});
let output = CliOutput::success(result_value, platform, "pipeline logs");
print_output(&output, &output_format)?;
}
PipelineCommand::Jobs { pipeline_id } => {
let result = provider.jobs(pipeline_id).await.map_err(|e| {
miette::miette!("Failed to get jobs for pipeline {pipeline_id}: {e}")
})?;
let output = CliOutput::success(result, platform, "pipeline jobs");
print_output(&output, &output_format)?;
}
PipelineCommand::Report { branch, days } => {
let result = provider.report(&branch, days).await.map_err(|e| {
miette::miette!("Failed to generate pipeline report for '{branch}': {e}")
})?;
let output = CliOutput::success(result, platform, "pipeline report");
print_output(&output, &output_format)?;
}
}
Ok(())
}
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_print_json_output() {
let value = serde_json::json!({"pipeline_id": 1, "status": "success"});
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_pipeline_status_default_branch() {
use clap::Parser;
let cli = crate::Cli::try_parse_from(["gitflow", "pipeline", "status"]).expect("parse");
match cli.command {
crate::Commands::Pipeline(PipelineCommand::Status { branch }) => {
assert_eq!(branch, "main");
}
_ => panic!("Expected PipelineCommand::Status"),
}
}
#[test]
fn test_should_parse_pipeline_status_with_branch() {
use clap::Parser;
let cli =
crate::Cli::try_parse_from(["gitflow", "pipeline", "status", "--branch", "develop"])
.expect("parse");
match cli.command {
crate::Commands::Pipeline(PipelineCommand::Status { branch }) => {
assert_eq!(branch, "develop");
}
_ => panic!("Expected PipelineCommand::Status"),
}
}
#[test]
fn test_should_parse_pipeline_logs() {
use clap::Parser;
let cli =
crate::Cli::try_parse_from(["gitflow", "pipeline", "logs", "--pipeline-id", "12345"])
.expect("parse");
match cli.command {
crate::Commands::Pipeline(PipelineCommand::Logs { pipeline_id }) => {
assert_eq!(pipeline_id, 12_345);
}
_ => panic!("Expected PipelineCommand::Logs"),
}
}
#[test]
fn test_should_parse_pipeline_jobs() {
use clap::Parser;
let cli =
crate::Cli::try_parse_from(["gitflow", "pipeline", "jobs", "--pipeline-id", "99"])
.expect("parse");
match cli.command {
crate::Commands::Pipeline(PipelineCommand::Jobs { pipeline_id }) => {
assert_eq!(pipeline_id, 99);
}
_ => panic!("Expected PipelineCommand::Jobs"),
}
}
#[test]
fn test_should_parse_pipeline_report_default() {
use clap::Parser;
let cli = crate::Cli::try_parse_from(["gitflow", "pipeline", "report"]).expect("parse");
match cli.command {
crate::Commands::Pipeline(PipelineCommand::Report { branch, days }) => {
assert_eq!(branch, "main");
assert_eq!(days, 30);
}
_ => panic!("Expected PipelineCommand::Report"),
}
}
#[test]
fn test_should_parse_pipeline_report_with_args() {
use clap::Parser;
let cli = crate::Cli::try_parse_from([
"gitflow",
"pipeline",
"report",
"--branch",
"release/1.0",
"--days",
"14",
])
.expect("parse");
match cli.command {
crate::Commands::Pipeline(PipelineCommand::Report { branch, days }) => {
assert_eq!(branch, "release/1.0");
assert_eq!(days, 14);
}
_ => panic!("Expected PipelineCommand::Report"),
}
}
}