This repository was archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 844
Expand file tree
/
Copy pathgithub.go
More file actions
199 lines (179 loc) · 4.59 KB
/
github.go
File metadata and controls
199 lines (179 loc) · 4.59 KB
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
package core
import (
"context"
"github.com/google/go-github/github"
)
type GithubOwner struct {
Login *string
ID *int64
Type *string
Name *string
AvatarURL *string
URL *string
Company *string
Blog *string
Location *string
Email *string
Bio *string
}
type GithubRepository struct {
Owner *string
ID *int64
Name *string
FullName *string
CloneURL *string
URL *string
DefaultBranch *string
Description *string
Homepage *string
}
func GetUserOrOrganization(login string, client *github.Client) (*GithubOwner, error) {
ctx := context.Background()
user, _, err := client.Users.Get(ctx, login)
if err != nil {
return nil, err
}
return &GithubOwner{
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 GetRepositoriesFromOwner(login *string, client *github.Client) ([]*GithubRepository, error) {
var allRepos []*GithubRepository
loginVal := *login
ctx := context.Background()
opt := &github.RepositoryListOptions{
Type: "sources",
}
for {
repos, resp, err := client.Repositories.List(ctx, loginVal, opt)
if err != nil {
return allRepos, err
}
for _, repo := range repos {
if !*repo.Fork {
r := GithubRepository{
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 GetOrganizationMembers(login *string, client *github.Client) ([]*GithubOwner, error) {
var allMembers []*GithubOwner
loginVal := *login
ctx := context.Background()
opt := &github.ListMembersOptions{}
for {
members, resp, err := client.Organizations.ListMembers(ctx, loginVal, opt)
if err != nil {
return allMembers, err
}
for _, member := range members {
allMembers = append(allMembers, &GithubOwner{Login: member.Login, ID: member.ID, Type: member.Type})
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return allMembers, nil
}
func DetermineRepositoryCount(client *github.Client) (int64, error){
ctx := context.Background()
opt := &github.RepositoryListAllOptions{
Since: 0,
}
sinceValue := 0
lastValue := 0
for {
repos, _, err := client.Repositories.ListAll(ctx, opt)
if err != nil {
return -1, err
}
for _, repo := range repos {
if !*repo.Fork {
sinceValue = int(*repo.ID)
}
}
if len(repos) == 0 {
if sinceValue == lastValue {
return int64(sinceValue), nil
}
sinceValue = (lastValue + sinceValue) / 2
} else {
lastValue = sinceValue
sinceValue *= 2
}
opt = &github.RepositoryListAllOptions{
Since: int64(sinceValue),
}
}
return 0, nil
}
func GetAllRepositories(client *github.Client, start int64, end int64) ([]*GithubRepository, error) {
var allRepos []*GithubRepository
ctx := context.Background()
opt := &github.RepositoryListAllOptions{
Since: start,
}
hard_coded_branch := "master"
scraped := false
sinceValue := start
for scraped != true {
repos, _, err := client.Repositories.ListAll(ctx, opt)
if err != nil {
return allRepos, err
}
for _, repo := range repos {
if !*repo.Fork {
r := GithubRepository{
Owner: repo.Owner.Login,
ID: repo.ID,
Name: repo.Name,
FullName: repo.FullName,
CloneURL: repo.CloneURL,
URL: repo.HTMLURL,
DefaultBranch: &hard_coded_branch,
Description: repo.Description,
Homepage: repo.Homepage,
}
allRepos = append(allRepos, &r)
sinceValue = int64(*r.ID)
if sinceValue >= end {
return allRepos, nil
}
}
}
if len(repos) == 0 {
scraped = true
}
opt = &github.RepositoryListAllOptions{
Since: int64(sinceValue),
}
}
return allRepos, nil
}