Skip to content

Commit

Permalink
Add support for context package to all endpoints. (google#529)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dmitshur authored Feb 20, 2017
1 parent 273afc2 commit 8ed4662
Show file tree
Hide file tree
Showing 127 changed files with 1,497 additions and 1,298 deletions.
6 changes: 4 additions & 2 deletions examples/basicauth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package main

import (
"bufio"
"context"
"fmt"
"os"
"strings"
Expand All @@ -34,14 +35,15 @@ func main() {
}

client := github.NewClient(tp.Client())
user, _, err := client.Users.Get("")
ctx := context.Background()
user, _, err := client.Users.Get(ctx, "")

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

if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions github/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package github

import "context"

// ActivityService handles communication with the activity related
// methods of the GitHub API.
//
Expand Down Expand Up @@ -51,14 +53,14 @@ type Feeds struct {
//
// Note: Private feeds are only returned when authenticating via Basic Auth
// since current feed URIs use the older, non revocable auth tokens.
func (s *ActivityService) ListFeeds() (*Feeds, *Response, error) {
func (s *ActivityService) ListFeeds(ctx context.Context) (*Feeds, *Response, error) {
req, err := s.client.NewRequest("GET", "feeds", nil)
if err != nil {
return nil, nil, err
}

f := &Feeds{}
resp, err := s.client.Do(req, f)
resp, err := s.client.Do(ctx, req, f)
if err != nil {
return nil, resp, err
}
Expand Down
33 changes: 17 additions & 16 deletions github/activity_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package github

import (
"context"
"encoding/json"
"fmt"
"time"
Expand Down Expand Up @@ -97,7 +98,7 @@ func (e *Event) Payload() (payload interface{}) {
// ListEvents drinks from the firehose of all public events across GitHub.
//
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-public-events
func (s *ActivityService) ListEvents(opt *ListOptions) ([]*Event, *Response, error) {
func (s *ActivityService) ListEvents(ctx context.Context, opt *ListOptions) ([]*Event, *Response, error) {
u, err := addOptions("events", opt)
if err != nil {
return nil, nil, err
Expand All @@ -109,7 +110,7 @@ func (s *ActivityService) ListEvents(opt *ListOptions) ([]*Event, *Response, err
}

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

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

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

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

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

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

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

var events []*Event
resp, err := s.client.Do(req, &events)
resp, err := s.client.Do(ctx, req, &events)
if err != nil {
return nil, resp, err
}
Expand Down
33 changes: 17 additions & 16 deletions github/activity_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package github

import (
"context"
"encoding/json"
"fmt"
"net/http"
Expand All @@ -26,7 +27,7 @@ func TestActivityService_ListEvents(t *testing.T) {
})

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

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

func TestActivityService_ListRepositoryEvents_invalidOwner(t *testing.T) {
_, _, err := client.Activity.ListRepositoryEvents("%", "%", nil)
_, _, err := client.Activity.ListRepositoryEvents(context.Background(), "%", "%", nil)
testURLParseError(t, err)
}

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

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

func TestActivityService_ListIssueEventsForRepository_invalidOwner(t *testing.T) {
_, _, err := client.Activity.ListIssueEventsForRepository("%", "%", nil)
_, _, err := client.Activity.ListIssueEventsForRepository(context.Background(), "%", "%", nil)
testURLParseError(t, err)
}

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

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

func TestActivityService_ListEventsForRepoNetwork_invalidOwner(t *testing.T) {
_, _, err := client.Activity.ListEventsForRepoNetwork("%", "%", nil)
_, _, err := client.Activity.ListEventsForRepoNetwork(context.Background(), "%", "%", nil)
testURLParseError(t, err)
}

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

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

func TestActivityService_ListEventsForOrganization_invalidOrg(t *testing.T) {
_, _, err := client.Activity.ListEventsForOrganization("%", nil)
_, _, err := client.Activity.ListEventsForOrganization(context.Background(), "%", nil)
testURLParseError(t, err)
}

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

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

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

func TestActivityService_ListEventsPerformedByUser_invalidUser(t *testing.T) {
_, _, err := client.Activity.ListEventsPerformedByUser("%", false, nil)
_, _, err := client.Activity.ListEventsPerformedByUser(context.Background(), "%", false, nil)
testURLParseError(t, err)
}

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

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

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

func TestActivityService_ListEventsReceivedByUser_invalidUser(t *testing.T) {
_, _, err := client.Activity.ListEventsReceivedByUser("%", false, nil)
_, _, err := client.Activity.ListEventsReceivedByUser(context.Background(), "%", false, nil)
testURLParseError(t, err)
}

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

opt := &ListOptions{Page: 2}
events, _, err := client.Activity.ListUserEventsForOrganization("o", "u", opt)
events, _, err := client.Activity.ListUserEventsForOrganization(context.Background(), "o", "u", opt)
if err != nil {
t.Errorf("Activities.ListUserEventsForOrganization returned error: %v", err)
}
Expand Down
Loading

0 comments on commit 8ed4662

Please sign in to comment.