gitpoulpi-hosting 1.0.0

Hosting and issue-tracker API clients (GitHub, GitLab, Gitea/Forgejo, Azure DevOps, Bitbucket, Gerrit, Jira, Trello, Linear, YouTrack, Redmine, local Markdown issues): project listing, clone helpers, SSH key generation and upload, remote-URL provider matching.
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
//! The Bitbucket Cloud client.

use std::sync::Arc;

use serde::de::DeserializeOwned;

use super::map::{Account, Page, PullRequest, Repo, SshKey, WorkspaceAccess};
use crate::client::{
    encode_component, CreatedIssue, CreatedPullRequest, HostingClient, NewIssue, NewPullRequest,
    MAX_PAGES,
};
use crate::conversation::{
    Comment, ConversationCaps, ItemKey, ItemKind, PageCursor, PostingIdentity, TimelineOpts,
    TimelinePage,
};
use crate::http::{HttpRequest, HttpTransport};
use crate::integration::Integration;
use crate::model::{
    best_pull_request, excess_scopes, missing_scopes, HostedUser, IssueFilter, IssueSummary,
    PrFilter, PullRequestSummary, StateFilter, TokenCheck, TokenFlavor,
};
use crate::project::{HostedProject, HostedSshKey};
use crate::{Error, Result};

/// The pull request endpoints cap `pagelen` at 50; repositories and the
/// rest accept 100.
const PR_PAGELEN: u32 = 50;
const PAGELEN: u32 = 100;

/// Why `list_issues` / `create_issue` always fail.
pub const ISSUES_RETIRED: &str =
    "Bitbucket issues were retired on 2026-08-20; link a Jira project instead";

mod conversation;
mod protection;

pub struct BitbucketClient {
    integration: Integration,
    token: String,
    transport: Arc<dyn HttpTransport>,
    pub(crate) scopes: crate::scope_handle::ScopeHandle,
}

impl BitbucketClient {
    pub(crate) fn new(
        integration: Integration,
        token: String,
        transport: Arc<dyn HttpTransport>,
    ) -> Self {
        Self {
            integration,
            token,
            transport,
            scopes: Default::default(),
        }
    }

    fn authed(&self, mut request: HttpRequest) -> HttpRequest {
        for (name, value) in crate::auth::auth_headers(&self.integration, &self.token) {
            request = request.header(name, value);
        }
        request
    }

    fn get(&self, url: String) -> HttpRequest {
        self.authed(HttpRequest::get(url))
    }

    fn api(&self) -> String {
        self.integration.api_base()
    }

    /// `scheme://host[:port]` of the API root: a `next` link may only point
    /// there, so the token is never sent to another host.
    fn origin(&self) -> String {
        let api = self.api();
        match api.split_once("://") {
            Some((scheme, rest)) => format!("{scheme}://{}", rest.split('/').next().unwrap_or("")),
            None => api,
        }
    }

    /// Every page of a list endpoint, following the `next` URLs the host
    /// returns (never constructing its own), capped at [`MAX_PAGES`].
    async fn fetch_all<T: DeserializeOwned>(&self, first: String, what: &str) -> Result<Vec<T>> {
        let origin = self.origin();
        let mut out = Vec::new();
        let mut url = first;
        for _ in 0..MAX_PAGES {
            let response = self.transport.send(self.get(url)).await?;
            response.error_for_status(what)?;
            let page: Page<T> = response.json()?;
            out.extend(page.values);
            match page.next.filter(|n| !n.is_empty()) {
                None => break,
                Some(next) if next.starts_with(&origin) => url = next,
                Some(next) => {
                    return Err(Error::Decode(format!(
                        "{what}: refusing to follow a next link to another host ({next})"
                    )))
                }
            }
        }
        Ok(out)
    }

    async fn fetch_account(&self, what: &str) -> Result<Account> {
        let response = self
            .transport
            .send(self.get(format!("{}/user", self.api())))
            .await?;
        response.error_for_status(what)?;
        response.json()
    }

    async fn workspaces(&self) -> Result<Vec<String>> {
        let url = format!("{}/user/workspaces?pagelen={PAGELEN}", self.api());
        let rows: Vec<WorkspaceAccess> =
            self.fetch_all(url, "listing Bitbucket workspaces").await?;
        Ok(rows.into_iter().map(|w| w.workspace.slug).collect())
    }

    async fn my_uuid(&self) -> Result<String> {
        Ok(self
            .fetch_account("reading the Bitbucket account")
            .await?
            .uuid)
    }

    fn pr_url(&self, project: &str) -> String {
        format!("{}/repositories/{project}/pullrequests", self.api())
    }

