Skip to content

Commit

Permalink
Add golang context support (#153)
Browse files Browse the repository at this point in the history
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()`
  • Loading branch information
kellyma2 authored Nov 16, 2023
1 parent 89fb5cf commit 40e4f30
Show file tree
Hide file tree
Showing 38 changed files with 640 additions and 555 deletions.
6 changes: 4 additions & 2 deletions access.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gerrit

import "context"

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

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

v := new(map[string]ProjectAccessInfo)
resp, err := s.client.Call("GET", u, nil, v)
resp, err := s.client.Call(ctx, "GET", u, nil, v)
return v, resp, err
}
5 changes: 3 additions & 2 deletions access_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gerrit_test

import (
"context"
"fmt"
"net/http"
"reflect"
Expand All @@ -25,7 +26,7 @@ func TestAccessService_ListAccessRights(t *testing.T) {
opt := &gerrit.ListAccessRightsOptions{
Project: []string{"go"},
}
access, _, err := testClient.Access.ListAccessRights(opt)
access, _, err := testClient.Access.ListAccessRights(context.Background(), opt)
if err != nil {
t.Errorf("Access.ListAccessRights returned error: %v", err)
}
Expand Down Expand Up @@ -60,7 +61,7 @@ func TestAccessService_ListAccessRights_WithoutOpts(t *testing.T) {
fmt.Fprint(w, `)]}'`+"\n"+`{}`)
})

access, _, err := testClient.Access.ListAccessRights(nil)
access, _, err := testClient.Access.ListAccessRights(context.Background(), nil)
if err != nil {
t.Errorf("Access.ListAccessRights returned error: %v", err)
}
Expand Down
161 changes: 81 additions & 80 deletions accounts.go

Large diffs are not rendered by default.

117 changes: 59 additions & 58 deletions changes.go

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions changes_attention.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package gerrit

import "fmt"
import (
"context"
"fmt"
)

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

return s.client.DeleteRequest(u, input)
return s.client.DeleteRequest(ctx, u, input)
}
45 changes: 23 additions & 22 deletions changes_edit.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gerrit

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

u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}

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

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

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

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

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

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

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

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

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

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

req, err := s.client.NewRequest("HEAD", u, nil)
req, err := s.client.NewRequest(ctx, "HEAD", u, nil)
if err != nil {
return nil, err
}
Expand Down
13 changes: 8 additions & 5 deletions changes_hashtags.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package gerrit

import "fmt"
import (
"context"
"fmt"
)

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

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

req, err := c.client.NewRequest("POST", u, input)
req, err := c.client.NewRequest(ctx, "POST", u, input)
if err != nil {
return nil, nil, err
}
Expand Down
11 changes: 7 additions & 4 deletions changes_hashtags_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gerrit_test

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -30,12 +31,13 @@ func TestChangesService_GetHashtags(t *testing.T) {
}))
defer ts.Close()

client, err := gerrit.NewClient(ts.URL, nil)
ctx := context.Background()
client, err := gerrit.NewClient(ctx, ts.URL, nil)
if err != nil {
t.Fatal(err)
}

hashtags, _, err := client.Changes.GetHashtags("123")
hashtags, _, err := client.Changes.GetHashtags(ctx, "123")
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -66,12 +68,13 @@ func TestChangesService_SetHashtags(t *testing.T) {
}))
defer ts.Close()

client, err := gerrit.NewClient(ts.URL, nil)
ctx := context.Background()
client, err := gerrit.NewClient(ctx, ts.URL, nil)
if err != nil {
t.Fatal(err)
}

hashtags, _, err := client.Changes.SetHashtags("123", &gerrit.HashtagsInput{
hashtags, _, err := client.Changes.SetHashtags(ctx, "123", &gerrit.HashtagsInput{
Add: []string{"hashtag3"},
Remove: []string{"hashtag2"},
})
Expand Down
29 changes: 15 additions & 14 deletions changes_reviewer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gerrit

import (
"context"
"fmt"
)

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

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

u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}

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

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

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

// ListVotes lists the votes for a specific reviewer of the change.
//
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#list-votes
func (s *ChangesService) ListVotes(changeID string, accountID string) (map[string]int, *Response, error) {
func (s *ChangesService) ListVotes(ctx context.Context, changeID string, accountID string) (map[string]int, *Response, error) {
u := fmt.Sprintf("changes/%s/reviewers/%s/votes/", changeID, accountID)
req, err := s.client.NewRequest("GET", u, nil)
req, err := s.client.NewRequest(ctx, "GET", u, nil)
if err != nil {
return nil, nil, err
}
Expand All @@ -157,7 +158,7 @@ func (s *ChangesService) ListVotes(changeID string, accountID string) (map[strin
// the change.
//
// Gerrit API docs: https://gerrit-review.googlesource.com/Documentation/rest-api-changes.html#delete-vote
func (s *ChangesService) DeleteVote(changeID string, accountID string, label string, input *DeleteVoteInput) (*Response, error) {
func (s *ChangesService) DeleteVote(ctx context.Context, changeID string, accountID string, label string, input *DeleteVoteInput) (*Response, error) {
u := fmt.Sprintf("changes/%s/reviewers/%s/votes/%s", changeID, accountID, label)
return s.client.DeleteRequest(u, input)
return s.client.DeleteRequest(ctx, u, input)
}
Loading

0 comments on commit 40e4f30

Please sign in to comment.