Skip to content

Commit 40e4f30

Browse files
authored
Add golang context support (#153)
This is a large cross cutting change to the existing interfaces for go-gerrit that introduces context support to all interfaces drilling down to the HTTP level. Contexts enable support for both passing information through (eg: tracing, logging, etc) and features like cancellation of slow operations (eg: REST calls). In order for consumers of this breaking change to adapt their code to use the new interface they must add a context parameter to their invocations. In most cases this can just be `context.Background()`
1 parent 89fb5cf commit 40e4f30

38 files changed

+640
-555
lines changed

access.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package gerrit
22

3+
import "context"
4+
35
// AccessService contains Access Right related REST endpoints
46
//
57
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-access.html
@@ -63,7 +65,7 @@ type ListAccessRightsOptions struct {
6365
// The entries in the map are sorted by project name.
6466
//
6567
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-access.html#list-access
66-
func (s *AccessService) ListAccessRights(opt *ListAccessRightsOptions) (*map[string]ProjectAccessInfo, *Response, error) {
68+
func (s *AccessService) ListAccessRights(ctx context.Context, opt *ListAccessRightsOptions) (*map[string]ProjectAccessInfo, *Response, error) {
6769
u := "access/"
6870

6971
u, err := addOptions(u, opt)
@@ -72,6 +74,6 @@ func (s *AccessService) ListAccessRights(opt *ListAccessRightsOptions) (*map[str
7274
}
7375

7476
v := new(map[string]ProjectAccessInfo)
75-
resp, err := s.client.Call("GET", u, nil, v)
77+
resp, err := s.client.Call(ctx, "GET", u, nil, v)
7678
return v, resp, err
7779
}

access_test.go

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

33
import (
4+
"context"
45
"fmt"
56
"net/http"
67
"reflect"
@@ -25,7 +26,7 @@ func TestAccessService_ListAccessRights(t *testing.T) {
2526
opt := &gerrit.ListAccessRightsOptions{
2627
Project: []string{"go"},
2728
}
28-
access, _, err := testClient.Access.ListAccessRights(opt)
29+
access, _, err := testClient.Access.ListAccessRights(context.Background(), opt)
2930
if err != nil {
3031
t.Errorf("Access.ListAccessRights returned error: %v", err)
3132
}
@@ -60,7 +61,7 @@ func TestAccessService_ListAccessRights_WithoutOpts(t *testing.T) {
6061
fmt.Fprint(w, `)]}'`+"\n"+`{}`)
6162
})
6263

63-
access, _, err := testClient.Access.ListAccessRights(nil)
64+
access, _, err := testClient.Access.ListAccessRights(context.Background(), nil)
6465
if err != nil {
6566
t.Errorf("Access.ListAccessRights returned error: %v", err)
6667
}

accounts.go

Lines changed: 81 additions & 80 deletions
Large diffs are not rendered by default.

changes.go

Lines changed: 59 additions & 58 deletions
Large diffs are not rendered by default.

changes_attention.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package gerrit
22

3-
import "fmt"
3+
import (
4+
"context"
5+
"fmt"
6+
)
47

58
// AttentionSetInfo entity contains details of users that are in the attention set.
69
//
@@ -32,8 +35,8 @@ type AttentionSetInput struct {
3235
// AttentionSetInput.Input must be provided
3336
//
3437
// https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#remove-from-attention-set
35-
func (s *ChangesService) RemoveAttention(changeID, accountID string, input *AttentionSetInput) (*Response, error) {
38+
func (s *ChangesService) RemoveAttention(ctx context.Context, changeID, accountID string, input *AttentionSetInput) (*Response, error) {
3639
u := fmt.Sprintf("changes/%s/attention/%s", changeID, accountID)
3740

38-
return s.client.DeleteRequest(u, input)
41+
return s.client.DeleteRequest(ctx, u, input)
3942
}

changes_edit.go

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gerrit
22

33
import (
4+
"context"
45
"fmt"
56
"net/url"
67
)
@@ -36,15 +37,15 @@ type ChangeEditDetailOptions struct {
3637
// Edits aren’t tracked in the database.
3738
//
3839
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#get-edit-detail
39-
func (s *ChangesService) GetChangeEditDetails(changeID string, opt *ChangeEditDetailOptions) (*EditInfo, *Response, error) {
40+
func (s *ChangesService) GetChangeEditDetails(ctx context.Context, changeID string, opt *ChangeEditDetailOptions) (*EditInfo, *Response, error) {
4041
u := fmt.Sprintf("changes/%s/edit", changeID)
4142

4243
u, err := addOptions(u, opt)
4344
if err != nil {
4445
return nil, nil, err
4546
}
4647

47-
req, err := s.client.NewRequest("GET", u, nil)
48+
req, err := s.client.NewRequest(ctx, "GET", u, nil)
4849
if err != nil {
4950
return nil, nil, err
5051
}
@@ -62,10 +63,10 @@ func (s *ChangesService) GetChangeEditDetails(changeID string, opt *ChangeEditDe
6263
// Currently only web links are returned.
6364
//
6465
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#get-edit-meta-data
65-
func (s *ChangesService) RetrieveMetaDataOfAFileFromChangeEdit(changeID, filePath string) (*EditFileInfo, *Response, error) {
66+
func (s *ChangesService) RetrieveMetaDataOfAFileFromChangeEdit(ctx context.Context, changeID, filePath string) (*EditFileInfo, *Response, error) {
6667
u := fmt.Sprintf("changes/%s/edit/%s/meta", changeID, filePath)
6768

68-
req, err := s.client.NewRequest("GET", u, nil)
69+
req, err := s.client.NewRequest(ctx, "GET", u, nil)
6970
if err != nil {
7071
return nil, nil, err
7172
}
@@ -83,9 +84,9 @@ func (s *ChangesService) RetrieveMetaDataOfAFileFromChangeEdit(changeID, filePat
8384
// The commit message is returned as base64 encoded string.
8485
//
8586
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#get-edit-message
86-
func (s *ChangesService) RetrieveCommitMessageFromChangeEdit(changeID string) (string, *Response, error) {
87+
func (s *ChangesService) RetrieveCommitMessageFromChangeEdit(ctx context.Context, changeID string) (string, *Response, error) {
8788
u := fmt.Sprintf("changes/%s/edit:message", changeID)
88-
return getStringResponseWithoutOptions(s.client, u)
89+
return getStringResponseWithoutOptions(ctx, s.client, u)
8990
}
9091

9192
// ChangeFileContentInChangeEdit put content of a file to a change edit.
@@ -95,10 +96,10 @@ func (s *ChangesService) RetrieveCommitMessageFromChangeEdit(changeID string) (s
9596
// As response “204 No Content” is returned.
9697
//
9798
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#put-edit-file
98-
func (s *ChangesService) ChangeFileContentInChangeEdit(changeID, filePath, content string) (*Response, error) {
99+
func (s *ChangesService) ChangeFileContentInChangeEdit(ctx context.Context, changeID, filePath, content string) (*Response, error) {
99100
u := fmt.Sprintf("changes/%s/edit/%s", changeID, url.QueryEscape(filePath))
100101

101-
req, err := s.client.NewRawPutRequest(u, content)
102+
req, err := s.client.NewRawPutRequest(ctx, u, content)
102103
if err != nil {
103104
return nil, err
104105
}
@@ -113,10 +114,10 @@ func (s *ChangesService) ChangeFileContentInChangeEdit(changeID, filePath, conte
113114
// As response “204 No Content” is returned.
114115
//
115116
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#put-change-edit-message
116-
func (s *ChangesService) ChangeCommitMessageInChangeEdit(changeID string, input *ChangeEditMessageInput) (*Response, error) {
117+
func (s *ChangesService) ChangeCommitMessageInChangeEdit(ctx context.Context, changeID string, input *ChangeEditMessageInput) (*Response, error) {
117118
u := fmt.Sprintf("changes/%s/edit:message", changeID)
118119

119-
req, err := s.client.NewRequest("PUT", u, input)
120+
req, err := s.client.NewRequest(ctx, "PUT", u, input)
120121
if err != nil {
121122
return nil, err
122123
}
@@ -131,30 +132,30 @@ func (s *ChangesService) ChangeCommitMessageInChangeEdit(changeID string, input
131132
// When change edit doesn’t exist for this change yet it is created.
132133
//
133134
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#delete-edit-file
134-
func (s *ChangesService) DeleteFileInChangeEdit(changeID, filePath string) (*Response, error) {
135+
func (s *ChangesService) DeleteFileInChangeEdit(ctx context.Context, changeID, filePath string) (*Response, error) {
135136
u := fmt.Sprintf("changes/%s/edit/%s", changeID, filePath)
136-
return s.client.DeleteRequest(u, nil)
137+
return s.client.DeleteRequest(ctx, u, nil)
137138
}
138139

139140
// DeleteChangeEdit deletes change edit.
140141
//
141142
// As response “204 No Content” is returned.
142143
//
143144
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#delete-edit
144-
func (s *ChangesService) DeleteChangeEdit(changeID string) (*Response, error) {
145+
func (s *ChangesService) DeleteChangeEdit(ctx context.Context, changeID string) (*Response, error) {
145146
u := fmt.Sprintf("changes/%s/edit", changeID)
146-
return s.client.DeleteRequest(u, nil)
147+
return s.client.DeleteRequest(ctx, u, nil)
147148
}
148149

149150
// PublishChangeEdit promotes change edit to a regular patch set.
150151
//
151152
// As response “204 No Content” is returned.
152153
//
153154
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#publish-edit
154-
func (s *ChangesService) PublishChangeEdit(changeID, notify string) (*Response, error) {
155+
func (s *ChangesService) PublishChangeEdit(ctx context.Context, changeID, notify string) (*Response, error) {
155156
u := fmt.Sprintf("changes/%s/edit:publish", changeID)
156157

157-
req, err := s.client.NewRequest("POST", u, map[string]string{
158+
req, err := s.client.NewRequest(ctx, "POST", u, map[string]string{
158159
"notify": notify,
159160
})
160161
if err != nil {
@@ -169,10 +170,10 @@ func (s *ChangesService) PublishChangeEdit(changeID, notify string) (*Response,
169170
// When change edit is already based on top of the latest patch set, the response “409 Conflict” is returned.
170171
//
171172
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#rebase-edit
172-
func (s *ChangesService) RebaseChangeEdit(changeID string) (*Response, error) {
173+
func (s *ChangesService) RebaseChangeEdit(ctx context.Context, changeID string) (*Response, error) {
173174
u := fmt.Sprintf("changes/%s/edit:rebase", changeID)
174175

175-
req, err := s.client.NewRequest("POST", u, nil)
176+
req, err := s.client.NewRequest(ctx, "POST", u, nil)
176177
if err != nil {
177178
return nil, err
178179
}
@@ -190,10 +191,10 @@ func (s *ChangesService) RebaseChangeEdit(changeID string) (*Response, error) {
190191
// If only the content type is required, callers should use HEAD to avoid downloading the encoded file contents.
191192
//
192193
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#get-edit-file
193-
func (s *ChangesService) RetrieveFileContentFromChangeEdit(changeID, filePath string) (*string, *Response, error) {
194+
func (s *ChangesService) RetrieveFileContentFromChangeEdit(ctx context.Context, changeID, filePath string) (*string, *Response, error) {
194195
u := fmt.Sprintf("changes/%s/edit/%s", changeID, filePath)
195196

196-
req, err := s.client.NewRequest("GET", u, nil)
197+
req, err := s.client.NewRequest(ctx, "GET", u, nil)
197198
if err != nil {
198199
return nil, nil, err
199200
}
@@ -214,10 +215,10 @@ func (s *ChangesService) RetrieveFileContentFromChangeEdit(changeID, filePath st
214215
// For further documentation please have a look at RetrieveFileContentFromChangeEdit.
215216
//
216217
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#get-edit-file
217-
func (s *ChangesService) RetrieveFileContentTypeFromChangeEdit(changeID, filePath string) (*Response, error) {
218+
func (s *ChangesService) RetrieveFileContentTypeFromChangeEdit(ctx context.Context, changeID, filePath string) (*Response, error) {
218219
u := fmt.Sprintf("changes/%s/edit/%s", changeID, filePath)
219220

220-
req, err := s.client.NewRequest("HEAD", u, nil)
221+
req, err := s.client.NewRequest(ctx, "HEAD", u, nil)
221222
if err != nil {
222223
return nil, err
223224
}

changes_hashtags.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package gerrit
22

3-
import "fmt"
3+
import (
4+
"context"
5+
"fmt"
6+
)
47

58
// GetHashtags gets the hashtags associated with a change.
69
//
710
// https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#get-hashtags
8-
func (c *ChangesService) GetHashtags(changeID string) ([]string, *Response, error) {
11+
func (c *ChangesService) GetHashtags(ctx context.Context, changeID string) ([]string, *Response, error) {
912
u := fmt.Sprintf("changes/%s/hashtags", changeID)
1013

11-
req, err := c.client.NewRequest("GET", u, nil)
14+
req, err := c.client.NewRequest(ctx, "GET", u, nil)
1215
if err != nil {
1316
return nil, nil, err
1417
}
@@ -35,10 +38,10 @@ type HashtagsInput struct {
3538
// As response the change’s hashtags are returned as a list of strings.
3639
//
3740
// https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#set-hashtags
38-
func (c *ChangesService) SetHashtags(changeID string, input *HashtagsInput) ([]string, *Response, error) {
41+
func (c *ChangesService) SetHashtags(ctx context.Context, changeID string, input *HashtagsInput) ([]string, *Response, error) {
3942
u := fmt.Sprintf("changes/%s/hashtags", changeID)
4043

41-
req, err := c.client.NewRequest("POST", u, input)
44+
req, err := c.client.NewRequest(ctx, "POST", u, input)
4245
if err != nil {
4346
return nil, nil, err
4447
}

changes_hashtags_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gerrit_test
22

33
import (
4+
"context"
45
"fmt"
56
"net/http"
67
"net/http/httptest"
@@ -30,12 +31,13 @@ func TestChangesService_GetHashtags(t *testing.T) {
3031
}))
3132
defer ts.Close()
3233

33-
client, err := gerrit.NewClient(ts.URL, nil)
34+
ctx := context.Background()
35+
client, err := gerrit.NewClient(ctx, ts.URL, nil)
3436
if err != nil {
3537
t.Fatal(err)
3638
}
3739

38-
hashtags, _, err := client.Changes.GetHashtags("123")
40+
hashtags, _, err := client.Changes.GetHashtags(ctx, "123")
3941
if err != nil {
4042
t.Fatal(err)
4143
}
@@ -66,12 +68,13 @@ func TestChangesService_SetHashtags(t *testing.T) {
6668
}))
6769
defer ts.Close()
6870

69-
client, err := gerrit.NewClient(ts.URL, nil)
71+
ctx := context.Background()
72+
client, err := gerrit.NewClient(ctx, ts.URL, nil)
7073
if err != nil {
7174
t.Fatal(err)
7275
}
7376

74-
hashtags, _, err := client.Changes.SetHashtags("123", &gerrit.HashtagsInput{
77+
hashtags, _, err := client.Changes.SetHashtags(ctx, "123", &gerrit.HashtagsInput{
7578
Add: []string{"hashtag3"},
7679
Remove: []string{"hashtag2"},
7780
})

changes_reviewer.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gerrit
22

33
import (
4+
"context"
45
"fmt"
56
)
67

@@ -37,10 +38,10 @@ type DeleteVoteInput struct {
3738
// ListReviewers lists the reviewers of a change.
3839
//
3940
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-reviewers
40-
func (s *ChangesService) ListReviewers(changeID string) (*[]ReviewerInfo, *Response, error) {
41+
func (s *ChangesService) ListReviewers(ctx context.Context, changeID string) (*[]ReviewerInfo, *Response, error) {
4142
u := fmt.Sprintf("changes/%s/reviewers/", changeID)
4243

43-
req, err := s.client.NewRequest("GET", u, nil)
44+
req, err := s.client.NewRequest(ctx, "GET", u, nil)
4445
if err != nil {
4546
return nil, nil, err
4647
}
@@ -58,15 +59,15 @@ func (s *ChangesService) ListReviewers(changeID string) (*[]ReviewerInfo, *Respo
5859
// If result limit is not passed, then the default 10 is used.
5960
//
6061
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#suggest-reviewers
61-
func (s *ChangesService) SuggestReviewers(changeID string, opt *QueryOptions) (*[]SuggestedReviewerInfo, *Response, error) {
62+
func (s *ChangesService) SuggestReviewers(ctx context.Context, changeID string, opt *QueryOptions) (*[]SuggestedReviewerInfo, *Response, error) {
6263
u := fmt.Sprintf("changes/%s/suggest_reviewers", changeID)
6364

6465
u, err := addOptions(u, opt)
6566
if err != nil {
6667
return nil, nil, err
6768
}
6869

69-
req, err := s.client.NewRequest("GET", u, nil)
70+
req, err := s.client.NewRequest(ctx, "GET", u, nil)
7071
if err != nil {
7172
return nil, nil, err
7273
}
@@ -83,10 +84,10 @@ func (s *ChangesService) SuggestReviewers(changeID string, opt *QueryOptions) (*
8384
// GetReviewer retrieves a reviewer of a change.
8485
//
8586
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#get-reviewer
86-
func (s *ChangesService) GetReviewer(changeID, accountID string) (*ReviewerInfo, *Response, error) {
87+
func (s *ChangesService) GetReviewer(ctx context.Context, changeID, accountID string) (*ReviewerInfo, *Response, error) {
8788
u := fmt.Sprintf("changes/%s/reviewers/%s", changeID, accountID)
8889

89-
req, err := s.client.NewRequest("GET", u, nil)
90+
req, err := s.client.NewRequest(ctx, "GET", u, nil)
9091
if err != nil {
9192
return nil, nil, err
9293
}
@@ -109,10 +110,10 @@ func (s *ChangesService) GetReviewer(changeID, accountID string) (*ReviewerInfo,
109110
// If a group with many members is added as reviewer a confirmation may be required.
110111
//
111112
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#add-reviewer
112-
func (s *ChangesService) AddReviewer(changeID string, input *ReviewerInput) (*AddReviewerResult, *Response, error) {
113+
func (s *ChangesService) AddReviewer(ctx context.Context, changeID string, input *ReviewerInput) (*AddReviewerResult, *Response, error) {
113114
u := fmt.Sprintf("changes/%s/reviewers", changeID)
114115

115-
req, err := s.client.NewRequest("POST", u, input)
116+
req, err := s.client.NewRequest(ctx, "POST", u, input)
116117
if err != nil {
117118
return nil, nil, err
118119
}
@@ -129,17 +130,17 @@ func (s *ChangesService) AddReviewer(changeID string, input *ReviewerInput) (*Ad
129130
// DeleteReviewer deletes a reviewer from a change.
130131
//
131132
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#delete-reviewer
132-
func (s *ChangesService) DeleteReviewer(changeID, accountID string) (*Response, error) {
133+
func (s *ChangesService) DeleteReviewer(ctx context.Context, changeID, accountID string) (*Response, error) {
133134
u := fmt.Sprintf("changes/%s/reviewers/%s", changeID, accountID)
134-
return s.client.DeleteRequest(u, nil)
135+
return s.client.DeleteRequest(ctx, u, nil)
135136
}
136137

137138
// ListVotes lists the votes for a specific reviewer of the change.
138139
//
139140
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-votes
140-
func (s *ChangesService) ListVotes(changeID string, accountID string) (map[string]int, *Response, error) {
141+
func (s *ChangesService) ListVotes(ctx context.Context, changeID string, accountID string) (map[string]int, *Response, error) {
141142
u := fmt.Sprintf("changes/%s/reviewers/%s/votes/", changeID, accountID)
142-
req, err := s.client.NewRequest("GET", u, nil)
143+
req, err := s.client.NewRequest(ctx, "GET", u, nil)
143144
if err != nil {
144145
return nil, nil, err
145146
}
@@ -157,7 +158,7 @@ func (s *ChangesService) ListVotes(changeID string, accountID string) (map[strin
157158
// the change.
158159
//
159160
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#delete-vote
160-
func (s *ChangesService) DeleteVote(changeID string, accountID string, label string, input *DeleteVoteInput) (*Response, error) {
161+
func (s *ChangesService) DeleteVote(ctx context.Context, changeID string, accountID string, label string, input *DeleteVoteInput) (*Response, error) {
161162
u := fmt.Sprintf("changes/%s/reviewers/%s/votes/%s", changeID, accountID, label)
162-
return s.client.DeleteRequest(u, input)
163+
return s.client.DeleteRequest(ctx, u, input)
163164
}

0 commit comments

Comments
 (0)