bb-cli 0.1.1

bb — a Bitbucket CLI, a gh for Bitbucket.
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
//! `bb repo list` — list repositories in a workspace.

use crate::api::models::Repository;
use crate::api::BitbucketClient;
use crate::core::{AuthError, ColorScheme, Context, FlagError};
use clap::Args;

use crate::auth;
use crate::render::{pad, sanitize};

/// JSON fields a repository can be projected to with `--json`.
const FIELDS: &[&str] = &[
    "slug",
    "name",
    "full_name",
    "is_private",
    "description",
    "mainbranch",
    "links",
];

#[derive(Args, Debug)]
pub struct ListArgs {
    /// Workspace to list repositories for (defaults to the current repo's workspace)
    #[arg(value_name = "WORKSPACE")]
    pub workspace: Option<String>,
    /// Maximum number of repositories to list
    #[arg(long, short = 'L', default_value_t = 30)]
    pub limit: usize,
    #[command(flatten)]
    pub json: crate::output::JsonFlags,
}

/// Run `bb repo list`.
///
/// # Errors
/// Returns [`AuthError`] (exit 4) if not authenticated for the target host,
/// [`FlagError`] (exit 1) if no workspace is given and none can be inferred, and
/// propagates [`ApiError`](crate::core::ApiError) from the listing call.
pub fn run(ctx: &Context, args: ListArgs) -> anyhow::Result<()> {
    let workspace = match args.workspace {
        Some(w) => w,
        // No arg: fall back to the current repo's workspace, but only error on
        // the *absence* of a workspace — git failures shouldn't mask the hint.
        None => ctx
            .base_repo()
            .map(|r| r.workspace().to_owned())
            .map_err(|_| FlagError::new("specify a WORKSPACE to list repositories for"))?,
    };

    let host = ctx.host();
    let header = auth::header_for(ctx.config.as_ref(), &host);
    if header.is_none() {
        return Err(AuthError::new(host).into());
    }
    let client = BitbucketClient::new(ctx.transport.clone(), header);

    let pagelen = args.limit.clamp(1, 100);
    let path = format!("/repositories/{workspace}?pagelen={pagelen}&sort=-updated_on");
    let repos: Vec<Repository> = match client.paginate(&path, Some(args.limit)) {
        Ok(r) => r,
        // A clean "not found" instead of leaking the raw endpoint + querystring.
        Err(e) if e.is_not_found() => {
            return Err(FlagError::new(format!("workspace {workspace} not found")).into());
        }
        Err(e) => return Err(e.into()),
    };

    if args.json.requested() {
        args.json.validate(FIELDS)?;
        args.json.emit(&ctx.io, serde_json::to_value(&repos)?)?;
        return Ok(());
    }

    if repos.is_empty() {
        ctx.io
            .println(&format!("No repositories found in {workspace}."));
        return Ok(());
    }

    if ctx.io.is_stdout_tty() {
        ctx.io.print(&render_table(&repos, ctx.io.color_scheme()));
    } else {
        ctx.io.print(&render_tsv(&repos));
    }
    Ok(())
}

/// The display cells for a repository: name, visibility, default branch.
fn cells(r: &Repository) -> [String; 3] {
    let name = r
        .full_name
        .clone()
        .or_else(|| r.slug.clone())
        .unwrap_or_default();
    let visibility = match r.is_private {
        Some(true) => "private",
        _ => "public",
    };
    let branch = r.mainbranch.as_ref().map_or("", |b| b.name.as_str());
    [sanitize(&name), visibility.to_owned(), sanitize(branch)]
}

/// Tab-separated, no header, no color (for pipes/scripts).
fn render_tsv(repos: &[Repository]) -> String {
    let mut out = String::new();
    for r in repos {
        let [name, vis, branch] = cells(r);
        out.push_str(&format!("{name}\t{vis}\t{branch}\n"));
    }
    out
}

