Repository navigation
Expand file tree
/
Copy pathclient_commit.go
More file actions
325 lines (289 loc) · 8.98 KB
/
Copy pathclient_commit.go
File metadata and controls
325 lines (289 loc) · 8.98 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package github
import (
"context"
"fmt"
"net/http"
"net/url"
"reflect"
"strings"
"time"
"github.com/rockbears/log"
"github.com/ovh/cds/engine/cache"
"github.com/ovh/cds/sdk"
)
func arrayContains(array interface{}, s interface{}) bool {
b := sdk.InterfaceSlice(array)
for _, i := range b {
if reflect.DeepEqual(i, s) {
return true
}
}
return false
}
func findAncestors(allCommits []Commit, since string) []string {
ancestors := []string{}
var i int
var limit = len(allCommits) * len(allCommits)
ancestorLoop:
if i > limit {
return ancestors
}
for _, c := range allCommits {
i++
if c.Sha == since {
for _, p := range c.Parents {
if !arrayContains(ancestors, p.Sha) {
ancestors = append(ancestors, p.Sha)
goto ancestorLoop
}
}
} else if arrayContains(ancestors, c.Sha) {
for _, p := range c.Parents {
if !arrayContains(ancestors, p.Sha) {
ancestors = append(ancestors, p.Sha)
goto ancestorLoop
}
}
}
}
return ancestors
}
func filterCommits(allCommits []Commit, since, until string) []Commit {
commits := []Commit{}
sinceAncestors := findAncestors(allCommits, since)
untilAncestors := findAncestors(allCommits, until)
//We have to delete all common ancestors between sinceAncestors and untilAncestors
toDelete := []string{}
for _, c := range untilAncestors {
if c == since {
toDelete = append(toDelete, c)
}
if arrayContains(sinceAncestors, c) {
toDelete = append(toDelete, c)
}
}
for _, d := range toDelete {
for i, x := range untilAncestors {
if x == d {
untilAncestors = append(untilAncestors[:i], untilAncestors[i+1:]...)
}
}
}
untilAncestors = append(untilAncestors, until)
for _, c := range allCommits {
if arrayContains(untilAncestors, c.Sha) {
commits = append(commits, c)
}
}
return commits
}
// Commits returns the commits list on a branch between a commit SHA (since) until another commit SHA (until). The branch is given by the branch of the first commit SHA (since)
func (g *githubClient) Commits(ctx context.Context, repo, theBranch, since, until string) ([]sdk.VCSCommit, error) {
var commitsResult []sdk.VCSCommit
log.Debug(ctx, "Looking for commits on repo %s since = %s until = %s", repo, since, until)
k := cache.Key("vcs", "github", "commits", repo, "since="+since, "until="+until)
find, err := g.Cache.Get(k, &commitsResult)
if err != nil {
log.Error(ctx, "cannot get from cache %s: %v", k, err)
}
if find {
return commitsResult, nil
}
var sinceDate time.Time
// Calculate since commit
if since == "" {
// If no since commit, take from the beginning of the branch
b, errB := g.Branch(ctx, repo, sdk.VCSBranchFilters{BranchName: theBranch})
if errB != nil {
return nil, errB
}
if b == nil {
return nil, fmt.Errorf("Commits>Cannot find branch %s", theBranch)
}
for _, c := range b.Parents {
cp, errCP := g.Commit(ctx, repo, c)
if errCP != nil {
return nil, errCP
}
d := time.Unix(cp.Timestamp/1000, 0)
if d.After(sinceDate) {
// To not get the parent commit
sinceDate = d.Add(1 * time.Second)
}
}
} else {
sinceCommit, errC := g.Commit(ctx, repo, since)
if errC != nil {
return nil, errC
}
sinceDate = time.Unix(sinceCommit.Timestamp/1000, 0)
}
var untilDate time.Time
if until == "" {
// If no until commit take until the end of the branch
untilDate = time.Now()
} else {
untilCommit, errC := g.Commit(ctx, repo, until)
if errC != nil {
return nil, errC
}
untilDate = time.Unix(untilCommit.Timestamp/1000, 0)
}
//Get Commit List
theCommits, err := g.allCommitBetween(ctx, repo, untilDate, sinceDate, theBranch)
if err != nil {
return nil, err
}
if since != "" {
log.Debug(ctx, "filter commit (%d) between %s and %s", len(theCommits), since, until)
theCommits = filterCommits(theCommits, since, until)
}
//Convert to sdk.VCSCommit
for _, c := range theCommits {
commit := sdk.VCSCommit{
Timestamp: c.Commit.Author.Date.Unix() * 1000,
Message: c.Commit.Message,
Hash: c.Sha,
URL: c.HTMLURL,
Author: sdk.VCSAuthor{
DisplayName: c.Commit.Author.Name,
Email: c.Commit.Author.Email,
Name: c.Commit.Author.Name,
Avatar: c.Author.AvatarURL,
},
}
commitsResult = append(commitsResult, commit)
}
key := cache.Key("vcs", "github", "commits", repo, "since="+since, "until="+until)
if err := g.Cache.SetWithTTL(key, commitsResult, 3*60*60); err != nil {
log.Error(ctx, "cannot SetWithTTL: %s: %v", key, err)
}
return commitsResult, nil
}
func (g *githubClient) allCommitBetween(ctx context.Context, repo string, untilDate time.Time, sinceDate time.Time, branch string) ([]Commit, error) {
var commits = []Commit{}
urlValues := url.Values{}
urlValues.Add("sha", branch)
urlValues.Add("since", sinceDate.Format(time.RFC3339))
urlValues.Add("until", untilDate.Format(time.RFC3339))
var nextPage = "/repos/" + repo + "/commits"
for nextPage != "" {
if ctx.Err() != nil {
break
}
if strings.Contains(nextPage, "?") {
nextPage += "&"
} else {
nextPage += "?"
}
status, body, headers, err := g.get(ctx, nextPage+urlValues.Encode(), withoutETag)
if err != nil {
log.Warn(ctx, "githubClient.Commits> Error %s", err)
return nil, err
}
if status >= 400 {
log.Warn(ctx, "githubClient.Commits> Error %s", errorAPI(body))
return nil, sdk.NewError(sdk.ErrUnknownError, errorAPI(body))
}
nextCommits := []Commit{}
if err := sdk.JSONUnmarshal(body, &nextCommits); err != nil {
log.Warn(ctx, "githubClient.Commits> Unable to parse github commits: %s", err)
return nil, err
}
commits = append(commits, nextCommits...)
nextPage = getNextPage(headers)
}
return commits, nil
}
// Commit Get a single commit
// https://developer.github.com/v3/repos/commits/#get-a-single-commit
func (g *githubClient) Commit(ctx context.Context, repo, hash string) (sdk.VCSCommit, error) {
url := "/repos/" + repo + "/commits/" + hash
status, body, _, err := g.get(ctx, url)
if err != nil {
log.Warn(ctx, "githubClient.Commit> Error %s", err)
return sdk.VCSCommit{}, err
}
if status >= 400 {
return sdk.VCSCommit{}, sdk.NewError(sdk.ErrRepoNotFound, errorAPI(body))
}
c := Commit{}
//Github may return 304 status because we are using conditional request with ETag based headers
if status == http.StatusNotModified {
//If repo isn't updated, lets get them from cache
k := cache.Key("vcs", "github", "commit", g.OAuthToken, url)
if _, err := g.Cache.Get(k, &c); err != nil {
log.Error(ctx, "cannot get from cache %s: %v", k, err)
}
} else {
if err := sdk.JSONUnmarshal(body, &c); err != nil {
log.Warn(ctx, "githubClient.Commit> Unable to parse github commit: %s", err)
return sdk.VCSCommit{}, err
}
//Put the body on cache for one hour and one minute
k := cache.Key("vcs", "github", "commit", g.OAuthToken, url)
if err := g.Cache.SetWithTTL(k, c, 61*60); err != nil {
log.Error(ctx, "cannot SetWithTTL: %s: %v", k, err)
}
}
commit := sdk.VCSCommit{
Timestamp: c.Commit.Author.Date.Unix() * 1000,
Message: c.Commit.Message,
Hash: c.Sha,
Author: sdk.VCSAuthor{
DisplayName: c.Commit.Author.Name,
Email: c.Commit.Author.Email,
Name: c.Author.Login,
Avatar: c.Author.AvatarURL,
},
URL: c.HTMLURL,
}
return commit, nil
}
func (g *githubClient) CommitsBetweenRefs(ctx context.Context, repo, base, head string) ([]sdk.VCSCommit, error) {
var commits []sdk.VCSCommit
url := fmt.Sprintf("/repos/%s/compare/%s...%s", repo, base, head)
status, body, _, err := g.get(ctx, url)
if err != nil {
log.Warn(ctx, "githubClient.CommitsBetweenRefs> Error %s", err)
return commits, err
}
if status >= 400 {
return commits, sdk.NewError(sdk.ErrRepoNotFound, errorAPI(body))
}
var diff DiffCommits
//Github may return 304 status because we are using conditional request with ETag based headers
if status == http.StatusNotModified {
//If repo isn't updated, lets get them from cache
k := cache.Key("vcs", "github", "commitdiff", g.OAuthToken, url)
if _, err := g.Cache.Get(k, &commits); err != nil {
log.Error(ctx, "cannot get from cache %s: %v", k, err)
}
} else {
if err := sdk.JSONUnmarshal(body, &diff); err != nil {
log.Warn(ctx, "githubClient.CommitsBetweenRefs> Unable to parse github commit: %s", err)
return commits, err
}
commits = make([]sdk.VCSCommit, len(diff.Commits))
for i, commit := range diff.Commits {
commits[i] = sdk.VCSCommit{
Timestamp: commit.Commit.Author.Date.Unix() * 1000,
Message: commit.Commit.Message,
Hash: commit.Sha,
Author: sdk.VCSAuthor{
DisplayName: commit.Commit.Author.Name,
Email: commit.Commit.Author.Email,
Name: commit.Author.Login,
Avatar: commit.Author.AvatarURL,
},
URL: commit.HTMLURL,
}
}
//Put the body on cache for one hour and one minute
k := cache.Key("vcs", "github", "commitdiff", g.OAuthToken, url)
if err := g.Cache.SetWithTTL(k, &commits, 61*60); err != nil {
log.Error(ctx, "cannot SetWithTTL: %s: %v", k, err)
}
}
return commits, nil
}