bitbucket-cli 1.0.1

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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
use std::path::PathBuf;

use anyhow::{Context, Result};
use reqwest::multipart::{Form, Part};

use super::BitbucketClient;
use crate::api::util::urlencode_segment;
use crate::models::{
    CreateIssueCommentRequest, CreateIssueRequest, Issue, IssueAttachment, IssueChange,
    IssueComment, IssueContentRequest, IssueKind, IssueMetaItem, IssuePriority, IssueState,
    Paginated, UserAccountId,
};

/// Fields of an issue that can be changed with
/// [`BitbucketClient::update_issue_full`]. Unset fields are left untouched.
#[derive(Default)]
pub struct IssueUpdate<'a> {
    pub title: Option<&'a str>,
    /// Raw markdown body for the issue description.
    pub content: Option<&'a str>,
    pub kind: Option<IssueKind>,
    pub priority: Option<IssuePriority>,
    /// Atlassian account id of the new assignee.
    pub assignee: Option<&'a str>,
    pub state: Option<IssueState>,
}

impl IssueUpdate<'_> {
    /// True when no field is set, i.e. the update would be a no-op.
    pub fn is_empty(&self) -> bool {
        self.title.is_none()
            && self.content.is_none()
            && self.kind.is_none()
            && self.priority.is_none()
            && self.assignee.is_none()
            && self.state.is_none()
    }
}

/// Filters and paging options for [`BitbucketClient::list_issues_filtered`].
#[derive(Default)]
pub(crate) struct IssueListFilters<'a> {
    pub state: Option<IssueState>,
    pub kind: Option<IssueKind>,
    pub priority: Option<IssuePriority>,
    /// Atlassian account id of the assignee to filter by.
    pub assignee: Option<&'a str>,
    /// Atlassian account id of the reporter to filter by.
    pub reporter: Option<&'a str>,
    /// Raw BBQL query; when set it wins over the individual filters above.
    pub query: Option<&'a str>,
    /// Sort field, e.g. `-updated_on` for most recently updated first.
    pub sort: Option<&'a str>,
    pub page: Option<u32>,
    pub pagelen: Option<u32>,
}

impl BitbucketClient {
    /// List issues for a repository
    pub async fn list_issues(
        &self,
        workspace: &str,
        repo_slug: &str,
        state: Option<IssueState>,
        page: Option<u32>,
        pagelen: Option<u32>,
    ) -> Result<Paginated<Issue>> {
        self.list_issues_filtered(
            workspace,
            repo_slug,
            &IssueListFilters {
                state,
                page,
                pagelen,
                ..Default::default()
            },
        )
        .await
    }

    /// List issues for a repository with the full set of filters, an optional
    /// raw BBQL query, and sorting.
    pub(crate) async fn list_issues_filtered(
        &self,
        workspace: &str,
        repo_slug: &str,
        filters: &IssueListFilters<'_>,
    ) -> Result<Paginated<Issue>> {
        let query = issue_list_query_full(filters);
        let query_refs: Vec<(&str, &str)> = query.iter().map(|(k, v)| (*k, v.as_str())).collect();

        let path = format!("/repositories/{}/{}/issues", workspace, repo_slug);
        self.get_with_query(&path, &query_refs).await
    }

    /// Get a specific issue
    pub async fn get_issue(
        &self,
        workspace: &str,
        repo_slug: &str,
        issue_id: u64,
    ) -> Result<Issue> {
        let path = format!(
            "/repositories/{}/{}/issues/{}",
            workspace, repo_slug, issue_id
        );
        self.get(&path).await
    }

    /// Create a new issue
    pub async fn create_issue(
        &self,
        workspace: &str,
        repo_slug: &str,
        request: &CreateIssueRequest,
    ) -> Result<Issue> {
        let path = format!("/repositories/{}/{}/issues", workspace, repo_slug);
        self.post(&path, request).await
    }

    /// Update an issue's title, description and/or state.
    ///
    /// Convenience wrapper over [`Self::update_issue_full`] kept for the
    /// existing close/reopen callers.
    pub async fn update_issue(
        &self,
        workspace: &str,
        repo_slug: &str,
        issue_id: u64,
        title: Option<&str>,
        content: Option<&str>,
        state: Option<IssueState>,
    ) -> Result<Issue> {
        self.update_issue_full(
            workspace,
            repo_slug,
            issue_id,
            &IssueUpdate {
                title,
                content,
                state,
                ..Default::default()
            },
        )
        .await
    }

