-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo-stats.go
165 lines (133 loc) · 4.37 KB
/
repo-stats.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
package main
import (
"fmt"
"strings"
"time"
)
// Repository contains the properties for the git repository.
type Repository struct {
Name string
Visibility string
Size int
Statistics []Statistic
TotalStats Totals
}
// Statistic contains the properties for the total repository statistics.
type Statistic struct {
Total int64
Weeks []Week
Author string
}
// Week contains the properties for weekly consolidated data.
type Week struct {
WeekNumber string
Additions int64
Deletions int64
Commits int64
}
// Totals contains the total counters for the repository statistics.
type Totals struct {
Commits int64
Additions int64
Deletions int64
Authors int
}
// Adds values to the Repository slice.
func (r *Repository) addRepo(repo map[string]interface{}, statistics []Statistic, totals Totals) {
r.Name = repo["name"].(string)
r.Visibility = checkVisibility(repo["private"].(bool))
r.Size = int(repo["size"].(float64))
r.Statistics = statistics
r.TotalStats = totals
}
// Add the values to the Statistic slice.
func (s *Statistic) addStats(statsItem map[string]interface{}, weeks []Week) {
s.Total = int64(statsItem["total"].(float64))
s.Weeks = weeks
author := statsItem["author"].(map[string]interface{})
s.Author = author["login"].(string)
}
// Add the values to the Week slice.
func (w *Week) addWeek(weekItem map[string]interface{}) {
weekNumberUnix := weekItem["w"].(float64)
w.WeekNumber = time.Unix(int64(weekNumberUnix), 0).Format(time.RFC3339)
w.Additions = int64(weekItem["a"].(float64))
w.Deletions = int64(weekItem["d"].(float64))
w.Commits = int64(weekItem["c"].(float64))
}
// Increase the Repository totals.
func (t *Totals) increaseActivity(weekItem map[string]interface{}) {
t.Additions += int64(weekItem["a"].(float64))
t.Deletions += int64(weekItem["d"].(float64))
t.Commits += int64(weekItem["c"].(float64))
}
// Increment the Repository total authors.
func (t *Totals) incrementAuthors() {
t.Authors++
}
// Visibility is listed in the markdown as either Public or Private.
func checkVisibility(private bool) string {
if private {
return "private"
}
return "public"
}
func repoStatsURI(uri string, repoName string) string {
return strings.Replace(uri, ":repo", repoName, 1)
}
// Error check.
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
// Get the config json into the Configuration struct.
configuration := setConfiguration()
// Get the json response.
repoList := getJsonResponse(configuration.URIRepos, configuration.Token, "repos")
// Declare a slice of all the repos.
repositories := make([]Repository, len(repoList))
// Loop through the slice, building the Repository struct.
for i, repoItem := range repoList {
// Only get stats for non-forked repos.
if repoItem.(map[string]interface{})["fork"].(bool) {
continue
}
// Print repo name to screen.
fmt.Printf("Repo %s", repoItem.(map[string]interface{})["name"].(string))
// For each repo, get the contributor statistics.
uri := repoStatsURI(configuration.URIStats, repoItem.(map[string]interface{})["name"].(string))
statsList := getJsonResponse(uri, configuration.Token, "stats")
// Declare a slice of all the stats
statistics := make([]Statistic, len(statsList))
// Declare the totals counters.
var totals Totals
// Loop through the slice, building the Statistics struct.
for j, statsItem := range statsList {
// Indicate that we are doing something.
fmt.Print(".")
// Get the "weeks" json object.
weeksList := statsItem.(map[string]interface{})["weeks"].([]interface{})
// Create a slice for the weeks data.
weeks := make([]Week, len(weeksList))
// Loop through the weeks json.
for k, weekItem := range weeksList {
// Add the values to the Week slice.
weeks[k].addWeek(weekItem.(map[string]interface{}))
// Add the weekly totals to the repository Totals struct.
totals.increaseActivity(weekItem.(map[string]interface{}))
}
// The json response is sectioned by authors - increment the counter.
totals.incrementAuthors()
// Add the values to the Statistic slice.
statistics[j].addStats(statsItem.(map[string]interface{}), weeks)
}
// Add the values to the Repository slice.
repositories[i].addRepo(repoItem.(map[string]interface{}), statistics, totals)
// Close the print line.
fmt.Print(".done!\n")
}
// Output the repositories in size order.
outputMarkdown(repositories)
}