-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
310 lines (260 loc) · 8.77 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
package main
import (
"encoding/base64"
"flag"
"fmt"
"io"
"log"
"os"
"sort"
"time"
"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/opts"
)
const (
dateFormat = "2006-01-02"
yyyyMM = "2006-01"
)
var TEST = false
func buildLineChart(providers *RepositoryProviders, since time.Time) {
var githubResponse *GithubAPIGQLResponse
if TEST {
githubResponse = readGitHubTestFile()
} else {
githubResponse = providers.github.gqlRequest()
}
var allStats ContributionStats
var gls *GitlabStats
var gitlabContributions map[string]int
if TEST {
gitlabContributions = readGitlabTestFile()
gls = &GitlabStats{}
} else {
gitlabContributions = providers.gitlab.GetAllContributions()
gls = providers.gitlab.GetOtherStats(since)
}
allStats = MergeContributionStats(githubResponse, gls)
allStats.GithubTotalContributions = githubResponse.Data.User.ContributionsCollection.ContributionCalendar.TotalContributions
allStats.GitlabTotalContributions = gls.TotalContributions
// merge gitlab and github contributions
githubContributions := getGitHubMonthlyContributions(githubResponse)
// add github map
var combinedContributions = make(map[string]int)
for k, v := range githubContributions {
combinedContributions[k] += v
}
// add gitlab map
for k, v := range gitlabContributions {
combinedContributions[k] += v
}
xAxis := getXAxis(combinedContributions)
toLine(xAxis, true,
GitContribution{Name: "Gitlab", Color: "#FC6D26", Type: "solid", Data: gitlabContributions},
GitContribution{Name: "Github", Color: "#00000", Type: "solid", Data: githubContributions},
)
allStats.Print()
}
// TODO Refactor getXAxis
func getXAxis(contributions map[string]int) (xAxis []string) {
// sort map by keys
keys := make([]string, 0, len(contributions))
for k := range contributions {
keys = append(keys, k)
}
sort.Strings(keys)
previousDate, _ := time.Parse(dateFormat, keys[0])
for i := range keys {
currentDate, _ := time.Parse(dateFormat, keys[i])
if previousDate.Month() == currentDate.Month() {
continue
}
// add missing months. Use previous date and increment since the current date is found
if currentDate.Month() != previousDate.AddDate(0, 1, 0).Month() {
incrementedDate := previousDate
for i := 0; currentDate.Month() != incrementedDate.AddDate(0, 1, 0).Month(); i++ {
incrementedDate = previousDate.AddDate(0, i, 0)
xAxis = append(xAxis, incrementedDate.Format(yyyyMM))
}
previousDate = currentDate
continue
}
// if the month is different
xAxis = append(xAxis, previousDate.Format(yyyyMM))
previousDate = currentDate
}
xAxis = append(xAxis, previousDate.Format(yyyyMM))
return xAxis
}
type GitContribution struct {
Name string
// Color of line in hexadecimal
Color string
// Type of line,options: "solid", "dashed", "dotted". default "solid"
Type string
Data map[string]int
}
func toLine(xAxis []string, combine bool, contributions ...GitContribution) {
line := charts.NewLine()
line.SetGlobalOptions(
charts.WithInitializationOpts(opts.Initialization{
PageTitle: "Contribution graph",
// Theme: "", // types.ThemePurplePassion
BackgroundColor: "transparent",
}),
)
line.SetXAxis(xAxis)
// initialize slice based on xAxis
contributionData := make([]opts.LineData, 0)
for i := range xAxis {
contributionData = append(contributionData,
opts.LineData{
Name: xAxis[i], // TODO: check if is a valid date?
Value: nil,
Symbol: "none",
},
)
}
// add up contributions to append another that holds a combination of all of them
if combine {
var combinedContributions = make(map[string]int)
for i := range contributions {
for k, v := range contributions[i].Data {
combinedContributions[k] += v
}
}
// add combined contribution
contributions = append(contributions,
GitContribution{
Name: "Combined",
Color: "#02CB84",
Type: "dotted",
Data: combinedContributions,
})
}
for i := range contributions {
line.AddSeries(
contributions[i].Name,
getContributionData(xAxis, contributions[i].Data),
charts.WithLineStyleOpts(opts.LineStyle{
Color: contributions[i].Color,
Type: contributions[i].Type,
}),
).SetSeriesOptions(
charts.WithLineChartOpts(opts.LineChart{Smooth: true}),
// charts.WithAreaStyleOpts(opts.AreaStyle{Opacity: 1}),
)
log.Prefix()
}
f, err := os.Create("out/charts/lines.html")
if err != nil {
panic(err)
}
defer f.Close()
line.Render(io.MultiWriter(f))
}
func getContributionData(xAxis []string, contribution map[string]int) []opts.LineData {
// initialize slice based on xAxis
contributionData := make([]opts.LineData, 0)
for i := range xAxis {
contributionData = append(contributionData,
opts.LineData{
Name: xAxis[i], // TODO: check if is a valid date?
Value: 0,
Symbol: "none",
},
)
}
var contributionCounter int
daysContributions := contribution
// sort map by keys
keys := make([]string, 0, len(daysContributions))
for k := range daysContributions {
keys = append(keys, k)
}
sort.Strings(keys)
previousDate, _ := time.Parse(dateFormat, keys[0])
for _, k := range keys {
currentDate, _ := time.Parse(dateFormat, k)
if previousDate.Month() != currentDate.Month() {
for i := range contributionData {
if contributionData[i].Name == previousDate.Format(yyyyMM) {
contributionData[i].Value = contributionCounter
continue
}
}
// reset
contributionCounter = 0
previousDate = currentDate
}
contributionCounter += daysContributions[k]
}
for i := range contributionData {
if contributionData[i].Name == previousDate.Format(yyyyMM) {
contributionData[i].Value = contributionCounter
continue
}
}
return contributionData
}
type ContributionStats struct {
GithubTotalCommitContributions int
GithubTotalPullRequestContributions int
GithubTotalPullRequestReviewContributions int
GithubTotalRepositoriesWithContributedCommits int
GithubTotalContributions int
GitlabTotalPushedCommits int
GitlabSuccessfulMergeRequests int
GitlabComments int
GitlabTotalContributions int
}
func MergeContributionStats(gh *GithubAPIGQLResponse, gl *GitlabStats) ContributionStats {
return ContributionStats{
// GitHub
GithubTotalCommitContributions: gh.Data.User.ContributionsCollection.TotalCommitContributions,
GithubTotalPullRequestContributions: gh.Data.User.ContributionsCollection.TotalPullRequestContributions,
GithubTotalPullRequestReviewContributions: gh.Data.User.ContributionsCollection.TotalPullRequestReviewContributions,
GithubTotalRepositoriesWithContributedCommits: gh.Data.User.ContributionsCollection.TotalRepositoriesWithContributedCommits,
// GitLab
GitlabTotalPushedCommits: gl.TotalPushedCommits,
GitlabSuccessfulMergeRequests: gl.SuccessfulMergeRequests,
GitlabComments: gl.Comments,
}
}
func (s *ContributionStats) Print() {
var msg string
msg += "\n--- GitHub ---"
msg += "\nContributions on GitHub: " + fmt.Sprint(s.GithubTotalContributions)
msg += "\n--- GitHub: Other stats ---"
msg += "\nTotal merge requests: " + fmt.Sprint(s.GithubTotalPullRequestContributions)
msg += "\nTotal commit contributions: " + fmt.Sprint(s.GithubTotalCommitContributions)
msg += "\nTotal merge request reviews: " + fmt.Sprint(s.GithubTotalPullRequestReviewContributions)
msg += "\nTotal repositories contributed: " + fmt.Sprint(s.GithubTotalRepositoriesWithContributedCommits)
msg += "\n--- Gitlab ---"
msg += "\nContributions on Gitlab: " + fmt.Sprint(s.GitlabTotalContributions)
msg += "\n--- Gitlab: Other stats ---"
msg += "\nTotal merge requests: " + fmt.Sprint(s.GitlabSuccessfulMergeRequests)
msg += "\nTotal submitted comments: " + fmt.Sprint(s.GitlabComments)
msg += "\nTotal commit contributions: " + fmt.Sprint(s.GitlabTotalPushedCommits)
log.Print(msg)
}
type RepositoryProviders struct {
github *GitHub
gitlab *GitLab
}
func main() {
test := flag.Bool("test", false, "Mocked responses")
oneYearAgo := time.Now().AddDate(-1, 0, 0).Format(dateFormat)
since := flag.String("since", oneYearAgo, "Since date with format 2006-12-21. Defaults to one year ago from now.")
gitlabUser := flag.String("gitlab.user", "", "GitLab user")
gitlabToken := flag.String("gitlab.token", "", "GitLab auth token")
githubToken := flag.String("github.token", "", "GitHub auth token")
flag.Parse()
TEST = *test
log.Printf("GIT-STATS started: test=%t, since %s", TEST, *since)
var providers RepositoryProviders
providers.github = NewGitHub(base64.StdEncoding.EncodeToString([]byte(*githubToken)))
providers.gitlab = NewGitlab(base64.StdEncoding.EncodeToString([]byte(*gitlabToken)))
providers.gitlab.user = *gitlabUser
sinceTime, _ := time.Parse(dateFormat, *since)
buildLineChart(&providers, sinceTime)
}