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
//! Typed Bitbucket Cloud API models (the subset needed for Epic 0). These are
//! deliberately lenient (`Option`-heavy) so partial responses deserialize.

use serde::{Deserialize, Serialize};

/// A Bitbucket user/account.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    pub account_id: Option<String>,
    pub username: Option<String>,
    pub nickname: Option<String>,
    pub display_name: Option<String>,
    pub uuid: Option<String>,
}

/// A workspace membership (`GET /2.0/workspaces/{ws}/members` → `{ user: {...} }`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Membership {
    pub user: Option<User>,
}

impl User {
    /// The best available human-facing name.
    #[must_use]
    pub fn label(&self) -> String {
        self.display_name
            .clone()
            .or_else(|| self.username.clone())
            .or_else(|| self.account_id.clone())
            .unwrap_or_else(|| "unknown".to_owned())
    }
}

/// A repository's main branch.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MainBranch {
    pub name: String,
}

/// A Bitbucket repository.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Repository {
    pub slug: Option<String>,
    pub name: Option<String>,
    pub full_name: Option<String>,
    pub is_private: Option<bool>,
    pub description: Option<String>,
    pub mainbranch: Option<MainBranch>,
    pub links: Option<RepoLinks>,
    pub has_issues: Option<bool>,
    /// The upstream repository this one was forked from, if any (`bb repo sync`).
    pub parent: Option<RepoRef>,
}

/// A repository's `links` (the subset we use: web URL + clone URLs).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RepoLinks {
    pub html: Option<Link>,
    #[serde(default)]
    pub clone: Vec<CloneLink>,
}

/// One clone URL entry (`{ "name": "https"|"ssh", "href": "..." }`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CloneLink {
    pub name: Option<String>,
    pub href: String,
}

impl Repository {
    #[must_use]
    pub fn html_url(&self) -> Option<&str> {
        self.links
            .as_ref()
            .and_then(|l| l.html.as_ref())
            .map(|h| h.href.as_str())
    }

    /// The clone URL for `protocol` (`"https"` or `"ssh"`), if present.
    #[must_use]
    pub fn clone_url(&self, protocol: &str) -> Option<&str> {
        self.links
            .as_ref()?
            .clone
            .iter()
            .find(|c| c.name.as_deref() == Some(protocol))
            .map(|c| c.href.as_str())
    }
}

/// A branch name wrapper (`{ "name": "..." }`).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Branch {
    pub name: String,
}

/// A PR source/destination endpoint
/// (`{ "branch": { "name": ... }, "repository": { "full_name": ... } }`).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct BranchRef {
    pub branch: Option<Branch>,
    pub repository: Option<RepoRef>,
    pub commit: Option<CommitRef>,
}

impl BranchRef {
    #[must_use]
    pub fn branch_name(&self) -> &str {
        self.branch.as_ref().map_or("", |b| b.name.as_str())
    }

    /// The `full_name` (`workspace/slug`) of this endpoint's repository, if
    /// present. Used to detect a cross-fork PR source.
    #[must_use]
    pub fn repo_full_name(&self) -> Option<&str> {
        self.repository
            .as_ref()
            .and_then(|r| r.full_name.as_deref())
    }

    /// The commit hash this endpoint points at, if present.
    #[must_use]
    pub fn commit_hash(&self) -> Option<&str> {
        self.commit.as_ref().and_then(|c| c.hash.as_deref())
    }
}

/// A repository reference embedded in a PR source/destination.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct RepoRef {
    pub full_name: Option<String>,
}

/// A commit reference (`{ "hash": "..." }`) on a branch/PR endpoint or pipeline
/// target.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CommitRef {
    pub hash: Option<String>,
}

/// A Bitbucket Pipelines run.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Pipeline {
    pub uuid: Option<String>,
    pub build_number: Option<u64>,
    pub state: Option<PipelineState>,
    pub target: Option<PipelineTarget>,
    pub created_on: Option<String>,
}

