Skip to content

Commit 36e45c4

Browse files
authored
fix(vcs): add get default branch for each vcs manager (#5921)
1 parent cbf6853 commit 36e45c4

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

engine/vcs/bitbucketcloud/client_branch.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ func (client *bitbucketcloudClient) Branches(ctx context.Context, fullname strin
7272

7373
// Branch returns only detail of a branch
7474
func (client *bitbucketcloudClient) Branch(ctx context.Context, fullname string, filters sdk.VCSBranchFilters) (*sdk.VCSBranch, error) {
75+
if filters.Default {
76+
repo, err := client.repoByFullname(ctx, fullname)
77+
if err != nil {
78+
return nil, err
79+
}
80+
filters.BranchName = repo.Mainbranch.Name
81+
}
82+
7583
repo, err := client.repoByFullname(ctx, fullname)
7684
if err != nil {
7785
return nil, err

engine/vcs/gerrit/client_branch.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@ func (c *gerritClient) Branches(ctx context.Context, fullname string, _ sdk.VCSB
4646

4747
//Branch retrieves the branch
4848
func (c *gerritClient) Branch(ctx context.Context, fullname string, filters sdk.VCSBranchFilters) (*sdk.VCSBranch, error) {
49+
if filters.Default {
50+
bs, err := c.Branches(ctx, fullname, sdk.VCSBranchesFilter{})
51+
if err != nil {
52+
return nil, err
53+
}
54+
for _, b := range bs {
55+
if b.Default {
56+
return &b, nil
57+
}
58+
}
59+
}
4960
branch, _, err := c.client.Projects.GetBranch(fullname, filters.BranchName)
5061
if err != nil {
5162
return nil, sdk.WrapError(err, "unable to get branch")

engine/vcs/gitlab/client_branch.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ func (c *gitlabClient) Branches(ctx context.Context, fullname string, _ sdk.VCSB
3434

3535
//Branch retrieves the branch
3636
func (c *gitlabClient) Branch(ctx context.Context, fullname string, filters sdk.VCSBranchFilters) (*sdk.VCSBranch, error) {
37+
if filters.Default {
38+
p, _, err := c.client.Projects.GetProject(fullname, nil)
39+
if err != nil {
40+
return nil, err
41+
}
42+
filters.BranchName = p.DefaultBranch
43+
}
3744
b, g, err := c.client.Branches.GetBranch(fullname, filters.BranchName)
3845
if err != nil {
3946
if g != nil && g.StatusCode == 404 {

0 commit comments

Comments
 (0)