bbcloud 0.23.0

Bitbucket Cloud CLI — open pull requests, read every comment, write replies, from the shell
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
#![allow(clippy::unwrap_used)] // test code is exempt from the unwrap/expect ban

use assert_cmd::Command;
use predicates::prelude::PredicateBooleanExt;
use predicates::str::contains;
use wiremock::matchers::{body_json, body_partial_json, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

fn bb(server: &MockServer) -> Command {
    let mut cmd = Command::cargo_bin("bb").unwrap();
    cmd.env("BB_NO_UPDATE_CHECK", "1");
    cmd.env("BB_EMAIL", "dev@example.com")
        .env("BB_TOKEN", "t0ken-value")
        .env("BB_API_BASE", server.uri())
        .env("BB_REPO", "acme/widgets")
        .env("NO_COLOR", "1");
    cmd
}

fn created(id: u64) -> serde_json::Value {
    serde_json::json!({
        "id": id,
        "content": { "raw": "looks good" },
        "user": { "display_name": "Me" },
        "created_on": "2026-08-04T10:00:00+00:00",
        "links": { "html": { "href": "https://bitbucket.org/acme/widgets/pull-requests/7#comment-900" } }
    })
}

#[tokio::test]
async fn posts_a_general_comment() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/repositories/acme/widgets/pullrequests/7/comments"))
        .and(body_json(
            serde_json::json!({ "content": { "raw": "looks good" } }),
        ))
        .respond_with(ResponseTemplate::new(201).set_body_json(created(900)))
        .expect(1)
        .mount(&server)
        .await;

    bb(&server)
        .args(["pr", "comment", "7", "--body", "looks good"])
        .assert()
        .success()
        .stdout(contains("900"));
}

#[tokio::test]
async fn posts_an_inline_comment_with_file_and_line() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/repositories/acme/widgets/pullrequests/7/comments"))
        .and(body_partial_json(serde_json::json!({
            "content": { "raw": "off by one" },
            "inline": { "path": "src/main.rs", "to": 42 }
        })))
        .respond_with(ResponseTemplate::new(201).set_body_json(created(901)))
        .expect(1)
        .mount(&server)
        .await;

    bb(&server)
        .args([
            "pr",
            "comment",
            "7",
            "--body",
            "off by one",
            "--file",
            "src/main.rs",
            "--line",
            "42",
        ])
        .assert()
        .success();
}

#[tokio::test]
async fn posts_a_reply_to_an_existing_comment() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/repositories/acme/widgets/pullrequests/7/comments"))
        .and(body_partial_json(serde_json::json!({
            "content": { "raw": "agreed" },
            "parent": { "id": 900 }
        })))
        .respond_with(ResponseTemplate::new(201).set_body_json(created(902)))
        .expect(1)
        .mount(&server)
        .await;

    bb(&server)
        .args([
            "pr",
            "comment",
            "7",
            "--body",
            "agreed",
            "--reply-to",
            "900",
        ])
        .assert()
        .success();
}

#[tokio::test]
async fn reads_the_body_from_stdin() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/repositories/acme/widgets/pullrequests/7/comments"))
        .and(body_json(
            serde_json::json!({ "content": { "raw": "from a pipe" } }),
        ))
        .respond_with(ResponseTemplate::new(201).set_body_json(created(903)))
        .expect(1)
        .mount(&server)
        .await;

    bb(&server)
        .args(["pr", "comment", "7", "--body-stdin"])
        .write_stdin("from a pipe\n")
        .assert()
        .success();
}

#[tokio::test]
async fn line_without_file_is_rejected_before_any_request() {
    let server = MockServer::start().await;
    bb(&server)
        .args(["pr", "comment", "7", "--body", "x", "--line", "42"])
        .assert()
        .failure()
        .stderr(contains("--file"));
}

#[tokio::test]
async fn empty_body_is_rejected() {
    let server = MockServer::start().await;
    bb(&server)
        .args(["pr", "comment", "7", "--body", "   "])
        .assert()
        .failure()
        .stderr(contains("empty"));
}

