1use anyhow::Result;
2use clap::Subcommand;
3use colored::Colorize;
4use tabled::{Table, Tabled};
5
6use super::commit::{author_label, first_line_truncated, short_hash};
7use super::{confirm_or_abort, output_json, parse_repo, print_json};
8use crate::api::BitbucketClient;
9use crate::cli::pagination::effective_limit;
10use crate::models::TagDetail;
11
12const MSG_WIDTH: usize = 50;
14
15#[derive(Subcommand)]
16pub enum TagCommands {
17 List {
19 repo: String,
21
22 #[arg(short = 'q', long)]
24 query: Option<String>,
25
26 #[arg(long)]
28 sort: Option<String>,
29
30 #[arg(short, long, default_value = "25")]
32 limit: u32,
33
34 #[arg(long)]
36 page: Option<u32>,
37 },
38
39 Create {
41 repo: String,
43
44 name: String,
46
47 #[arg(long, value_name = "HASH")]
49 from: String,
50
51 #[arg(short = 'm', long)]
53 message: Option<String>,
54 },
55
56 View {
58 repo: String,
60
61 name: String,
63 },
64
65 Delete {
67 repo: String,
69
70 name: String,
72
73 #[arg(short, long)]
75 yes: bool,
76 },
77}
78
79#[derive(Tabled)]
80struct TagRow {
81 #[tabled(rename = "NAME")]
82 name: String,
83 #[tabled(rename = "COMMIT")]
84 commit: String,
85 #[tabled(rename = "DATE")]
86 date: String,
87 #[tabled(rename = "MESSAGE")]
88 message: String,
89}
90
91impl From<&TagDetail> for TagRow {
92 fn from(tag: &TagDetail) -> Self {
93 let target = tag.target.as_ref();
94 TagRow {
95 name: tag.name.clone(),
96 commit: target.map(|t| short_hash(&t.hash)).unwrap_or_default(),
97 date: tag
100 .date
101 .or_else(|| target.and_then(|t| t.date))
102 .map(|d| d.format("%Y-%m-%d").to_string())
103 .unwrap_or_default(),
104 message: tag
105 .message
106 .as_deref()
107 .map(|m| first_line_truncated(m, MSG_WIDTH))
108 .unwrap_or_default(),
109 }
110 }
111}
112
113impl TagCommands {
114 pub async fn run(self) -> Result<()> {
115 match self {
116 TagCommands::List {
117 repo,
118 query,
119 sort,
120 limit,
121 page,
122 } => {
123 let (workspace, repo_slug) = parse_repo(&repo)?;
124 let client = BitbucketClient::from_stored().await?;
125 let tags = client
126 .list_tags_filtered(
127 &workspace,
128 &repo_slug,
129 query.as_deref(),
130 sort.as_deref(),
131 page,
132 Some(effective_limit(limit)),
133 )
134 .await?;
135
136 if output_json() {
137 return print_json(&tags.values);
138 }
139
140 if tags.values.is_empty() {
141 println!("No tags found in {}", repo);
142 return Ok(());
143 }
144
145 let rows: Vec<TagRow> = tags.values.iter().map(TagRow::from).collect();
146 println!("{}", Table::new(rows));
147
148 if tags.next.is_some() {
149 println!(
150 "\n{} More tags available. Use --limit or --page to see more.",
151 "ℹ".blue()
152 );
153 }
154
155 Ok(())
156 }
157
158 TagCommands::Create {
159 repo,
160 name,
161 from,
162 message,
163 } => {
164 let (workspace, repo_slug) = parse_repo(&repo)?;
165 let client = BitbucketClient::from_stored().await?;
166
167 let tag = client
168 .create_tag(&workspace, &repo_slug, &name, &from, message.as_deref())
169 .await?;
170
171 if output_json() {
172 return print_json(&tag);
173 }
174
175 println!(
176 "{} Created tag {} at {}",
177 "✓".green(),
178 tag.name.cyan(),
179 tag.target
180 .as_ref()
181 .map(|t| short_hash(&t.hash))
182 .unwrap_or_else(|| short_hash(&from))
183 );
184
185 Ok(())
186 }
187
188 TagCommands::View { repo, name } => {
189 let (workspace, repo_slug) = parse_repo(&repo)?;
190 let client = BitbucketClient::from_stored().await?;
191 let tag = client.get_tag(&workspace, &repo_slug, &name).await?;
192
193 if output_json() {
194 return print_json(&tag);
195 }
196
197 println!("{}", tag.name.bold());
198 println!("{}", "─".repeat(50));
199
200 if let Some(date) = tag.date {
201 println!(
202 "{} {}",
203 "Tagged:".dimmed(),
204 date.format("%Y-%m-%d %H:%M:%S")
205 );
206 }
207
208 if let Some(message) = &tag.message {
209 if !message.trim().is_empty() {
210 println!("{} {}", "Message:".dimmed(), message.trim_end());
211 }
212 }
213
214 if let Some(target) = &tag.target {
215 println!();
216 println!("{} {}", "Commit:".dimmed(), target.hash);
217 println!(
218 "{} {}",
219 "Author:".dimmed(),
220 author_label(target.author.as_ref())
221 );
222
223 if let Some(date) = target.date {
224 println!("{} {}", "Date:".dimmed(), date.format("%Y-%m-%d %H:%M:%S"));
225 }
226
227 if let Some(message) = &target.message {
228 println!();
229 println!("{}", message.trim_end());
230 }
231 }
232
233 Ok(())
234 }
235
236 TagCommands::Delete { repo, name, yes } => {
237 let (workspace, repo_slug) = parse_repo(&repo)?;
238
239 if !yes && !confirm_or_abort(format!("Delete tag {} from {}?", name.red(), repo))? {
240 return Ok(());
241 }
242
243 let client = BitbucketClient::from_stored().await?;
244 client.delete_tag(&workspace, &repo_slug, &name).await?;
245
246 if output_json() {
247 return print_json(&serde_json::json!({ "ok": true }));
248 }
249
250 println!("{} Deleted tag {}", "✓".green(), name);
251
252 Ok(())
253 }
254 }
255 }
256}