Skip to content

Commit ba333c8

Browse files
authored
fix(bitbucket): remove trailing comma in API calls (#2258)
<!-- Thanks for sending a pull request! Here are some tips for you: 1. If this is your first time, please read our contributor guidelines: https://github.com/terramate-io/terramate/blob/main/CONTRIBUTING.md 2. If the PR is unfinished, mark it as draft: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request 3. Please update the PR title using the Conventional Commits convention: https://www.conventionalcommits.org/en/v1.0.0/ Example: feat: add support for XYZ. --> ## What this PR does / why we need it: Cherry-picked from #2257 Thanks @lieranderl for the contribution! ## Does this PR introduce a user-facing change? <!-- If no, just write "no" in the block below. If yes, please explain the change and update documentation and the CHANGELOG.md file accordingly. --> ``` yes, see changelog ``` <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Fix Bitbucket PR lookup query to avoid a trailing comma in `fields`, add a test, update changelog, and bump version. > > - **Bitbucket Integration**: > - Fix `GetPullRequestsByCommit` fields query construction in `cloud/integrations/bitbucket/bitbucket.go` to join field names without a trailing comma. > - **Tests**: > - Add `cloud/integrations/bitbucket/bitbucket_test.go` validating no trailing comma in the `fields` parameter. > - **Docs/Changelog**: > - Update `CHANGELOG.md` under `Unreleased` with a fix note for the Bitbucket integration. > - **Versioning**: > - Bump `VERSION` to `0.15.2-dev`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 0df55c1. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
2 parents a75dbce + 0df55c1 commit ba333c8

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the:
2020
- Backward compatibility in versions `0.0.z` is **not guaranteed** when `z` is increased.
2121
- Backward compatibility in versions `0.y.z` is **not guaranteed** when `y` is increased.
2222

23+
## Unreleased
24+
25+
### Fixed
26+
27+
- Fix invalid API query in Bitbucket integration.
28+
2329
## 0.15.1
2430

2531
### Added

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.15.1
1+
0.15.2-dev

cloud/integrations/bitbucket/bitbucket.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"io"
1111
"net/http"
12+
"strings"
1213
"time"
1314

1415
"github.com/terramate-io/terramate/errors"
@@ -178,13 +179,13 @@ func (c *Client) GetPullRequestsByCommit(ctx context.Context, commit string) (pr
178179
"links",
179180
}
180181

181-
fieldsQuery := ""
182+
var fieldsQuery []string
182183
for _, f := range fields {
183-
fieldsQuery += fmt.Sprintf("values.%s,", f)
184+
fieldsQuery = append(fieldsQuery, fmt.Sprintf("values.%s", f))
184185
}
185186

186187
url := fmt.Sprintf("%s/repositories/%s/%s/commit/%s/pullrequests?fields=%s",
187-
c.baseURL(), c.Workspace, c.RepoSlug, commit, fieldsQuery)
188+
c.baseURL(), c.Workspace, c.RepoSlug, commit, strings.Join(fieldsQuery, ","))
188189

189190
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
190191
if err != nil {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2025 Terramate GmbH
2+
// SPDX-License-Identifier: MPL-2.0
3+
4+
package bitbucket
5+
6+
import (
7+
"context"
8+
"net/http"
9+
"net/http/httptest"
10+
"strings"
11+
"testing"
12+
)
13+
14+
func TestClient_GetPullRequestsByCommit_TrailingComma(t *testing.T) {
15+
// Setup a mock server that checks for the trailing comma
16+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
17+
// Check that the URL path and query parameters are correct
18+
if !strings.Contains(r.URL.Path, "/repositories/workspace/repo/commit/commit-hash/pullrequests") {
19+
w.WriteHeader(http.StatusNotFound)
20+
return
21+
}
22+
23+
fields := r.URL.Query().Get("fields")
24+
// Assert that 'fields' query parameter does NOT end with a comma
25+
if strings.HasSuffix(fields, ",") {
26+
w.WriteHeader(http.StatusBadRequest)
27+
_, _ = w.Write([]byte("Trailing comma detected in fields parameter"))
28+
return
29+
}
30+
31+
// Return a valid empty response if the request is correct
32+
w.WriteHeader(http.StatusOK)
33+
_, _ = w.Write([]byte(`{"values": []}`))
34+
}))
35+
defer server.Close()
36+
37+
client := Client{
38+
BaseURL: server.URL,
39+
Workspace: "workspace",
40+
RepoSlug: "repo",
41+
Token: "token",
42+
}
43+
44+
_, err := client.GetPullRequestsByCommit(context.Background(), "commit-hash")
45+
46+
// We expect NO error now
47+
if err != nil {
48+
t.Fatalf("Expected no error, but got: %v", err)
49+
}
50+
}

0 commit comments

Comments
 (0)