forked from GitLab-Red-Team/gitrob
/
apiClient.go
102 lines (93 loc) · 2.38 KB
/
apiClient.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
package github
import (
"context"
"github.com/codeEmitter/gitrob/common"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
type Client struct {
apiClient *github.Client
}
func (c Client) NewClient(token string) (apiClient Client) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
c.apiClient = github.NewClient(tc)
c.apiClient.UserAgent = common.UserAgent
return c
}
func (c Client) GetUserOrOrganization(login string) (*common.Owner, error) {
ctx := context.Background()
user, _, err := c.apiClient.Users.Get(ctx, login)
if err != nil {
return nil, err
}
return &common.Owner{
Login: user.Login,
ID: user.ID,
Type: user.Type,
Name: user.Name,
AvatarURL: user.AvatarURL,
URL: user.HTMLURL,
Company: user.Company,
Blog: user.Blog,
Location: user.Location,
Email: user.Email,
Bio: user.Bio,
}, nil
}
func (c Client) GetRepositoriesFromOwner(target common.Owner) ([]*common.Repository, error) {
var allRepos []*common.Repository
ctx := context.Background()
opt := &github.RepositoryListOptions{
Type: "sources",
}
for {
repos, resp, err := c.apiClient.Repositories.List(ctx, *target.Login, opt)
if err != nil {
return allRepos, err
}
for _, repo := range repos {
if !*repo.Fork {
r := common.Repository{
Owner: repo.Owner.Login,
ID: repo.ID,
Name: repo.Name,
FullName: repo.FullName,
CloneURL: repo.CloneURL,
URL: repo.HTMLURL,
DefaultBranch: repo.DefaultBranch,
Description: repo.Description,
Homepage: repo.Homepage,
}
allRepos = append(allRepos, &r)
}
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return allRepos, nil
}
func (c Client) GetOrganizationMembers(target common.Owner) ([]*common.Owner, error) {
var allMembers []*common.Owner
ctx := context.Background()
opt := &github.ListMembersOptions{}
for {
members, resp, err := c.apiClient.Organizations.ListMembers(ctx, *target.Login, opt)
if err != nil {
return allMembers, err
}
for _, member := range members {
allMembers = append(allMembers, &common.Owner{Login: member.Login, ID: member.ID, Type: member.Type})
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return allMembers, nil
}