/// A TTY table: a header row plus aligned columns (name colored).
fn render_table(repos: &[Repository], cs: ColorScheme) -> String {
    let rows: Vec<[String; 3]> = repos.iter().map(cells).collect();

    let headers = ["NAME", "VISIBILITY", "BRANCH"];
    let mut widths = headers.map(str::len);
    for row in &rows {
        for (i, cell) in row.iter().enumerate() {
            widths[i] = widths[i].max(cell.chars().count());
        }
    }

    let mut out = String::new();
    for (i, h) in headers.iter().enumerate() {
        out.push_str(&pad(&cs.bold(h), h.chars().count(), widths[i]));
        if i + 1 < headers.len() {
            out.push_str("  ");
        }
    }
    out.push('\n');

    for row in &rows {
        let cells = [cs.cyan(&row[0]), row[1].clone(), row[2].clone()];
        let plain_lens = [
            row[0].chars().count(),
            row[1].chars().count(),
            row[2].chars().count(),
        ];
        for (i, cell) in cells.iter().enumerate() {
            out.push_str(&pad(cell, plain_lens[i], widths[i]));
            if i + 1 < cells.len() {
                out.push_str("  ");
            }
        }
        out.push('\n');
    }
    out
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use crate::api::testing::FakeTransport;
    use crate::config::FileConfig;
    use crate::core::{ConfigProvider, GitClient, Method, Transport};
    use crate::git::{ShellGit, StubRunner};

    use super::*;
    use crate::testsupport::{test_context, ScriptedPrompter};

    /// Git stub answering `remote -v` (so `base_repo` can default).
    fn git() -> Arc<dyn GitClient> {
        let s = Arc::new(StubRunner::new());
        s.register(
            "remote -v",
            0,
            "origin\tgit@bitbucket.org:acme/widgets.git (fetch)\n\
             origin\tgit@bitbucket.org:acme/widgets.git (push)\n",
        );
        Arc::new(ShellGit::new(s))
    }

    /// Git stub that errors on `remote -v` (no Bitbucket remote present).
    fn git_no_remote() -> Arc<dyn GitClient> {
        let s = Arc::new(StubRunner::new());
        s.register("remote -v", 0, "");
        Arc::new(ShellGit::new(s))
    }

    /// Git stub that must never be called (an explicit workspace is passed).
    fn no_git() -> Arc<dyn GitClient> {
        Arc::new(ShellGit::new(Arc::new(StubRunner::new())))
    }

    fn config() -> Arc<dyn ConfigProvider> {
        let cfg = FileConfig::blank();
        cfg.set("bitbucket.org", "auth_type", "app_password")
            .unwrap();
        cfg.set("bitbucket.org", "username", "u").unwrap();
        cfg.set("bitbucket.org", "token", "t").unwrap();
        Arc::new(cfg)
    }

    fn list_args(workspace: Option<&str>) -> ListArgs {
        ListArgs {
            workspace: workspace.map(ToOwned::to_owned),
            limit: 30,
            json: crate::output::JsonFlags::default(),
        }
    }

    const TWO_REPOS: &str = r#"{
        "values": [
            {"slug":"widgets","full_name":"acme/widgets","is_private":true,
             "mainbranch":{"name":"main"}},
            {"slug":"gadgets","full_name":"acme/gadgets","is_private":false,
             "mainbranch":{"name":"trunk"}}
        ]
    }"#;

    #[test]
    fn list_tsv_when_not_tty() {
        let h = Arc::new(FakeTransport::new());
        h.stub(
            "list",
            FakeTransport::rest(Method::Get, "/repositories/acme"),
            FakeTransport::json(200, TWO_REPOS),
        );
        let transport: Arc<dyn Transport> = h.clone();
        let prompter = Arc::new(ScriptedPrompter::new());
        let (ctx, bufs) = test_context(transport, no_git(), config(), prompter, false);

        run(&ctx, list_args(Some("acme"))).unwrap();

        let out = bufs.stdout_string();
        assert_eq!(
            out,
            "acme/widgets\tprivate\tmain\nacme/gadgets\tpublic\ttrunk\n"
        );
        assert!(!out.contains("NAME"), "TSV must have no header");
    }

    #[test]
    fn list_table_when_tty() {
        let h = Arc::new(FakeTransport::new());
        h.stub(
            "list",
            FakeTransport::rest(Method::Get, "/repositories/acme"),
            FakeTransport::json(200, TWO_REPOS),
        );
        let transport: Arc<dyn Transport> = h.clone();
        let prompter = Arc::new(ScriptedPrompter::new());
        let (ctx, bufs) = test_context(transport, no_git(), config(), prompter, true);

        run(&ctx, list_args(Some("acme"))).unwrap();

        let out = bufs.stdout_string();
        let first = out.lines().next().unwrap();
        assert!(first.contains("NAME"), "out: {out}");
        assert!(first.contains("VISIBILITY"), "out: {out}");
        assert!(out.contains("acme/widgets"));
        assert!(out.contains("acme/gadgets"));
        assert!(out.contains("private"));
    }

    #[test]
    fn list_defaults_to_base_repo_workspace() {
        let h = Arc::new(FakeTransport::new());
        h.stub(
            "list",
            FakeTransport::rest(Method::Get, "/repositories/acme"),
            FakeTransport::json(200, TWO_REPOS),
        );
        let transport: Arc<dyn Transport> = h.clone();
        let prompter = Arc::new(ScriptedPrompter::new());
        let (ctx, bufs) = test_context(transport, git(), config(), prompter, false);

        run(&ctx, list_args(None)).unwrap();
        assert!(bufs.stdout_string().contains("acme/widgets"));
    }

    #[test]
    fn list_no_workspace_and_no_repo_is_flag_error() {
        let h = Arc::new(FakeTransport::new());
        let transport: Arc<dyn Transport> = h.clone();
        let prompter = Arc::new(ScriptedPrompter::new());
        let (ctx, _bufs) = test_context(transport, git_no_remote(), config(), prompter, false);

        let err = run(&ctx, list_args(None)).unwrap_err();
        let flag = err.downcast_ref::<FlagError>();
        assert!(flag.is_some(), "expected FlagError, got: {err}");
        assert!(flag.unwrap().0.contains("specify a WORKSPACE"));
    }

    #[test]
    fn list_unknown_workspace_is_clean_flag_error() {
        // A bad workspace 404s the paginate call; the error must be a friendly
        // "workspace ... not found", not a leak of the raw endpoint/querystring.
        let h = Arc::new(FakeTransport::new());
        h.stub(
            "list 404",
            FakeTransport::rest(Method::Get, "/repositories/zzz"),
            FakeTransport::json(
                404,
                r#"{"error":{"message":"No workspace with identifier zzz."}}"#,
            ),
        );
        let transport: Arc<dyn Transport> = h.clone();
        let prompter = Arc::new(ScriptedPrompter::new());
        let (ctx, _bufs) = test_context(transport, no_git(), config(), prompter, false);

        let err = run(&ctx, list_args(Some("zzz"))).unwrap_err();
        let flag = err.downcast_ref::<FlagError>();
        assert!(flag.is_some(), "expected FlagError, got: {err}");
        let msg = &flag.unwrap().0;
        assert!(msg.contains("workspace zzz not found"), "msg: {msg}");
        assert!(
            !msg.contains("https://"),
            "must not leak the endpoint: {msg}"
        );
        assert!(
            !msg.contains("pagelen"),
            "must not leak the querystring: {msg}"
        );
    }

    #[test]
    fn list_empty_prints_message() {
        let h = Arc::new(FakeTransport::new());
        h.stub(
            "list empty",
            FakeTransport::rest(Method::Get, "/repositories/acme"),
            FakeTransport::json(200, r#"{"values": []}"#),
        );
        let transport: Arc<dyn Transport> = h.clone();
        let prompter = Arc::new(ScriptedPrompter::new());
        let (ctx, bufs) = test_context(transport, no_git(), config(), prompter, false);

        run(&ctx, list_args(Some("acme"))).unwrap();
        assert!(bufs
            .stdout_string()
            .contains("No repositories found in acme."));
    }

    #[test]
    fn list_pagelen_clamped_and_sorted() {
        let h = Arc::new(FakeTransport::new());
        h.stub(
            "list big limit",
            FakeTransport::rest(Method::Get, "/repositories/acme"),
            FakeTransport::json(200, r#"{"values": []}"#),
        );
        let transport: Arc<dyn Transport> = h.clone();
        let prompter = Arc::new(ScriptedPrompter::new());
        let (ctx, _bufs) = test_context(transport, no_git(), config(), prompter, false);

        let a = ListArgs {
            limit: 250,
            ..list_args(Some("acme"))
        };
        run(&ctx, a).unwrap();

        let reqs = h.requests.lock().unwrap();
        let url = &reqs[0].url;
        // 250 clamps to the API max of 100.
        assert!(url.contains("pagelen=100"), "url: {url}");
        assert!(url.contains("sort=-updated_on"), "url: {url}");
    }

    #[test]
    fn list_json_emits_projected_fields() {
        let h = Arc::new(FakeTransport::new());
        h.stub(
            "list json",
            FakeTransport::rest(Method::Get, "/repositories/acme"),
            FakeTransport::json(200, TWO_REPOS),
        );
        let transport: Arc<dyn Transport> = h.clone();
        let prompter = Arc::new(ScriptedPrompter::new());
        let (ctx, bufs) = test_context(transport, no_git(), config(), prompter, false);

        let a = ListArgs {
            json: crate::output::JsonFlags {
                json: vec!["slug".into(), "full_name".into(), "is_private".into()],
                jq: None,
                template: None,
            },
            ..list_args(Some("acme"))
        };
        run(&ctx, a).unwrap();

        let out = bufs.stdout_string();
        let v: serde_json::Value = serde_json::from_str(&out).expect("valid JSON");
        let arr = v.as_array().expect("array");
        assert_eq!(arr.len(), 2);
        assert_eq!(arr[0]["slug"], "widgets");
        assert_eq!(arr[0]["full_name"], "acme/widgets");
        assert_eq!(arr[0]["is_private"], true);
        // Unrequested fields are projected away.
        assert!(arr[0].get("mainbranch").is_none(), "out: {out}");
    }

    #[test]
    fn list_json_empty_is_empty_array() {
        let h = Arc::new(FakeTransport::new());
        h.stub(
            "list json empty",
            FakeTransport::rest(Method::Get, "/repositories/acme"),
            FakeTransport::json(200, r#"{"values": []}"#),
        );
        let transport: Arc<dyn Transport> = h.clone();
        let prompter = Arc::new(ScriptedPrompter::new());
        let (ctx, bufs) = test_context(transport, no_git(), config(), prompter, false);

        let a = ListArgs {
            json: crate::output::JsonFlags {
                json: vec!["slug".into()],
                jq: None,
                template: None,
            },
            ..list_args(Some("acme"))
        };
        run(&ctx, a).unwrap();

        let v: serde_json::Value = serde_json::from_str(&bufs.stdout_string()).expect("valid JSON");
        assert_eq!(v, serde_json::json!([]));
    }

    #[test]
    fn list_json_unknown_field_is_flag_error() {
        let h = Arc::new(FakeTransport::new());
        h.stub(
            "list json bogus",
            FakeTransport::rest(Method::Get, "/repositories/acme"),
            FakeTransport::json(200, TWO_REPOS),
        );
        let transport: Arc<dyn Transport> = h.clone();
        let prompter = Arc::new(ScriptedPrompter::new());
        let (ctx, _bufs) = test_context(transport, no_git(), config(), prompter, false);

        let a = ListArgs {
            json: crate::output::JsonFlags {
                json: vec!["bogus".into()],
                jq: None,
                template: None,
            },
            ..list_args(Some("acme"))
        };
        let err = run(&ctx, a).unwrap_err();
        assert!(err.downcast_ref::<FlagError>().is_some());
    }

    #[test]
    fn list_not_logged_in_is_auth_error() {
        let h = Arc::new(FakeTransport::new());
        let transport: Arc<dyn Transport> = h.clone();
        let prompter = Arc::new(ScriptedPrompter::new());
        let cfg: Arc<dyn ConfigProvider> = Arc::new(FileConfig::blank());
        let (ctx, _bufs) = test_context(transport, no_git(), cfg, prompter, false);

        let err = run(&ctx, list_args(Some("acme"))).unwrap_err();
        assert!(err.downcast_ref::<AuthError>().is_some());
    }
}