diff --git a/scm/driver/azure/git.go b/scm/driver/azure/git.go index 8be89cbb..4e8f2607 100644 --- a/scm/driver/azure/git.go +++ b/scm/driver/azure/git.go @@ -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"` diff --git a/scm/driver/bitbucket/git.go b/scm/driver/bitbucket/git.go index f4a8c9bf..c55432ed 100644 --- a/scm/driver/bitbucket/git.go +++ b/scm/driver/bitbucket/git.go @@ -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"` diff --git a/scm/driver/bitbucket/git_test.go b/scm/driver/bitbucket/git_test.go index 50215306..23c8de8d 100644 --- a/scm/driver/bitbucket/git_test.go +++ b/scm/driver/bitbucket/git_test.go @@ -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) + } +} diff --git a/scm/driver/fake/git.go b/scm/driver/fake/git.go index 703bce30..47e3bf4d 100644 --- a/scm/driver/fake/git.go +++ b/scm/driver/fake/git.go @@ -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 +} diff --git a/scm/driver/gitea/git.go b/scm/driver/gitea/git.go index 11095257..7187c0c2 100644 --- a/scm/driver/gitea/git.go +++ b/scm/driver/gitea/git.go @@ -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 // diff --git a/scm/driver/gitea/git_test.go b/scm/driver/gitea/git_test.go index 5ae24fd9..1bb216ea 100644 --- a/scm/driver/gitea/git_test.go +++ b/scm/driver/gitea/git_test.go @@ -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) + } +} diff --git a/scm/driver/github/git.go b/scm/driver/github/git.go index 61cc087d..b3dccfaa 100644 --- a/scm/driver/github/git.go +++ b/scm/driver/github/git.go @@ -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"` diff --git a/scm/driver/github/git_test.go b/scm/driver/github/git_test.go index 013b4610..f90ba647 100644 --- a/scm/driver/github/git_test.go +++ b/scm/driver/github/git_test.go @@ -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)) +} diff --git a/scm/driver/gitlab/git.go b/scm/driver/gitlab/git.go index 0ee90de9..4c7e069e 100644 --- a/scm/driver/gitlab/git.go +++ b/scm/driver/gitlab/git.go @@ -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"` } diff --git a/scm/driver/gitlab/git_test.go b/scm/driver/gitlab/git_test.go index 1f0ad50b..9109b89f 100644 --- a/scm/driver/gitlab/git_test.go +++ b/scm/driver/gitlab/git_test.go @@ -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)) +} diff --git a/scm/driver/gogs/git.go b/scm/driver/gogs/git.go index 3253e56b..2a821d9b 100644 --- a/scm/driver/gogs/git.go +++ b/scm/driver/gogs/git.go @@ -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 // diff --git a/scm/driver/gogs/git_test.go b/scm/driver/gogs/git_test.go index f1a48583..814680d4 100644 --- a/scm/driver/gogs/git_test.go +++ b/scm/driver/gogs/git_test.go @@ -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) + } +} diff --git a/scm/driver/stash/git.go b/scm/driver/stash/git.go index c311be90..ab8b050b 100644 --- a/scm/driver/stash/git.go +++ b/scm/driver/stash/git.go @@ -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"` diff --git a/scm/driver/stash/git_test.go b/scm/driver/stash/git_test.go index 32a3e978..47034f3a 100644 --- a/scm/driver/stash/git_test.go +++ b/scm/driver/stash/git_test.go @@ -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) + } +} diff --git a/scm/driver/stash/testdata/default_branch.json.golden b/scm/driver/stash/testdata/default_branch.json.golden new file mode 100644 index 00000000..6b0b7f70 --- /dev/null +++ b/scm/driver/stash/testdata/default_branch.json.golden @@ -0,0 +1,5 @@ +{ + "Name": "master", + "Path": "refs/heads/master", + "Sha": "8d51122def5632836d1cb1026e879069e10a1e13" +} \ No newline at end of file diff --git a/scm/git.go b/scm/git.go index bc6e53c2..c2af7328 100644 --- a/scm/git.go +++ b/scm/git.go @@ -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)