-
Notifications
You must be signed in to change notification settings - Fork 2
/
github.go
216 lines (198 loc) · 5.92 KB
/
github.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
package main
import (
"cmp"
"context"
"fmt"
"log"
"os"
"slices"
"github.com/google/go-github/v58/github"
"github.com/gregjones/httpcache"
"github.com/sourcegraph/conc"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
const (
// repositoriesCount is const for allocation memory to store repositories
repositoriesCount = 1000
// langReposCount is const for allocation memory to store langRepo
langReposCount = 100
)
// GitHub struct for requests
type GitHub struct {
client *github.Client
}
// Repository struct for storing parameters from Repository
type Repository struct {
FullName string
URL string
Language string
Description string
}
// New creates new GitHub client
func New(token string) (client *GitHub) {
gh := github.NewClient(
httpcache.NewMemoryCacheTransport().Client(),
)
if token != "" {
gh = gh.WithAuthToken(token)
}
return &GitHub{client: gh}
}
// GetRepositories getting repositories from GitHub
func (g *GitHub) GetRepositories(ctx context.Context) (langRepoMap map[string][]Repository, repositories []Repository) {
repositories = make([]Repository, 0, repositoriesCount)
langRepoMap = make(map[string][]Repository, langReposCount)
_, resp, err := g.client.Activity.ListStarred(ctx, username, nil)
if err != nil {
log.Fatalln("Error: cannot fetch starred:", err)
}
ch := make(chan []*github.StarredRepository, 1)
go func() {
wg := conc.WaitGroup{}
for i := 1; i <= resp.LastPage; i++ {
i := i
wg.Go(func() {
opt := &github.ActivityListStarredOptions{
ListOptions: github.ListOptions{
Page: i,
},
}
repos, _, err := g.client.Activity.ListStarred(ctx, username, opt)
if err != nil {
log.Fatalln("Error: cannot fetch starred:", err)
}
ch <- repos
})
}
wg.Wait()
close(ch)
}()
for repos := range ch {
for _, r := range repos {
repo := Repository{
FullName: r.Repository.GetFullName(),
URL: r.Repository.GetHTMLURL(),
Language: r.Repository.GetLanguage(),
Description: r.Repository.GetDescription(),
}
repositories = append(repositories, repo)
lang := "Others"
if repo.Language != "" {
lang = capitalize(repo.Language)
}
if _, ok := langRepoMap[lang]; !ok {
langRepoMap[lang] = make([]Repository, 0, langReposCount)
}
langRepoMap[lang] = append(langRepoMap[lang], repo)
}
}
if len(repositories) == 0 {
return nil, repositories
}
slices.SortFunc(repositories, func(a, b Repository) int {
return cmp.Compare(a.FullName, b.FullName)
})
for _, repositories := range langRepoMap {
slices.SortFunc(repositories, func(a, b Repository) int {
return cmp.Compare(a.FullName, b.FullName)
})
}
return langRepoMap, repositories
}
// UpdateReadmeFile updates README file
func (g *GitHub) UpdateReadmeFile(ctx context.Context) {
if _, resp, err := g.client.Repositories.Get(ctx, username, repository); err != nil || resp.StatusCode != 200 {
fmt.Printf("Error: check repository (%s) is exist : %v\n", repository, err)
os.Exit(2)
}
readmeFile, _, resp, err := g.client.Repositories.GetContents(ctx, username, repository, "README.md", &github.RepositoryContentGetOptions{})
// if file is not exist, just create it
if err != nil || resp.StatusCode != 200 {
if _, _, err := g.client.Repositories.CreateFile(ctx, username, repository, "README.md", &github.RepositoryContentFileOptions{
Message: &message,
Content: []byte(buffer.String()),
}); err != nil {
fmt.Printf("Error: cannot create file: %v\n", err)
os.Exit(3)
}
return
}
// if file is exist, update it
if _, _, err = g.client.Repositories.UpdateFile(ctx, username, repository, "README.md", &github.RepositoryContentFileOptions{
Message: &message,
Content: []byte(buffer.String()),
SHA: readmeFile.SHA,
}); err != nil {
fmt.Printf("Error: cannot update file: %v\n", err)
os.Exit(3)
}
}
var pl = map[string]string{
"abcl": "ABCL",
"alf": "ALF",
"algol": "ALGOL",
"apl": "APL",
"applescript": "AppleScript",
"basic": "BASIC",
"beanshell": "BeanShell",
"beta": "BETA",
"chuck": "ChucK",
"cleo": "CLEO",
"clist": "CLIST",
"cobol": "COBOL",
"coldfusion": "ColdFusion",
"css": "CSS",
"dasl": "DASL",
"f-script": "F-Script",
"foxpro": "FoxPro",
"html": "HTML",
"hypertalk": "HyperTalk",
"ici": "ICI",
"io": "IO",
"jass": "JASS",
"javascript": "JavaScript",
"jovial": "JOVIAL",
"latex": "LaTeX",
"lua": "LUA",
"matlab": "MATLAB",
"ml": "ML",
"moo": "MOO",
"object-z": "Object-Z",
"objective-c": "Objective-C",
"opal": "OPAL",
"ops5": "OPS5",
"pcastl": "PCASTL",
"php": "PHP",
"pl/c": "PL/C",
"pl/i": "PL/I",
"powershell": "PowerShell",
"rebol": "REBOL",
"rexx": "REXX",
"roop": "ROOP",
"rpg": "RPG",
"s-lang": "S-Lang",
"salsa": "SALSA",
"sass": "SASS",
"scss": "SCSS",
"sgml": "SGML",
"small": "SMALL",
"sr": "SR",
"tex": "TeX",
"typescript": "TypeScript",
"vbscript": "VBScript",
"viml": "VimL",
"visual foxpro": "Visual FoxPro",
"wikitext": "WikiText",
"windows powershell": "Windows PowerShell",
"xhtml": "XHTML",
"xl": "XL",
"xml": "XML",
"xotcl": "XOTcl",
}
func capitalize(in string) string {
if lang, ok := pl[cases.Lower(language.English).String(in)]; ok {
return lang
}
return cases.Title(language.English).String(in)
}