-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathskyline.go
More file actions
210 lines (182 loc) · 6.8 KB
/
Copy pathskyline.go
File metadata and controls
210 lines (182 loc) · 6.8 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
// Package skyline provides the entry point for the GitHub Skyline Generator.
// It generates a 3D model of GitHub contributions in STL format.
package skyline
import (
"fmt"
"strings"
"time"
"github.com/github/gh-skyline/internal/ascii"
"github.com/github/gh-skyline/internal/errors"
"github.com/github/gh-skyline/internal/github"
"github.com/github/gh-skyline/internal/logger"
"github.com/github/gh-skyline/internal/stl"
"github.com/github/gh-skyline/internal/types"
"github.com/github/gh-skyline/internal/utils"
)
// GitHubClientInterface defines the methods for interacting with GitHub API
type GitHubClientInterface interface {
GetAuthenticatedUser() (string, error)
GetUserJoinYear(username string) (int, error)
FetchContributions(username string, year int) (*types.ContributionsResponse, error)
FetchOrgContributions(username string, org string, year int) (*types.OrgContributionsResponse, error)
}
// GenerateSkyline creates a 3D model with ASCII art preview of GitHub contributions for the specified year range, or "full lifetime" of the user
func GenerateSkyline(startYear, endYear int, targetUser string, org string, full bool, output string, artOnly bool) error {
log := logger.GetLogger()
client, err := github.InitializeGitHubClient()
if err != nil {
return errors.New(errors.NetworkError, "failed to initialize GitHub client", err)
}
if targetUser == "" {
if err := log.Debug("No target user specified, using authenticated user"); err != nil {
return err
}
username, err := client.GetAuthenticatedUser()
if err != nil {
return errors.New(errors.NetworkError, "failed to get authenticated user", err)
}
targetUser = username
}
if full {
joinYear, err := client.GetUserJoinYear(targetUser)
if err != nil {
return errors.New(errors.NetworkError, "failed to get user join year", err)
}
startYear = joinYear
endYear = time.Now().Year()
}
var allContributions [][][]types.ContributionDay
for year := startYear; year <= endYear; year++ {
var contributions [][]types.ContributionDay
var err error
if org != "" {
contributions, err = fetchOrgContributionData(client, targetUser, org, year)
} else {
contributions, err = fetchContributionData(client, targetUser, year)
}
if err != nil {
return err
}
allContributions = append(allContributions, contributions)
// Generate ASCII art for each year
asciiArt, err := ascii.GenerateASCII(contributions, targetUser, year, (year == startYear) && !artOnly, !artOnly)
if err != nil {
if warnErr := log.Warning("Failed to generate ASCII preview: %v", err); warnErr != nil {
return warnErr
}
} else {
if year == startYear {
// For first year, show full ASCII art including header
fmt.Println(asciiArt)
} else {
// For subsequent years, skip the header
lines := strings.Split(asciiArt, "\n")
gridStart := 0
for i, line := range lines {
containsEmptyBlock := strings.Contains(line, string(ascii.EmptyBlock))
containsFoundationLow := strings.Contains(line, string(ascii.FoundationLow))
isNotOnlyEmptyBlocks := strings.Trim(line, string(ascii.EmptyBlock)) != ""
if (containsEmptyBlock || containsFoundationLow) && isNotOnlyEmptyBlocks {
gridStart = i
break
}
}
// Print just the grid and user info
fmt.Println(strings.Join(lines[gridStart:], "\n"))
}
}
}
if !artOnly {
// Generate filename
outputPath := utils.GenerateOutputFilename(targetUser, startYear, endYear, output)
// Generate the STL file
if len(allContributions) == 1 {
return stl.GenerateSTL(allContributions[0], outputPath, targetUser, startYear)
}
return stl.GenerateSTLRange(allContributions, outputPath, targetUser, startYear, endYear)
}
return nil
}
// fetchContributionData retrieves and formats the contribution data for the specified year.
func fetchContributionData(client *github.Client, username string, year int) ([][]types.ContributionDay, error) {
response, err := client.FetchContributions(username, year)
if err != nil {
return nil, fmt.Errorf("failed to fetch contributions: %w", err)
}
weeks := response.User.ContributionsCollection.ContributionCalendar.Weeks
contributionGrid := make([][]types.ContributionDay, len(weeks))
for i, week := range weeks {
contributionGrid[i] = week.ContributionDays
}
return contributionGrid, nil
}
// fetchOrgContributionData retrieves contributions filtered to a specific organization.
func fetchOrgContributionData(client *github.Client, username string, org string, year int) ([][]types.ContributionDay, error) {
response, err := client.FetchOrgContributions(username, org, year)
if err != nil {
return nil, fmt.Errorf("failed to fetch org contributions: %w", err)
}
dailyCounts := make(map[string]int)
for _, repo := range response.User.ContributionsCollection.CommitContributionsByRepository {
if strings.EqualFold(repo.Repository.Owner.Login, org) {
for _, node := range repo.Contributions.Nodes {
date := node.OccurredAt[:10]
dailyCounts[date]++
}
}
}
for _, repo := range response.User.ContributionsCollection.IssueContributionsByRepository {
if strings.EqualFold(repo.Repository.Owner.Login, org) {
for _, node := range repo.Contributions.Nodes {
date := node.OccurredAt[:10]
dailyCounts[date]++
}
}
}
for _, repo := range response.User.ContributionsCollection.PullRequestContributionsByRepository {
if strings.EqualFold(repo.Repository.Owner.Login, org) {
for _, node := range repo.Contributions.Nodes {
date := node.OccurredAt[:10]
dailyCounts[date]++
}
}
}
for _, repo := range response.User.ContributionsCollection.PullRequestReviewContributionsByRepository {
if strings.EqualFold(repo.Repository.Owner.Login, org) {
for _, node := range repo.Contributions.Nodes {
date := node.OccurredAt[:10]
dailyCounts[date]++
}
}
}
return buildContributionGrid(year, dailyCounts), nil
}
func buildContributionGrid(year int, dailyCounts map[string]int) [][]types.ContributionDay {
startDate := time.Date(year, 1, 1, 0, 0, 0, 0, time.UTC)
endDate := time.Date(year, 12, 31, 0, 0, 0, 0, time.UTC)
for startDate.Weekday() != time.Sunday {
startDate = startDate.AddDate(0, 0, -1)
}
var weeks [][]types.ContributionDay
var currentWeek []types.ContributionDay
for d := startDate; !d.After(endDate) || len(currentWeek) > 0; d = d.AddDate(0, 0, 1) {
dateStr := d.Format("2006-01-02")
count := dailyCounts[dateStr]
if d.Year() == year || (d.Year() == year-1 && d.After(startDate.AddDate(0, 0, -1))) {
currentWeek = append(currentWeek, types.ContributionDay{
Date: dateStr,
ContributionCount: count,
})
}
if d.Weekday() == time.Saturday || d.Equal(endDate) {
if len(currentWeek) > 0 {
weeks = append(weeks, currentWeek)
currentWeek = nil
}
}
if d.After(endDate) && d.Weekday() == time.Saturday {
break
}
}
return weeks
}