    /// Update any combination of an issue's editable fields; unset fields are
    /// omitted from the request body and left unchanged server-side.
    pub async fn update_issue_full(
        &self,
        workspace: &str,
        repo_slug: &str,
        issue_id: u64,
        update: &IssueUpdate<'_>,
    ) -> Result<Issue> {
        #[derive(serde::Serialize)]
        struct UpdateRequest<'a> {
            #[serde(skip_serializing_if = "Option::is_none")]
            title: Option<&'a str>,
            #[serde(skip_serializing_if = "Option::is_none")]
            content: Option<IssueContentRequest>,
            #[serde(skip_serializing_if = "Option::is_none")]
            kind: Option<&'a IssueKind>,
            #[serde(skip_serializing_if = "Option::is_none")]
            priority: Option<&'a IssuePriority>,
            #[serde(skip_serializing_if = "Option::is_none")]
            assignee: Option<UserAccountId>,
            #[serde(skip_serializing_if = "Option::is_none")]
            state: Option<&'a IssueState>,
        }

        let request = UpdateRequest {
            title: update.title,
            content: update
                .content
                .map(|c| IssueContentRequest { raw: c.to_string() }),
            kind: update.kind.as_ref(),
            priority: update.priority.as_ref(),
            assignee: update.assignee.map(|a| UserAccountId {
                account_id: a.to_string(),
            }),
            state: update.state.as_ref(),
        };

