-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
191 lines (149 loc) · 4.25 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"sort"
"strings"
"unicode"
"github.com/google/go-github/v43/github"
"github.com/gosimple/slug"
"golang.org/x/oauth2"
)
type starredRepository struct {
owner string
name string
description string
language string
stars int
}
func toLower(v string) string {
return strings.Map(unicode.ToLower, v)
}
func getGitHubTokenFromEnv() string {
token_var := "GITHUB_TOKEN"
token := os.Getenv(token_var)
if len(token) == 0 {
panic(fmt.Sprintf("required environment variable %s is undefined", token_var))
}
return token
}
func getOAuthClient(token string) (*context.Context, *http.Client) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
return &ctx, oauth2.NewClient(ctx, ts)
}
func getAuthenticatedUser(ctx *context.Context, client *github.Client) *github.User {
user, _, err := client.Users.Get(*ctx, "")
if err != nil {
panic(err)
}
return user
}
func getAuthenticatedUserStarredRepos(ctx *context.Context, client *github.Client, login string, page int) []starredRepository {
opts := &github.ActivityListStarredOptions{
ListOptions: github.ListOptions{
Page: page,
PerPage: 50,
},
}
sts, _, err := client.Activity.ListStarred(*ctx, login, opts)
if err != nil {
panic(err)
}
s := []starredRepository{}
for _, starred := range sts {
r := starred.GetRepository()
if r == nil {
continue
}
lang := r.GetLanguage()
if len(lang) == 0 {
lang = "(NA)"
}
s = append(s, starredRepository{
owner: r.GetOwner().GetLogin(),
name: r.GetName(),
description: r.GetDescription(),
language: lang,
stars: r.GetStargazersCount(),
})
}
return s
}
func organizeReposByLanguage(starred *[]starredRepository) ([]string, map[string][]starredRepository) {
organizedRepos := make(map[string][]starredRepository)
for _, s := range *starred {
_, found := organizedRepos[s.language]
if !found {
organizedRepos[s.language] = []starredRepository{}
}
organizedRepos[s.language] = append(organizedRepos[s.language], s)
}
langs := []string{}
for k, v := range organizedRepos {
langs = append(langs, k)
sort.Slice(organizedRepos[k], func(i, j int) bool { return toLower(v[i].name) < toLower(v[j].name) })
}
sort.Slice(langs, func(i, j int) bool { return toLower(langs[i]) < toLower(langs[j]) })
return langs, organizedRepos
}
func main() {
ctx, oauthClient := getOAuthClient(getGitHubTokenFromEnv())
client := github.NewClient(oauthClient)
authenticatedUser := getAuthenticatedUser(ctx, client)
page := 1
starred := []starredRepository{}
// loop until we got all the starred repos
for {
s := getAuthenticatedUserStarredRepos(ctx, client, *authenticatedUser.Login, page)
if len(s) == 0 {
break
}
starred = append(starred, s...)
page++
}
langs, organizedRepos := organizeReposByLanguage(&starred)
// every user on GitHub MUST have a username (and therefore, a valid URL that points to it)
userLogin := *authenticatedUser.Login
userAccountUrl := fmt.Sprintf("https://github.com/%s", userLogin)
// but the display is optional and we should check it
userName := ""
if authenticatedUser.Name != nil {
userName = fmt.Sprintf(" (%s)", *authenticatedUser.Name)
}
fmt.Printf("# GitHub Stars\n\n")
fmt.Printf("Starred by [@%s%s](%s).\n\n\n", userLogin, userName, userAccountUrl)
fmt.Printf("## Summary\n\n")
langSlugMap := map[string]int{}
for _, lang := range langs {
langSlug := slug.Make(lang)
v, ok := langSlugMap[langSlug]
if ok {
v += 1
langSlugMap[langSlug] += v
langSlug = fmt.Sprintf("%s-%d", langSlug, v)
} else {
langSlugMap[langSlug] = 0
}
fmt.Printf(" - [%s](#%s)\n", lang, langSlug)
}
fmt.Printf("\n")
for _, lang := range langs {
fmt.Printf("\n## %s\n\n", lang)
fmt.Printf("[🔝 back to top](#summary)\n\n")
for _, repo := range organizedRepos[lang] {
repoAndOwner := fmt.Sprintf("%s/%s", repo.owner, repo.name)
repoUrl := fmt.Sprintf("https://github.com/%s", repoAndOwner)
fmt.Printf("### `%s`\n\n", repo.name)
fmt.Printf(" - [%s](%s) | ⭐ %d \n", repoAndOwner, repoUrl, repo.stars)
if len(repo.description) > 0 {
fmt.Printf(" - %s\n", repo.description)
}
fmt.Printf("\n")
}
}
}