-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
331 lines (312 loc) · 8.49 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"runtime/debug"
"strconv"
"strings"
"time"
"github.com/cli/go-gh/v2/pkg/api"
"github.com/cli/go-gh/v2/pkg/repository"
)
type config struct {
repo string
org string
user string
topic string
page int
verbose bool
version bool
}
func parseFlags() config {
org := flag.String("org", "", "an optional GitHub organization (i.e. 'python') to scan the repositories from (100 max) ; use repository for current folder if omitted and no '-repo' nor '-user' flag")
page := flag.Int("page", 1, "page number for '-repo' and '-user' flags, 100 repositories per page")
repo := flag.String("repo", "", "an optional GitHub repository (i.e. 'python/peps') ; use repository for current folder if omitted and no '-org' nor '-user' flag")
topic := flag.String("topic", "", "an optional GitHub topic (i.e. 'testing') to filter the repositories ; ignored if no '-user' nor '-org' flag")
user := flag.String("user", "", "an optional GitHub user (i.e. 'torvalds') to scan the repositories from (100 max) ; use repository for current folder if omitted and no '-repo' nor '-org' flag")
verbose := flag.Bool("verbose", false, "verbose mode outputs several lines per repository ; non-verbose mode outputs a one-liner per repository ; default: false")
version := flag.Bool("version", false, "outputs version-related information")
flag.Parse()
return config{*repo, *org, *user, *topic, *page, *verbose, *version}
}
type owner struct{ Login string }
type repo struct {
Name string
Full_name string
Owner owner
Description string
Topics []string
Visibility string
Fork bool
}
type collaborator struct{}
type version struct {
commit string
date time.Time
dirty bool
}
func main() {
config := parseFlags()
if config.version {
version := getVersion()
dirty := ""
if version.dirty {
dirty = "(dirty)"
}
fmt.Printf("Commit %s (%s) %s\n", version.commit, version.date, dirty)
} else if len(config.org) > 0 || len(config.user) > 0 {
repos, error := getRepos(config)
if error != nil {
fmt.Print(error)
os.Exit(2)
}
for _, repo := range repos {
repoMessage, repo, validRepo := scanRepo(config, repo.Full_name)
if validRepo {
fmt.Print(repoMessage)
collaboratorsMessage := scanCollaborators(config, repo.Full_name)
fmt.Print(collaboratorsMessage)
if strings.Compare(repo.Visibility, "public") == 0 {
communityScoreMessage := scanCommunityScore(config, repo.Full_name)
fmt.Print(communityScoreMessage)
}
}
fmt.Println()
}
} else {
repoWithOrg, error := getRepo(config)
if error != nil {
fmt.Print(error)
if strings.Contains(error.Error(), "none of the git remotes configured for this repository point to a known GitHub host") {
print("If current folder is related to a GitHub repository, please check 'gh auth status' and 'gh config list'.")
}
os.Exit(1)
}
repoMessage, repo, validRepo := scanRepo(config, repoWithOrg)
if validRepo {
fmt.Print(repoMessage)
collaboratorsMessage := scanCollaborators(config, repoWithOrg)
fmt.Print(collaboratorsMessage)
if !repo.Fork && strings.Compare(repo.Visibility, "public") == 0 {
communityScoreMessage := scanCommunityScore(config, repoWithOrg)
fmt.Print(communityScoreMessage)
}
fmt.Println()
}
}
}
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
func getRepos(config config) ([]repo, error) {
if len(config.org) == 0 && len(config.user) == 0 {
return []repo{}, nil
}
client, err := api.DefaultRESTClient()
if err != nil {
fmt.Print(err)
return []repo{}, err
}
if len(config.org) > 0 {
// https://docs.github.com/en/rest/reference/repos#list-organization-repositories
repos := []repo{}
err = client.Get(
"orgs/"+config.org+"/repos?sort=full_name&per_page=100&page="+strconv.Itoa(config.page),
&repos)
return reposWithTopic(repos, config.topic), err
} else {
// https://docs.github.com/en/rest/reference/repos#list-repositories-for-a-user
repos := []repo{}
err = client.Get(
"users/"+config.user+"/repos?sort=full_name&per_page=100&page="+strconv.Itoa(config.page),
&repos)
return reposWithTopic(repos, config.topic), err
}
}
func reposWithTopic(repos []repo, topic string) []repo {
if len(topic) > 0 {
filtered := []repo{}
for _, repo := range repos {
if contains(repo.Topics, topic) {
filtered = append(filtered, repo)
}
}
return filtered
}
return repos
}
func getRepo(config config) (string, error) {
if len(config.repo) > 1 {
return config.repo, nil
}
if config.verbose {
fmt.Printf("(current repo)\n")
}
currentRepo, error := repository.Current()
if error != nil {
return "", error
}
return currentRepo.Owner + "/" + currentRepo.Name, nil
}
func scanRepo(config config, repoWithOrg string) (message string, repository repo, validRepo bool) {
// https://docs.github.com/en/rest/reference/repos#get-a-repository-readme
readme := struct {
Name string
}{}
client, err := api.DefaultRESTClient()
if err != nil {
fmt.Print(err)
return
}
err = client.Get(
"repos/"+repoWithOrg+"/readme",
&readme)
if config.verbose {
message += repoWithOrg + " has: "
}
if !config.verbose && (len(config.repo) > 1 || len(config.user) > 1 || len(config.org) > 1) {
message += repoWithOrg + ": "
}
if len(readme.Name) > 0 {
if config.verbose {
message += "\n - a README ☑️"
} else {
message += "README ☑️, "
}
} else if strings.HasPrefix(err.Error(), "HTTP 404: Not Found") {
if config.verbose {
message += "\n - no README 😇"
} else {
message += "no README 😇, "
}
} else {
fmt.Print(err)
}
repo := struct {
Name string
Full_name string
Owner owner
Description string
Topics []string
Visibility string
Fork bool
}{}
errRepo := client.Get(
"repos/"+repoWithOrg,
&repo)
if errRepo != nil {
fmt.Print(errRepo)
return
}
if len(repo.Description) > 0 {
if config.verbose {
message += "\n - a description ☑️"
} else {
message += "description ☑️, "
}
} else {
if config.verbose {
message += "\n - no description 😇"
} else {
message += "no description 😇, "
}
}
if len(repo.Topics) > 0 {
if config.verbose {
message += "\n - topics ☑️"
} else {
message += "topics ☑️, "
}
} else {
if config.verbose {
message += "\n - no topics 😇"
} else {
message += "no topics 😇, "
}
}
return message, repo, true
}
func scanCollaborators(config config, repoWithOrg string) string {
// https://docs.github.com/en/rest/reference/collaborators#list-repository-collaborators
client, err := api.DefaultRESTClient()
if err != nil {
fmt.Print(err)
return ""
}
collaborators := []collaborator{}
err = client.Get(
"repos/"+repoWithOrg+"/collaborators",
&collaborators)
message := ""
if err != nil && len(err.Error()) > 0 {
if strings.HasPrefix(err.Error(), "HTTP 403") {
// 🤫
} else {
fmt.Print(err)
}
} else if len(collaborators) <= 1 {
if config.verbose {
message += fmt.Sprintf("\n - %d collaborator 👤", len(collaborators))
} else {
message += fmt.Sprintf("%d collaborator 👤, ", len(collaborators))
}
} else {
if config.verbose {
message += fmt.Sprintf("\n - %d collaborators 👥", len(collaborators))
} else {
message += fmt.Sprintf("%d collaborators 👥, ", len(collaborators))
}
}
return message
}
func scanCommunityScore(config config, repoWithOrg string) string {
// https://docs.github.com/en/rest/reference/metrics#get-community-profile-metrics
communityProfile := struct {
Health_percentage int64
}{}
client, err := api.DefaultRESTClient()
if err != nil {
fmt.Print(err)
return ""
}
err = client.Get(
"repos/"+repoWithOrg+"/community/profile",
&communityProfile)
if err != nil {
fmt.Print(err)
return ""
}
message := ""
if config.verbose {
message += fmt.Sprintf("\n - a community profile score of %d 💯", communityProfile.Health_percentage)
} else {
message += fmt.Sprintf("community profile score: %d 💯", communityProfile.Health_percentage)
}
return message
}
func getVersion() version {
info, ok := debug.ReadBuildInfo()
if !ok {
panic("Cannot read build info")
}
revision := "?"
dirtyBuild := false
date := time.Now()
for _, kv := range info.Settings {
switch kv.Key {
case "vcs.revision":
revision = kv.Value
case "vcs.time":
date, _ = time.Parse(time.RFC3339, kv.Value)
case "vcs.modified":
dirtyBuild = kv.Value == "true"
}
}
return version{revision, date, dirtyBuild}
}