@@ -23,6 +23,7 @@ import (
2323 "net/http"
2424 "net/url"
2525 "os"
26+ "strings"
2627 "time"
2728
2829 "github.com/JetBrains/qodana-cli/v2025/platform/msg"
@@ -177,8 +178,25 @@ func getBitBucketClient() *bbapi.APIClient {
177178 config .HTTPClient = & http.Client {
178179 Timeout : httpTimeout ,
179180 }
181+
182+ apiURL := os .Getenv ("QD_BITBUCKET_URL" )
183+ if apiURL == "" {
184+ if gitOrigin := os .Getenv ("BITBUCKET_GIT_HTTP_ORIGIN" ); gitOrigin != "" {
185+ if parsedURL , err := url .Parse (gitOrigin ); err == nil {
186+ if ! strings .Contains (parsedURL .Host , "bitbucket.org" ) {
187+ // Construct API URL for self-hosted BitBucket Data Center/Server
188+ // Reference: https://developer.atlassian.com/server/bitbucket/rest/v1000/intro/
189+ apiURL = fmt .Sprintf ("%s://%s/rest/api/1.0" , parsedURL .Scheme , parsedURL .Host )
190+ }
191+ }
192+ }
193+ if apiURL == "" {
194+ apiURL = "https://api.bitbucket.org/2.0"
195+ }
196+ }
197+
180198 server := bbapi.ServerConfiguration {
181- URL : "https://api.bitbucket.org/2.0" ,
199+ URL : apiURL ,
182200 Description : `HTTPS API endpoint` ,
183201 }
184202 if qdenv .IsBitBucket () {
@@ -202,13 +220,21 @@ func getBitBucketClient() *bbapi.APIClient {
202220
203221// checkBitBucketApiError checks if the API call was successful
204222func checkBitBucketApiError (err error , resp * http.Response , expectedCode int ) error {
223+ selfHostedHint := "Note: If you are using BitBucket Data Center/Server (self-hosted), " +
224+ "please set the QD_BITBUCKET_URL environment variable to your BitBucket API endpoint " +
225+ "(e.g., QD_BITBUCKET_URL=https://bitbucket.example.com/rest/api/1.0). " +
226+ "Read more at https://developer.atlassian.com/server/bitbucket/rest/v1000/intro/"
227+
205228 if err != nil {
206- return fmt .Errorf ("bitbucket Cloud API error: %w" , err )
229+ return fmt .Errorf ("BitBucket API error: %w%s " , err , selfHostedHint )
207230 }
208231 if resp != nil && resp .StatusCode != expectedCode {
209232 body , _ := io .ReadAll (resp .Body )
210233 log .Debugf ("Unexpected response: %s" , body )
211- return fmt .Errorf ("bitbucket Cloud API error: %w" , err )
234+ return fmt .Errorf (
235+ "BitBucket API returned unexpected status code %d (expected %d)\n \n %s" ,
236+ resp .StatusCode , expectedCode , selfHostedHint ,
237+ )
212238 }
213239 return nil
214240}
0 commit comments