    async fn pull_requests(
        &self,
        project: &str,
        states: &[&str],
        q: &[String],
        what: &str,
    ) -> Result<Vec<PullRequest>> {
        let mut url = format!(
            "{}?pagelen={PR_PAGELEN}&sort=-updated_on",
            self.pr_url(project)
        );
        for state in states {
            url.push_str(&format!("&state={state}"));
        }
        if !q.is_empty() {
            url.push_str(&format!("&q={}", encode_component(&q.join(" AND "))));
        }
        self.fetch_all(url, what).await
    }
}

/// A string literal in Bitbucket's filter language.
fn q_string(text: &str) -> String {
    format!("\"{}\"", text.replace('\\', "\\\\").replace('"', "\\\""))
}

#[async_trait::async_trait]
impl HostingClient for BitbucketClient {
    fn integration(&self) -> &Integration {
        &self.integration
    }

    async fn list_projects(&self) -> Result<Vec<HostedProject>> {
        let mut out = Vec::new();
        for workspace in self.workspaces().await? {
            let url = format!(
                "{}/repositories/{}?role=member&sort=-updated_on&pagelen={PAGELEN}",
                self.api(),
                encode_component(&workspace)
            );
            match self
                .fetch_all::<Repo>(url, "listing Bitbucket repositories")
                .await
            {
                Ok(rows) => out.extend(
                    rows.into_iter()
                        .map(|r| r.into_project(&self.integration.id)),
                ),
                // A workspace the token cannot read (scope or SSO) must not
                // hide the others.
                Err(Error::Forbidden { .. }) => {}
                Err(Error::Api { status: 404, .. }) => {}
                Err(e) => return Err(e),
            }
        }
        Ok(out)
    }

    async fn account_items(
        &self,
        _query: &crate::account::AccountQuery,
    ) -> Result<crate::account::AccountItems> {
        Err(Error::Unsupported(
            crate::account::NO_BITBUCKET_CLOUD.to_string(),
        ))
    }

    async fn list_ssh_keys(&self) -> Result<Vec<HostedSshKey>> {
        let uuid = encode_component(&self.my_uuid().await?);
        let url = format!("{}/users/{uuid}/ssh-keys?pagelen={PAGELEN}", self.api());
        let rows: Vec<SshKey> = self.fetch_all(url, "listing Bitbucket SSH keys").await?;
        Ok(rows.into_iter().map(SshKey::into_key).collect())
    }

    async fn upload_ssh_key(&self, title: &str, public_key: &str) -> Result<HostedSshKey> {
        let uuid = encode_component(&self.my_uuid().await?);
        let url = format!("{}/users/{uuid}/ssh-keys", self.api());
        let body = serde_json::json!({ "label": title, "key": public_key.trim() });
        let response = self
            .transport
            .send(self.authed(HttpRequest::post_json(url, &body)))
            .await?;
        response.error_for_status("uploading an SSH key to Bitbucket")?;
        Ok(response.json::<SshKey>()?.into_key())
    }

    async fn delete_ssh_key(&self, id: &str) -> Result<()> {
        let uuid = encode_component(&self.my_uuid().await?);
        let url = format!(
            "{}/users/{uuid}/ssh-keys/{}",
            self.api(),
            encode_component(id)
        );
        let response = self
            .transport
            .send(self.authed(HttpRequest::delete(url)))
            .await?;
        response.error_for_status("deleting a Bitbucket SSH key")
    }

    async fn create_pull_request(&self, req: &NewPullRequest) -> Result<CreatedPullRequest> {
        let mut source = serde_json::json!({ "branch": { "name": req.source_branch } });
        if let Some(fork) = req.fork_source() {
            source["repository"] = serde_json::json!({ "full_name": fork });
        }
        let body = serde_json::json!({
            "title": req.title,
            "description": req.body,
            "source": source,
            "destination": { "branch": { "name": req.target_branch } },
            "draft": req.draft,
        });
        let response = self
            .transport
            .send(self.authed(HttpRequest::post_json(self.pr_url(&req.project), &body)))
            .await?;
        response.error_for_status("opening a Bitbucket pull request")?;
        let created: PullRequest = response.json()?;
        let number = created.id;
        Ok(CreatedPullRequest {
            number,
            web_url: created.links.html.href.unwrap_or_default(),
            ..Default::default()
        })
    }

    async fn protected_branches(
        &self,
        project: &str,
    ) -> Result<crate::protection::ProtectedBranches> {
        self.protection(project).await
    }

    async fn current_user(&self) -> Result<HostedUser> {
        Ok(self
            .fetch_account("checking the Bitbucket token")
            .await?
            .into_user())
    }

