Skip to content

Commit 6f91ac9

Browse files
committed
feat:add GetDefaultBranch endpoint in git service
added GetDefaultBranch func in git service for all git provider except azure and their unit tests. Signed-off-by: Zaki Shaikh <[email protected]>
1 parent 560abea commit 6f91ac9

File tree

16 files changed

+276
-0
lines changed

16 files changed

+276
-0
lines changed

scm/driver/azure/git.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, source, target st
131131
return convertChangeList(changes), res, err
132132
}
133133

134+
func (s *gitService) GetDefaultBranch(ctx context.Context, repo string) (*scm.Reference, *scm.Response, error) {
135+
return nil, nil, scm.ErrNotSupported
136+
}
137+
134138
type gitRef struct {
135139
Name string `json:"name"`
136140
OldObjectID string `json:"oldObjectId"`

scm/driver/bitbucket/git.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, ref1, ref2 string
132132
return convertDiffstats(out), res, err
133133
}
134134

135+
func (s *gitService) GetDefaultBranch(ctx context.Context, repo string) (*scm.Reference, *scm.Response, error) {
136+
repository, res, err := s.client.Repositories.Find(ctx, repo)
137+
if err != nil {
138+
return nil, res, err
139+
}
140+
return s.FindBranch(ctx, repo, repository.Branch)
141+
}
142+
135143
type branch struct {
136144
Type string `json:"type"`
137145
Name string `json:"name"`

scm/driver/bitbucket/git_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,37 @@ func TestGitCompareCommits(t *testing.T) {
255255
t.Log(diff)
256256
}
257257
}
258+
259+
func TestGitGetDefaultBranch(t *testing.T) {
260+
defer gock.Off()
261+
262+
gock.New("https://api.bitbucket.org").
263+
Get("/2.0/repositories/atlassian/stash-example-plugin").
264+
Reply(200).
265+
Type("application/json").
266+
File("testdata/repo.json")
267+
268+
gock.New("https://api.bitbucket.org").
269+
Get("/2.0/repositories/atlassian/stash-example-plugin/refs/branches/master").
270+
Reply(200).
271+
Type("application/json").
272+
File("testdata/branch.json")
273+
274+
client, _ := New("https://api.bitbucket.org")
275+
got, _, err := client.Git.GetDefaultBranch(context.Background(), "atlassian/stash-example-plugin")
276+
if err != nil {
277+
t.Error(err)
278+
}
279+
280+
want := new(scm.Reference)
281+
raw, _ := os.ReadFile("testdata/branch.json.golden")
282+
err = json.Unmarshal(raw, &want)
283+
if err != nil {
284+
t.Error(err)
285+
}
286+
287+
if diff := cmp.Diff(want, got); diff != "" {
288+
t.Errorf("Unexpected Results")
289+
t.Log(diff)
290+
}
291+
}

scm/driver/fake/git.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,7 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, ref1, ref2 string
6666
func (s *gitService) ListTags(ctx context.Context, repo string, opts *scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
6767
panic("implement me")
6868
}
69+
70+
func (s *gitService) GetDefaultBranch(ctx context.Context, repo string) (*scm.Reference, *scm.Response, error) {
71+
return nil, nil, scm.ErrNotSupported
72+
}

scm/driver/gitea/git.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, ref1, ref2 string
102102
return nil, nil, scm.ErrNotSupported
103103
}
104104

105+
func (s *gitService) GetDefaultBranch(ctx context.Context, repo string) (*scm.Reference, *scm.Response, error) {
106+
repository, res, err := s.client.Repositories.Find(ctx, repo)
107+
if err != nil {
108+
return nil, res, err
109+
}
110+
return s.FindBranch(ctx, repo, repository.Branch)
111+
}
112+
105113
//
106114
// native data structures
107115
//

scm/driver/gitea/git_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,39 @@ func TestTagList(t *testing.T) {
210210

211211
t.Run("Page", testPage(res))
212212
}
213+
214+
func TestGitGetDefaultBranch(t *testing.T) {
215+
defer gock.Off()
216+
217+
mockServerVersion()
218+
219+
gock.New("https://demo.gitea.com").
220+
Get("/api/v1/repos/go-gitea/gitea").
221+
Reply(200).
222+
Type("application/json").
223+
File("testdata/repo.json")
224+
225+
gock.New("https://demo.gitea.com").
226+
Get("/api/v1/repos/go-gitea/gitea/branches/master").
227+
Reply(200).
228+
Type("application/json").
229+
File("testdata/branch.json")
230+
231+
client, _ := New("https://demo.gitea.com")
232+
got, _, err := client.Git.GetDefaultBranch(context.Background(), "go-gitea/gitea")
233+
if err != nil {
234+
t.Error(err)
235+
}
236+
237+
want := new(scm.Reference)
238+
raw, _ := os.ReadFile("testdata/branch.json.golden")
239+
err = json.Unmarshal(raw, want)
240+
if err != nil {
241+
t.Error(err)
242+
}
243+
244+
if diff := cmp.Diff(want, got); diff != "" {
245+
t.Errorf("Unexpected Results")
246+
t.Log(diff)
247+
}
248+
}