        let path = format!(
            "/repositories/{}/{}/issues/{}",
            workspace, repo_slug, issue_id
        );
        self.put(&path, &request).await
    }

    /// Delete an issue
    pub async fn delete_issue(
        &self,
        workspace: &str,
        repo_slug: &str,
        issue_id: u64,
    ) -> Result<()> {
        let path = format!(
            "/repositories/{}/{}/issues/{}",
            workspace, repo_slug, issue_id
        );
        self.delete(&path).await
    }

    /// List comments on an issue
    pub async fn list_issue_comments(
        &self,
        workspace: &str,
        repo_slug: &str,
        issue_id: u64,
        pagelen: Option<u32>,
    ) -> Result<Paginated<IssueComment>> {
        let query: Vec<(&str, String)> = pagelen
            .map(|len| ("pagelen", len.to_string()))
            .into_iter()
            .collect();
        let query_refs: Vec<(&str, &str)> = query.iter().map(|(k, v)| (*k, v.as_str())).collect();

        let path = format!(
            "/repositories/{}/{}/issues/{}/comments",
            workspace, repo_slug, issue_id
        );
        self.get_with_query(&path, &query_refs).await
    }

    /// Add a comment to an issue
    pub async fn add_issue_comment(
        &self,
        workspace: &str,
        repo_slug: &str,
        issue_id: u64,
        content: &str,
    ) -> Result<IssueComment> {
        let request = CreateIssueCommentRequest {
            content: IssueContentRequest {
                raw: content.to_string(),
            },
        };

        let path = format!(
            "/repositories/{}/{}/issues/{}/comments",
            workspace, repo_slug, issue_id
        );
        self.post(&path, &request).await
    }

    /// Update the body of an existing comment on an issue.
    pub async fn update_issue_comment(
        &self,
        workspace: &str,
        repo_slug: &str,
        issue_id: u64,
        comment_id: u64,
        content: &str,
    ) -> Result<IssueComment> {
        let request = CreateIssueCommentRequest {
            content: IssueContentRequest {
                raw: content.to_string(),
            },
        };

        let path = format!(
            "/repositories/{}/{}/issues/{}/comments/{}",
            workspace, repo_slug, issue_id, comment_id
        );
        self.put(&path, &request).await
    }

    /// Delete a comment from an issue.
    pub async fn delete_issue_comment(
        &self,
        workspace: &str,
        repo_slug: &str,
        issue_id: u64,
        comment_id: u64,
    ) -> Result<()> {
        let path = format!(
            "/repositories/{}/{}/issues/{}/comments/{}",
            workspace, repo_slug, issue_id, comment_id
        );
        self.delete(&path).await
    }

    /// List entries from an issue's change log (state transitions,
    /// reassignments, ...).
    pub async fn list_issue_changes(
        &self,
        workspace: &str,
        repo_slug: &str,
        issue_id: u64,
        pagelen: Option<u32>,
    ) -> Result<Paginated<IssueChange>> {
        let query: Vec<(&str, String)> = pagelen
            .map(|len| ("pagelen", len.to_string()))
            .into_iter()
            .collect();
        let query_refs: Vec<(&str, &str)> = query.iter().map(|(k, v)| (*k, v.as_str())).collect();

        let path = format!(
            "/repositories/{}/{}/issues/{}/changes",
            workspace, repo_slug, issue_id
        );
        self.get_with_query(&path, &query_refs).await
    }

    /// List the components defined in a repository's issue tracker,
    /// following pagination until every page has been fetched.
    pub async fn list_issue_components(
        &self,
        workspace: &str,
        repo_slug: &str,
    ) -> Result<Vec<IssueMetaItem>> {
        let path = format!("/repositories/{}/{}/components", workspace, repo_slug);
        self.get_all_pages::<IssueMetaItem>(&path).await
    }

    /// List the milestones defined in a repository's issue tracker,
    /// following pagination until every page has been fetched.
    pub async fn list_issue_milestones(
        &self,
        workspace: &str,
        repo_slug: &str,
    ) -> Result<Vec<IssueMetaItem>> {
        let path = format!("/repositories/{}/{}/milestones", workspace, repo_slug);
        self.get_all_pages::<IssueMetaItem>(&path).await
    }

    /// List the versions defined in a repository's issue tracker,
    /// following pagination until every page has been fetched.
    pub async fn list_issue_versions(
        &self,
        workspace: &str,
        repo_slug: &str,
    ) -> Result<Vec<IssueMetaItem>> {
        let path = format!("/repositories/{}/{}/versions", workspace, repo_slug);
        self.get_all_pages::<IssueMetaItem>(&path).await
    }

    /// List the files attached to an issue, following pagination until every
    /// page has been fetched.
    pub async fn list_issue_attachments(
        &self,
        workspace: &str,
        repo_slug: &str,
        issue_id: u64,
    ) -> Result<Vec<IssueAttachment>> {
        let path = format!(
            "/repositories/{}/{}/issues/{}/attachments",
            workspace, repo_slug, issue_id
        );
        self.get_all_pages::<IssueAttachment>(&path).await
    }

    /// Attach one or more files to an issue via a multipart upload.
    ///
    /// Follows the repository downloads upload convention: every file goes
    /// under the "files" form field, keyed server-side by filename.
    pub async fn upload_issue_attachments(
        &self,
        workspace: &str,
        repo_slug: &str,
        issue_id: u64,
        files: &[(String, PathBuf)],
    ) -> Result<()> {
        let mut form = Form::new();

        for (upload_name, path) in files {
            // Bodies are buffered in memory, matching upload_downloads:
            // streaming would require the reqwest `stream` feature +
            // tokio-util; acceptable for CLI-sized attachments.
            let bytes = std::fs::read(path)
                .with_context(|| format!("Failed to read file '{}'", path.display()))?;
            let part = Part::bytes(bytes).file_name(upload_name.clone());
            form = form.part("files", part);
        }

        let path = format!(
            "/repositories/{}/{}/issues/{}/attachments",
            workspace, repo_slug, issue_id
        );
        self.post_multipart(&path, form).await
    }

    /// Delete an attachment from an issue by its path (usually the filename
    /// as returned by the attachments list).
    pub async fn delete_issue_attachment(
        &self,
        workspace: &str,
        repo_slug: &str,
        issue_id: u64,
        attachment_path: &str,
    ) -> Result<()> {
        let path = format!(
            "/repositories/{}/{}/issues/{}/attachments/{}",
            workspace,
            repo_slug,
            issue_id,
            urlencode_segment(attachment_path)
        );
        self.delete(&path).await
    }

    /// Vote for an issue
    pub async fn vote_issue(&self, workspace: &str, repo_slug: &str, issue_id: u64) -> Result<()> {
        let path = format!(
            "/repositories/{}/{}/issues/{}/vote",
            workspace, repo_slug, issue_id
        );
        self.put::<serde_json::Value, _>(&path, &serde_json::json!({}))
            .await?;
        Ok(())
    }

    /// Remove vote from an issue
    pub async fn unvote_issue(
        &self,
        workspace: &str,
        repo_slug: &str,
        issue_id: u64,
    ) -> Result<()> {
        let path = format!(
            "/repositories/{}/{}/issues/{}/vote",
            workspace, repo_slug, issue_id
        );
        self.delete(&path).await
    }

    /// Watch an issue
    pub async fn watch_issue(&self, workspace: &str, repo_slug: &str, issue_id: u64) -> Result<()> {
        let path = format!(
            "/repositories/{}/{}/issues/{}/watch",
            workspace, repo_slug, issue_id
        );
        self.put::<serde_json::Value, _>(&path, &serde_json::json!({}))
            .await?;
        Ok(())
    }

    /// Unwatch an issue
    pub async fn unwatch_issue(
        &self,
        workspace: &str,
        repo_slug: &str,
        issue_id: u64,
    ) -> Result<()> {
        let path = format!(
            "/repositories/{}/{}/issues/{}/watch",
            workspace, repo_slug, issue_id
        );
        self.delete(&path).await
    }
}