impl Pipeline {
    /// `state.name` (e.g. `PENDING`, `IN_PROGRESS`, `COMPLETED`).
    #[must_use]
    pub fn state_name(&self) -> &str {
        self.state
            .as_ref()
            .and_then(|s| s.name.as_deref())
            .unwrap_or("")
    }

    /// `state.result.name` (e.g. `SUCCESSFUL`, `FAILED`, `STOPPED`), if completed.
    #[must_use]
    pub fn result_name(&self) -> &str {
        self.state
            .as_ref()
            .and_then(|s| s.result.as_ref())
            .and_then(|r| r.name.as_deref())
            .unwrap_or("")
    }
}

/// A pipeline (or step) state: a `name` plus an optional terminal `result`.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PipelineState {
    pub name: Option<String>,
    pub result: Option<PipelineResult>,
}

/// A pipeline/step terminal result (`{ "name": "SUCCESSFUL" | "FAILED" | ... }`).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PipelineResult {
    pub name: Option<String>,
}

/// What a pipeline ran against (`{ "ref_name": ..., "commit": { "hash": ... } }`).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PipelineTarget {
    pub ref_name: Option<String>,
    pub commit: Option<CommitRef>,
}

/// One step within a pipeline.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PipelineStep {
    pub uuid: Option<String>,
    pub name: Option<String>,
    pub state: Option<PipelineState>,
}

/// A build status attached to a commit (the closest analog to a PR "check").
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommitStatus {
    pub key: Option<String>,
    pub name: Option<String>,
    /// `SUCCESSFUL` | `FAILED` | `INPROGRESS` | `STOPPED`.
    pub state: Option<String>,
    pub url: Option<String>,
}

impl CommitStatus {
    /// Whether this status represents a failure.
    #[must_use]
    pub fn is_failed(&self) -> bool {
        self.state.as_deref() == Some("FAILED")
    }
}

/// A single link (`{ "href": "..." }`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Link {
    pub href: String,
}

/// The `links` object (the subset we use).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Links {
    pub html: Option<Link>,
}

impl Links {
    #[must_use]
    pub fn html_href(&self) -> Option<&str> {
        self.html.as_ref().map(|l| l.href.as_str())
    }
}

/// A pull request.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PullRequest {
    pub id: u64,
    pub title: Option<String>,
    pub state: Option<String>,
    #[serde(default)]
    pub source: BranchRef,
    #[serde(default)]
    pub destination: BranchRef,
    #[serde(default)]
    pub links: Links,
    pub author: Option<User>,
    pub description: Option<String>,
    pub summary: Option<Rendered>,
    pub close_source_branch: Option<bool>,
    #[serde(default)]
    pub participants: Vec<Participant>,
    #[serde(default)]
    pub reviewers: Vec<User>,
}

/// A Bitbucket issue (issue tracker).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Issue {
    pub id: u64,
    pub title: Option<String>,
    pub state: Option<String>,
    pub kind: Option<String>,
    pub priority: Option<String>,
    pub content: Option<Rendered>,
    pub reporter: Option<User>,
    #[serde(default)]
    pub links: Links,
}

impl Issue {
    #[must_use]
    pub fn html_url(&self) -> Option<&str> {
        self.links.html_href()
    }

    /// The issue body (`content.raw`).
    #[must_use]
    pub fn body(&self) -> Option<&str> {
        self.content.as_ref().and_then(|c| c.raw.as_deref())
    }
}

/// Rendered content (e.g. `summary.raw`).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Rendered {
    pub raw: Option<String>,
}

/// A PR participant with their approval state.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Participant {
    pub user: Option<User>,
    pub role: Option<String>,
    #[serde(default)]
    pub approved: bool,
}

impl PullRequest {
    #[must_use]
    pub fn html_url(&self) -> Option<&str> {
        self.links.html_href()
    }

    /// Best available description text (`description`, then `summary.raw`).
    #[must_use]
    pub fn body(&self) -> Option<&str> {
        self.description
            .as_deref()
            .or_else(|| self.summary.as_ref().and_then(|s| s.raw.as_deref()))
    }

