Skip to content

Commit 23d6cb9

Browse files
authored
Add support for context package to all endpoints. (google#529)
This is a large breaking API change. It is unavoidable and necessary to resolve google#526. This breaking change is part of bump to libraryVersion 3. It adds ctx context.Context as first parameter to all endpoint methods, including Do. Updating for this API change should be easy and the compiler will help catch instances that need to be updated. For example: main.go:193: not enough arguments in call to gh.Activity.MarkRepositoryNotificationsRead have (string, string, time.Time) want (context.Context, string, string, time.Time) ... You should pass the available context as first parameter. If your code does not have context.Context available and you want to maintain previous normal behavior, use context.Background(). Don't pass nil context; use context.TODO() instead if you wish to delay figuring out the best context to use. Then you can address the TODO at a later time. Refer to documentation of package context at https://godoc.org/context for more information. This commit also changes ./tests/fields to use context.Background() instead of deprecated oauth2.NoContext. Resolves google#526.
1 parent 2ec691a commit 23d6cb9

File tree

127 files changed

+1497
-1298
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+1497
-1298
lines changed

examples/basicauth/main.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package main
1010

1111
import (
1212
"bufio"
13+
"context"
1314
"fmt"
1415
"os"
1516
"strings"
@@ -34,14 +35,15 @@ func main() {
3435
}
3536

3637
client := github.NewClient(tp.Client())
37-
user, _, err := client.Users.Get("")
38+
ctx := context.Background()
39+
user, _, err := client.Users.Get(ctx, "")
3840

3941
// Is this a two-factor auth error? If so, prompt for OTP and try again.
4042
if _, ok := err.(*github.TwoFactorAuthError); ok {
4143
fmt.Print("\nGitHub OTP: ")
4244
otp, _ := r.ReadString('\n')
4345
tp.OTP = strings.TrimSpace(otp)
44-
user, _, err = client.Users.Get("")
46+
user, _, err = client.Users.Get(ctx, "")
4547
}
4648

4749
if err != nil {

github/activity.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package github
77

8+
import "context"
9+
810
// ActivityService handles communication with the activity related
911
// methods of the GitHub API.
1012
//
@@ -51,14 +53,14 @@ type Feeds struct {
5153
//
5254
// Note: Private feeds are only returned when authenticating via Basic Auth
5355
// since current feed URIs use the older, non revocable auth tokens.
54-
func (s *ActivityService) ListFeeds() (*Feeds, *Response, error) {
56+
func (s *ActivityService) ListFeeds(ctx context.Context) (*Feeds, *Response, error) {
5557
req, err := s.client.NewRequest("GET", "feeds", nil)
5658
if err != nil {
5759
return nil, nil, err
5860
}
5961

6062
f := &Feeds{}
61-
resp, err := s.client.Do(req, f)
63+
resp, err := s.client.Do(ctx, req, f)
6264
if err != nil {
6365
return nil, resp, err
6466
}

github/activity_events.go

+17-16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package github
77

88
import (
9+
"context"
910
"encoding/json"
1011
"fmt"
1112
"time"
@@ -97,7 +98,7 @@ func (e *Event) Payload() (payload interface{}) {
9798
// ListEvents drinks from the firehose of all public events across GitHub.
9899
//
99100
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-public-events
100-
func (s *ActivityService) ListEvents(opt *ListOptions) ([]*Event, *Response, error) {
101+
func (s *ActivityService) ListEvents(ctx context.Context, opt *ListOptions) ([]*Event, *Response, error) {
101102
u, err := addOptions("events", opt)
102103
if err != nil {
103104
return nil, nil, err
@@ -109,7 +110,7 @@ func (s *ActivityService) ListEvents(opt *ListOptions) ([]*Event, *Response, err
109110
}
110111

111112
var events []*Event
112-
resp, err := s.client.Do(req, &events)
113+
resp, err := s.client.Do(ctx, req, &events)
113114
if err != nil {
114115
return nil, resp, err
115116
}
@@ -120,7 +121,7 @@ func (s *ActivityService) ListEvents(opt *ListOptions) ([]*Event, *Response, err
120121
// ListRepositoryEvents lists events for a repository.
121122
//
122123
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-repository-events
123-
func (s *ActivityService) ListRepositoryEvents(owner, repo string, opt *ListOptions) ([]*Event, *Response, error) {
124+
func (s *ActivityService) ListRepositoryEvents(ctx context.Context, owner, repo string, opt *ListOptions) ([]*Event, *Response, error) {
124125
u := fmt.Sprintf("repos/%v/%v/events", owner, repo)
125126
u, err := addOptions(u, opt)
126127
if err != nil {
@@ -133,7 +134,7 @@ func (s *ActivityService) ListRepositoryEvents(owner, repo string, opt *ListOpti
133134
}
134135

135136
var events []*Event
136-
resp, err := s.client.Do(req, &events)
137+
resp, err := s.client.Do(ctx, req, &events)
137138
if err != nil {
138139
return nil, resp, err
139140
}
@@ -144,7 +145,7 @@ func (s *ActivityService) ListRepositoryEvents(owner, repo string, opt *ListOpti
144145
// ListIssueEventsForRepository lists issue events for a repository.
145146
//
146147
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-issue-events-for-a-repository
147-
func (s *ActivityService) ListIssueEventsForRepository(owner, repo string, opt *ListOptions) ([]*IssueEvent, *Response, error) {
148+
func (s *ActivityService) ListIssueEventsForRepository(ctx context.Context, owner, repo string, opt *ListOptions) ([]*IssueEvent, *Response, error) {
148149
u := fmt.Sprintf("repos/%v/%v/issues/events", owner, repo)
149150
u, err := addOptions(u, opt)
150151
if err != nil {
@@ -157,7 +158,7 @@ func (s *ActivityService) ListIssueEventsForRepository(owner, repo string, opt *
157158
}
158159

159160
var events []*IssueEvent
160-
resp, err := s.client.Do(req, &events)
161+
resp, err := s.client.Do(ctx, req, &events)
161162
if err != nil {
162163
return nil, resp, err
163164
}
@@ -168,7 +169,7 @@ func (s *ActivityService) ListIssueEventsForRepository(owner, repo string, opt *
168169
// ListEventsForRepoNetwork lists public events for a network of repositories.
169170
//
170171
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-public-events-for-a-network-of-repositories
171-
func (s *ActivityService) ListEventsForRepoNetwork(owner, repo string, opt *ListOptions) ([]*Event, *Response, error) {
172+
func (s *ActivityService) ListEventsForRepoNetwork(ctx context.Context, owner, repo string, opt *ListOptions) ([]*Event, *Response, error) {
172173
u := fmt.Sprintf("networks/%v/%v/events", owner, repo)
173174
u, err := addOptions(u, opt)
174175
if err != nil {
@@ -181,7 +182,7 @@ func (s *ActivityService) ListEventsForRepoNetwork(owner, repo string, opt *List
181182
}
182183

183184
var events []*Event
184-
resp, err := s.client.Do(req, &events)
185+
resp, err := s.client.Do(ctx, req, &events)
185186
if err != nil {
186187
return nil, resp, err
187188
}
@@ -192,7 +193,7 @@ func (s *ActivityService) ListEventsForRepoNetwork(owner, repo string, opt *List
192193
// ListEventsForOrganization lists public events for an organization.
193194
//
194195
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-public-events-for-an-organization
195-
func (s *ActivityService) ListEventsForOrganization(org string, opt *ListOptions) ([]*Event, *Response, error) {
196+
func (s *ActivityService) ListEventsForOrganization(ctx context.Context, org string, opt *ListOptions) ([]*Event, *Response, error) {
196197
u := fmt.Sprintf("orgs/%v/events", org)
197198
u, err := addOptions(u, opt)
198199
if err != nil {
@@ -205,7 +206,7 @@ func (s *ActivityService) ListEventsForOrganization(org string, opt *ListOptions
205206
}
206207

207208
var events []*Event
208-
resp, err := s.client.Do(req, &events)
209+
resp, err := s.client.Do(ctx, req, &events)
209210
if err != nil {
210211
return nil, resp, err
211212
}
@@ -217,7 +218,7 @@ func (s *ActivityService) ListEventsForOrganization(org string, opt *ListOptions
217218
// true, only public events will be returned.
218219
//
219220
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-events-performed-by-a-user
220-
func (s *ActivityService) ListEventsPerformedByUser(user string, publicOnly bool, opt *ListOptions) ([]*Event, *Response, error) {
221+
func (s *ActivityService) ListEventsPerformedByUser(ctx context.Context, user string, publicOnly bool, opt *ListOptions) ([]*Event, *Response, error) {
221222
var u string
222223
if publicOnly {
223224
u = fmt.Sprintf("users/%v/events/public", user)
@@ -235,7 +236,7 @@ func (s *ActivityService) ListEventsPerformedByUser(user string, publicOnly bool
235236
}
236237

237238
var events []*Event
238-
resp, err := s.client.Do(req, &events)
239+
resp, err := s.client.Do(ctx, req, &events)
239240
if err != nil {
240241
return nil, resp, err
241242
}
@@ -247,7 +248,7 @@ func (s *ActivityService) ListEventsPerformedByUser(user string, publicOnly bool
247248
// true, only public events will be returned.
248249
//
249250
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-events-that-a-user-has-received
250-
func (s *ActivityService) ListEventsReceivedByUser(user string, publicOnly bool, opt *ListOptions) ([]*Event, *Response, error) {
251+
func (s *ActivityService) ListEventsReceivedByUser(ctx context.Context, user string, publicOnly bool, opt *ListOptions) ([]*Event, *Response, error) {
251252
var u string
252253
if publicOnly {
253254
u = fmt.Sprintf("users/%v/received_events/public", user)
@@ -265,7 +266,7 @@ func (s *ActivityService) ListEventsReceivedByUser(user string, publicOnly bool,
265266
}
266267

267268
var events []*Event
268-
resp, err := s.client.Do(req, &events)
269+
resp, err := s.client.Do(ctx, req, &events)
269270
if err != nil {
270271
return nil, resp, err
271272
}
@@ -277,7 +278,7 @@ func (s *ActivityService) ListEventsReceivedByUser(user string, publicOnly bool,
277278
// must be authenticated as the user to view this.
278279
//
279280
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-events-for-an-organization
280-
func (s *ActivityService) ListUserEventsForOrganization(org, user string, opt *ListOptions) ([]*Event, *Response, error) {
281+
func (s *ActivityService) ListUserEventsForOrganization(ctx context.Context, org, user string, opt *ListOptions) ([]*Event, *Response, error) {
281282
u := fmt.Sprintf("users/%v/events/orgs/%v", user, org)
282283
u, err := addOptions(u, opt)
283284
if err != nil {
@@ -290,7 +291,7 @@ func (s *ActivityService) ListUserEventsForOrganization(org, user string, opt *L
290291
}
291292

292293
var events []*Event
293-
resp, err := s.client.Do(req, &events)
294+
resp, err := s.client.Do(ctx, req, &events)
294295
if err != nil {
295296
return nil, resp, err
296297
}

github/activity_events_test.go

+17-16
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package github
77

88
import (
9+
"context"
910
"encoding/json"
1011
"fmt"
1112
"net/http"
@@ -26,7 +27,7 @@ func TestActivityService_ListEvents(t *testing.T) {
2627
})
2728

2829
opt := &ListOptions{Page: 2}
29-
events, _, err := client.Activity.ListEvents(opt)
30+
events, _, err := client.Activity.ListEvents(context.Background(), opt)
3031
if err != nil {
3132
t.Errorf("Activities.ListEvents returned error: %v", err)
3233
}
@@ -50,7 +51,7 @@ func TestActivityService_ListRepositoryEvents(t *testing.T) {
5051
})
5152

5253
opt := &ListOptions{Page: 2}
53-
events, _, err := client.Activity.ListRepositoryEvents("o", "r", opt)
54+
events, _, err := client.Activity.ListRepositoryEvents(context.Background(), "o", "r", opt)
5455
if err != nil {
5556
t.Errorf("Activities.ListRepositoryEvents returned error: %v", err)
5657
}
@@ -62,7 +63,7 @@ func TestActivityService_ListRepositoryEvents(t *testing.T) {
6263
}
6364

6465
func TestActivityService_ListRepositoryEvents_invalidOwner(t *testing.T) {
65-
_, _, err := client.Activity.ListRepositoryEvents("%", "%", nil)
66+
_, _, err := client.Activity.ListRepositoryEvents(context.Background(), "%", "%", nil)
6667
testURLParseError(t, err)
6768
}
6869

@@ -79,7 +80,7 @@ func TestActivityService_ListIssueEventsForRepository(t *testing.T) {
7980
})
8081

8182
opt := &ListOptions{Page: 2}
82-
events, _, err := client.Activity.ListIssueEventsForRepository("o", "r", opt)
83+
events, _, err := client.Activity.ListIssueEventsForRepository(context.Background(), "o", "r", opt)
8384
if err != nil {
8485
t.Errorf("Activities.ListIssueEventsForRepository returned error: %v", err)
8586
}
@@ -91,7 +92,7 @@ func TestActivityService_ListIssueEventsForRepository(t *testing.T) {
9192
}
9293

9394
func TestActivityService_ListIssueEventsForRepository_invalidOwner(t *testing.T) {
94-
_, _, err := client.Activity.ListIssueEventsForRepository("%", "%", nil)
95+
_, _, err := client.Activity.ListIssueEventsForRepository(context.Background(), "%", "%", nil)
9596
testURLParseError(t, err)
9697
}
9798

@@ -108,7 +109,7 @@ func TestActivityService_ListEventsForRepoNetwork(t *testing.T) {
108109
})
109110

110111
opt := &ListOptions{Page: 2}
111-
events, _, err := client.Activity.ListEventsForRepoNetwork("o", "r", opt)
112+
events, _, err := client.Activity.ListEventsForRepoNetwork(context.Background(), "o", "r", opt)
112113
if err != nil {
113114
t.Errorf("Activities.ListEventsForRepoNetwork returned error: %v", err)
114115
}
@@ -120,7 +121,7 @@ func TestActivityService_ListEventsForRepoNetwork(t *testing.T) {
120121
}
121122

122123
func TestActivityService_ListEventsForRepoNetwork_invalidOwner(t *testing.T) {
123-
_, _, err := client.Activity.ListEventsForRepoNetwork("%", "%", nil)
124+
_, _, err := client.Activity.ListEventsForRepoNetwork(context.Background(), "%", "%", nil)
124125
testURLParseError(t, err)
125126
}
126127

@@ -137,7 +138,7 @@ func TestActivityService_ListEventsForOrganization(t *testing.T) {
137138
})
138139

139140
opt := &ListOptions{Page: 2}
140-
events, _, err := client.Activity.ListEventsForOrganization("o", opt)
141+
events, _, err := client.Activity.ListEventsForOrganization(context.Background(), "o", opt)
141142
if err != nil {
142143
t.Errorf("Activities.ListEventsForOrganization returned error: %v", err)
143144
}
@@ -149,7 +150,7 @@ func TestActivityService_ListEventsForOrganization(t *testing.T) {
149150
}
150151

151152
func TestActivityService_ListEventsForOrganization_invalidOrg(t *testing.T) {
152-
_, _, err := client.Activity.ListEventsForOrganization("%", nil)
153+
_, _, err := client.Activity.ListEventsForOrganization(context.Background(), "%", nil)
153154
testURLParseError(t, err)
154155
}
155156

@@ -166,7 +167,7 @@ func TestActivityService_ListEventsPerformedByUser_all(t *testing.T) {
166167
})
167168

168169
opt := &ListOptions{Page: 2}
169-
events, _, err := client.Activity.ListEventsPerformedByUser("u", false, opt)
170+
events, _, err := client.Activity.ListEventsPerformedByUser(context.Background(), "u", false, opt)
170171
if err != nil {
171172
t.Errorf("Events.ListPerformedByUser returned error: %v", err)
172173
}
@@ -186,7 +187,7 @@ func TestActivityService_ListEventsPerformedByUser_publicOnly(t *testing.T) {
186187
fmt.Fprint(w, `[{"id":"1"},{"id":"2"}]`)
187188
})
188189

189-
events, _, err := client.Activity.ListEventsPerformedByUser("u", true, nil)
190+
events, _, err := client.Activity.ListEventsPerformedByUser(context.Background(), "u", true, nil)
190191
if err != nil {
191192
t.Errorf("Events.ListPerformedByUser returned error: %v", err)
192193
}
@@ -198,7 +199,7 @@ func TestActivityService_ListEventsPerformedByUser_publicOnly(t *testing.T) {
198199
}
199200

200201
func TestActivityService_ListEventsPerformedByUser_invalidUser(t *testing.T) {
201-
_, _, err := client.Activity.ListEventsPerformedByUser("%", false, nil)
202+
_, _, err := client.Activity.ListEventsPerformedByUser(context.Background(), "%", false, nil)
202203
testURLParseError(t, err)
203204
}
204205

@@ -215,7 +216,7 @@ func TestActivityService_ListEventsReceivedByUser_all(t *testing.T) {
215216
})
216217

217218
opt := &ListOptions{Page: 2}
218-
events, _, err := client.Activity.ListEventsReceivedByUser("u", false, opt)
219+
events, _, err := client.Activity.ListEventsReceivedByUser(context.Background(), "u", false, opt)
219220
if err != nil {
220221
t.Errorf("Events.ListReceivedByUser returned error: %v", err)
221222
}
@@ -235,7 +236,7 @@ func TestActivityService_ListEventsReceivedByUser_publicOnly(t *testing.T) {
235236
fmt.Fprint(w, `[{"id":"1"},{"id":"2"}]`)
236237
})
237238

238-
events, _, err := client.Activity.ListEventsReceivedByUser("u", true, nil)
239+
events, _, err := client.Activity.ListEventsReceivedByUser(context.Background(), "u", true, nil)
239240
if err != nil {
240241
t.Errorf("Events.ListReceivedByUser returned error: %v", err)
241242
}
@@ -247,7 +248,7 @@ func TestActivityService_ListEventsReceivedByUser_publicOnly(t *testing.T) {
247248
}
248249

249250
func TestActivityService_ListEventsReceivedByUser_invalidUser(t *testing.T) {
250-
_, _, err := client.Activity.ListEventsReceivedByUser("%", false, nil)
251+
_, _, err := client.Activity.ListEventsReceivedByUser(context.Background(), "%", false, nil)
251252
testURLParseError(t, err)
252253
}
253254

@@ -264,7 +265,7 @@ func TestActivityService_ListUserEventsForOrganization(t *testing.T) {
264265
})
265266

266267
opt := &ListOptions{Page: 2}
267-
events, _, err := client.Activity.ListUserEventsForOrganization("o", "u", opt)
268+
events, _, err := client.Activity.ListUserEventsForOrganization(context.Background(), "o", "u", opt)
268269
if err != nil {
269270
t.Errorf("Activities.ListUserEventsForOrganization returned error: %v", err)
270271
}

0 commit comments

Comments
 (0)