use anyhow::{Context, Result};
use clap::{Subcommand, ValueEnum};
use colored::Colorize;
use tabled::{Table, Tabled};
use super::parse_repo;
use crate::api::BitbucketClient;
use crate::models::{
BranchInfo, CreatePullRequestRequest, MergePullRequestRequest, MergeStrategy,
PullRequestBranchRef, PullRequestComment, PullRequestState,
};
#[derive(Subcommand)]
pub enum PrCommands {
List {
repo: String,
#[arg(short, long, value_enum)]
state: Option<PrState>,
#[arg(short, long, default_value = "25")]
limit: u32,
},
View {
repo: String,
id: u64,
#[arg(short, long)]
web: bool,
},
Create {
repo: String,
#[arg(short, long)]
title: String,
#[arg(short, long)]
source: String,
#[arg(short, long)]
destination: Option<String>,
#[arg(short = 'b', long)]
body: Option<String>,
#[arg(long)]
close_source_branch: bool,
},
Merge {
repo: String,
id: u64,
#[arg(short, long, value_enum, default_value = "merge-commit")]
strategy: MergeStrategyArg,
#[arg(short, long)]
message: Option<String>,
#[arg(long)]
close_source_branch: bool,
},
Approve {
repo: String,
id: u64,
},
Decline {
repo: String,
id: u64,
},
Checkout {
repo: String,
id: u64,
},
Diff {
repo: String,
id: u64,
},
Comment {
repo: String,
id: u64,
#[arg(short, long)]
body: String,
},
ListComments {
repo: String,
id: u64,
#[arg(short, long, default_value = "25")]
limit: u32,
},
ViewComment {
repo: String,
#[arg(value_name = "PR_ID")]
id: u64,
comment_id: u64,
},
Pipelines {
repo: String,
id: u64,
#[arg(short, long, default_value = "100")]
scan_limit: u32,
},
}
#[derive(ValueEnum, Clone)]
pub enum PrState {
Open,
Merged,
Declined,
Superseded,
}
impl From<PrState> for PullRequestState {
fn from(state: PrState) -> Self {
match state {
PrState::Open => PullRequestState::Open,
PrState::Merged => PullRequestState::Merged,
PrState::Declined => PullRequestState::Declined,
PrState::Superseded => PullRequestState::Superseded,
}
}
}
#[derive(ValueEnum, Clone)]
pub enum MergeStrategyArg {
MergeCommit,
Squash,
FastForward,
}
impl From<MergeStrategyArg> for MergeStrategy {
fn from(strategy: MergeStrategyArg) -> Self {
match strategy {
MergeStrategyArg::MergeCommit => MergeStrategy::MergeCommit,
MergeStrategyArg::Squash => MergeStrategy::Squash,
MergeStrategyArg::FastForward => MergeStrategy::FastForward,
}
}
}
#[derive(Tabled)]
struct PrRow {
#[tabled(rename = "ID")]
id: u64,
#[tabled(rename = "TITLE")]
title: String,
#[tabled(rename = "AUTHOR")]
author: String,
#[tabled(rename = "STATE")]
state: String,
#[tabled(rename = "UPDATED")]
updated: String,
}
#[derive(Tabled)]
struct PipelineRow {
#[tabled(rename = "#")]
build: u64,
#[tabled(rename = "STATUS")]
status: String,
#[tabled(rename = "BRANCH")]
branch: String,
#[tabled(rename = "COMMIT")]
commit: String,
#[tabled(rename = "TRIGGERED")]
triggered: String,
#[tabled(rename = "DURATION")]
duration: String,
}
#[derive(Tabled)]
struct CommentRow {
#[tabled(rename = "ID")]
id: u64,
#[tabled(rename = "AUTHOR")]
author: String,
#[tabled(rename = "CREATED")]
created: String,
#[tabled(rename = "TYPE")]
comment_type: String,
#[tabled(rename = "CONTENT")]
content: String,
}
impl PrCommands {
pub async fn run(self) -> Result<()> {
match self {
PrCommands::List { repo, state, limit } => {
let (workspace, repo_slug) = parse_repo(&repo)?;
let client = BitbucketClient::from_stored().await?;
let prs = client
.list_pull_requests(
&workspace,
&repo_slug,
state.map(|s| s.into()),
None,
Some(limit),
)
.await?;
if prs.values.is_empty() {
println!("No pull requests found");
return Ok(());
}
let rows: Vec<PrRow> = prs
.values
.iter()
.map(|pr| PrRow {
id: pr.id,
title: pr.title.chars().take(50).collect(),
author: pr.author.display_name.clone(),
state: format_state(&pr.state),
updated: pr.updated_on.format("%Y-%m-%d").to_string(),
})
.collect();
let table = Table::new(rows).to_string();
println!("{}", table);
Ok(())
}
PrCommands::View { repo, id, web } => {
let (workspace, repo_slug) = parse_repo(&repo)?;
let client = BitbucketClient::from_stored().await?;
let pr = client.get_pull_request(&workspace, &repo_slug, id).await?;
if web {
if let Some(links) = &pr.links {
if let Some(html) = &links.html {
open::that(&html.href)?;
println!("Opened {} in browser", html.href.cyan());
return Ok(());
}
}
anyhow::bail!("Could not find PR URL");
}
println!("{} {} #{}", format_state(&pr.state), pr.title.bold(), pr.id);
println!("{}", "─".repeat(60));
println!(
"{} {} → {}",
"Branches:".dimmed(),
pr.source.branch.name.cyan(),
pr.destination.branch.name.green()
);
println!("{} {}", "Author:".dimmed(), pr.author.display_name);
println!(
"{} {}",
"Created:".dimmed(),
pr.created_on.format("%Y-%m-%d %H:%M")
);
println!(
"{} {}",
"Updated:".dimmed(),
pr.updated_on.format("%Y-%m-%d %H:%M")
);
if let Some(count) = pr.comment_count {
println!("{} {}", "Comments:".dimmed(), count);
}
if let Some(tasks) = pr.task_count {
if tasks > 0 {
println!("{} {}", "Tasks:".dimmed(), tasks);
}
}
if let Some(participants) = &pr.participants {
let approvals: Vec<_> = participants
.iter()
.filter(|p| p.approved)
.map(|p| p.user.display_name.clone())
.collect();
if !approvals.is_empty() {
println!(
"{} {}",
"Approved by:".dimmed(),
approvals.join(", ").green()
);
}
}
if let Some(description) = &pr.description {
if !description.is_empty() {
println!();
println!("{}", description);
}
}
if let Some(links) = &pr.links {
if let Some(html) = &links.html {
println!();
println!("{} {}", "URL:".dimmed(), html.href.cyan());
}
}
Ok(())
}
PrCommands::Create {
repo,
title,
source,
destination,
body,
close_source_branch,
} => {
let (workspace, repo_slug) = parse_repo(&repo)?;
let client = BitbucketClient::from_stored().await?;
let request = CreatePullRequestRequest {
title,
source: PullRequestBranchRef {
branch: BranchInfo { name: source },
},
destination: destination.map(|d| PullRequestBranchRef {
branch: BranchInfo { name: d },
}),
description: body,
close_source_branch: Some(close_source_branch),
reviewers: None,
};
let pr = client
.create_pull_request(&workspace, &repo_slug, &request)
.await?;
println!("{} Created pull request #{}", "✓".green(), pr.id);
if let Some(links) = &pr.links {
if let Some(html) = &links.html {
println!("{} {}", "URL:".dimmed(), html.href.cyan());
}
}
Ok(())
}
PrCommands::Merge {
repo,
id,
strategy,
message,
close_source_branch,
} => {
let (workspace, repo_slug) = parse_repo(&repo)?;
let client = BitbucketClient::from_stored().await?;
let request = MergePullRequestRequest {
merge_type: Some("pullrequest".to_string()),
message,
close_source_branch: Some(close_source_branch),
merge_strategy: Some(strategy.into()),
};
let pr = client
.merge_pull_request(&workspace, &repo_slug, id, Some(&request))
.await?;
println!("{} Merged pull request #{}", "✓".green(), pr.id);
Ok(())
}
PrCommands::Approve { repo, id } => {
let (workspace, repo_slug) = parse_repo(&repo)?;
let client = BitbucketClient::from_stored().await?;
client
.approve_pull_request(&workspace, &repo_slug, id)
.await?;
println!("{} Approved pull request #{}", "✓".green(), id);
Ok(())
}
PrCommands::Decline { repo, id } => {
let (workspace, repo_slug) = parse_repo(&repo)?;
let client = BitbucketClient::from_stored().await?;
client
.decline_pull_request(&workspace, &repo_slug, id)
.await?;
println!("{} Declined pull request #{}", "✓".green(), id);
Ok(())
}
PrCommands::Checkout { repo, id } => {
let (workspace, repo_slug) = parse_repo(&repo)?;
let client = BitbucketClient::from_stored().await?;
let pr = client.get_pull_request(&workspace, &repo_slug, id).await?;
let branch = &pr.source.branch.name;
println!("Fetching and checking out branch {}...", branch.cyan());
let status = std::process::Command::new("git")
.args(["fetch", "origin", branch])
.status()
.context("Failed to fetch branch")?;
if !status.success() {
anyhow::bail!("git fetch failed");
}
let status = std::process::Command::new("git")
.args(["checkout", branch])
.status()
.context("Failed to checkout branch")?;
if status.success() {
println!("{} Checked out branch {}", "✓".green(), branch);
} else {
let status = std::process::Command::new("git")
.args(["checkout", "-b", branch, &format!("origin/{}", branch)])
.status()
.context("Failed to create tracking branch")?;
if status.success() {
println!("{} Created and checked out branch {}", "✓".green(), branch);
} else {
anyhow::bail!("git checkout failed");
}
}
Ok(())
}
PrCommands::Diff { repo, id } => {
let (workspace, repo_slug) = parse_repo(&repo)?;
let client = BitbucketClient::from_stored().await?;
let diff = client.get_pr_diff(&workspace, &repo_slug, id).await?;
println!("{}", diff);
Ok(())
}
PrCommands::Comment { repo, id, body } => {
let (workspace, repo_slug) = parse_repo(&repo)?;
let client = BitbucketClient::from_stored().await?;
client
.add_pr_comment(&workspace, &repo_slug, id, &body)
.await?;
println!("{} Added comment to pull request #{}", "✓".green(), id);
Ok(())
}
PrCommands::ListComments { repo, id, limit } => {
let (workspace, repo_slug) = parse_repo(&repo)?;
let client = BitbucketClient::from_stored().await?;
let comments = client
.list_recent_pr_comments(&workspace, &repo_slug, id, limit as usize)
.await?;
if comments.is_empty() {
println!("No comments found");
return Ok(());
}
let hit_limit = comments.len() >= limit as usize;
let values = newest_comments_chronological(comments, limit as usize);
let rows: Vec<CommentRow> = values
.iter()
.map(|c| CommentRow {
id: c.id,
author: c.user.display_name.clone(),
created: c.created_on.format("%Y-%m-%d %H:%M").to_string(),
comment_type: if c.inline.is_some() {
"inline".to_string()
} else {
"general".to_string()
},
content: c.content.raw.chars().take(50).collect(),
})
.collect();
let table = Table::new(rows).to_string();
println!("{}", table);
if hit_limit {
println!(
"Showing the {} most recent comments. Use --limit to see more.",
limit
);
}
Ok(())
}
PrCommands::Pipelines {
repo,
id,
scan_limit,
} => {
let (workspace, repo_slug) = parse_repo(&repo)?;
let client = BitbucketClient::from_stored().await?;
let pr = client.get_pull_request(&workspace, &repo_slug, id).await?;
let head_commit = pr
.source
.commit
.as_ref()
.map(|c| c.hash.clone())
.context("PR has no head commit hash")?;
let pipelines = client
.list_pipelines_for_commit(&workspace, &repo_slug, &head_commit, scan_limit)
.await?;
if pipelines.is_empty() {
println!(
"No pipelines found for PR #{} head commit {} (scanned {} most recent).",
id,
head_commit.chars().take(12).collect::<String>(),
scan_limit.clamp(1, 100)
);
return Ok(());
}
let rows: Vec<PipelineRow> = pipelines
.iter()
.map(|p| {
let duration = match (p.build_seconds_used, &p.state.name) {
(Some(s), _) => super::pipeline::format_duration(s),
(None, crate::models::PipelineStateName::InProgress) => {
"running...".to_string()
}
_ => "-".to_string(),
};
PipelineRow {
build: p.build_number,
status: super::pipeline::format_status(
&p.state.name,
p.state.result.as_ref().map(|r| &r.name),
),
branch: p.target.ref_name.clone().unwrap_or_else(|| "-".to_string()),
commit: p
.target
.commit
.as_ref()
.map(|c| c.hash.chars().take(12).collect())
.unwrap_or_else(|| "-".to_string()),
triggered: p.created_on.format("%Y-%m-%d %H:%M").to_string(),
duration,
}
})
.collect();
println!("{}", Table::new(rows));
Ok(())
}
PrCommands::ViewComment {
repo,
id,
comment_id,
} => {
let (workspace, repo_slug) = parse_repo(&repo)?;
let client = BitbucketClient::from_stored().await?;
let comment = client
.get_pr_comment(&workspace, &repo_slug, id, comment_id)
.await?;
println!("{} #{} on PR #{}", "Comment".bold(), comment.id, id);
println!("{}", "─".repeat(60));
println!("{} {}", "Author:".dimmed(), comment.user.display_name);
println!(
"{} {}",
"Created:".dimmed(),
comment.created_on.format("%Y-%m-%d %H:%M")
);
if let Some(updated) = comment.updated_on {
println!(
"{} {}",
"Updated:".dimmed(),
updated.format("%Y-%m-%d %H:%M")
);
}
if let Some(inline) = &comment.inline {
let line = inline.to.or(inline.from);
let location = match line {
Some(l) => format!("{}:{}", inline.path, l),
None => inline.path.clone(),
};
println!("{} inline", "Type:".dimmed());
println!("{} {}", "File:".dimmed(), location.cyan());
} else {
println!("{} general", "Type:".dimmed());
}
println!();
println!("{}", comment.content.raw);
if let Some(links) = &comment.links {
if let Some(html) = &links.html {
println!();
println!("{} {}", "URL:".dimmed(), html.href.cyan());
}
}
Ok(())
}
}
}
}
fn format_state(state: &PullRequestState) -> String {
match state {
PullRequestState::Open => "OPEN".green().to_string(),
PullRequestState::Merged => "MERGED".purple().to_string(),
PullRequestState::Declined => "DECLINED".red().to_string(),
PullRequestState::Superseded => "SUPERSEDED".yellow().to_string(),
}
}
fn newest_comments_chronological(
mut comments: Vec<PullRequestComment>,
limit: usize,
) -> Vec<PullRequestComment> {
comments.sort_by_key(|c| std::cmp::Reverse(c.created_on));
comments.truncate(limit);
comments.reverse();
comments
}
#[cfg(test)]
mod tests {
use super::newest_comments_chronological;
use crate::models::{CommentContent, PullRequestComment, User};
use chrono::{DateTime, Utc};
fn ts(s: &str) -> DateTime<Utc> {
s.parse().unwrap()
}
fn comment(id: u64, created_on: DateTime<Utc>) -> PullRequestComment {
PullRequestComment {
id,
content: CommentContent {
raw: format!("comment {}", id),
markup: None,
html: None,
},
user: User {
uuid: "{user-uuid}".to_string(),
username: None,
display_name: "Test User".to_string(),
account_id: None,
user_type: "user".to_string(),
links: None,
},
created_on,
updated_on: None,
deleted: None,
inline: None,
parent: None,
links: None,
}
}
#[test]
fn keeps_the_newest_comments_in_ascending_display_order() {
let t1 = ts("2024-01-01T00:00:00Z");
let t2 = ts("2024-01-02T00:00:00Z");
let t3 = ts("2024-01-03T00:00:00Z");
let input = vec![comment(3, t3), comment(1, t1), comment(2, t2)];
let result = newest_comments_chronological(input, 2);
let times: Vec<DateTime<Utc>> = result.iter().map(|c| c.created_on).collect();
assert_eq!(times, vec![t2, t3]);
}
#[test]
fn limit_larger_than_input_returns_all_in_ascending_order() {
let t1 = ts("2024-01-01T00:00:00Z");
let t2 = ts("2024-01-02T00:00:00Z");
let t3 = ts("2024-01-03T00:00:00Z");
let input = vec![comment(3, t3), comment(1, t1), comment(2, t2)];
let result = newest_comments_chronological(input, 10);
let times: Vec<DateTime<Utc>> = result.iter().map(|c| c.created_on).collect();
assert_eq!(times, vec![t1, t2, t3]);
}
}