/
client.go
379 lines (309 loc) · 11 KB
/
client.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package git
import (
"context"
"fmt"
"github.com/dotWicho/utilities"
"github.com/google/go-github/v32/github"
"golang.org/x/oauth2"
"io"
"net/http"
"path"
"strings"
)
// Operations interface
type Operations interface {
Commit(repoName, commitSHA string) *github.Commit
Compare(repoName, base, head string) *github.CommitsComparison
Merge(repoName, base, head, message string) *github.RepositoryCommit
Repositories(repoType, repoSort string) []*github.Repository
Repository(repoName string) *github.Repository
Branches(repoName string) []*github.Branch
Branch(repoName, branchName string) *github.Branch
Tags(repoName string) []*github.RepositoryTag
TagByName(repoName, tagName string) *github.RepositoryTag
ReferenceByBranch(repoName, branchName string) *github.Reference
ReferenceByHeads(repoName, branchName string) *github.Reference
ReferenceByTag(repoName, tagName string) *github.Reference
CreateRefs(repoName, branchName, SHARef string) *github.Reference
Tree(repoName, sourceFiles string, reference *github.Reference) *github.Tree
Users() []*github.User
User(userName string) *github.User
CreatePullRequest(repoName, srcBranch, dstBranch, subject, description string) *github.PullRequest
AssignReviewers(id int, repoName string, reviewers []string) *github.PullRequest
Download(repoName, refName, filePath string) (body io.ReadCloser, err error)
optsPullRequest(subject, srcBranch, dstBranch, description string) *github.NewPullRequest
}
// Client encapsulate in a more simply implementation the Google's go-github
type Client struct {
Organization string
AllPages bool
token string
github *github.Client
ctx context.Context
tkSource oauth2.TokenSource
tClient *http.Client
}
// New creates a github Client with a provided token
func New(token string) *Client {
client := &Client{token: token, ctx: context.Background()}
client.tkSource = oauth2.StaticTokenSource(&oauth2.Token{AccessToken: client.token})
client.tClient = oauth2.NewClient(client.ctx, client.tkSource)
client.github = github.NewClient(client.tClient)
client.AllPages = false
return client
}
// Commit returns an Object Commit based on repoName and commitSHA
func (c *Client) Commit(repoName, commitSHA string) *github.Commit {
if commit, _, err := c.github.Git.GetCommit(c.ctx, c.Organization, repoName, commitSHA); err == nil {
return commit
}
return nil
}
// Compare returns an Object Commit based on repoName and commitSHA
func (c *Client) Compare(repoName, base, head string) *github.CommitsComparison {
if commit, _, err := c.github.Repositories.CompareCommits(c.ctx, c.Organization, repoName, base, head); err == nil {
return commit
}
return nil
}
// Merge returns an Object Commit based on merge to repoName:head into repoName:base
func (c *Client) Merge(repoName, base, head, message string) *github.RepositoryCommit {
request := &github.RepositoryMergeRequest{Base: &base, Head: &head, CommitMessage: &message}
if commit, _, err := c.github.Repositories.Merge(c.ctx, c.Organization, repoName, request); err == nil {
return commit
}
return nil
}
// Repositories list all Organization repositories
func (c *Client) Repositories(repoType, repoSort string) []*github.Repository {
//
opts := &github.RepositoryListByOrgOptions{Type: repoType, Sort: repoSort, ListOptions: github.ListOptions{PerPage: 128, Page: 0}}
var repos []*github.Repository
for {
repo, response, err := c.github.Repositories.ListByOrg(c.ctx, c.Organization, opts)
if err != nil {
return nil
}
repos = append(repos, repo...)
if response.NextPage == 0 || !c.AllPages {
break
}
opts.Page = response.NextPage
}
return repos
}
// Repository return a repo selected by name
func (c *Client) Repository(repoName string) *github.Repository {
if repo, _, err := c.github.Repositories.Get(c.ctx, c.Organization, repoName); err == nil {
return repo
}
return nil
}
// Branches returns all branches for a repoName
func (c *Client) Branches(repoName string) []*github.Branch {
//
opts := &github.BranchListOptions{Protected: nil, ListOptions: github.ListOptions{PerPage: 4, Page: 0}}
var branches []*github.Branch
for {
branch, response, err := c.github.Repositories.ListBranches(c.ctx, c.Organization, repoName, opts)
if err != nil {
return nil
}
branches = append(branches, branch...)
if response.NextPage == 0 || !c.AllPages {
break
}
opts.Page = response.NextPage
}
return branches
}
// Branch returns an Object branch based on repoName and branchName
func (c *Client) Branch(repoName, branchName string) *github.Branch {
var branch *github.Branch
var err error
if branch, _, err = c.github.Repositories.GetBranch(c.ctx, c.Organization, repoName, branchName); err != nil {
return nil
}
return branch
}
// Tags returns all tags for a repoName
func (c *Client) Tags(repoName string) []*github.RepositoryTag {
//
opts := &github.ListOptions{PerPage: 12, Page: 0}
var tags []*github.RepositoryTag
for {
tag, response, err := c.github.Repositories.ListTags(c.ctx, c.Organization, repoName, opts)
if err != nil {
return nil
}
tags = append(tags, tag...)
if response.NextPage == 0 || !c.AllPages {
break
}
opts.Page = response.NextPage
}
return tags
}
// TagByName returns an Object Tag based in repoName and tagName
func (c *Client) TagByName(repoName, tagName string) *github.RepositoryTag {
//
opts := &github.ListOptions{PerPage: 128, Page: 0}
var theTag *github.RepositoryTag
for {
tag, response, err := c.github.Repositories.ListTags(c.ctx, c.Organization, repoName, opts)
if err != nil {
return nil
}
for _, testing := range tag {
if *testing.Name == tagName {
theTag = testing
break
}
}
if response.NextPage == 0 || theTag != nil {
break
}
opts.Page = response.NextPage
}
return theTag
}
// ReferenceByBranch returns an Object Reference based in repoName and branchName
func (c *Client) ReferenceByBranch(repoName, branchName string) *github.Reference {
if ref, _, err := c.github.Git.GetRef(c.ctx, c.Organization, repoName, "refs/branch/"+branchName); err == nil {
return ref
}
return nil
}
// ReferenceByHeads returns an Object Reference based in repoName and branchName from heads
func (c *Client) ReferenceByHeads(repoName, branchName string) *github.Reference {
if ref, _, err := c.github.Git.GetRef(c.ctx, c.Organization, repoName, "refs/heads/"+branchName); err == nil {
return ref
}
return nil
}
// ReferenceByTag returns an Object Reference based in repoName and tagName
func (c *Client) ReferenceByTag(repoName, tagName string) *github.Reference {
if ref, _, err := c.github.Git.GetRef(c.ctx, c.Organization, repoName, "refs/tags/"+tagName); err == nil {
return ref
}
return nil
}
// CreateRefs permits create an Object Reference based on repoName, branchName and SHAReference
func (c *Client) CreateRefs(repoName, branchName, SHARef string) *github.Reference {
newRef := &github.Reference{Ref: github.String("refs/heads/" + branchName), Object: &github.GitObject{SHA: &SHARef}}
if ref, _, err := c.github.Git.CreateRef(c.ctx, c.Organization, repoName, newRef); err == nil {
return ref
}
return nil
}
// Tree permits create an Object Tree given a fileName list
func (c *Client) Tree(repoName, sourceFiles string, reference *github.Reference) *github.Tree {
// Create a tree with what to commit.
var entries []*github.TreeEntry
// Load each file into the tree.
for _, fileArg := range strings.Split(sourceFiles, ",") {
content := utilities.ReadFile(fileArg)
if content == nil {
return nil
}
entries = append(entries, &github.TreeEntry{Path: github.String(fileArg), Type: github.String("blob"), Content: github.String(string(content)), Mode: github.String("100644")})
}
if tree, _, err := c.github.Git.CreateTree(c.ctx, c.Organization, repoName, *reference.Object.SHA, entries); err == nil {
return tree
}
return nil
}
// Users returns all Users in an Organization
func (c *Client) Users() []*github.User {
//
opts := &github.UserListOptions{Since: 0, ListOptions: github.ListOptions{PerPage: 100, Page: 0}}
var users []*github.User
for {
user, response, err := c.github.Users.ListAll(c.ctx, opts)
if err != nil {
return nil
}
users = append(users, user...)
if response.NextPage == 0 || !c.AllPages {
break
}
opts.Page = response.NextPage
}
return users
}
// User returns an Object User by its userName
func (c *Client) User(userName string) *github.User {
if len(userName) == 0 {
userName = ""
}
if user, _, err := c.github.Users.Get(c.ctx, userName); err == nil {
return user
}
return nil
}
// CreatePullRequest permits create an PullRequest into repoName using source and destiny branches
func (c *Client) CreatePullRequest(repoName, srcBranch, dstBranch, subject, description string) *github.PullRequest {
if len(repoName) == 0 || len(srcBranch) == 0 || len(subject) == 0 {
return nil
}
if strings.Contains(srcBranch, ":") && len(c.Organization) == 0 {
dstBranch = fmt.Sprintf("%s:%s", c.Organization, dstBranch)
}
newPR := c.optsPullRequest(subject, srcBranch, dstBranch, description)
if pr, _, err := c.github.PullRequests.Create(c.ctx, c.Organization, repoName, newPR); err == nil {
return pr
} else {
panic(err)
}
return nil
}
// AssignReviewers permits assign Reviewers to an one PullRequest
func (c *Client) AssignReviewers(id int, repoName string, reviewers []string) *github.PullRequest {
if len(reviewers) == 0 {
return nil
}
rr := github.ReviewersRequest{Reviewers: reviewers, TeamReviewers: nil}
if pr, _, err := c.github.PullRequests.RequestReviewers(c.ctx, c.Organization, repoName, id, rr); err == nil {
return pr
}
return nil
}
// Download returns body response of GET DownloadURL corresponding to filePath
func (c *Client) Download(repoName, refName, filePath string) (body io.ReadCloser, err error) {
if len(repoName) == 0 {
return nil, fmt.Errorf("repo cannot be null nor empty")
}
if len(filePath) == 0 {
return nil, fmt.Errorf("filePath cannot be null nor empty")
}
dirPath := path.Dir(filePath)
fileName := path.Base(filePath)
opts := &github.RepositoryContentGetOptions{Ref: refName}
dirContents := make([]*github.RepositoryContent, 0)
_, dirContents, _, err = c.github.Repositories.GetContents(c.ctx, c.Organization, repoName, dirPath, opts)
if err != nil {
return nil, err
}
for _, contents := range dirContents {
if contents.Type != nil && *contents.Type == "file" {
if contents.Name != nil && *contents.Name == fileName {
var response *http.Response
if response, err = http.Get(*contents.DownloadURL); err != nil {
return nil, err
}
return response.Body, nil
}
}
}
return nil, fmt.Errorf("filename %s not found", fileName)
}
// optsPullRequest populate a NewPullRequests with its info
func (c *Client) optsPullRequest(subject, srcBranch, dstBranch, description string) *github.NewPullRequest {
return &github.NewPullRequest{
Title: &subject,
Head: &srcBranch,
Base: &dstBranch,
Body: &description,
MaintainerCanModify: github.Bool(true),
}
}