    /// Users who have approved this PR.
    #[must_use]
    pub fn approvals(&self) -> Vec<&User> {
        self.participants
            .iter()
            .filter(|p| p.approved)
            .filter_map(|p| p.user.as_ref())
            .collect()
    }
}

/// A repository webhook (`.../hooks`).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Webhook {
    pub uuid: Option<String>,
    pub url: Option<String>,
    pub description: Option<String>,
    pub active: Option<bool>,
    #[serde(default)]
    pub events: Vec<String>,
}

/// A repository deploy key (`.../deploy-keys`).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DeployKey {
    pub id: Option<i64>,
    pub label: Option<String>,
    pub key: Option<String>,
}

/// A branch restriction (`.../branch-restrictions`; the BB branch-protection unit).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct BranchRestriction {
    pub id: Option<i64>,
    pub kind: Option<String>,
    pub pattern: Option<String>,
    pub branch_match_kind: Option<String>,
}

/// A Bitbucket workspace.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Workspace {
    pub slug: Option<String>,
    pub name: Option<String>,
    pub uuid: Option<String>,
    pub is_private: Option<bool>,
}

/// A workspace membership for the current user
/// (`GET /2.0/user/permissions/workspaces` → `{ permission, workspace }`).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct WorkspaceMembership {
    pub permission: Option<String>,
    pub workspace: Option<Workspace>,
}

/// A Bitbucket project within a workspace.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Project {
    pub key: Option<String>,
    pub name: Option<String>,
    pub is_private: Option<bool>,
    pub description: Option<String>,
    pub links: Option<RepoLinks>,
}

/// A Bitbucket Snippet (the `gh gist` analog).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Snippet {
    pub id: Option<String>,
    pub title: Option<String>,
    pub is_private: Option<bool>,
    pub owner: Option<User>,
    pub created_on: Option<String>,
    pub links: Option<RepoLinks>,
    /// Files keyed by filename.
    #[serde(default)]
    pub files: std::collections::BTreeMap<String, SnippetFile>,
}

/// One file within a snippet (`files: { "<name>": { ... } }`).
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SnippetFile {
    pub links: Option<RepoLinks>,
}

impl Snippet {
    /// The web (html) URL, if present.
    #[must_use]
    pub fn html_url(&self) -> Option<&str> {
        self.links
            .as_ref()
            .and_then(|l| l.html.as_ref())
            .map(|h| h.href.as_str())
    }

    /// The clone URL for `protocol` (`"https"` | `"ssh"`), if present.
    #[must_use]
    pub fn clone_url(&self, protocol: &str) -> Option<&str> {
        self.links
            .as_ref()?
            .clone
            .iter()
            .find(|c| c.name.as_deref() == Some(protocol))
            .map(|c| c.href.as_str())
    }

    /// The filenames in this snippet, sorted.
    #[must_use]
    pub fn filenames(&self) -> Vec<&str> {
        self.files.keys().map(String::as_str).collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn deserializes_pull_request() {
        let json = r#"{
            "id": 42,
            "title": "Add widget",
            "state": "OPEN",
            "source": { "branch": { "name": "feature/x" } },
            "destination": { "branch": { "name": "main" } },
            "links": { "html": { "href": "https://bitbucket.org/acme/widgets/pull-requests/42" } }
        }"#;
        let pr: PullRequest = serde_json::from_str(json).unwrap();
        assert_eq!(pr.id, 42);
        assert_eq!(pr.source.branch_name(), "feature/x");
        assert_eq!(pr.destination.branch_name(), "main");
        assert_eq!(
            pr.html_url(),
            Some("https://bitbucket.org/acme/widgets/pull-requests/42")
        );
    }

    #[test]
    fn user_label_prefers_display_name() {
        let u: User = serde_json::from_str(r#"{"username":"d","display_name":"David"}"#).unwrap();
        assert_eq!(u.label(), "David");
    }
}