#[tokio::test]
async fn reply_cannot_be_combined_with_inline_location() {
    let server = MockServer::start().await;
    bb(&server)
        .args([
            "pr",
            "comment",
            "7",
            "--body",
            "x",
            "--reply-to",
            "900",
            "--file",
            "src/main.rs",
            "--line",
            "1",
        ])
        .assert()
        .failure()
        .stderr(contains("--reply-to"));
}

/// `--yes` is the approval, so the request goes out and nothing else is fetched:
/// the comment lookup exists only to fill the prompt a human would have seen.
#[tokio::test]
async fn resolve_and_its_reversal_hit_the_right_verbs() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path(
            "/repositories/acme/widgets/pullrequests/7/comments/900/resolve",
        ))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "user": { "display_name": "Me" }
        })))
        .expect(1)
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path(
            "/repositories/acme/widgets/pullrequests/7/comments/900",
        ))
        .respond_with(ResponseTemplate::new(200).set_body_json(created(900)))
        .expect(0)
        .mount(&server)
        .await;
    Mock::given(method("DELETE"))
        .and(path(
            "/repositories/acme/widgets/pullrequests/7/comments/901/resolve",
        ))
        .respond_with(ResponseTemplate::new(204))
        .expect(1)
        .mount(&server)
        .await;

    bb(&server)
        .args(["pr", "resolve", "7", "900", "--yes"])
        .assert()
        .success()
        .stdout(contains("900"));
    // Reopening restores a reviewer's point rather than hiding one, so it needs
    // no approval.
    bb(&server)
        .args(["pr", "unresolve", "7", "901"])
        .assert()
        .success()
        .stdout(contains("901"));
}

/// The gate: with no terminal there is nobody to approve, so the command must
/// name the flag and leave the thread alone. `expect(0)` is the real assertion —
/// a gate that errors *after* resolving would be no gate at all.
#[tokio::test]
async fn resolve_without_approval_sends_no_request() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path(
            "/repositories/acme/widgets/pullrequests/7/comments/900/resolve",
        ))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({})))
        .expect(0)
        .mount(&server)
        .await;

    bb(&server)
        .args(["pr", "resolve", "7", "900"])
        .write_stdin("y\n") // a piped `yes` must not count as approval either
        .assert()
        .failure()
        .stderr(contains("--yes"));
}

/// The prompt describes the thread, so declining leaves nothing resolved — the
/// same guarantee, one step later. Approving cannot be driven from a piped stdin
/// (that is the point of the gate), so the terminal branch is covered by the
/// unit tests over `describe` in `src/commands/pr_comments.rs`.
#[tokio::test]
async fn resolve_json_stays_pure_when_the_gate_rejects() {
    let server = MockServer::start().await;
    let out = bb(&server)
        .args(["pr", "resolve", "7", "900", "--json"])
        .output()
        .unwrap();
    assert!(!out.status.success());
    assert!(
        out.stdout.is_empty(),
        "stdout must stay empty in json mode: {}",
        String::from_utf8_lossy(&out.stdout)
    );
}

#[tokio::test]
async fn resolve_json_names_the_comment_and_the_pull_request() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path(
            "/repositories/acme/widgets/pullrequests/7/comments/900/resolve",
        ))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({})))
        .mount(&server)
        .await;
    Mock::given(method("DELETE"))
        .and(path(
            "/repositories/acme/widgets/pullrequests/7/comments/900/resolve",
        ))
        .respond_with(ResponseTemplate::new(204))
        .mount(&server)
        .await;

    let out = bb(&server)
        .args(["pr", "resolve", "7", "900", "--yes", "--json"])
        .output()
        .unwrap();
    let value: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(value["resolved"], 900);
    assert_eq!(value["pull_request"], 7);

    let out = bb(&server)
        .args(["pr", "unresolve", "7", "900", "--json"])
        .output()
        .unwrap();
    let value: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(value["unresolved"], 900);
    assert_eq!(value["pull_request"], 7);
}

