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

Diff for: scm/driver/azure/git.go

+4
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"`

Diff for: scm/driver/bitbucket/git.go

+8
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"`

Diff for: scm/driver/bitbucket/git_test.go

+34
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+
}

Diff for: scm/driver/fake/git.go

+4
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+
}

Diff for: scm/driver/gitea/git.go

+8
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
//

Diff for: scm/driver/gitea/git_test.go

+36
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+
}

Diff for: scm/driver/github/git.go

+8
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"`

Diff for: scm/driver/github/git_test.go

+40
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+
}

Diff for: scm/driver/gitlab/git.go

+8
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
}

Diff for: scm/driver/gitlab/git_test.go

+40
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+
}

Diff for: scm/driver/gogs/git.go

+8
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, ref1, ref2 string
6969
return nil, nil, scm.ErrNotSupported
7070
}
7171

72+
func (s *gitService) GetDefaultBranch(ctx context.Context, repo string) (*scm.Reference, *scm.Response, error) {
73+
repository, res, err := s.client.Repositories.Find(ctx, repo)
74+
if err != nil {
75+
return nil, res, err
76+
}
77+
return s.FindBranch(ctx, repo, repository.Branch)
78+
}
79+
7280
//
7381
// native data structures
7482
//

Diff for: scm/driver/gogs/git_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,37 @@ func TestTagList(t *testing.T) {
150150
t.Errorf("Expect Not Supported error")
151151
}
152152
}
153+
154+
func TestGitGetDefaultBranch(t *testing.T) {
155+
defer gock.Off()
156+
157+
gock.New("https://try.gogs.io").
158+
Get("/api/v1/repos/gogits/gogs").
159+
Reply(200).
160+
Type("application/json").
161+
File("testdata/repo.json")
162+
163+
gock.New("https://try.gogs.io").
164+
Get("/api/v1/repos/gogits/gogs/branches/master").
165+
Reply(200).
166+
Type("application/json").
167+
File("testdata/branch.json")
168+
169+
client, _ := New("https://try.gogs.io")
170+
got, _, err := client.Git.GetDefaultBranch(context.Background(), "gogits/gogs")
171+
if err != nil {
172+
t.Error(err)
173+
}
174+
175+
want := new(scm.Reference)
176+
raw, _ := os.ReadFile("testdata/branch.json.golden")
177+
err = json.Unmarshal(raw, want)
178+
if err != nil {
179+
t.Error(err)
180+
}
181+
182+
if diff := cmp.Diff(want, got); diff != "" {
183+
t.Errorf("Unexpected Results")
184+
t.Log(diff)
185+
}
186+
}

Diff for: scm/driver/stash/git.go

+8
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ func (s *gitService) CompareCommits(ctx context.Context, repo, ref1, ref2 string
153153
return convertDiffstats(out), res, err
154154
}
155155

156+
func (s *gitService) GetDefaultBranch(ctx context.Context, repo string) (*scm.Reference, *scm.Response, error) {
157+
namespace, name := scm.Split(repo)
158+
branch := new(branch)
159+
pathBranch := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/branches/default", namespace, name)
160+
resp, err := s.client.do(ctx, "GET", pathBranch, nil, branch)
161+
return convertBranch(branch), resp, err
162+
}
163+
156164
type deleteRefInput struct {
157165
DryRun bool `json:"dryRun,omitempty"`
158166
EndPoint string `json:"endPoint,omitempty"`

Diff for: scm/driver/stash/git_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,31 @@ func TestGitCompareCommits(t *testing.T) {
274274
t.Log(diff)
275275
}
276276
}
277+
278+
func TestGitGetDefaultBranch(t *testing.T) {
279+
defer gock.Off()
280+
281+
gock.New("http://example.com:7990").
282+
Get("/rest/api/1.0/projects/PRJ/repos/my-repo/branches/default").
283+
Reply(200).
284+
Type("application/json").
285+
File("testdata/default_branch.json")
286+
287+
client, _ := New("http://example.com:7990")
288+
got, _, err := client.Git.GetDefaultBranch(context.Background(), "PRJ/my-repo")
289+
if err != nil {
290+
t.Error(err)
291+
}
292+
293+
want := &scm.Reference{}
294+
raw, _ := os.ReadFile("testdata/default_branch.json.golden")
295+
err = json.Unmarshal(raw, &want)
296+
if err != nil {
297+
t.Error(err)
298+
}
299+
300+
if diff := cmp.Diff(want, got); diff != "" {
301+
t.Errorf("Unexpected Results")
302+
t.Log(diff)
303+
}
304+
}

Diff for: scm/driver/stash/testdata/default_branch.json.golden

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"Name": "master",
3+
"Path": "refs/heads/master",
4+
"Sha": "8d51122def5632836d1cb1026e879069e10a1e13"
5+
}

Diff for: scm/git.go

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ type (
6969
// FindBranch finds a git branch by name.
7070
FindBranch(ctx context.Context, repo, name string) (*Reference, *Response, error)
7171

72+
// GetDefaultBranch finds default branch of the repo.
73+
GetDefaultBranch(ctx context.Context, repo string) (*Reference, *Response, error)
74+
7275
// FindCommit finds a git commit by ref.
7376
FindCommit(ctx context.Context, repo, ref string) (*Commit, *Response, error)
7477

0 commit comments

Comments
 (0)