// Copyright (c) 2026 Probo Inc . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. package drivers import ( "context" "encoding/json" "fmt" "net/http" "net/url" "go.probo.inc/probo/pkg/coredata" ) // BitbucketDriver fetches workspace members from the Bitbucket Cloud // REST API using a pre-authenticated HTTP client (Bearer token). // // The Bitbucket member object exposes very little: account_id, display // name, optional email (often hidden by privacy), nickname. There is no // role / MFA / last-login data available, so those fields are left at // their zero defaults / nil / Unknown. type BitbucketDriver struct { httpClient *http.Client workspace string } var _ Driver = (*BitbucketDriver)(nil) func NewBitbucketDriver(httpClient *http.Client, workspace string) *BitbucketDriver { return &BitbucketDriver{ httpClient: &http.Client{ Transport: &retryRoundTripper{ next: httpClient.Transport, maxRetries: 3, }, }, workspace: workspace, } } type bitbucketMember struct { User struct { AccountID string `json:"account_id"` DisplayName string `json:"display_name"` Nickname string `json:"nickname"` Email string `json:"email"` } `json:"user"` } type bitbucketMembersPage struct { Values []bitbucketMember `json:"values"` Next string `json:"next"` } func (d *BitbucketDriver) ListAccounts(ctx context.Context) ([]AccountRecord, error) { var records []AccountRecord u, err := url.JoinPath("https://api.bitbucket.org", "2.0", "workspaces", url.PathEscape(d.workspace), "members") if err != nil { return nil, fmt.Errorf("cannot build bitbucket members URL: %w", err) } parsed, err := url.Parse(u) if err != nil { return nil, fmt.Errorf("cannot parse bitbucket members URL: %w", err) } q := parsed.Query() q.Set("fields", "+values.user.email") q.Set("pagelen", "100") parsed.RawQuery = q.Encode() next := parsed.String() for range maxPaginationPages { page, err := d.queryMembers(ctx, next) if err != nil { return nil, err } for _, m := range page.Values { fullName := m.User.DisplayName if fullName == "" { fullName = m.User.Nickname } record := AccountRecord{ Email: m.User.Email, FullName: fullName, ExternalID: m.User.AccountID, MFAStatus: coredata.MFAStatusUnknown, AuthMethod: coredata.AccessReviewEntryAuthMethodUnknown, AccountType: coredata.AccessReviewEntryAccountTypeUser, } records = append(records, record) } next = page.Next if next == "" { return records, nil } } return nil, fmt.Errorf("cannot list all bitbucket accounts: %w", ErrPaginationLimitReached) } func (d *BitbucketDriver) queryMembers(ctx context.Context, endpoint string) (*bitbucketMembersPage, error) { req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) if err != nil { return nil, fmt.Errorf("cannot create bitbucket members request: %w", err) } req.Header.Set("Accept", "application/json") httpResp, err := d.httpClient.Do(req) if err != nil { return nil, fmt.Errorf("cannot execute bitbucket members request: %w", err) } defer func() { _ = httpResp.Body.Close() }() if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 { return nil, fmt.Errorf("cannot fetch bitbucket members: unexpected status %d", httpResp.StatusCode) } var page bitbucketMembersPage if err := json.NewDecoder(httpResp.Body).Decode(&page); err != nil { return nil, fmt.Errorf("cannot decode bitbucket members response: %w", err) } return &page, nil }