bitbucket-cli 1.1.0

A powerful command-line interface for Bitbucket Cloud - manage repos, PRs, issues, and pipelines from your terminal with OAuth 2.0
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
use std::io::{IsTerminal, Read};

use anyhow::{Context, Result};
use clap::Subcommand;
use colored::Colorize;
use tabled::{Table, Tabled};

use super::{confirm_or_abort, output_json, parse_repo, print_json};
use crate::api::BitbucketClient;
use crate::models::{CreateWebhookRequest, SecretUpdate, Webhook};

#[derive(Subcommand)]
pub enum WebhookCommands {
    /// List webhooks on a repository
    List {
        /// Repository in format workspace/repo-slug
        repo: String,

        /// Number of results per page (max 100)
        #[arg(short, long, default_value = "25")]
        limit: u32,
    },

    /// Create a webhook
    Create {
        /// Repository in format workspace/repo-slug
        repo: String,

        /// Destination URL the events are delivered to
        #[arg(long)]
        url: String,

        /// Event key to subscribe to (repeatable, e.g. --event repo:push --event pullrequest:created)
        #[arg(long = "event", value_name = "EVENT", required = true)]
        events: Vec<String>,

        /// Description of the webhook
        #[arg(short, long)]
        description: Option<String>,

        /// Signing secret for the X-Hub-Signature HMAC. Pass `-` to read the
        /// secret from stdin (recommended: a literal value is visible in the
        /// process list and shell history). Omit to leave the hook unsigned;
        /// no equivalent of `update --clear-secret` is needed at creation.
        #[arg(long, value_name = "SECRET")]
        secret: Option<String>,

        /// Create the webhook in a disabled state
        #[arg(long)]
        inactive: bool,
    },

    /// View webhook details
    View {
        /// Repository in format workspace/repo-slug
        repo: String,

        /// Webhook UUID (the brace-wrapped UUID shown by `webhook list`)
        uid: String,
    },

    /// Update a webhook (unset fields keep their current values)
    Update {
        /// Repository in format workspace/repo-slug
        repo: String,

        /// Webhook UUID (the brace-wrapped UUID shown by `webhook list`)
        uid: String,

        /// New destination URL
        #[arg(long)]
        url: Option<String>,

        /// Replace the subscribed events (repeatable)
        #[arg(long = "event", value_name = "EVENT")]
        events: Vec<String>,

        /// New description
        #[arg(short, long)]
        description: Option<String>,

        /// Set/replace the signing secret for the X-Hub-Signature HMAC. Pass
        /// `-` to read the secret from stdin (recommended: a literal value is
        /// visible in the process list and shell history).
        #[arg(long, value_name = "SECRET", conflicts_with = "clear_secret")]
        secret: Option<String>,

        /// Remove the configured signing secret from the webhook
        #[arg(long)]
        clear_secret: bool,

        /// Enable or disable the webhook
        #[arg(long, value_name = "true|false")]
        active: Option<bool>,
    },

    /// Delete a webhook
    Delete {
        /// Repository in format workspace/repo-slug
        repo: String,

        /// Webhook UUID (the brace-wrapped UUID shown by `webhook list`)
        uid: String,

        /// Skip confirmation prompt
        #[arg(short, long)]
        yes: bool,
    },
}

#[derive(Tabled)]
struct WebhookRow {
    #[tabled(rename = "UUID")]
    uid: String,
    #[tabled(rename = "URL")]
    url: String,
    #[tabled(rename = "ACTIVE")]
    active: String,
    #[tabled(rename = "SECRET")]
    secret: String,
    #[tabled(rename = "EVENTS")]
    events: String,
}

