Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Remove more code duplication
  • Loading branch information
gmlewis committed Mar 24, 2022
commit a3a5a8a94abc6139f2d4f3fa6dd7029938a479d4
2 changes: 1 addition & 1 deletion github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -1302,10 +1302,10 @@ func (c *Client) getDownloadArtifactFromURL(ctx context.Context, u string, follo
if err != nil {
return nil, err
}
resp.Body.Close()

// If redirect response is returned, follow it
if followRedirects && resp.StatusCode == http.StatusMovedPermanently {
resp.Body.Close()
u = resp.Header.Get("Location")
resp, err = c.getDownloadArtifactFromURL(ctx, u, false)
if err != nil {
Expand Down
34 changes: 2 additions & 32 deletions github/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,8 @@ func (s *RepositoriesService) ListBranches(ctx context.Context, owner string, re
func (s *RepositoriesService) GetBranch(ctx context.Context, owner, repo, branch string, followRedirects bool) (*Branch, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/branches/%v", owner, repo, branch)

resp, err := s.getBranchFromURL(ctx, u, followRedirects)
// the DownloadArtifact in this case is the branch.
resp, err := s.client.getDownloadArtifactFromURL(ctx, u, followRedirects)
if err != nil {
return nil, nil, err
}
Expand All @@ -1083,37 +1084,6 @@ func (s *RepositoriesService) GetBranch(ctx context.Context, owner, repo, branch
return b, newResponse(resp), nil
}

func (s *RepositoriesService) getBranchFromURL(ctx context.Context, u string, followRedirects bool) (*http.Response, error) {
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}

var resp *http.Response
// Use http.DefaultTransport if no custom Transport is configured
req = withContext(ctx, req)
if s.client.client.Transport == nil {
resp, err = http.DefaultTransport.RoundTrip(req)
} else {
resp, err = s.client.client.Transport.RoundTrip(req)
}
if err != nil {
return nil, err
}

// If redirect response is returned, follow it
if followRedirects && resp.StatusCode == http.StatusMovedPermanently {
resp.Body.Close()
u = resp.Header.Get("Location")
resp, err = s.getBranchFromURL(ctx, u, false)
if err != nil {
return nil, err
}
}

return resp, nil
}

// renameBranchRequest represents a request to rename a branch.
type renameBranchRequest struct {
NewName string `json:"new_name"`
Expand Down