forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
git.go
269 lines (238 loc) · 8.14 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
// 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 azure
import (
"context"
"fmt"
"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) {
return "", nil, scm.ErrNotSupported
}
func (s *gitService) DeleteRef(ctx context.Context, repo, ref string) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}
func (s *gitService) CreateRef(ctx context.Context, repo, ref, sha string) (*scm.Reference, *scm.Response, error) {
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/refs/update-refs?view=azure-devops-rest-6.0
if s.client.project == "" {
return nil, nil, ProjectRequiredError()
}
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/refs?api-version=6.0", s.client.owner, s.client.project, repo)
in := make(crudBranch, 1)
in[0].Name = scm.ExpandRef(ref, "refs/heads")
in[0].NewObjectID = sha
in[0].OldObjectID = "0000000000000000000000000000000000000000"
res, err := s.client.do(ctx, "POST", endpoint, in, nil)
return nil, res, err
}
func (s *gitService) FindBranch(ctx context.Context, repo, name string) (*scm.Reference, *scm.Response, error) {
if s.client.project == "" {
return nil, nil, ProjectRequiredError()
}
return nil, nil, scm.ErrNotSupported
}
func (s *gitService) FindCommit(ctx context.Context, repo, ref string) (*scm.Commit, *scm.Response, error) {
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/commits/get?view=azure-devops-rest-6.0#get-by-id
if s.client.project == "" {
return nil, nil, ProjectRequiredError()
}
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/commits/%s?api-version=6.0", s.client.owner, s.client.project, repo, ref)
out := new(gitCommit)
res, err := s.client.do(ctx, "GET", endpoint, nil, out)
return convertCommit(out), res, err
}
func (s *gitService) FindTag(ctx context.Context, repo, name string) (*scm.Reference, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *gitService) ListBranches(ctx context.Context, repo string, _ *scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/refs/list?view=azure-devops-rest-6.0
if s.client.project == "" {
return nil, nil, ProjectRequiredError()
}
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/refs?includeMyBranches=true&api-version=6.0", s.client.owner, s.client.project, repo)
out := new(branchList)
res, err := s.client.do(ctx, "GET", endpoint, nil, &out)
return convertBranchList(out.Value), res, err
}
func (s *gitService) ListCommits(ctx context.Context, repo string, opts scm.CommitListOptions) ([]*scm.Commit, *scm.Response, error) {
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/commits/get-commits?view=azure-devops-rest-6.0
if s.client.project == "" {
return nil, nil, ProjectRequiredError()
}
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/commits?", s.client.owner, s.client.project, repo)
if opts.Ref != "" {
endpoint += fmt.Sprintf("searchCriteria.itemVersion.version=%s&", opts.Ref)
}
if opts.Path != "" {
endpoint += fmt.Sprintf("searchCriteria.itemPath=%s&", opts.Path)
}
endpoint += "api-version=6.0"
out := new(commitList)
res, err := s.client.do(ctx, "GET", endpoint, nil, &out)
return convertCommitList(out.Value), res, err
}
func (s *gitService) ListTags(ctx context.Context, repo string, opts *scm.ListOptions) ([]*scm.Reference, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *gitService) ListChanges(ctx context.Context, repo, ref string, _ *scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *gitService) CompareCommits(ctx context.Context, repo, source, target string, _ *scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
// https://docs.microsoft.com/en-us/rest/api/azure/devops/git/diffs/get?view=azure-devops-rest-6.0
if s.client.project == "" {
return nil, nil, ProjectRequiredError()
}
endpoint := fmt.Sprintf("%s/%s/_apis/git/repositories/%s/diffs/commits?", s.client.owner, s.client.project, repo)
// add base
endpoint += fmt.Sprintf("baseVersion=%s&baseVersionType=commit&", source)
// add target
endpoint += fmt.Sprintf("targetVersion=%s&targetVersionType=commit&api-version=6.0", target)
out := new(compare)
res, err := s.client.do(ctx, "GET", endpoint, nil, &out)
changes := out.Changes
return convertChangeList(changes), res, err
}
type crudBranch []struct {
Name string `json:"name"`
OldObjectID string `json:"oldObjectId"`
NewObjectID string `json:"newObjectId"`
}
type branchList struct {
Value []*branch `json:"value"`
Count int `json:"count"`
}
type branch struct {
Name string `json:"name"`
ObjectID string `json:"objectId"`
Creator struct {
DisplayName string `json:"displayName"`
URL string `json:"url"`
Links struct {
Avatar struct {
Href string `json:"href"`
} `json:"avatar"`
} `json:"_links"`
ID string `json:"id"`
UniqueName string `json:"uniqueName"`
ImageURL string `json:"imageUrl"`
Descriptor string `json:"descriptor"`
} `json:"creator"`
URL string `json:"url"`
}
type commitList struct {
Value []*gitCommit `json:"value"`
Count int `json:"count"`
}
type gitCommit struct {
CommitID string `json:"commitId"`
Author struct {
Name string `json:"name"`
Email string `json:"email"`
Date time.Time `json:"date"`
} `json:"author"`
Committer struct {
Name string `json:"name"`
Email string `json:"email"`
Date time.Time `json:"date"`
} `json:"committer"`
Comment string `json:"comment"`
CommentTruncated bool `json:"commentTruncated"`
ChangeCounts struct {
Add int `json:"Add"`
Edit int `json:"Edit"`
Delete int `json:"Delete"`
} `json:"changeCounts"`
URL string `json:"url"`
RemoteURL string `json:"remoteUrl"`
}
type file struct {
ChangeType string `json:"changeType"`
Item struct {
CommitID string `json:"commitId"`
GitObjectType string `json:"gitObjectType"`
IsFolder bool `json:"isFolder"`
ObjectID string `json:"objectId"`
OriginalObjectID string `json:"originalObjectId"`
Path string `json:"path"`
URL string `json:"url"`
} `json:"item"`
}
type compare struct {
AheadCount int64 `json:"aheadCount"`
AllChangesIncluded bool `json:"allChangesIncluded"`
BaseCommit string `json:"baseCommit"`
BehindCount int64 `json:"behindCount"`
ChangeCounts struct {
Add int64 `json:"Add"`
Edit int64 `json:"Edit"`
} `json:"changeCounts"`
Changes []*file `json:"changes"`
CommonCommit string `json:"commonCommit"`
TargetCommit string `json:"targetCommit"`
}
func convertBranchList(from []*branch) []*scm.Reference {
to := []*scm.Reference{}
for _, v := range from {
to = append(to, convertBranch(v))
}
return to
}
func convertBranch(from *branch) *scm.Reference {
return &scm.Reference{
Name: scm.TrimRef(from.Name),
Path: from.Name,
Sha: from.ObjectID,
}
}
func convertCommitList(from []*gitCommit) []*scm.Commit {
to := []*scm.Commit{}
for _, v := range from {
to = append(to, convertCommit(v))
}
return to
}
func convertCommit(from *gitCommit) *scm.Commit {
return &scm.Commit{
Message: from.Comment,
Sha: from.CommitID,
Link: from.URL,
Author: scm.Signature{
Login: from.Author.Name,
Name: from.Author.Name,
Email: from.Author.Email,
Date: from.Author.Date,
},
Committer: scm.Signature{
Login: from.Committer.Name,
Name: from.Committer.Name,
Email: from.Committer.Email,
Date: from.Committer.Date,
},
}
}
func convertChangeList(from []*file) []*scm.Change {
to := []*scm.Change{}
for _, v := range from {
to = append(to, convertChange(v))
}
return to
}
func convertChange(from *file) *scm.Change {
returnVal := &scm.Change{
Path: from.Item.Path,
}
switch from.ChangeType {
case "add":
returnVal.Added = true
case "delete":
returnVal.Deleted = true
case "rename":
returnVal.Renamed = true
}
return returnVal
}