Skip to content

Commit fc9c919

Browse files
committed
fix(scm): address golangci-lint issues
Signed-off-by: Ushira Dineth <[email protected]>
1 parent 698ad96 commit fc9c919

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

internal/scms/bitbucket/commit_status.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bitbucket
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net/http"
78
"strings"
@@ -72,13 +73,16 @@ func (cs *CommitStatus) Set(ctx context.Context, commitStatus *v1alpha1.CommitSt
7273
statusCode := http.StatusCreated
7374
if err != nil {
7475
statusCode = http.StatusInternalServerError
75-
if bbErr, ok := err.(*bitbucket.UnexpectedResponseStatusError); ok {
76+
bbErr := &bitbucket.UnexpectedResponseStatusError{}
77+
if errors.As(err, &bbErr) {
7678
errMsg := bbErr.Error()
7779
switch {
7880
case strings.HasPrefix(errMsg, "401"):
7981
statusCode = http.StatusUnauthorized
8082
case strings.HasPrefix(errMsg, "404"):
8183
statusCode = http.StatusNotFound
84+
default:
85+
statusCode = http.StatusInternalServerError
8286
}
8387
}
8488
}
@@ -92,7 +96,7 @@ func (cs *CommitStatus) Set(ctx context.Context, commitStatus *v1alpha1.CommitSt
9296
logger.V(4).Info("bitbucket response status", "status", statusCode)
9397

9498
// Parse the response
95-
resultMap, ok := result.(map[string]interface{})
99+
resultMap, ok := result.(map[string]any)
96100
if !ok {
97101
return nil, fmt.Errorf("unexpected response type from Bitbucket API: %T", result)
98102
}

internal/scms/bitbucket/git_operations.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ func NewBitbucketGitAuthenticationProvider(scmProvider v1alpha1.GenericScmProvid
3636
}
3737

3838
// GetGitHttpsRepoUrl constructs the HTTPS URL for a Bitbucket repository based on the provided GitRepository object.
39-
func (_ GitAuthenticationProvider) GetGitHttpsRepoUrl(repo v1alpha1.GitRepository) string {
40-
var repoUrl string
41-
repoUrl = fmt.Sprintf("https://bitbucket.com/%s/%s.git", repo.Spec.Bitbucket.Workspace, repo.Spec.Bitbucket.Repository)
39+
func (GitAuthenticationProvider) GetGitHttpsRepoUrl(repo v1alpha1.GitRepository) string {
40+
repoUrl := fmt.Sprintf("https://bitbucket.com/%s/%s.git", repo.Spec.Bitbucket.Workspace, repo.Spec.Bitbucket.Repository)
4241
if _, err := url.Parse(repoUrl); err != nil {
4342
return ""
4443
}
@@ -51,7 +50,7 @@ func (bb GitAuthenticationProvider) GetToken(ctx context.Context) (string, error
5150
}
5251

5352
// GetUser returns a placeholder user for Bitbucket authentication.
54-
func (_ GitAuthenticationProvider) GetUser(ctx context.Context) (string, error) {
53+
func (GitAuthenticationProvider) GetUser(ctx context.Context) (string, error) {
5554
return "x-token-auth", nil
5655
}
5756

internal/scms/bitbucket/pullrequest.go

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bitbucket
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net/http"
78
"strconv"
@@ -70,13 +71,16 @@ func (pr *PullRequest) Create(ctx context.Context, title, head, base, desc strin
7071
statusCode := http.StatusCreated
7172
if err != nil {
7273
statusCode = http.StatusInternalServerError
73-
if bbErr, ok := err.(*bitbucket.UnexpectedResponseStatusError); ok {
74+
bbErr := &bitbucket.UnexpectedResponseStatusError{}
75+
if errors.As(err, &bbErr) {
7476
errMsg := bbErr.Error()
7577
switch {
7678
case strings.HasPrefix(errMsg, "400"):
7779
statusCode = http.StatusBadRequest
7880
case strings.HasPrefix(errMsg, "401"):
7981
statusCode = http.StatusUnauthorized
82+
default:
83+
statusCode = http.StatusInternalServerError
8084
}
8185
}
8286
}
@@ -88,14 +92,14 @@ func (pr *PullRequest) Create(ctx context.Context, title, head, base, desc strin
8892
}
8993

9094
// Extract pull request ID from response
91-
respMap, ok := resp.(map[string]interface{})
95+
respMap, ok := resp.(map[string]any)
9296
if !ok {
9397
return "", fmt.Errorf("unexpected response type from Bitbucket API: %T", resp)
9498
}
9599

96100
idValue, exists := respMap["id"]
97101
if !exists {
98-
return "", fmt.Errorf("pull request ID not found in Bitbucket API response")
102+
return "", errors.New("pull request ID not found in Bitbucket API response")
99103
}
100104

101105
idFloat, ok := idValue.(float64)
@@ -137,7 +141,8 @@ func (pr *PullRequest) Update(ctx context.Context, title, description string, pr
137141
statusCode := http.StatusOK
138142
if err != nil {
139143
statusCode = http.StatusInternalServerError
140-
if bbErr, ok := err.(*bitbucket.UnexpectedResponseStatusError); ok {
144+
bbErr := &bitbucket.UnexpectedResponseStatusError{}
145+
if errors.As(err, &bbErr) {
141146
errMsg := bbErr.Error()
142147
switch {
143148
case strings.HasPrefix(errMsg, "400"):
@@ -146,6 +151,8 @@ func (pr *PullRequest) Update(ctx context.Context, title, description string, pr
146151
statusCode = http.StatusUnauthorized
147152
case strings.HasPrefix(errMsg, "404"):
148153
statusCode = http.StatusNotFound
154+
default:
155+
statusCode = http.StatusInternalServerError
149156
}
150157
}
151158
}
@@ -188,10 +195,10 @@ func (pr *PullRequest) Close(ctx context.Context, prObj v1alpha1.PullRequest) er
188195
statusCode := http.StatusOK
189196
if err != nil {
190197
statusCode = http.StatusInternalServerError
191-
if bbErr, ok := err.(*bitbucket.UnexpectedResponseStatusError); ok {
198+
bbErr := &bitbucket.UnexpectedResponseStatusError{}
199+
if errors.As(err, &bbErr) {
192200
errMsg := bbErr.Error()
193-
switch {
194-
case strings.HasPrefix(errMsg, "555"):
201+
if strings.HasPrefix(errMsg, "555") {
195202
statusCode = http.StatusInternalServerError
196203
}
197204
}
@@ -236,13 +243,16 @@ func (pr *PullRequest) Merge(ctx context.Context, prObj v1alpha1.PullRequest) er
236243
statusCode := http.StatusOK
237244
if err != nil {
238245
statusCode = http.StatusInternalServerError
239-
if bbErr, ok := err.(*bitbucket.UnexpectedResponseStatusError); ok {
246+
bbErr := &bitbucket.UnexpectedResponseStatusError{}
247+
if errors.As(err, &bbErr) {
240248
errMsg := bbErr.Error()
241249
switch {
242250
case strings.HasPrefix(errMsg, "409"):
243251
statusCode = http.StatusConflict
244252
case strings.HasPrefix(errMsg, "555"):
245253
statusCode = http.StatusInternalServerError
254+
default:
255+
statusCode = http.StatusInternalServerError
246256
}
247257
}
248258
}
@@ -292,13 +302,16 @@ func (pr *PullRequest) FindOpen(ctx context.Context, pullRequest v1alpha1.PullRe
292302
statusCode := http.StatusOK
293303
if err != nil {
294304
statusCode = http.StatusInternalServerError
295-
if bbErr, ok := err.(*bitbucket.UnexpectedResponseStatusError); ok {
305+
bbErr := &bitbucket.UnexpectedResponseStatusError{}
306+
if errors.As(err, &bbErr) {
296307
errMsg := bbErr.Error()
297308
switch {
298309
case strings.HasPrefix(errMsg, "401"):
299310
statusCode = http.StatusUnauthorized
300311
case strings.HasPrefix(errMsg, "404"):
301312
statusCode = http.StatusNotFound
313+
default:
314+
statusCode = http.StatusInternalServerError
302315
}
303316
}
304317
}
@@ -312,27 +325,31 @@ func (pr *PullRequest) FindOpen(ctx context.Context, pullRequest v1alpha1.PullRe
312325
logger.V(4).Info("bitbucket response status", "status", statusCode)
313326

314327
// Parse the paginated response
315-
resultMap, ok := result.(map[string]interface{})
328+
resultMap, ok := result.(map[string]any)
316329
if !ok {
317330
return false, "", time.Time{}, fmt.Errorf("unexpected response type from Bitbucket API: %T", result)
318331
}
319332

320-
values, _ := resultMap["values"]
321-
prs, ok := values.([]interface{})
333+
values, ok := resultMap["values"]
334+
if !ok {
335+
return false, "", time.Time{}, nil
336+
}
337+
338+
prs, ok := values.([]any)
322339
if !ok || len(prs) == 0 {
323340
return false, "", time.Time{}, nil
324341
}
325342

326343
// Get the first matching PR
327-
firstPR, ok := prs[0].(map[string]interface{})
344+
firstPR, ok := prs[0].(map[string]any)
328345
if !ok {
329-
return false, "", time.Time{}, fmt.Errorf("unexpected PR format in response")
346+
return false, "", time.Time{}, errors.New("unexpected PR format in response")
330347
}
331348

332349
// Extract and validate PR ID
333350
idValue, exists := firstPR["id"]
334351
if !exists {
335-
return false, "", time.Time{}, fmt.Errorf("PR ID not found in response")
352+
return false, "", time.Time{}, errors.New("PR ID not found in response")
336353
}
337354
idFloat, ok := idValue.(float64)
338355
if !ok {
@@ -342,7 +359,7 @@ func (pr *PullRequest) FindOpen(ctx context.Context, pullRequest v1alpha1.PullRe
342359
// Extract and validate created_on timestamp
343360
createdOn, exists := firstPR["created_on"]
344361
if !exists {
345-
return false, "", time.Time{}, fmt.Errorf("created_on not found in response")
362+
return false, "", time.Time{}, errors.New("created_on not found in response")
346363
}
347364
createdStr, ok := createdOn.(string)
348365
if !ok {

0 commit comments

Comments
 (0)