Skip to content

Commit b951a01

Browse files
committed
Add GetDefaultBranch endpoint in git service
added GetDefaultBranch endpoint in git service for Bitbucket server and its unit tests. Signed-off-by: Zaki Shaikh <[email protected]>
1 parent d8fc906 commit b951a01

File tree

11 files changed

+72
-0
lines changed

11 files changed

+72
-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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ 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+
return nil, nil, scm.ErrNotSupported
137+
}
138+
135139
type branch struct {
136140
Type string `json:"type"`
137141
Name string `json:"name"`

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ 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+
return nil, nil, scm.ErrNotSupported
107+
}
108+
105109
//
106110
// native data structures
107111
//

scm/driver/github/git.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ 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+
return nil, nil, scm.ErrNotSupported
120+
}
121+
118122
type branch struct {
119123
Name string `json:"name"`
120124
Commit commit `json:"commit"`

scm/driver/gitlab/git.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ 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+
return nil, nil, scm.ErrNotSupported
128+
}
129+
126130
type compare struct {
127131
Diffs []*change `json:"diffs"`
128132
}

scm/driver/gogs/git.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ 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+
return nil, nil, scm.ErrNotSupported
74+
}
75+
7276
//
7377
// native data structures
7478
//

scm/driver/stash/git.go

Lines changed: 8 additions & 0 deletions
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"`

scm/driver/stash/git_test.go

Lines changed: 28 additions & 0 deletions
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+
}
Lines changed: 5 additions & 0 deletions
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+
}

scm/git.go

Lines changed: 3 additions & 0 deletions
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)