bitbucket_cli/cli/
branch_restriction.rs1use anyhow::Result;
2use clap::Subcommand;
3use colored::Colorize;
4use tabled::{Table, Tabled};
5
6use super::{confirm_or_abort, output_json, parse_repo, print_json};
7use crate::api::BitbucketClient;
8
9#[derive(Subcommand)]
10pub enum BranchRestrictionCommands {
11 List {
13 repo: String,
15
16 #[arg(short, long, default_value = "25")]
18 limit: u32,
19 },
20
21 Create {
23 repo: String,
25
26 #[arg(long)]
28 kind: String,
29
30 #[arg(long)]
32 pattern: String,
33
34 #[arg(long, value_name = "N")]
36 value: Option<u64>,
37 },
38
39 Delete {
41 repo: String,
43
44 id: u64,
46
47 #[arg(short, long)]
49 yes: bool,
50 },
51}
52
53#[derive(Tabled)]
54struct RestrictionRow {
55 #[tabled(rename = "ID")]
56 id: String,
57 #[tabled(rename = "KIND")]
58 kind: String,
59 #[tabled(rename = "PATTERN")]
60 pattern: String,
61 #[tabled(rename = "VALUE")]
62 value: String,
63}
64
65impl BranchRestrictionCommands {
66 pub async fn run(self) -> Result<()> {
67 match self {
68 BranchRestrictionCommands::List { repo, limit } => {
69 let (workspace, repo_slug) = parse_repo(&repo)?;
70 let client = BitbucketClient::from_stored().await?;
71 let restrictions = client
72 .list_branch_restrictions(&workspace, &repo_slug, Some(limit.clamp(1, 100)))
73 .await?;
74
75 if output_json() {
76 return print_json(&restrictions.values);
77 }
78
79 if restrictions.values.is_empty() {
80 println!("No branch restrictions found in {}", repo);
81 return Ok(());
82 }
83
84 let rows: Vec<RestrictionRow> = restrictions
85 .values
86 .iter()
87 .map(|r| RestrictionRow {
88 id: r.id.map(|id| id.to_string()).unwrap_or_default(),
89 kind: r.kind.clone(),
90 pattern: r.pattern.clone().unwrap_or_default(),
91 value: r
92 .value
93 .map(|v| v.to_string())
94 .unwrap_or_else(|| "-".to_string()),
95 })
96 .collect();
97
98 println!("{}", Table::new(rows));
99
100 if restrictions.next.is_some() {
101 println!(
102 "\n{} More branch restrictions available. Use --limit to see more.",
103 "ℹ".blue()
104 );
105 }
106
107 Ok(())
108 }
109
110 BranchRestrictionCommands::Create {
111 repo,
112 kind,
113 pattern,
114 value,
115 } => {
116 let (workspace, repo_slug) = parse_repo(&repo)?;
117 let client = BitbucketClient::from_stored().await?;
118
119 let restriction = client
120 .create_branch_restriction(&workspace, &repo_slug, &kind, &pattern, value)
121 .await?;
122
123 if output_json() {
124 return print_json(&restriction);
125 }
126
127 let id_label = restriction
128 .id
129 .map(|id| format!(" (id {})", id))
130 .unwrap_or_default();
131
132 println!(
133 "{} Created {} restriction on {} for pattern {}{}",
134 "✓".green(),
135 restriction.kind.cyan(),
136 repo,
137 restriction.pattern.as_deref().unwrap_or(&pattern).cyan(),
138 id_label
139 );
140
141 Ok(())
142 }
143
144 BranchRestrictionCommands::Delete { repo, id, yes } => {
145 let (workspace, repo_slug) = parse_repo(&repo)?;
146
147 if !yes
148 && !confirm_or_abort(format!(
149 "Delete branch restriction {} from {}?",
150 id.to_string().red(),
151 repo
152 ))?
153 {
154 return Ok(());
155 }
156
157 let client = BitbucketClient::from_stored().await?;
158 client
159 .delete_branch_restriction(&workspace, &repo_slug, id)
160 .await?;
161
162 if output_json() {
163 return print_json(&serde_json::json!({ "ok": true }));
164 }
165
166 println!("{} Deleted branch restriction {}", "✓".green(), id);
167
168 Ok(())
169 }
170 }
171 }
172}