/// A thread that was already resolved answers 404, which must surface as exit 3
/// rather than a success the caller would read as "done".
#[tokio::test]
async fn resolving_an_unknown_comment_exits_three() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path(
            "/repositories/acme/widgets/pullrequests/7/comments/404/resolve",
        ))
        .respond_with(ResponseTemplate::new(404))
        .mount(&server)
        .await;

    bb(&server)
        .args(["pr", "resolve", "7", "404", "--yes"])
        .assert()
        .code(3);
}

#[tokio::test]
async fn posts_a_pending_comment() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/repositories/acme/widgets/pullrequests/7/comments"))
        .and(body_json(serde_json::json!({
            "content": { "raw": "nit: rename" },
            "pending": true
        })))
        .respond_with(ResponseTemplate::new(201).set_body_json(serde_json::json!({
            "id": 905,
            "content": { "raw": "nit: rename" },
            "user": { "display_name": "Me" },
            "created_on": "2026-08-04T10:00:00+00:00",
            "pending": true
        })))
        .expect(1)
        .mount(&server)
        .await;

    bb(&server)
        .args(["pr", "comment", "7", "--body", "nit: rename", "--pending"])
        .assert()
        .success()
        .stdout(contains("pending comment 905"))
        .stderr(contains("published").not());
}

#[tokio::test]
async fn pending_flag_combines_with_inline_location() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/repositories/acme/widgets/pullrequests/7/comments"))
        .and(body_json(serde_json::json!({
            "content": { "raw": "nit: rename" },
            "inline": { "path": "src/main.rs", "to": 42 },
            "pending": true
        })))
        .respond_with(ResponseTemplate::new(201).set_body_json(created(906)))
        .expect(1)
        .mount(&server)
        .await;

    bb(&server)
        .args([
            "pr",
            "comment",
            "7",
            "--body",
            "nit: rename",
            "--file",
            "src/main.rs",
            "--line",
            "42",
            "--pending",
        ])
        .assert()
        .success();
}

#[tokio::test]
async fn pending_flag_combines_with_reply_to() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/repositories/acme/widgets/pullrequests/7/comments"))
        .and(body_json(serde_json::json!({
            "content": { "raw": "agreed" },
            "parent": { "id": 600 },
            "pending": true
        })))
        .respond_with(ResponseTemplate::new(201).set_body_json(created(908)))
        .expect(1)
        .mount(&server)
        .await;

    bb(&server)
        .args([
            "pr",
            "comment",
            "7",
            "--body",
            "agreed",
            "--reply-to",
            "600",
            "--pending",
        ])
        .assert()
        .success();
}

/// Bitbucket does not always honour `pending: true` — when it doesn't, bb warns
/// on stderr instead of claiming success silently, and `--json` stdout stays pure.
#[tokio::test]
async fn warns_when_bitbucket_publishes_a_pending_comment_immediately() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/repositories/acme/widgets/pullrequests/7/comments"))
        .respond_with(ResponseTemplate::new(201).set_body_json(created(907)))
        .mount(&server)
        .await;

    let out = bb(&server)
        .args([
            "pr",
            "comment",
            "7",
            "--body",
            "looks good",
            "--pending",
            "--json",
        ])
        .output()
        .unwrap();
    assert!(out.status.success());
    assert!(
        String::from_utf8_lossy(&out.stderr).contains("did not keep it pending"),
        "stderr: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let value: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(value["pending"], false);
}

#[tokio::test]
async fn comment_json_reports_the_new_id() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/repositories/acme/widgets/pullrequests/7/comments"))
        .respond_with(ResponseTemplate::new(201).set_body_json(created(904)))
        .mount(&server)
        .await;

    let out = bb(&server)
        .args(["pr", "comment", "7", "--body", "looks good", "--json"])
        .output()
        .unwrap();
    let value: serde_json::Value = serde_json::from_slice(&out.stdout).unwrap();
    assert_eq!(value["id"], 904);
}