-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
91 lines (70 loc) · 1.75 KB
/
api.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
package main
import (
"log"
"net/url"
"path"
"regexp"
"strconv"
"strings"
"github.com/cli/go-gh"
"github.com/cli/go-gh/pkg/api"
)
type repo struct {
Name string
Owner struct{ Login string }
URL string
Clone_URL string
Languages_URL string
Page string
}
func (r repo) cacheDir() string {
return path.Join(getCacheDir(), r.Owner.Login, r.Name)
}
func (r repo) tmpDir(tmpRoot string) string {
return path.Join(tmpRoot, r.Owner.Login, r.Name)
}
func getRepos(visibility string, affiliations []string, languages []string, regex regexp.Regexp) []repo {
client, err := gh.RESTClient(nil)
if err != nil {
log.Fatalln(err)
}
values := url.Values{}
values.Set("visibility", visibility)
values.Set("affiliation", strings.Join(affiliations, ","))
values.Set("per_page", "100")
filteredResponse := []repo{}
for i := 1; true; i++ {
response := []repo{}
values.Set("page", strconv.Itoa(i))
err = client.Get("user/repos?"+values.Encode(), &response)
if err != nil {
log.Fatalln(err)
}
if len(response) == 0 {
break
}
for _, r := range response {
if regex.MatchString(r.Name) && (languages == nil || r.containsSomeLanguage(client, languages)) {
filteredResponse = append(filteredResponse, r)
}
}
}
return filteredResponse
}
func (r repo) containsSomeLanguage(client api.RESTClient, languages []string) bool {
response := map[string]int{}
err := client.Get(r.Languages_URL, &response)
if err != nil {
log.Fatalln(err)
}
lowerLanguages := []string{}
for _, v := range languages {
lowerLanguages = append(lowerLanguages, strings.ToLower(v))
}
for language := range response {
if containsString(languages, strings.ToLower(language)) {
return true
}
}
return false
}