This repository has been archived by the owner on Oct 3, 2018. It is now read-only.
forked from fudanchii/go-gitlab-client
-
Notifications
You must be signed in to change notification settings - Fork 13
/
repositories.go
323 lines (246 loc) · 7.45 KB
/
repositories.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
320
321
322
323
package gogitlab
import (
"encoding/json"
"fmt"
"net/url"
"time"
)
const (
repo_url_branches = "/projects/:id/repository/branches" // List repository branches
repo_url_branch = "/projects/:id/repository/branches/:branch" // Get a specific branch of a project.
repo_url_tags = "/projects/:id/repository/tags" // List project repository tags
repo_url_commits = "/projects/:id/repository/commits" // List repository commits
repo_url_commit_comments = "/projects/:id/repository/commits/:sha/comments" // New comment or list of commit comments
repo_url_tree = "/projects/:id/repository/tree" // List repository tree
repo_url_raw_file = "/projects/:id/repository/blobs/:sha" // Get raw file content for specific commit/branch
)
type BranchCommit struct {
Id string `json:"id,omitempty"`
Tree string `json:"tree,omitempty"`
AuthoredDateRaw string `json:"authored_date,omitempty"`
CommittedDateRaw string `json:"committed_date,omitempty"`
Message string `json:"message,omitempty"`
Author *Person `json:"author,omitempty"`
Committer *Person `json:"committer,omitempty"`
/*
"parents": [
{"id": "9b0c4b08e7890337fc8111e66f809c8bbec467a9"},
{"id": "3ac634dca850cab70ab14b43ad6073d1e0a7827f"}
]
*/
}
type Branch struct {
Name string `json:"name,omitempty"`
Protected bool `json:"protected,omitempty"`
Commit *BranchCommit `json:"commit,omitempty"`
}
type Tag struct {
Name string `json:"name,omitempty"`
Protected bool `json:"protected,omitempty"`
Commit *BranchCommit `json:"commit,omitempty"`
}
type Commit struct {
Id string
Short_Id string
Title string
Author_Name string
Author_Email string
Created_At string
CreatedAt time.Time
}
type File struct {
Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Mode string `json:"mode,omitempty"`
Children []*File
}
type CommitComment struct {
Author *Member `json:"author,omitempty"`
Line int `json:"line,omitempty"`
LineType string `json:"line_type,omitempty"`
Note string `json:"note,omitempty"`
Path string `json:"path,omitempty"`
}
/*
Get a list of repository branches from a project, sorted by name alphabetically.
GET /projects/:id/repository/branches
Parameters:
id The ID of a project
Usage:
branches, err := gitlab.RepoBranches("your_projet_id")
if err != nil {
fmt.Println(err.Error())
}
for _, branch := range branches {
fmt.Printf("%+v\n", branch)
}
*/
func (g *Gitlab) RepoBranches(id string) ([]*Branch, error) {
url, opaque := g.ResourceUrlRaw(repo_url_branches, map[string]string{":id": id})
var branches []*Branch
contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
if err == nil {
err = json.Unmarshal(contents, &branches)
}
return branches, err
}
/*
Get a single project repository branch.
GET /projects/:id/repository/branches/:branch
Parameters:
id The ID of a project
branch The name of the branch
*/
func (g *Gitlab) RepoBranch(id, refName string) (*Branch, error) {
url, opaque := g.ResourceUrlRaw(repo_url_branch, map[string]string{
":id": id,
":branch": refName,
})
branch := new(Branch)
contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
if err == nil {
err = json.Unmarshal(contents, &branch)
}
return branch, err
}
/*
Get a list of repository tags from a project, sorted by name in reverse alphabetical order.
GET /projects/:id/repository/tags
Parameters:
id The ID of a project
Usage:
tags, err := gitlab.RepoTags("your_projet_id")
if err != nil {
fmt.Println(err.Error())
}
for _, tag := range tags {
fmt.Printf("%+v\n", tag)
}
*/
func (g *Gitlab) RepoTags(id string) ([]*Tag, error) {
url, opaque := g.ResourceUrlRaw(repo_url_tags, map[string]string{":id": id})
var tags []*Tag
contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
if err == nil {
err = json.Unmarshal(contents, &tags)
}
return tags, err
}
/*
Get a list of repository commits in a project.
GET /projects/:id/repository/commits
Parameters:
id The ID of a project
refName The name of a repository branch or tag or if not given the default branch
Usage:
commits, err := gitlab.RepoCommits("your_projet_id")
if err != nil {
fmt.Println(err.Error())
}
for _, commit := range commits {
fmt.Printf("%+v\n", commit)
}
*/
func (g *Gitlab) RepoCommits(id string) ([]*Commit, error) {
url, opaque := g.ResourceUrlRaw(repo_url_commits, map[string]string{":id": id})
var commits []*Commit
contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
if err == nil {
err = json.Unmarshal(contents, &commits)
if err == nil {
for _, commit := range commits {
t, _ := time.Parse(dateLayout, commit.Created_At)
commit.CreatedAt = t
}
}
}
return commits, err
}
/*
Get a list of comments in a repository commit.
GET /projects/:id/repository/commits/:sha/comments
Parameters:
id The ID of a project
sha The sha of the commit
Usage:
comments, err := gitlab.RepoCommitComments("your_projet_id", "commit_sha")
if err != nil {
fmt.Println(err.Error())
}
for _, comment := range comments {
fmt.Printf("%+v\n", comment)
}
*/
func (g *Gitlab) RepoCommitComments(id string, sha string) ([]*CommitComment, error) {
url, opaque := g.ResourceUrlRaw(repo_url_commit_comments, map[string]string{":id": id, ":sha": sha})
var comments []*CommitComment
contents, err := g.buildAndExecRequestRaw("GET", url, opaque, nil)
if err == nil {
err = json.Unmarshal(contents, &comments)
}
return comments, err
}
/*
Create a comment in a repository commit.
POST /projects/:id/repository/commits/:sha/comments
Parameters:
id The ID of a project
sha The sha of the commit
body The body of the comment
Usage:
comment, err := gitlab.SendRepoCommitComment("your_projet_id", "commit_sha", "your comment goes here")
if err != nil {
fmt.Println(err.Error())
}
fmt.Printf("%+v\n", comment)
*/
func (g *Gitlab) SendRepoCommitComment(id string, sha string, body string) (*CommitComment, error) {
url, opaque := g.ResourceUrlRaw(repo_url_commit_comments, map[string]string{":id": id, ":sha": sha})
var comment *CommitComment
contents, err := g.buildAndExecRequestRaw("POST", url, opaque, []byte(fmt.Sprintf("note=%s", body)))
if err == nil {
err = json.Unmarshal(contents, &comment)
}
return comment, err
}
/*
Get Raw file content
*/
func (g *Gitlab) RepoRawFile(id, sha, filepath string) ([]byte, error) {
url_ := g.ResourceUrlQuery(repo_url_raw_file, map[string]string{
":id": id,
":sha": sha,
}, map[string]string{
"filepath": filepath,
})
p, err := url.Parse(url_)
if err != nil {
return nil, err
}
opaque := "//" + p.Host + p.Path
contents, err := g.buildAndExecRequestRaw("GET", url_, opaque, nil)
return contents, err
}
/*
Get Raw file content
*/
func (g *Gitlab) RepoTree(id, ref, path string) ([]*File, error) {
url := g.ResourceUrlQuery(repo_url_tree, map[string]string{
":id": id,
}, map[string]string{
"ref": ref,
"path": path,
})
var files []*File
contents, err := g.buildAndExecRequest("GET", url, nil)
if err == nil {
err = json.Unmarshal(contents, &files)
}
for _, f := range files {
if f.Type == "tree" {
f.Children, err = g.RepoTree(id, ref, path+"/"+f.Name)
}
}
return files, err
}