    async fn check_token(&self) -> Result<TokenCheck> {
        // Bitbucket sends the token's scopes in `x-oauth-scopes` when it
        // sends them at all (an API token does not): absent is unknown,
        // never "missing".
        let response = self
            .transport
            .send(self.get(format!("{}/user", self.api())))
            .await?;
        response.error_for_status("checking the Bitbucket token")?;
        let scopes = response
            .headers
            .iter()
            .find(|(k, _)| k.eq_ignore_ascii_case("x-oauth-scopes"))
            .map(|(_, v)| {
                v.split([',', ' '])
                    .map(str::trim)
                    .filter(|s| !s.is_empty())
                    .map(str::to_string)
                    .collect::<Vec<_>>()
            })
            .filter(|s| !s.is_empty());
        let required = self
            .integration
            .kind
            .token_help()
            .map(|h| h.scopes)
            .unwrap_or(&[]);
        let (missing, excess) = scopes.as_deref().map_or_else(Default::default, |granted| {
            (
                missing_scopes(required, granted),
                excess_scopes(required, &[], granted),
            )
        });
        let account: Account = response.json()?;
        Ok(TokenCheck {
            user: account.into_user(),
            token_flavor: Some(if scopes.is_some() {
                TokenFlavor::Classic
            } else {
                TokenFlavor::Unknown
            }),
            scopes,
            missing_scopes: missing,
            excess_scopes: excess,
            ..Default::default()
        })
    }

    async fn list_pull_requests(
        &self,
        project: &str,
        filter: &PrFilter,
    ) -> Result<Vec<PullRequestSummary>> {
        let states: &[&str] = match filter.state {
            StateFilter::Open => &["OPEN"],
            StateFilter::Closed => &["MERGED", "DECLINED", "SUPERSEDED"],
            StateFilter::All => &["OPEN", "MERGED", "DECLINED", "SUPERSEDED"],
        };
        let me = if filter.mine || filter.review_requested {
            Some(self.my_uuid().await?)
        } else {
            None
        };
        let mut q = Vec::new();
        if let (true, Some(me)) = (filter.mine, &me) {
            q.push(format!("author.uuid={}", q_string(me)));
        }
        let rows = self
            .pull_requests(project, states, &q, "listing Bitbucket pull requests")
            .await?;
        Ok(rows
            .into_iter()
            .map(|r| r.into_summary(project, me.as_deref()))
            .filter(|s| !filter.review_requested || s.review_requested)
            .collect())
    }

    async fn pull_request_for_branch(
        &self,
        project: &str,
        branch: &str,
    ) -> Result<Option<PullRequestSummary>> {
        let q = [format!("source.branch.name={}", q_string(branch))];
        let rows = self
            .pull_requests(
                project,
                &["OPEN", "MERGED", "DECLINED", "SUPERSEDED"],
                &q,
                "looking up the pull request for a branch",
            )
            .await?;
        Ok(best_pull_request(
            rows.into_iter()
                .map(|r| r.into_summary(project, None))
                .collect(),
        ))
    }

    async fn list_issues(
        &self,
        _project: &str,
        _filter: &IssueFilter,
    ) -> Result<Vec<IssueSummary>> {
        Err(Error::Unsupported(ISSUES_RETIRED.to_string()))
    }

    async fn create_issue(&self, _req: &NewIssue) -> Result<CreatedIssue> {
        Err(Error::Unsupported(ISSUES_RETIRED.to_string()))
    }

    fn conversation_caps(&self, kind: ItemKind) -> ConversationCaps {
        ConversationCaps::bitbucket(kind)
    }

    async fn posting_identity(&self, project: &str) -> Result<PostingIdentity> {
        self.conv_identity(project).await
    }

    async fn item_timeline(
        &self,
        project: &str,
        key: &ItemKey,
        older: Option<&PageCursor>,
        opts: &TimelineOpts,
    ) -> Result<TimelinePage> {
        self.conv_timeline(project, key, older, opts).await
    }

    async fn post_item_comment(
        &self,
        project: &str,
        key: &ItemKey,
        body: &str,
        internal: bool,
    ) -> Result<Comment> {
        self.conv_post(project, key, body, internal).await
    }

    async fn reply_in_discussion(
        &self,
        project: &str,
        key: &ItemKey,
        discussion_id: &str,
        body: &str,
    ) -> Result<Comment> {
        self.conv_reply(project, key, discussion_id, body).await
    }

    async fn edit_item_comment(
        &self,
        project: &str,
        key: &ItemKey,
        comment_id: &str,
        body: &str,
    ) -> Result<Comment> {
        self.conv_edit(project, key, comment_id, body).await
    }

    async fn delete_item_comment(
        &self,
        project: &str,
        key: &ItemKey,
        comment_id: &str,
    ) -> Result<()> {
        self.conv_delete(project, key, comment_id).await
    }
}