forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 83
/
git.go
319 lines (290 loc) · 9.54 KB
/
git.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
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
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package stash
import (
"context"
"fmt"
"net/url"
"strings"
"time"
"github.com/jenkins-x/go-scm/scm"
)
// TODO(bradrydzewski) commit link is an empty string.
// TODO(bradrydzewski) commit list is not supported; behavior incompatible with other drivers.
type gitService struct {
client *wrapper
}
func (s *gitService) FindRef(ctx context.Context, repo, ref string) (string, *scm.Response, error) {
ref = strings.TrimPrefix(ref, "heads/")
namespace, name := scm.Split(repo)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/commits/%s", namespace, name, url.PathEscape(ref))
out := commit{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
if err != nil {
return "", res, err
}
if out.ID != "" {
return out.ID, res, err
}
idx := strings.LastIndex(ref, "/")
if idx >= 0 {
ref = ref[idx+1:]
}
return ref, res, err
}
func (s *gitService) CreateRef(ctx context.Context, repo, ref, sha string) (*scm.Reference, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *gitService) DeleteRef(ctx context.Context, repo, ref string) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}
func (s *gitService) FindBranch(ctx context.Context, repo, branch string) (*scm.Reference, *scm.Response, error) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/branches?filterText=%s", namespace, name, url.QueryEscape(branch))
out := new(branches)
res, err := s.client.do(ctx, "GET", path, nil, out)
if err != nil {
return nil, res, err
}
for _, v := range out.Values {
if v.DisplayID == branch {
return convertBranch(v), res, err
}
}
return nil, res, scm.ErrNotFound
}
func (s *gitService) FindCommit(ctx context.Context, repo, ref string) (*scm.Commit, *scm.Response, error) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/commits/%s", namespace, name, url.PathEscape(ref))
out := new(commit)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertCommit(out), res, err
}
func (s *gitService) FindTag(ctx context.Context, repo, tag string) (*scm.Reference, *scm.Response, error) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/tags?filterText=%s", namespace, name, url.QueryEscape(tag))
out := new(branches)
res, err := s.client.do(ctx, "GET", path, nil, out)
if err != nil {
return nil, res, err
}
for _, v := range out.Values {
if v.DisplayID == tag {
return convertTag(v), res, err
}
}
return nil, res, scm.ErrNotFound
}
func (s *gitService) ListBranches(ctx context.Context, repo string, opts *scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/branches?%s", namespace, name, encodeListOptions(opts))
out := new(branches)
res, err := s.client.do(ctx, "GET", path, nil, out)
if !out.pagination.LastPage.Bool {
res.Page.First = 1
res.Page.Next = opts.Page + 1
}
return convertBranchList(out), res, err
}
func (s *gitService) ListCommits(ctx context.Context, repo string, opts scm.CommitListOptions) ([]*scm.Commit, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *gitService) ListTags(ctx context.Context, repo string, opts *scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/tags?%s", namespace, name, encodeListOptions(opts))
out := new(branches)
res, err := s.client.do(ctx, "GET", path, nil, &out)
if !out.pagination.LastPage.Bool {
res.Page.First = 1
res.Page.Next = opts.Page + 1
}
return convertTagList(out), res, err
}
func (s *gitService) ListChanges(ctx context.Context, repo, ref string, opts *scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/commits/%s/changes?%s", namespace, name, url.PathEscape(ref), encodeListOptions(opts))
out := new(diffstats)
res, err := s.client.do(ctx, "GET", path, nil, &out)
if !out.pagination.LastPage.Bool {
res.Page.First = 1
res.Page.Next = opts.Page + 1
}
return convertDiffstats(out), res, err
}
func (s *gitService) CompareCommits(ctx context.Context, repo, ref1, ref2 string, opts *scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
namespace, name := scm.Split(repo)
opts.From = url.PathEscape(ref1)
opts.To = url.PathEscape(ref2)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/compare/changes?%s", namespace, name, encodeListOptions(opts))
out := new(diffstats)
res, err := s.client.do(ctx, "GET", path, nil, &out)
if !out.pagination.LastPage.Bool {
res.Page.First = 1
res.Page.Next = opts.Page + 1
}
return convertDiffstats(out), res, err
}
type branch struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
Type string `json:"type"`
LatestCommit string `json:"latestCommit"`
LatestChangeset string `json:"latestChangeset"`
IsDefault bool `json:"isDefault"`
}
type branches struct {
pagination
Values []*branch `json:"values"`
}
type diffstats struct {
pagination
Values []*diffstat
}
type diffpath struct {
Components []string `json:"components"`
Parent string `json:"parent"`
Name string `json:"name"`
Extension string `json:"extension"`
ToString string `json:"toString"`
}
type diffstat struct {
ContentID string `json:"contentId"`
FromContentID string `json:"fromContentId"`
Path diffpath `json:"path"`
SrcPath *diffpath `json:"srcPath,omitempty"`
PercentUnchanged int `json:"percentUnchanged"`
Type string `json:"type"`
NodeType string `json:"nodeType"`
SrcExecutable bool `json:"srcExecutable"`
Links struct {
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
Properties struct {
GitChangeType string `json:"gitChangeType"`
} `json:"properties"`
}
type commit struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
Author struct {
Name string `json:"name"`
EmailAddress string `json:"emailAddress"`
ID int `json:"id"`
DisplayName string `json:"displayName"`
Active bool `json:"active"`
Slug string `json:"slug"`
Type string `json:"type"`
Links struct {
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
} `json:"author"`
AuthorTimestamp int64 `json:"authorTimestamp"`
Committer struct {
Name string `json:"name"`
EmailAddress string `json:"emailAddress"`
ID int `json:"id"`
DisplayName string `json:"displayName"`
Active bool `json:"active"`
Slug string `json:"slug"`
Type string `json:"type"`
Links struct {
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
} `json:"committer"`
CommitterTimestamp int64 `json:"committerTimestamp"`
Message string `json:"message"`
Parents []struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
Author struct {
Name string `json:"name"`
EmailAddress string `json:"emailAddress"`
} `json:"author"`
AuthorTimestamp int64 `json:"authorTimestamp"`
Committer struct {
Name string `json:"name"`
EmailAddress string `json:"emailAddress"`
} `json:"committer"`
CommitterTimestamp int64 `json:"committerTimestamp"`
Message string `json:"message"`
Parents []struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
} `json:"parents"`
} `json:"parents"`
}
func convertDiffstats(from *diffstats) []*scm.Change {
to := []*scm.Change{}
for _, v := range from.Values {
to = append(to, convertDiffstat(v))
}
return to
}
func convertDiffstat(from *diffstat) *scm.Change {
to := &scm.Change{
Path: from.Path.ToString,
Added: from.Type == "ADD",
Renamed: from.Type == "MOVE",
Deleted: from.Type == "DELETE",
}
if from.SrcPath != nil {
to.PreviousPath = from.SrcPath.ToString
}
return to
}
func convertCommit(from *commit) *scm.Commit {
return &scm.Commit{
Message: from.Message,
Sha: from.ID,
// Link: "%s/projects/%s/repos/%s/commits/%s",
Author: scm.Signature{
Name: from.Author.DisplayName,
Email: from.Author.EmailAddress,
Date: time.Unix(from.AuthorTimestamp/1000, 0),
Login: from.Author.Slug,
Avatar: avatarLink(from.Author.EmailAddress),
},
Committer: scm.Signature{
Name: from.Committer.DisplayName,
Email: from.Committer.EmailAddress,
Date: time.Unix(from.CommitterTimestamp/1000, 0),
Login: from.Committer.Slug,
Avatar: avatarLink(from.Committer.EmailAddress),
},
}
}
func convertBranchList(from *branches) []*scm.Reference {
to := []*scm.Reference{}
for _, v := range from.Values {
to = append(to, convertBranch(v))
}
return to
}
func convertBranch(from *branch) *scm.Reference {
return &scm.Reference{
Name: scm.TrimRef(from.DisplayID),
Path: scm.ExpandRef(from.DisplayID, "refs/heads/"),
Sha: from.LatestCommit,
}
}
func convertTagList(from *branches) []*scm.Reference {
to := []*scm.Reference{}
for _, v := range from.Values {
to = append(to, convertTag(v))
}
return to
}
func convertTag(from *branch) *scm.Reference {
return &scm.Reference{
Name: scm.TrimRef(from.DisplayID),
Path: scm.ExpandRef(from.DisplayID, "refs/tags/"),
Sha: from.LatestCommit,
}
}