forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
git.go
296 lines (267 loc) · 7.74 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
// 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 bitbucket
import (
"context"
"fmt"
"strings"
"time"
"github.com/jenkins-x/go-scm/scm"
)
type gitService struct {
client *wrapper
}
func (s *gitService) FindRef(ctx context.Context, repo, ref string) (string, *scm.Response, error) {
commit, res, err := s.FindCommit(ctx, repo, ref)
if err != nil && res.Status != 404 {
return "", res, err
}
if commit != nil {
if commit.Sha != "" {
return commit.Sha, res, nil
}
}
idx := strings.LastIndex(ref, "/")
if idx >= 0 {
ref = ref[idx+1:]
}
return ref, nil, nil
/*
path := fmt.Sprintf("2.0/repositories/%s/refs?%s", repo, encodeRefQueryOptions(ref))
out := new(branches)
res, err := s.client.do(ctx, "GET", path, nil, out)
if debugDump {
var buf bytes.Buffer
res, err := s.client.do(ctx, "GET", path, nil, &buf)
fmt.Printf("%s\n", buf.String())
return "", res, err
}
branches := convertBranchList(out)
if len(branches) == 0 {
return "", res, err
}
return branches[0].Name, 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, name string) (*scm.Reference, *scm.Response, error) {
path := fmt.Sprintf("2.0/repositories/%s/refs/branches/%s", repo, name)
out := new(branch)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertBranch(out), res, err
}
func (s *gitService) FindCommit(ctx context.Context, repo, ref string) (*scm.Commit, *scm.Response, error) {
path := fmt.Sprintf("2.0/repositories/%s/commit/%s", repo, 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, name string) (*scm.Reference, *scm.Response, error) {
path := fmt.Sprintf("2.0/repositories/%s/refs/tags/%s", repo, name)
out := new(branch)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertTag(out), res, err
}
func (s *gitService) ListBranches(ctx context.Context, repo string, opts scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
path := fmt.Sprintf("2.0/repositories/%s/refs/branches?%s", repo, encodeListOptions(opts))
out := new(branches)
res, err := s.client.do(ctx, "GET", path, nil, out)
copyPagination(out.pagination, res)
return convertBranchList(out), res, err
}
func (s *gitService) ListCommits(ctx context.Context, repo string, opts scm.CommitListOptions) ([]*scm.Commit, *scm.Response, error) {
path := fmt.Sprintf("2.0/repositories/%s/commits/%s?%s", repo, opts.Ref, encodeCommitListOptions(opts))
out := new(commits)
res, err := s.client.do(ctx, "GET", path, nil, out)
copyPagination(out.pagination, res)
return convertCommitList(out), res, err
}
func (s *gitService) ListTags(ctx context.Context, repo string, opts scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
path := fmt.Sprintf("2.0/repositories/%s/refs/tags?%s", repo, encodeListOptions(opts))
out := new(branches)
res, err := s.client.do(ctx, "GET", path, nil, &out)
copyPagination(out.pagination, res)
return convertTagList(out), res, err
}
func (s *gitService) ListChanges(ctx context.Context, repo, ref string, opts scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
path := fmt.Sprintf("2.0/repositories/%s/diffstat/%s?%s", repo, ref, encodeListOptions(opts))
out := new(diffstats)
res, err := s.client.do(ctx, "GET", path, nil, &out)
copyPagination(out.pagination, res)
return convertDiffstats(out), res, err
}
type branch struct {
Type string `json:"type"`
Name string `json:"name"`
Target struct {
Hash string `json:"hash"`
} `json:"target"`
}
type commits struct {
pagination
Values []*commit `json:"values"`
}
type branches struct {
pagination
Values []*branch `json:"values"`
}
type diffstats struct {
pagination
Values []*diffstat
}
type diffstat struct {
Status string `json:"status"`
Old struct {
Path string `json:"path"`
Type string `json:"type"`
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
} `json:"old"`
LinesRemoved int `json:"lines_removed"`
LinesAdded int `json:"lines_added"`
New struct {
Path string `json:"path"`
Type string `json:"type"`
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
} `json:"new"`
}
type commit struct {
Hash string `json:"hash"`
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
Comments struct {
Href string `json:"href"`
} `json:"comments"`
Patch struct {
Href string `json:"href"`
} `json:"patch"`
HTML struct {
Href string `json:"href"`
} `json:"html"`
Diff struct {
Href string `json:"href"`
} `json:"diff"`
Approve struct {
Href string `json:"href"`
} `json:"approve"`
Statuses struct {
Href string `json:"href"`
} `json:"statuses"`
} `json:"links"`
Author struct {
Raw string `json:"raw"`
User struct {
Username string `json:"username"`
DisplayName string `json:"display_name"`
AccountID string `json:"account_id"`
Links struct {
Self struct {
Href string `json:"href"`
} `json:"self"`
HTML struct {
Href string `json:"href"`
} `json:"html"`
Avatar struct {
Href string `json:"href"`
} `json:"avatar"`
} `json:"links"`
Type string `json:"type"`
UUID string `json:"uuid"`
} `json:"user"`
} `json:"author"`
Summary struct {
Raw string `json:"raw"`
Markup string `json:"markup"`
HTML string `json:"html"`
Type string `json:"type"`
} `json:"summary"`
Date time.Time `json:"date"`
Message string `json:"message"`
Type string `json:"type"`
}
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 {
return &scm.Change{
Path: from.New.Path,
Added: from.Status == "added",
Renamed: from.Status == "renamed",
Deleted: from.Status == "removed",
}
}
func convertCommitList(from *commits) []*scm.Commit {
to := []*scm.Commit{}
for _, v := range from.Values {
to = append(to, convertCommit(v))
}
return to
}
func convertCommit(from *commit) *scm.Commit {
return &scm.Commit{
Message: from.Message,
Sha: from.Hash,
Link: from.Links.HTML.Href,
Author: scm.Signature{
Name: from.Author.User.DisplayName,
Email: extractEmail(from.Author.Raw),
Date: from.Date,
Login: from.Author.User.Username,
Avatar: from.Author.User.Links.Avatar.Href,
},
Committer: scm.Signature{
Name: from.Author.User.DisplayName,
Email: extractEmail(from.Author.Raw),
Date: from.Date,
Login: from.Author.User.Username,
Avatar: from.Author.User.Links.Avatar.Href,
},
}
}
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.Name),
Path: scm.ExpandRef(from.Name, "refs/heads/"),
Sha: from.Target.Hash,
}
}
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.Name),
Path: scm.ExpandRef(from.Name, "refs/tags/"),
Sha: from.Target.Hash,
}
}