Skip to content

Add GetDefaultBranch Endpoint in Git Service #475

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

Merged
Merged
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: 4 additions & 0 deletions scm/driver/azure/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, source, target st
return convertChangeList(changes), res, err
}

func (s *gitService) GetDefaultBranch(ctx context.Context, repo string) (*scm.Reference, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}

type gitRef struct {
Name string `json:"name"`
OldObjectID string `json:"oldObjectId"`
Expand Down
8 changes: 8 additions & 0 deletions scm/driver/bitbucket/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, ref1, ref2 string
return convertDiffstats(out), res, err
}

func (s *gitService) GetDefaultBranch(ctx context.Context, repo string) (*scm.Reference, *scm.Response, error) {
repository, res, err := s.client.Repositories.Find(ctx, repo)
if err != nil {
return nil, res, err
}
return s.FindBranch(ctx, repo, repository.Branch)
}

type branch struct {
Type string `json:"type"`
Name string `json:"name"`
Expand Down
34 changes: 34 additions & 0 deletions scm/driver/bitbucket/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,37 @@ func TestGitCompareCommits(t *testing.T) {
t.Log(diff)
}
}

func TestGitGetDefaultBranch(t *testing.T) {
defer gock.Off()

gock.New("https://api.bitbucket.org").
Get("/2.0/repositories/atlassian/stash-example-plugin").
Reply(200).
Type("application/json").
File("testdata/repo.json")

gock.New("https://api.bitbucket.org").
Get("/2.0/repositories/atlassian/stash-example-plugin/refs/branches/master").
Reply(200).
Type("application/json").
File("testdata/branch.json")

client, _ := New("https://api.bitbucket.org")
got, _, err := client.Git.GetDefaultBranch(context.Background(), "atlassian/stash-example-plugin")
if err != nil {
t.Error(err)
}

want := new(scm.Reference)
raw, _ := os.ReadFile("testdata/branch.json.golden")
err = json.Unmarshal(raw, &want)
if err != nil {
t.Error(err)
}

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
4 changes: 4 additions & 0 deletions scm/driver/fake/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, ref1, ref2 string
func (s *gitService) ListTags(ctx context.Context, repo string, opts *scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
panic("implement me")
}

func (s *gitService) GetDefaultBranch(ctx context.Context, repo string) (*scm.Reference, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
8 changes: 8 additions & 0 deletions scm/driver/gitea/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, ref1, ref2 string
return nil, nil, scm.ErrNotSupported
}

func (s *gitService) GetDefaultBranch(ctx context.Context, repo string) (*scm.Reference, *scm.Response, error) {
repository, res, err := s.client.Repositories.Find(ctx, repo)
if err != nil {
return nil, res, err
}
return s.FindBranch(ctx, repo, repository.Branch)
}

//
// native data structures
//
Expand Down
36 changes: 36 additions & 0 deletions scm/driver/gitea/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,39 @@ func TestTagList(t *testing.T) {

t.Run("Page", testPage(res))
}

func TestGitGetDefaultBranch(t *testing.T) {
defer gock.Off()

mockServerVersion()

gock.New("https://demo.gitea.com").
Get("/api/v1/repos/go-gitea/gitea").
Reply(200).
Type("application/json").
File("testdata/repo.json")

gock.New("https://demo.gitea.com").
Get("/api/v1/repos/go-gitea/gitea/branches/master").
Reply(200).
Type("application/json").
File("testdata/branch.json")

client, _ := New("https://demo.gitea.com")
got, _, err := client.Git.GetDefaultBranch(context.Background(), "go-gitea/gitea")
if err != nil {
t.Error(err)
}

want := new(scm.Reference)
raw, _ := os.ReadFile("testdata/branch.json.golden")
err = json.Unmarshal(raw, want)
if err != nil {
t.Error(err)
}

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
8 changes: 8 additions & 0 deletions scm/driver/github/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, ref1, ref2 string
return convertChangeList(out.Files), res, err
}

func (s *gitService) GetDefaultBranch(ctx context.Context, repo string) (*scm.Reference, *scm.Response, error) {
repository, res, err := s.client.Repositories.Find(ctx, repo)
if err != nil {
return nil, res, err
}
return s.FindBranch(ctx, repo, repository.Branch)
}

type branch struct {
Name string `json:"name"`
Commit commit `json:"commit"`
Expand Down
40 changes: 40 additions & 0 deletions scm/driver/github/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,43 @@ func TestGitCreateRef(t *testing.T) {
t.Run("Request", testRequest(res))
t.Run("Rate", testRate(res))
}

func TestGitGetDefaultBranch(t *testing.T) {
defer gock.Off()

gock.New("https://api.github.com").
Get("/repos/octocat/hello-world").
Reply(200).
Type("application/json").
SetHeaders(mockHeaders).
File("testdata/repo.json")

gock.New("https://api.github.com").
Get("/repos/octocat/hello-world/branches/master").
Reply(200).
Type("application/json").
SetHeaders(mockHeaders).
File("testdata/branch.json")

client := NewDefault()
got, res, err := client.Git.GetDefaultBranch(context.Background(), "octocat/hello-world")
if err != nil {
t.Error(err)
return
}

want := new(scm.Reference)
raw, _ := os.ReadFile("testdata/branch.json.golden")
err = json.Unmarshal(raw, &want)
if err != nil {
t.Error(err)
}

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}

t.Run("Request", testRequest(res))
t.Run("Rate", testRate(res))
}
8 changes: 8 additions & 0 deletions scm/driver/gitlab/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, ref1, ref2 string
return convertChangeList(out.Diffs), res, err
}

