/
client_branch.go
124 lines (104 loc) · 3 KB
/
client_branch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package bitbucketcloud
import (
"context"
"fmt"
"net/url"
"github.com/rockbears/log"
"github.com/ovh/cds/sdk"
)
// Branches returns list of branches for a repo
func (client *bitbucketcloudClient) Branches(ctx context.Context, fullname string, filters sdk.VCSBranchesFilter) ([]sdk.VCSBranch, error) {
repo, err := client.repoByFullname(ctx, fullname)
if err != nil {
return nil, sdk.WrapError(err, "cannot get repo by fullname")
}
var branches []Branch
path := fmt.Sprintf("/repositories/%s/refs/branches", fullname)
params := url.Values{}
params.Set("pagelen", "100")
params.Set("sort", "-target.date")
nextPage := 1
for {
if ctx.Err() != nil {
break
}
if filters.Limit != 0 && len(branches) >= int(filters.Limit) {
break
}
if nextPage != 1 {
params.Set("page", fmt.Sprintf("%d", nextPage))
}
var response Branches
if err := client.do(ctx, "GET", "core", path, params, nil, &response); err != nil {
return nil, sdk.WrapError(err, "Unable to get branches")
}
if cap(branches) == 0 {
branches = make([]Branch, 0, response.Size)
}
branches = append(branches, response.Values...)
if response.Next == "" {
break
} else {
nextPage++
}
}
branchesResult := make([]sdk.VCSBranch, 0, len(branches))
for _, b := range branches {
branch := sdk.VCSBranch{
DisplayID: b.Name,
ID: b.Name,
LatestCommit: b.Target.Hash,
Default: b.Name == repo.Mainbranch.Name,
}
for _, p := range b.Target.Parents {
branch.Parents = append(branch.Parents, p.Hash)
}
branchesResult = append(branchesResult, branch)
}
return branchesResult, nil
}
// Branch returns only detail of a branch
func (client *bitbucketcloudClient) Branch(ctx context.Context, fullname string, filters sdk.VCSBranchFilters) (*sdk.VCSBranch, error) {
if filters.Default {
repo, err := client.repoByFullname(ctx, fullname)
if err != nil {
return nil, err
}
filters.BranchName = repo.Mainbranch.Name
}
repo, err := client.repoByFullname(ctx, fullname)
if err != nil {
return nil, err
}
url := fmt.Sprintf("/repositories/%s/refs/branches/%s", fullname, filters.BranchName)
status, body, _, err := client.get(ctx, url)
if err != nil {
return nil, err
}
if status == 404 {
return nil, sdk.WithStack(sdk.ErrNoBranch)
}
if status >= 400 {
return nil, sdk.NewError(sdk.ErrUnknownError, errorAPI(body))
}
var branch Branch
if err := sdk.JSONUnmarshal(body, &branch); err != nil {
log.Warn(ctx, "bitbucketcloudClient.Branch> Unable to parse github branch: %s", err)
return nil, err
}
if branch.Name == "" {
return nil, fmt.Errorf("bitbucketcloudClient.Branch > Cannot find branch %s", filters.BranchName)
}
branchResult := &sdk.VCSBranch{
DisplayID: branch.Name,
ID: branch.Name,
LatestCommit: branch.Target.Hash,
Default: branch.Name == repo.Mainbranch.Name,
}
if branch.Target.Hash != "" {
for _, p := range branch.Target.Parents {
branchResult.Parents = append(branchResult.Parents, p.Hash)
}
}
return branchResult, nil
}