/// Build the query parameters for listing issues.
///
/// Kept with its original three-argument shape so the original tests keep
/// covering the delegation path; the full builder lives in
/// [`issue_list_query_full`], which production code reaches through
/// [`BitbucketClient::list_issues_filtered`].
#[cfg(test)]
fn issue_list_query(
    state: Option<IssueState>,
    page: Option<u32>,
    pagelen: Option<u32>,
) -> Vec<(&'static str, String)> {
    issue_list_query_full(&IssueListFilters {
        state,
        page,
        pagelen,
        ..Default::default()
    })
}

/// Build the full set of query parameters for listing issues.
///
/// A raw BBQL `query` wins over the individual filters; otherwise the
/// filters are AND-ed together into a single `q` expression.
fn issue_list_query_full(filters: &IssueListFilters<'_>) -> Vec<(&'static str, String)> {
    let mut query = Vec::new();

    let q = match filters.query {
        Some(raw) => Some(raw.to_string()),
        None => issue_query_filter(
            filters.state.as_ref(),
            filters.kind.as_ref(),
            filters.priority.as_ref(),
            filters.assignee,
            filters.reporter,
        ),
    };

    if let Some(q) = q {
        query.push(("q", q));
    }
    if let Some(sort) = filters.sort {
        query.push(("sort", sort.to_string()));
    }
    if let Some(p) = filters.page {
        query.push(("page", p.to_string()));
    }
    if let Some(len) = filters.pagelen {
        query.push(("pagelen", len.to_string()));
    }

    query
}

/// Combine individual issue filters into a single BBQL `q` expression, or
/// `None` when no filter is set.
///
/// Every value is quoted, since some states (e.g. "on hold") contain spaces.
/// Assignee and reporter filter on `*.account_id`, matching the account ids
/// the rest of the CLI accepts.
fn issue_query_filter(
    state: Option<&IssueState>,
    kind: Option<&IssueKind>,
    priority: Option<&IssuePriority>,
    assignee: Option<&str>,
    reporter: Option<&str>,
) -> Option<String> {
    let mut parts = Vec::new();

    if let Some(s) = state {
        parts.push(format!("state=\"{}\"", s));
    }
    if let Some(k) = kind {
        parts.push(format!("kind=\"{}\"", k));
    }
    if let Some(p) = priority {
        parts.push(format!("priority=\"{}\"", p));
    }
    if let Some(a) = assignee {
        parts.push(format!("assignee.account_id={}", bbql_quote(a)));
    }
    if let Some(r) = reporter {
        parts.push(format!("reporter.account_id={}", bbql_quote(r)));
    }

    if parts.is_empty() {
        None
    } else {
        Some(parts.join(" AND "))
    }
}

/// Quote a free-text value for use inside a BBQL `q=` expression: wrap it in
/// double quotes, backslash-escaping any `\` (first) then any `"` so the value
/// cannot break out of the quoted string and inject BBQL. Used for
/// user-supplied `--assignee`/`--reporter` values; state/kind/priority come
/// from enums and are already safe.
fn bbql_quote(s: &str) -> String {
    let escaped = s.replace('\\', "\\\\").replace('"', "\\\"");
    format!("\"{}\"", escaped)
}

#[cfg(test)]
mod tests {
    use super::{
        IssueListFilters, bbql_quote, issue_list_query, issue_list_query_full, issue_query_filter,
        urlencode_segment,
    };
    use crate::models::{IssueKind, IssuePriority, IssueState};