impl WebhookCommands {
    pub async fn run(self) -> Result<()> {
        match self {
            WebhookCommands::List { repo, limit } => {
                let (workspace, repo_slug) = parse_repo(&repo)?;
                let client = BitbucketClient::from_stored().await?;
                let hooks = client
                    .list_webhooks(&workspace, &repo_slug, Some(limit.clamp(1, 100)))
                    .await?;

                if output_json() {
                    return print_json(&hooks.values);
                }

                if hooks.values.is_empty() {
                    println!("No webhooks found in {}", repo);
                    return Ok(());
                }

                let rows: Vec<WebhookRow> = hooks
                    .values
                    .iter()
                    .map(|h| WebhookRow {
                        uid: h.uuid.clone().unwrap_or_default(),
                        url: h.url.clone(),
                        active: if h.active.unwrap_or(false) {
                            "Yes"
                        } else {
                            "No"
                        }
                        .to_string(),
                        secret: if h.secret_set.unwrap_or(false) {
                            "Yes"
                        } else {
                            "No"
                        }
                        .to_string(),
                        events: h.events.join(", ").chars().take(60).collect(),
                    })
                    .collect();

                println!("{}", Table::new(rows));

                if hooks.next.is_some() {
                    println!(
                        "\n{} More webhooks available. Use --limit to see more.",
                        "ℹ".blue()
                    );
                }

                Ok(())
            }

            WebhookCommands::Create {
                repo,
                url,
                events,
                description,
                secret,
                inactive,
            } => {
                let (workspace, repo_slug) = parse_repo(&repo)?;
                let client = BitbucketClient::from_stored().await?;

                let request = CreateWebhookRequest {
                    description,
                    url,
                    active: !inactive,
                    events,
                    secret: resolve_secret_update(secret, false)?,
                };

                let hook = client
                    .create_webhook(&workspace, &repo_slug, &request)
                    .await?;

                if output_json() {
                    return print_json(&hook);
                }

                println!(
                    "{} Created webhook {} for {}",
                    "✓".green(),
                    hook.uuid.as_deref().unwrap_or("(no uid)").cyan(),
                    hook.url
                );

                Ok(())
            }

            WebhookCommands::View { repo, uid } => {
                let (workspace, repo_slug) = parse_repo(&repo)?;
                let client = BitbucketClient::from_stored().await?;
                let hook = client.get_webhook(&workspace, &repo_slug, &uid).await?;

                if output_json() {
                    return print_json(&hook);
                }

                println!("{}", hook.uuid.as_deref().unwrap_or(&uid).bold());
                println!("{}", "─".repeat(50));

                if let Some(desc) = &hook.description {
                    if !desc.is_empty() {
                        println!("{}", desc);
                        println!();
                    }
                }

                println!("{} {}", "URL:".dimmed(), hook.url.cyan());
                println!(
                    "{} {}",
                    "Active:".dimmed(),
                    if hook.active.unwrap_or(false) {
                        "Yes"
                    } else {
                        "No"
                    }
                );
                println!(
                    "{} {}",
                    "Secret:".dimmed(),
                    if hook.secret_set.unwrap_or(false) {
                        "Set"
                    } else {
                        "Not set"
                    }
                );

                if hook.events.is_empty() {
                    println!("{} (none)", "Events:".dimmed());
                } else {
                    println!("{}", "Events:".dimmed());
                    for event in &hook.events {
                        println!("  {}", event);
                    }
                }

                Ok(())
            }

            WebhookCommands::Update {
                repo,
                uid,
                url,
                events,
                description,
                secret,
                clear_secret,
                active,
            } => {
                let (workspace, repo_slug) = parse_repo(&repo)?;

                let secret_update = resolve_secret_update(secret, clear_secret)?;

                if url.is_none()
                    && events.is_empty()
                    && description.is_none()
                    && active.is_none()
                    && secret_update == SecretUpdate::Unchanged
                {
                    anyhow::bail!(
                        "Nothing to update. Pass at least one option, e.g. --url or --active."
                    );
                }

                let client = BitbucketClient::from_stored().await?;

                // PUT replaces the whole subscription, so merge the requested
                // changes into the current state before sending.
                let current = client.get_webhook(&workspace, &repo_slug, &uid).await?;
                let request =
                    build_webhook_update(current, url, events, description, active, secret_update);

                let updated = client
                    .update_webhook(&workspace, &repo_slug, &uid, &request)
                    .await?;

                if output_json() {
                    return print_json(&updated);
                }

                println!(
                    "{} Updated webhook {}",
                    "✓".green(),
                    updated.uuid.as_deref().unwrap_or(&uid).cyan()
                );

                Ok(())
            }

            WebhookCommands::Delete { repo, uid, yes } => {
                let (workspace, repo_slug) = parse_repo(&repo)?;

                if !yes
                    && !confirm_or_abort(format!("Delete webhook {} from {}?", uid.red(), repo))?
                {
                    return Ok(());
                }

                let client = BitbucketClient::from_stored().await?;
                client.delete_webhook(&workspace, &repo_slug, &uid).await?;

                if output_json() {
                    return print_json(&serde_json::json!({ "ok": true }));
                }

                println!("{} Deleted webhook {}", "✓".green(), uid);

                Ok(())
            }
        }
    }
}

/// Translate the `--secret` / `--clear-secret` flags into a [`SecretUpdate`].
///
/// `--secret <value>` sets the secret; the sentinel `--secret -` reads the
/// secret from stdin so callers can pipe it in without exposing it in the
/// process list or shell history. `--clear-secret` removes the secret. Passing
/// neither leaves the secret untouched. Clap enforces that the two flags are
/// mutually exclusive, so `secret.is_some()` takes precedence defensively.
fn resolve_secret_update(secret: Option<String>, clear_secret: bool) -> Result<SecretUpdate> {
    match (secret, clear_secret) {
        (Some(value), _) if value == "-" => Ok(SecretUpdate::Set(read_secret_from_stdin()?)),
        (Some(value), _) => Ok(SecretUpdate::Set(value)),
        (None, true) => Ok(SecretUpdate::Clear),
        (None, false) => Ok(SecretUpdate::Unchanged),
    }
}

