-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-top-languages.go
More file actions
239 lines (200 loc) · 6.37 KB
/
github-top-languages.go
File metadata and controls
239 lines (200 loc) · 6.37 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
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
package main
import (
"flag"
"log"
"os"
"strconv"
"strings"
"github.com/gouef/githubtoplanguages/generators"
"github.com/gouef/githubtoplanguages/requests"
"github.com/gouef/utils"
"github.com/joho/godotenv"
)
func main() {
godotenv.Load()
tokenFlag := flag.String("gh-token", "", "Github API token")
userFlag := flag.String("user", "", "Github username")
limitFlag := flag.Int("limit", 12, "Limit of languages")
outputFlag := flag.String("output", "", "Name of file (without .svg")
ignoredOrgsFlag := flag.String("ignore-orgs", "", "Comma-separated list of ignored organizations")
ignoredReposFlag := flag.String("ignore-repos", "", "Comma-separated list of ignored repositories")
ignoredLangsFlag := flag.String("ignore-langs", "", "Comma-separated list of ignored languages")
withForksFlag := flag.String("with-forks", "false", "Include forked repositories in the analysis (true/false)")
withStreakFlag := flag.String("with-streak", "true", "Include streak statistics of your github account (true/false)")
showStatsFlag := flag.String("show-stats", "false", "Show statistics section")
statsFeaturesFlag := flag.String("stats", "", "Comma-separated list of stats features to include")
flag.Parse()
token := *tokenFlag
if token == "" {
token = os.Getenv("GITHUB_TOKEN")
}
if token == "" {
log.Fatal("GITHUB_TOKEN is not set")
}
user := *userFlag
if user == "" {
user = os.Getenv("GITHUB_USERNAME")
}
if user == "" {
log.Fatal("GITHUB_USERNAME is not set")
}
withForks, _ := strconv.ParseBool(getPriorityValue(*withForksFlag, "GITHUB_WITH_FORKS"))
withStreak, _ := strconv.ParseBool(getPriorityValue(*withStreakFlag, "GITHUB_WITH_STREAK"))
showStats, _ := strconv.ParseBool(getPriorityValue(*showStatsFlag, "GITHUB_SHOW_STATS"))
statsFeatures := getPriorityValue(*statsFeaturesFlag, "GITHUB_STATS_FEATURES")
output := *outputFlag
if output == "" {
output = "toplanguages"
}
ignoredOrganizations := explode(",", getPriorityValue(*ignoredOrgsFlag, "GITHUB_IGNORE_ORGANIZATIONS"))
ignoredRepositories := explode(",", getPriorityValue(*ignoredReposFlag, "GITHUB_IGNORE_REPOS"))
ignoredLanguages := explode(",", getPriorityValue(*ignoredLangsFlag, "GITHUB_IGNORE_LANGS"))
ignored := append(ignoredOrganizations, ignoredRepositories...)
limitEnv := os.Getenv("GITHUB_TOP_LIMIT")
limit := *limitFlag
if limitEnv != "" {
limit, _ = strconv.Atoi(limitEnv)
}
var ignoredSize int
// Organizations
result, err := requests.FetchOrganizations(user, token, ignored...)
if err != nil {
log.Fatalf("Failed to fetch organizations: %v", err)
}
var repositories []string
languages := make(map[string]int)
colors := make(map[string]string)
log.Println("Processing organizations..." + strconv.Itoa(len(result.Repositories)))
for _, repoList := range result.Repositories {
repositories = append(repositories, repoList.Name)
for _, lang := range repoList.Languages {
if isImageLanguage(lang.Name) {
continue
}
if shouldIgnoreLanguage(lang.Name, ignoredLanguages) {
ignoredSize += lang.Size
continue
}
languages[lang.Name] += lang.Size
colors[lang.Name] = lang.Color
}
}
// User
result, err = requests.FetchUser(user, token, false, ignored...)
if err != nil {
log.Fatalf("Failed to fetch user: %v", err)
}
for _, repoList := range result.Repositories {
if utils.InArray(repoList.Name, repositories) {
continue
}
repositories = append(repositories, repoList.Name)
for _, lang := range repoList.Languages {
if isImageLanguage(lang.Name) {
continue
}
if shouldIgnoreLanguage(lang.Name, ignoredLanguages) {
ignoredSize += lang.Size
continue
}
languages[lang.Name] += lang.Size
colors[lang.Name] = lang.Color
}
}
// Forks
if withForks {
result, err = requests.FetchUser(user, token, true, ignored...)
if err != nil {
log.Fatalf("Failed to fetch user forks: %v", err)
}
for _, repoList := range result.Repositories {
for _, lang := range repoList.Languages {
if isImageLanguage(lang.Name) {
continue
}
if shouldIgnoreLanguage(lang.Name, ignoredLanguages) {
ignoredSize += lang.Size
continue
}
languages[lang.Name] += lang.Size
colors[lang.Name] = lang.Color
}
}
}
log.Println("Downloading latest language definitions from GitHub Linguist...")
extensionMap, err := requests.LoadLinguistLanguages()
if err != nil {
log.Fatalf("Failed to load dynamic languages: %v", err)
}
//PRs
prResult, err := requests.FetchUserPRLanguages(token, extensionMap, ignored...)
if err != nil {
log.Fatalf("Failed to fetch PR languages: %v", err)
}
for _, repoList := range prResult.Repositories {
for _, lang := range repoList.Languages {
if isImageLanguage(lang.Name) {
continue
}
if shouldIgnoreLanguage(lang.Name, ignoredLanguages) {
ignoredSize += lang.Size
continue
}
languages[lang.Name] += lang.Size
colors[lang.Name] = lang.Color
}
}
resultLanguages := sortLanguages(languages, limit, ignoredSize)
for _, lang := range resultLanguages {
if lang.Name == "Others" {
lang.Color = "#d4d4d4"
continue
}
lang.Color = colors[lang.Name]
}
var streakStats *requests.StreakStats = nil
if withStreak {
streakStats, err = requests.FetchContributionStats(user, token)
if err != nil {
log.Printf("Warning: Failed to fetch contribution streaks: %v", err)
streakStats = &requests.StreakStats{}
}
}
var globalStats *requests.GlobalStats = nil
if showStats {
globalStats, err = requests.FetchGlobalStats(user, token)
if err != nil {
log.Printf("Warning: Failed to fetch global stats: %v", err)
globalStats = &requests.GlobalStats{}
}
}
var generatorLanguages []*generators.Language
for _, lang := range resultLanguages {
generatorLanguages = append(generatorLanguages, &generators.Language{
Name: lang.Name,
Color: lang.Color,
Percentage: lang.Percentage,
})
}
generators.GenerateCard(generatorLanguages, streakStats, globalStats, withStreak, showStats, statsFeatures, output)
}
func getPriorityValue(flagValue, envKey string) string {
if flagValue != "" {
return flagValue
}
return os.Getenv(envKey)
}
func explode(delimiter, str string) []string {
if str == "" {
return []string{}
}
parts := strings.Split(str, delimiter)
var cleaned []string
for _, part := range parts {
trimmed := strings.TrimSpace(part)
if trimmed != "" {
cleaned = append(cleaned, trimmed)
}
}
return cleaned
}