/
client_branch.go
181 lines (160 loc) · 5.42 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package github
import (
"context"
"fmt"
"net/http"
"github.com/rockbears/log"
"github.com/ovh/cds/engine/cache"
"github.com/ovh/cds/sdk"
)
// Branches returns list of branches for a repo
// https://developer.github.com/v3/repos/branches/#list-branches
func (g *githubClient) Branches(ctx context.Context, fullname string, filters sdk.VCSBranchesFilter) ([]sdk.VCSBranch, error) {
var branches = []Branch{}
repo, err := g.repoByFullname(ctx, fullname)
if err != nil {
return nil, err
}
var noEtag bool
var attempt int
var nextPage = "/repos/" + fullname + "/branches"
for nextPage != "" {
if ctx.Err() != nil {
break
}
if filters.Limit > 0 && len(branches) >= int(filters.Limit) {
break
}
var opt getArgFunc
if noEtag {
opt = withoutETag
} else {
opt = withETag
}
attempt++
status, body, headers, err := g.get(ctx, nextPage, opt)
if err != nil {
log.Warn(ctx, "githubClient.Branches> Error %s", err)
return nil, err
}
if status >= 400 {
return nil, sdk.NewError(sdk.ErrUnknownError, errorAPI(body))
}
nextBranches := []Branch{}
//Github may return 304 status because we are using conditional request with ETag based headers
if status == http.StatusNotModified {
//If repos aren't updated, lets get them from cache
k := cache.Key("vcs", "github", "branches", sdk.Hash512(g.OAuthToken+g.username), "/repos/"+fullname+"/branches")
_, err := g.Cache.Get(k, &branches)
if err != nil {
log.Error(ctx, "cannot get from cache %s: %v", k, err)
}
if len(branches) != 0 || attempt > 5 {
//We found branches, let's exit the loop
break
}
//If we did not found any branch in cache, let's retry (same nextPage) without etag
noEtag = true
continue
} else {
if err := sdk.JSONUnmarshal(body, &nextBranches); err != nil {
log.Warn(ctx, "githubClient.Branches> Unable to parse github branches: %s", err)
return nil, err
}
}
branches = append(branches, nextBranches...)
nextPage = getNextPage(headers)
}
//Put the body on cache for one hour and one minute
k := cache.Key("vcs", "github", "branches", sdk.Hash512(g.OAuthToken+g.username), "/repos/"+fullname+"/branches")
if err := g.Cache.SetWithTTL(k, branches, 61*60); err != nil {
log.Error(ctx, "cannot SetWithTTL: %s: %v", k, err)
}
branchesResult := []sdk.VCSBranch{}
for _, b := range branches {
branch := sdk.VCSBranch{
DisplayID: b.Name,
ID: b.Name,
LatestCommit: b.Commit.Sha,
Default: b.Name == repo.DefaultBranch,
}
for _, p := range b.Commit.Parents {
branch.Parents = append(branch.Parents, p.Sha)
}
branchesResult = append(branchesResult, branch)
}
return branchesResult, nil
}
// Branch returns only detail of a branch
func (g *githubClient) Branch(ctx context.Context, fullname string, filters sdk.VCSBranchFilters) (*sdk.VCSBranch, error) {
if filters.Default {
repo, err := g.repoByFullname(ctx, fullname)
if err != nil {
return nil, err
}
filters.BranchName = repo.DefaultBranch
}
cacheBranchKey := cache.Key("vcs", "github", "branches", sdk.Hash512(g.OAuthToken+g.username), "/repos/"+fullname+"/branch/"+filters.BranchName)
repo, err := g.repoByFullname(ctx, fullname)
if err != nil {
return nil, err
}
url := "/repos/" + fullname + "/branches/" + filters.BranchName
status, body, _, err := g.get(ctx, url)
if err != nil {
if err := g.Cache.Delete(cacheBranchKey); err != nil {
log.Error(ctx, "githubClient.Branch> unable to delete cache key %v: %v", cacheBranchKey, err)
}
return nil, err
}
if status == 404 {
return nil, sdk.WithStack(sdk.ErrNoBranch)
}
if status >= 400 {
if err := g.Cache.Delete(cacheBranchKey); err != nil {
log.Error(ctx, "githubClient.Branch> unable to delete cache key %v: %v", cacheBranchKey, err)
}
return nil, sdk.NewError(sdk.ErrUnknownError, errorAPI(body))
}
//Github may return 304 status because we are using conditional request with ETag based headers
var branch Branch
if status == http.StatusNotModified {
//If repos aren't updated, lets get them from cache
find, err := g.Cache.Get(cacheBranchKey, &branch)
if err != nil {
log.Error(ctx, "cannot get from cache %s: %v", cacheBranchKey, err)
}
if !find {
log.Error(ctx, "Unable to get branch (%s) from the cache", cacheBranchKey)
}
} else {
if err := sdk.JSONUnmarshal(body, &branch); err != nil {
log.Warn(ctx, "githubClient.Branch> Unable to parse github branch: %s", err)
return nil, err
}
}
if branch.Name == "" {
log.Warn(ctx, "githubClient.Branch> Cannot find branch %v: %s", branch, filters.BranchName)
if err := g.Cache.Delete(cacheBranchKey); err != nil {
log.Error(ctx, "githubClient.Branch> unable to delete cache key %v: %v", cacheBranchKey, err)
}
return nil, fmt.Errorf("githubClient.Branch > Cannot find branch %s", filters.BranchName)
}
//Put the body on cache for one hour and one minute
k := cache.Key("vcs", "github", "branches", sdk.Hash512(g.OAuthToken+g.username), "/repos/"+fullname+"/branch/"+filters.BranchName)
if err := g.Cache.SetWithTTL(k, branch, 61*60); err != nil {
log.Error(ctx, "cannot SetWithTTL: %s: %v", k, err)
}
branchResult := &sdk.VCSBranch{
DisplayID: branch.Name,
ID: branch.Name,
LatestCommit: branch.Commit.Sha,
Default: branch.Name == repo.DefaultBranch,
}
if branch.Commit.Sha != "" {
for _, p := range branch.Commit.Parents {
branchResult.Parents = append(branchResult.Parents, p.Sha)
}
}
return branchResult, nil
}