Skip to content

Commit 7daa4cf

Browse files
author
Edvard Makhlin
committed
Is that good enough memoization?
1 parent 3d73c92 commit 7daa4cf

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

server/events/vcs/bitbucketcloud/client.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ func NewClient(httpClient *http.Client, username string, password string, atlant
4040
}
4141
}
4242

43+
var MY_UUID = ""
44+
4345
// GetModifiedFiles returns the names of files that were modified in the merge request
4446
// relative to the repo root, e.g. parent/child/file.txt.
4547
func (b *Client) GetModifiedFiles(logger logging.SimpleLogging, repo models.Repo, pull models.PullRequest) ([]string, error) {
@@ -134,7 +136,10 @@ func (b *Client) HidePrevCommandComments(logger logging.SimpleLogging, repo mode
134136
if strings.Contains(firstLine, strings.ToLower(command)) {
135137
// we found our old comment that references that command
136138
logger.Debug("Deleting comment with id %s", *c.ID)
137-
b.DeletePullRequestComment(repo, pullNum, *c.ID)
139+
err = b.DeletePullRequestComment(repo, pullNum, *c.ID)
140+
if err != nil {
141+
return err
142+
}
138143
}
139144
}
140145
}
@@ -165,25 +170,30 @@ func (b *Client) GetPullRequestComments(repo models.Repo, pullNum int) (comments
165170
}
166171

167172
func (b *Client) GetMyUUID() (uuid string, err error) {
168-
path := fmt.Sprintf("%s/2.0/user", b.BaseURL)
169-
resp, err := b.makeRequest("GET", path, nil)
173+
if MY_UUID == "" {
174+
path := fmt.Sprintf("%s/2.0/user", b.BaseURL)
175+
resp, err := b.makeRequest("GET", path, nil)
170176

171-
if err != nil {
172-
return uuid, err
173-
}
177+
if err != nil {
178+
return uuid, err
179+
}
174180

175-
var user User
176-
if err := json.Unmarshal(resp, &user); err != nil {
177-
return uuid, errors.Wrapf(err, "Could not parse response %q", string(resp))
178-
}
181+
var user User
182+
if err := json.Unmarshal(resp, &user); err != nil {
183+
return uuid, errors.Wrapf(err, "Could not parse response %q", string(resp))
184+
}
179185

180-
if err := validator.New().Struct(user); err != nil {
181-
return uuid, errors.Wrapf(err, "API response %q was missing a field", string(resp))
182-
}
186+
if err := validator.New().Struct(user); err != nil {
187+
return uuid, errors.Wrapf(err, "API response %q was missing a field", string(resp))
188+
}
183189

184-
uuid = *user.UUID
185-
return uuid, nil
190+
uuid = *user.UUID
191+
MY_UUID = uuid
186192

193+
return uuid, nil
194+
} else {
195+
return MY_UUID, nil
196+
}
187197
}
188198

189199
// PullIsApproved returns true if the merge request was approved.

server/events/vcs/bitbucketcloud/client_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ func TestClient_GetMyUUID(t *testing.T) {
374374
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
375375
switch r.RequestURI {
376376
case "/2.0/user":
377-
w.Write([]byte(json)) // nolint: errcheck
377+
w.Write(json) // nolint: errcheck
378378
return
379379
default:
380380
t.Errorf("got unexpected request at %q", r.RequestURI)
@@ -397,7 +397,7 @@ func TestClient_GetComment(t *testing.T) {
397397
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
398398
switch r.RequestURI {
399399
case "/2.0/repositories/myorg/myrepo/pullrequests/5/comments":
400-
w.Write([]byte(json)) // nolint: errcheck
400+
w.Write(json) // nolint: errcheck
401401
return
402402
default:
403403
t.Errorf("got unexpected request at %q", r.RequestURI)
@@ -472,7 +472,7 @@ func TestClient_HidePRComments(t *testing.T) {
472472
// we have two comments in the test file
473473
// The code is going to delete them all and then create a new one
474474
case "/2.0/repositories/myorg/myrepo/pullrequests/5/comments/498931882":
475-
w.Write([]byte(""))
475+
w.Write([]byte("")) // nolint: errcheck
476476
called += 1
477477
return
478478
// This is the second one
@@ -484,10 +484,10 @@ func TestClient_HidePRComments(t *testing.T) {
484484
Assert(t, r.Method != "DELETE", "Shouldn't delete this one")
485485
return
486486
case "/2.0/repositories/myorg/myrepo/pullrequests/5/comments":
487-
w.Write([]byte(comments)) // nolint: errcheck
487+
w.Write(comments) // nolint: errcheck
488488
return
489489
case "/2.0/user":
490-
w.Write([]byte(json)) // nolint: errcheck
490+
w.Write(json) // nolint: errcheck
491491
return
492492
default:
493493
t.Errorf("got unexpected request at %q", r.RequestURI)
@@ -514,4 +514,3 @@ func TestClient_HidePRComments(t *testing.T) {
514514
Ok(t, err)
515515
Equals(t, 2, called)
516516
}
517-

0 commit comments

Comments
 (0)