Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions github/actions_artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ func (s *ActionsService) getDownloadArtifactFromURL(ctx context.Context, u strin
var resp *http.Response
// Use http.DefaultTransport if no custom Transport is configured
req = withContext(ctx, req)
if s.client.client.Transport == nil {
if s.client.Client.Transport == nil {
resp, err = http.DefaultTransport.RoundTrip(req)
} else {
resp, err = s.client.client.Transport.RoundTrip(req)
resp, err = s.client.Client.Transport.RoundTrip(req)
}
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion github/actions_artifacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ func TestActionsSerivice_DownloadArtifact(t *testing.T) {
})

// Add custom round tripper
client.client.Transport = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
client.Client.Transport = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
return nil, errors.New("failed to download artifact")
})
testBadOptions(t, methodName, func() (err error) {
Expand Down
4 changes: 2 additions & 2 deletions github/actions_workflow_jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ func (s *ActionsService) getWorkflowLogsFromURL(ctx context.Context, u string, f
var resp *http.Response
// Use http.DefaultTransport if no custom Transport is configured
req = withContext(ctx, req)
if s.client.client.Transport == nil {
if s.client.Client.Transport == nil {
resp, err = http.DefaultTransport.RoundTrip(req)
} else {
resp, err = s.client.client.Transport.RoundTrip(req)
resp, err = s.client.Client.Transport.RoundTrip(req)
}
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion github/actions_workflow_jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestActionsService_GetWorkflowJobLogs(t *testing.T) {
})

// Add custom round tripper
client.client.Transport = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
client.Client.Transport = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
return nil, errors.New("failed to get workflow logs")
})
testBadOptions(t, methodName, func() (err error) {
Expand Down
6 changes: 3 additions & 3 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ var errNonNilContext = errors.New("context must be non-nil")
// A Client manages communication with the GitHub API.
type Client struct {
clientMu sync.Mutex // clientMu protects the client during calls that modify the CheckRedirect func.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will exposing client directly lead to problems with this mutex? I think two different github.Client objects would each use separate locks to protect the same *http.Client object, leading to race conditions.

Maybe instead there could be a new Client() or HTTPClient() function that returns a shallow copy of the internal client field? That would enable reuse of the transport and timeout while allowing each instance to safely update the redirect function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... that's an interesting question...

The idea was to prevent multiple calls that check for redirects to avoid swapping out the client at the same time.

But maybe a read-only Client() method as you recommend would be a better solution anyway.

client *http.Client // HTTP client used to communicate with the API.
Client *http.Client // HTTP client used to communicate with the API.

// Base URL for API requests. Defaults to the public GitHub API, but can be
// set to a domain endpoint to use with GitHub Enterprise. BaseURL should
Expand Down Expand Up @@ -273,7 +273,7 @@ func NewClient(httpClient *http.Client) *Client {
baseURL, _ := url.Parse(defaultBaseURL)
uploadURL, _ := url.Parse(uploadBaseURL)

c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent, UploadURL: uploadURL}
c := &Client{Client: httpClient, BaseURL: baseURL, UserAgent: userAgent, UploadURL: uploadURL}
c.common.client = c
c.Actions = (*ActionsService)(&c.common)
c.Activity = (*ActivityService)(&c.common)
Expand Down Expand Up @@ -575,7 +575,7 @@ func (c *Client) BareDo(ctx context.Context, req *http.Request) (*Response, erro
}
}

resp, err := c.client.Do(req)
resp, err := c.Client.Do(req)
if err != nil {
// If we got an error, and the context has been canceled,
// the context's error is probably more useful.
Expand Down
2 changes: 1 addition & 1 deletion github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func TestNewClient(t *testing.T) {
}

c2 := NewClient(nil)
if c.client == c2.client {
if c.Client == c2.Client {
t.Error("NewClient returned same http.Clients, but they should differ")
}
}
Expand Down
6 changes: 3 additions & 3 deletions github/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ func (s *MigrationService) MigrationArchiveURL(ctx context.Context, org string,

// Disable the redirect mechanism because AWS fails if the GitHub auth token is provided.
var loc string
saveRedirect := s.client.client.CheckRedirect
s.client.client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
saveRedirect := s.client.Client.CheckRedirect
s.client.Client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
loc = req.URL.String()
return errors.New("disable redirect")
}
defer func() { s.client.client.CheckRedirect = saveRedirect }()
defer func() { s.client.Client.CheckRedirect = saveRedirect }()

_, err = s.client.Do(ctx, req, nil) // expect error from disable redirect
if err == nil {
Expand Down
6 changes: 3 additions & 3 deletions github/migrations_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ func (s *MigrationService) UserMigrationArchiveURL(ctx context.Context, id int64
m := &UserMigration{}

var loc string
originalRedirect := s.client.client.CheckRedirect
s.client.client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
originalRedirect := s.client.Client.CheckRedirect
s.client.Client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
loc = req.URL.String()
return http.ErrUseLastResponse
}
defer func() {
s.client.client.CheckRedirect = originalRedirect
s.client.Client.CheckRedirect = originalRedirect
}()
resp, err := s.client.Do(ctx, req, m)
if err == nil {
Expand Down
4 changes: 2 additions & 2 deletions github/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -972,10 +972,10 @@ func (s *RepositoriesService) getBranchFromURL(ctx context.Context, u string, fo
var resp *http.Response
// Use http.DefaultTransport if no custom Transport is configured
req = withContext(ctx, req)
if s.client.client.Transport == nil {
if s.client.Client.Transport == nil {
resp, err = http.DefaultTransport.RoundTrip(req)
} else {
resp, err = s.client.client.Transport.RoundTrip(req)
resp, err = s.client.Client.Transport.RoundTrip(req)
}
if err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions github/repos_contents.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (s *RepositoriesService) DownloadContents(ctx context.Context, owner, repo,
if contents.DownloadURL == nil || *contents.DownloadURL == "" {
return nil, resp, fmt.Errorf("No download link found for %s", filepath)
}
dlResp, err := s.client.client.Get(*contents.DownloadURL)
dlResp, err := s.client.Client.Get(*contents.DownloadURL)
if err != nil {
return nil, &Response{Response: dlResp}, err
}
Expand Down Expand Up @@ -164,7 +164,7 @@ func (s *RepositoriesService) DownloadContentsWithMeta(ctx context.Context, owne
if contents.DownloadURL == nil || *contents.DownloadURL == "" {
return nil, contents, resp, fmt.Errorf("No download link found for %s", filepath)
}
dlResp, err := s.client.client.Get(*contents.DownloadURL)
dlResp, err := s.client.Client.Get(*contents.DownloadURL)
if err != nil {
return nil, contents, &Response{Response: dlResp}, err
}
Expand Down Expand Up @@ -304,10 +304,10 @@ func (s *RepositoriesService) getArchiveLinkFromURL(ctx context.Context, u strin
var resp *http.Response
// Use http.DefaultTransport if no custom Transport is configured
req = withContext(ctx, req)
if s.client.client.Transport == nil {
if s.client.Client.Transport == nil {
resp, err = http.DefaultTransport.RoundTrip(req)
} else {
resp, err = s.client.client.Transport.RoundTrip(req)
resp, err = s.client.Client.Transport.RoundTrip(req)
}
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion github/repos_contents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ func TestRepositoriesService_GetArchiveLink(t *testing.T) {
})

// Add custom round tripper
client.client.Transport = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
client.Client.Transport = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
return nil, errors.New("failed to get archive link")
})
testBadOptions(t, methodName, func() (err error) {
Expand Down
8 changes: 4 additions & 4 deletions github/repos_releases.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,15 @@ func (s *RepositoriesService) DownloadReleaseAsset(ctx context.Context, owner, r
defer s.client.clientMu.Unlock()

var loc string
saveRedirect := s.client.client.CheckRedirect
s.client.client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
saveRedirect := s.client.Client.CheckRedirect
s.client.Client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
loc = req.URL.String()
return errors.New("disable redirect")
}
defer func() { s.client.client.CheckRedirect = saveRedirect }()
defer func() { s.client.Client.CheckRedirect = saveRedirect }()

req = withContext(ctx, req)
resp, err := s.client.client.Do(req)
resp, err := s.client.Client.Do(req)
if err != nil {
if !strings.Contains(err.Error(), "disable redirect") {
return nil, "", err
Expand Down
2 changes: 1 addition & 1 deletion github/repos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ func TestRepositoriesService_GetBranch_notFound(t *testing.T) {
}

// Add custom round tripper
client.client.Transport = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
client.Client.Transport = roundTripperFunc(func(r *http.Request) (*http.Response, error) {
return nil, errors.New("failed to get branch")
})

Expand Down