/// Read a signing secret from stdin, trimming a single trailing newline so
/// `printf %s | ...` and `echo | ...` both work. Rejects an empty secret so a
/// stray empty pipe can't silently create an unsigned hook the user believed
/// was signed.
fn read_secret_from_stdin() -> Result<String> {
    let mut buf = String::new();
    if std::io::stdin().is_terminal() {
        anyhow::bail!(
            "`--secret -` expects the secret on stdin, but stdin is a terminal. \
             Pipe the secret in, e.g. `printf %s \"$SECRET\" | bitbucket ... --secret -`."
        );
    }
    std::io::stdin()
        .read_to_string(&mut buf)
        .context("Failed to read secret from stdin")?;
    let secret = buf.strip_suffix('\n').unwrap_or(&buf);
    let secret = secret.strip_suffix('\r').unwrap_or(secret);
    if secret.is_empty() {
        anyhow::bail!("Secret read from stdin was empty");
    }
    Ok(secret.to_string())
}

/// Merge `webhook update` flags into the current subscription to produce the
/// full replacement body Bitbucket's PUT expects. Unset flags keep the current
/// values; a webhook with no recorded `active` state defaults to enabled.
fn build_webhook_update(
    current: Webhook,
    url: Option<String>,
    events: Vec<String>,
    description: Option<String>,
    active: Option<bool>,
    secret: SecretUpdate,
) -> CreateWebhookRequest {
    CreateWebhookRequest {
        description: description.or(current.description),
        url: url.unwrap_or(current.url),
        active: active.or(current.active).unwrap_or(true),
        events: if events.is_empty() {
            current.events
        } else {
            events
        },
        secret,
    }
}

#[cfg(test)]
mod tests {
    use super::{build_webhook_update, resolve_secret_update};
    use crate::models::{SecretUpdate, Webhook};

    fn current() -> Webhook {
        Webhook {
            uuid: Some("{uid}".to_string()),
            url: "https://old.example.com/hook".to_string(),
            description: Some("old description".to_string()),
            active: Some(true),
            events: vec!["repo:push".to_string()],
            secret_set: Some(true),
        }
    }

    #[test]
    fn build_webhook_update_keeps_current_values_when_flags_unset() {
        let request = build_webhook_update(
            current(),
            None,
            Vec::new(),
            None,
            None,
            SecretUpdate::Unchanged,
        );
        assert_eq!(request.url, "https://old.example.com/hook");
        assert_eq!(request.description.as_deref(), Some("old description"));
        assert!(request.active);
        assert_eq!(request.events, vec!["repo:push".to_string()]);
        assert_eq!(request.secret, SecretUpdate::Unchanged);
    }

    #[test]
    fn build_webhook_update_applies_changed_fields() {
        let request = build_webhook_update(
            current(),
            Some("https://new.example.com/hook".to_string()),
            vec!["pullrequest:created".to_string()],
            Some("new description".to_string()),
            Some(false),
            SecretUpdate::Set("topsecret".to_string()),
        );
        assert_eq!(request.url, "https://new.example.com/hook");
        assert_eq!(request.description.as_deref(), Some("new description"));
        assert!(!request.active);
        assert_eq!(request.events, vec!["pullrequest:created".to_string()]);
        assert_eq!(request.secret, SecretUpdate::Set("topsecret".to_string()));
    }

    #[test]
    fn build_webhook_update_defaults_unknown_active_state_to_enabled() {
        let mut hook = current();
        hook.active = None;
        let request =
            build_webhook_update(hook, None, Vec::new(), None, None, SecretUpdate::Unchanged);
        assert!(request.active);
    }

    #[test]
    fn build_webhook_update_can_clear_secret() {
        let request =
            build_webhook_update(current(), None, Vec::new(), None, None, SecretUpdate::Clear);
        assert_eq!(request.secret, SecretUpdate::Clear);
    }

    #[test]
    fn build_webhook_update_leaves_secret_unchanged_by_default() {
        // Merging must never resurrect a plaintext secret from the read model
        // (Bitbucket never returns one), so the default is always Unchanged.
        let request = build_webhook_update(
            current(),
            Some("https://new.example.com/hook".to_string()),
            Vec::new(),
            None,
            None,
            SecretUpdate::Unchanged,
        );
        assert_eq!(request.secret, SecretUpdate::Unchanged);
    }

    #[test]
    fn resolve_secret_update_sets_literal_value() {
        let resolved = resolve_secret_update(Some("hunter2".to_string()), false).unwrap();
        assert_eq!(resolved, SecretUpdate::Set("hunter2".to_string()));
    }

    #[test]
    fn resolve_secret_update_clears_when_flag_set() {
        let resolved = resolve_secret_update(None, true).unwrap();
        assert_eq!(resolved, SecretUpdate::Clear);
    }

    #[test]
    fn resolve_secret_update_unchanged_when_no_flags() {
        let resolved = resolve_secret_update(None, false).unwrap();
        assert_eq!(resolved, SecretUpdate::Unchanged);
    }

    #[test]
    fn resolve_secret_update_literal_value_wins_over_clear() {
        // Clap enforces mutual exclusion, but the resolver must still be
        // deterministic if both ever arrive together: an explicit value wins.
        let resolved = resolve_secret_update(Some("hunter2".to_string()), true).unwrap();
        assert_eq!(resolved, SecretUpdate::Set("hunter2".to_string()));
    }
}