    #[test]
    fn open_state_builds_quoted_bbql_filter() {
        assert_eq!(
            issue_list_query(Some(IssueState::Open), None, None),
            vec![("q", "state=\"open\"".to_string())]
        );
    }

    #[test]
    fn on_hold_state_keeps_two_word_value_quoted() {
        assert_eq!(
            issue_list_query(Some(IssueState::OnHold), None, None),
            vec![("q", "state=\"on hold\"".to_string())]
        );
    }

    #[test]
    fn no_state_omits_q_pair() {
        let query = issue_list_query(None, None, None);
        assert!(query.is_empty());
    }

    #[test]
    fn page_and_pagelen_pass_through_as_strings() {
        assert_eq!(
            issue_list_query(None, Some(2), Some(50)),
            vec![("page", "2".to_string()), ("pagelen", "50".to_string())]
        );
    }

    #[test]
    fn state_page_and_pagelen_combine_in_order() {
        assert_eq!(
            issue_list_query(Some(IssueState::Open), Some(3), Some(25)),
            vec![
                ("q", "state=\"open\"".to_string()),
                ("page", "3".to_string()),
                ("pagelen", "25".to_string()),
            ]
        );
    }

    #[test]
    fn query_filter_is_none_without_filters() {
        assert_eq!(issue_query_filter(None, None, None, None, None), None);
    }

    #[test]
    fn query_filter_quotes_on_hold_state() {
        assert_eq!(
            issue_query_filter(Some(&IssueState::OnHold), None, None, None, None).as_deref(),
            Some("state=\"on hold\"")
        );
    }

    #[test]
    fn query_filter_ands_all_filters_in_order() {
        assert_eq!(
            issue_query_filter(
                Some(&IssueState::Open),
                Some(&IssueKind::Bug),
                Some(&IssuePriority::Major),
                Some("acc-123"),
                Some("acc-456"),
            )
            .as_deref(),
            Some(
                "state=\"open\" AND kind=\"bug\" AND priority=\"major\" AND \
                 assignee.account_id=\"acc-123\" AND reporter.account_id=\"acc-456\""
            )
        );
    }

    #[test]
    fn query_filter_handles_people_filters_alone() {
        assert_eq!(
            issue_query_filter(None, None, None, Some("acc-123"), None).as_deref(),
            Some("assignee.account_id=\"acc-123\"")
        );
        assert_eq!(
            issue_query_filter(None, None, None, None, Some("acc-456")).as_deref(),
            Some("reporter.account_id=\"acc-456\"")
        );
    }

    #[test]
    fn full_query_raw_query_wins_over_filters() {
        let filters = IssueListFilters {
            state: Some(IssueState::Open),
            kind: Some(IssueKind::Bug),
            query: Some("kind=\"task\""),
            ..Default::default()
        };
        assert_eq!(
            issue_list_query_full(&filters),
            vec![("q", "kind=\"task\"".to_string())]
        );
    }

    #[test]
    fn full_query_includes_sort_page_and_pagelen_in_order() {
        let filters = IssueListFilters {
            state: Some(IssueState::Open),
            sort: Some("-updated_on"),
            page: Some(2),
            pagelen: Some(50),
            ..Default::default()
        };
        assert_eq!(
            issue_list_query_full(&filters),
            vec![
                ("q", "state=\"open\"".to_string()),
                ("sort", "-updated_on".to_string()),
                ("page", "2".to_string()),
                ("pagelen", "50".to_string()),
            ]
        );
    }

    #[test]
    fn bbql_quote_escapes_backslash_and_double_quote() {
        // A value with both a `"` (would close the string) and a `\` (would
        // escape the next char) must come back fully escaped and wrapped.
        assert_eq!(bbql_quote(r#"a"b\c"#), r#""a\"b\\c""#);
        // A plain value is just wrapped in quotes.
        assert_eq!(bbql_quote("acc-123"), r#""acc-123""#);
        // An injection attempt cannot break out of the quoted string.
        assert_eq!(
            bbql_quote(r#"x" OR state="open"#),
            r#""x\" OR state=\"open""#
        );
    }

    #[test]
    fn urlencode_encodes_spaces_and_leaves_safe_chars() {
        assert_eq!(urlencode_segment("shot 1.png"), "shot%201.png");
        assert_eq!(urlencode_segment("a-b_c.d~1"), "a-b_c.d~1");
    }
}