Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify GetBranch to handle redirects #1901

Merged
merged 4 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
39 changes: 34 additions & 5 deletions github/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
)

Expand Down Expand Up @@ -933,20 +934,48 @@ func (s *RepositoriesService) ListBranches(ctx context.Context, owner string, re
// GetBranch gets the specified branch for a repository.
//
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-a-branch
func (s *RepositoriesService) GetBranch(ctx context.Context, owner, repo, branch string) (*Branch, *Response, error) {
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)
req, err := s.client.NewRequest("GET", u, nil)

resp, err := s.getBranchFromURL(ctx, u, followRedirects)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status)
gmlewis marked this conversation as resolved.
Show resolved Hide resolved
}

b := new(Branch)
resp, err := s.client.Do(ctx, req, b)
err = json.NewDecoder(resp.Body).Decode(b)
return b, newResponse(resp), err
}

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, resp, err
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)
gmlewis marked this conversation as resolved.
Show resolved Hide resolved
}
if err != nil {
return nil, err
gmlewis marked this conversation as resolved.
Show resolved Hide resolved
}

return b, resp, nil
// If redirect response is returned, follow it
if followRedirects && resp.StatusCode == http.StatusMovedPermanently {
u = resp.Header.Get("Location")
gmlewis marked this conversation as resolved.
Show resolved Hide resolved
resp, err = s.getBranchFromURL(ctx, u, false)
}
return resp, err
}

// GetBranchProtection gets the protection of a given branch.
Expand Down
12 changes: 2 additions & 10 deletions github/repos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ func TestRepositoriesService_GetBranch(t *testing.T) {
})

ctx := context.Background()
branch, _, err := client.Repositories.GetBranch(ctx, "o", "r", "b")
branch, _, err := client.Repositories.GetBranch(ctx, "o", "r", "b", false)
if err != nil {
t.Errorf("Repositories.GetBranch returned error: %v", err)
}
Expand All @@ -908,17 +908,9 @@ func TestRepositoriesService_GetBranch(t *testing.T) {

const methodName = "GetBranch"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Repositories.GetBranch(ctx, "\n", "\n", "\n")
_, _, err = client.Repositories.GetBranch(ctx, "\n", "\n", "\n", false)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.GetBranch(ctx, "o", "r", "b")
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestRepositoriesService_GetBranchProtection(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions test/integration/repos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func TestRepositories_BranchesTags(t *testing.T) {
t.Fatalf("Repositories.ListBranches('git', 'git') returned no branches")
}

_, _, err = client.Repositories.GetBranch(context.Background(), "git", "git", *branches[0].Name)
_, _, err = client.Repositories.GetBranch(context.Background(), "git", "git", *branches[0].Name, false)
if err != nil {
t.Fatalf("Repositories.GetBranch() returned error: %v", err)
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func TestRepositories_EditBranches(t *testing.T) {
t.Fatalf("createRandomTestRepository returned error: %v", err)
}

branch, _, err := client.Repositories.GetBranch(context.Background(), *repo.Owner.Login, *repo.Name, "master")
branch, _, err := client.Repositories.GetBranch(context.Background(), *repo.Owner.Login, *repo.Name, "master", false)
if err != nil {
t.Fatalf("Repositories.GetBranch() returned error: %v", err)
}
Expand Down