func (s *gitService) GetDefaultBranch(ctx context.Context, repo string) (*scm.Reference, *scm.Response, error) {
repository, res, err := s.client.Repositories.Find(ctx, repo)
if err != nil {
return nil, res, err
}
return s.FindBranch(ctx, repo, repository.Branch)
}

type compare struct {
Diffs []*change `json:"diffs"`
}
Expand Down
40 changes: 40 additions & 0 deletions scm/driver/gitlab/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,43 @@ func TestGitCreateRef(t *testing.T) {
t.Run("Request", testRequest(res))
t.Run("Rate", testRate(res))
}

func TestGitGetDefaultBranch(t *testing.T) {
defer gock.Off()

gock.New("https://gitlab.com").
Get("/api/v4/projects/diaspora/diaspora").
Reply(200).
Type("application/json").
SetHeaders(mockHeaders).
File("testdata/repo.json")

gock.New("https://gitlab.com").
Get("/api/v4/projects/diaspora/diaspora/repository/branches/master").
Reply(200).
Type("application/json").
SetHeaders(mockHeaders).
File("testdata/branch.json")

client := NewDefault()
got, res, err := client.Git.GetDefaultBranch(context.Background(), "diaspora/diaspora")
if err != nil {
t.Error(err)
return
}

want := new(scm.Reference)
raw, _ := os.ReadFile("testdata/branch.json.golden")
err = json.Unmarshal(raw, &want)
if err != nil {
t.Error(err)
}

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}

t.Run("Request", testRequest(res))
t.Run("Rate", testRate(res))
}
8 changes: 8 additions & 0 deletions scm/driver/gogs/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, ref1, ref2 string
return nil, nil, scm.ErrNotSupported
}

func (s *gitService) GetDefaultBranch(ctx context.Context, repo string) (*scm.Reference, *scm.Response, error) {
repository, res, err := s.client.Repositories.Find(ctx, repo)
if err != nil {
return nil, res, err
}
return s.FindBranch(ctx, repo, repository.Branch)
}

//
// native data structures
//
Expand Down
34 changes: 34 additions & 0 deletions scm/driver/gogs/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,37 @@ func TestTagList(t *testing.T) {
t.Errorf("Expect Not Supported error")
}
}

func TestGitGetDefaultBranch(t *testing.T) {
defer gock.Off()

gock.New("https://try.gogs.io").
Get("/api/v1/repos/gogits/gogs").
Reply(200).
Type("application/json").
File("testdata/repo.json")

gock.New("https://try.gogs.io").
Get("/api/v1/repos/gogits/gogs/branches/master").
Reply(200).
Type("application/json").
File("testdata/branch.json")

client, _ := New("https://try.gogs.io")
got, _, err := client.Git.GetDefaultBranch(context.Background(), "gogits/gogs")
if err != nil {
t.Error(err)
}

want := new(scm.Reference)
raw, _ := os.ReadFile("testdata/branch.json.golden")
err = json.Unmarshal(raw, want)
if err != nil {
t.Error(err)
}

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
8 changes: 8 additions & 0 deletions scm/driver/stash/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, ref1, ref2 string
return convertDiffstats(out), res, err
}

func (s *gitService) GetDefaultBranch(ctx context.Context, repo string) (*scm.Reference, *scm.Response, error) {
namespace, name := scm.Split(repo)
branch := new(branch)
pathBranch := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/branches/default", namespace, name)
resp, err := s.client.do(ctx, "GET", pathBranch, nil, branch)
return convertBranch(branch), resp, err
}

type deleteRefInput struct {
DryRun bool `json:"dryRun,omitempty"`
EndPoint string `json:"endPoint,omitempty"`
Expand Down
28 changes: 28 additions & 0 deletions scm/driver/stash/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,31 @@ func TestGitCompareCommits(t *testing.T) {
t.Log(diff)
}
}

func TestGitGetDefaultBranch(t *testing.T) {
defer gock.Off()

gock.New("http://example.com:7990").
Get("/rest/api/1.0/projects/PRJ/repos/my-repo/branches/default").
Reply(200).
Type("application/json").
File("testdata/default_branch.json")

client, _ := New("http://example.com:7990")
got, _, err := client.Git.GetDefaultBranch(context.Background(), "PRJ/my-repo")
if err != nil {
t.Error(err)
}

want := &scm.Reference{}
raw, _ := os.ReadFile("testdata/default_branch.json.golden")
err = json.Unmarshal(raw, &want)
if err != nil {
t.Error(err)
}

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Unexpected Results")
t.Log(diff)
}
}
5 changes: 5 additions & 0 deletions scm/driver/stash/testdata/default_branch.json.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Name": "master",
"Path": "refs/heads/master",
"Sha": "8d51122def5632836d1cb1026e879069e10a1e13"
}
3 changes: 3 additions & 0 deletions scm/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ type (
// FindBranch finds a git branch by name.
FindBranch(ctx context.Context, repo, name string) (*Reference, *Response, error)

// GetDefaultBranch finds default branch of the repo.
GetDefaultBranch(ctx context.Context, repo string) (*Reference, *Response, error)

// FindCommit finds a git commit by ref.
FindCommit(ctx context.Context, repo, ref string) (*Commit, *Response, error)

Expand Down