scm/driver/github/git.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, ref1, ref2 string
115115
return convertChangeList(out.Files), res, err
116116
}
117117

118+
func (s *gitService) GetDefaultBranch(ctx context.Context, repo string) (*scm.Reference, *scm.Response, error) {
119+
repository, res, err := s.client.Repositories.Find(ctx, repo)
120+
if err != nil {
121+
return nil, res, err
122+
}
123+
return s.FindBranch(ctx, repo, repository.Branch)
124+
}
125+
118126
type branch struct {
119127
Name string `json:"name"`
120128
Commit commit `json:"commit"`

scm/driver/github/git_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,43 @@ func TestGitCreateRef(t *testing.T) {
301301
t.Run("Request", testRequest(res))
302302
t.Run("Rate", testRate(res))
303303
}
304+
305+
func TestGitGetDefaultBranch(t *testing.T) {
306+
defer gock.Off()
307+
308+
gock.New("https://api.github.com").
309+
Get("/repos/octocat/hello-world").
310+
Reply(200).
311+
Type("application/json").
312+
SetHeaders(mockHeaders).
313+
File("testdata/repo.json")
314+
315+
gock.New("https://api.github.com").
316+
Get("/repos/octocat/hello-world/branches/master").
317+
Reply(200).
318+
Type("application/json").
319+
SetHeaders(mockHeaders).
320+
File("testdata/branch.json")
321+
322+
client := NewDefault()
323+
got, res, err := client.Git.GetDefaultBranch(context.Background(), "octocat/hello-world")
324+
if err != nil {
325+
t.Error(err)
326+
return
327+
}
328+
329+
want := new(scm.Reference)
330+
raw, _ := os.ReadFile("testdata/branch.json.golden")
331+
err = json.Unmarshal(raw, &want)
332+
if err != nil {
333+
t.Error(err)
334+
}
335+
336+
if diff := cmp.Diff(want, got); diff != "" {
337+
t.Errorf("Unexpected Results")
338+
t.Log(diff)
339+
}
340+
341+
t.Run("Request", testRequest(res))
342+
t.Run("Rate", testRate(res))
343+
}

scm/driver/gitlab/git.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, ref1, ref2 string
123123
return convertChangeList(out.Diffs), res, err
124124
}
125125

126+
func (s *gitService) GetDefaultBranch(ctx context.Context, repo string) (*scm.Reference, *scm.Response, error) {
127+
repository, res, err := s.client.Repositories.Find(ctx, repo)
128+
if err != nil {
129+
return nil, res, err
130+
}
131+
return s.FindBranch(ctx, repo, repository.Branch)
132+
}
133+
126134
type compare struct {
127135
Diffs []*change `json:"diffs"`
128136
}

scm/driver/gitlab/git_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,43 @@ func TestGitCreateRef(t *testing.T) {
333333
t.Run("Request", testRequest(res))
334334
t.Run("Rate", testRate(res))
335335
}
336+
337+
func TestGitGetDefaultBranch(t *testing.T) {
338+
defer gock.Off()
339+
340+
gock.New("https://gitlab.com").
341+
Get("/api/v4/projects/diaspora/diaspora").
342+
Reply(200).
343+
Type("application/json").
344+
SetHeaders(mockHeaders).
345+
File("testdata/repo.json")
346+
347+
gock.New("https://gitlab.com").
348+
Get("/api/v4/projects/diaspora/diaspora/repository/branches/master").
349+
Reply(200).
350+
Type("application/json").
351+
SetHeaders(mockHeaders).
352+
File("testdata/branch.json")
353+
354+
client := NewDefault()
355+
got, res, err := client.Git.GetDefaultBranch(context.Background(), "diaspora/diaspora")
356+
if err != nil {
357+
t.Error(err)
358+
return
359+
}
360+
361+
want := new(scm.Reference)
362+
raw, _ := os.ReadFile("testdata/branch.json.golden")
363+
err = json.Unmarshal(raw, &want)
364+
if err != nil {
365+
t.Error(err)
366+
}
367+
368+
if diff := cmp.Diff(want, got); diff != "" {
369+
t.Errorf("Unexpected Results")
370+
t.Log(diff)
371+
}
372+
373+
t.Run("Request", testRequest(res))
374+
t.Run("Rate", testRate(res))
375+
}

0 commit comments

Comments
 (0)