-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
289 lines (260 loc) · 7.32 KB
/
Copy pathgithub.go
File metadata and controls
289 lines (260 loc) · 7.32 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
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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"sort"
"strings"
"time"
)
const graphqlURL = "https://api.github.com/graphql"
type ghClient struct {
token string
}
func newGHClient(token string) *ghClient {
return &ghClient{token: token}
}
func (c *ghClient) query(query string, vars map[string]interface{}) ([]byte, error) {
body := map[string]interface{}{"query": query, "variables": vars}
b, _ := json.Marshal(body)
req, err := http.NewRequest("POST", graphqlURL, strings.NewReader(string(b)))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+c.token)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", "GitViz")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
out, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var gqlResp struct {
Data json.RawMessage `json:"data"`
Errors []struct {
Message string `json:"message"`
} `json:"errors"`
}
if err := json.Unmarshal(out, &gqlResp); err != nil {
return nil, fmt.Errorf("json: %w", err)
}
if len(gqlResp.Errors) > 0 {
return nil, fmt.Errorf("graphql: %s", gqlResp.Errors[0].Message)
}
return gqlResp.Data, nil
}
func fetchRepoData(owner, repo, token string) (*RepoData, error) {
if token == "" {
return nil, fmt.Errorf("no token available")
}
c := newGHClient(token)
q := `
query($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
name
description
url
stargazerCount
forkCount
issues(states: OPEN) { totalCount }
pullRequests(states: OPEN) { totalCount }
licenseInfo { name }
primaryLanguage { name }
languages(first: 10, orderBy: {field: SIZE, direction: DESC}) {
edges {
size
node { name color }
}
}
defaultBranchRef {
target {
... on Commit {
history(first: 100) {
totalCount
edges {
node {
committedDate
author { name user { login avatarUrl } }
additions deletions changedFiles
}
}
}
}
}
}
releases(first: 10, orderBy: {field: CREATED_AT, direction: DESC}) {
nodes { name tagName publishedAt url }
}
}
}`
data, err := c.query(q, map[string]interface{}{"owner": owner, "repo": repo})
if err != nil {
return nil, err
}
var parsed struct {
Repository struct {
Name string `json:"name"`
Description string `json:"description"`
URL string `json:"url"`
StargazerCount int `json:"stargazerCount"`
ForkCount int `json:"forkCount"`
Issues struct { TotalCount int } `json:"issues"`
PullRequests struct { TotalCount int } `json:"pullRequests"`
LicenseInfo *struct { Name string } `json:"licenseInfo"`
PrimaryLanguage *struct { Name string } `json:"primaryLanguage"`
Languages struct {
Edges []struct {
Size int `json:"size"`
Node struct {
Name string `json:"name"`
Color string `json:"color"`
} `json:"node"`
} `json:"edges"`
} `json:"languages"`
DefaultBranchRef *struct {
Target struct {
History struct {
TotalCount int `json:"totalCount"`
Edges []struct {
Node struct {
CommittedDate string `json:"committedDate"`
Author *struct {
Name string `json:"name"`
User *struct {
Login string `json:"login"`
AvatarURL string `json:"avatarUrl"`
} `json:"user"`
} `json:"author"`
Additions int `json:"additions"`
Deletions int `json:"deletions"`
ChangedFiles int `json:"changedFiles"`
} `json:"node"`
} `json:"edges"`
} `json:"history"`
} `json:"target"`
} `json:"defaultBranchRef"`
Releases struct {
Nodes []struct {
Name string `json:"name"`
TagName string `json:"tagName"`
PublishedAt string `json:"publishedAt"`
URL string `json:"url"`
} `json:"nodes"`
} `json:"releases"`
} `json:"repository"`
}
if err := json.Unmarshal(data, &parsed); err != nil {
return nil, fmt.Errorf("parse: %w", err)
}
r := parsed.Repository
result := &RepoData{
FullName: owner + "/" + r.Name,
Description: r.Description,
URL: r.URL,
Stars: r.StargazerCount,
Forks: r.ForkCount,
OpenIssues: r.Issues.TotalCount,
OpenPRs: r.PullRequests.TotalCount,
Owner: owner,
Repo: r.Name,
FetchedAt: time.Now(),
}
if r.LicenseInfo != nil {
result.License = r.LicenseInfo.Name
}
if r.PrimaryLanguage != nil {
result.PrimaryLang = r.PrimaryLanguage.Name
}
var totalLangBytes int
for _, e := range r.Languages.Edges {
totalLangBytes += e.Size
}
for _, e := range r.Languages.Edges {
pct := 0.0
if totalLangBytes > 0 {
pct = float64(e.Size) / float64(totalLangBytes) * 100
}
result.Languages = append(result.Languages, LangEntry{
Name: e.Node.Name, Bytes: e.Size, Color: e.Node.Color, Pct: pct,
})
}
if r.DefaultBranchRef != nil {
edges := r.DefaultBranchRef.Target.History.Edges
result.Stats.TotalCommits = r.DefaultBranchRef.Target.History.TotalCount
authorMap := make(map[string]*AuthorStat)
weekMap := make(map[string]int)
for _, e := range edges {
n := e.Node
result.Stats.TotalAdditions += n.Additions
result.Stats.TotalDeletions += n.Deletions
login := "unknown"
name := n.Author.Name
avatar := ""
if n.Author.User != nil {
login = n.Author.User.Login
avatar = n.Author.User.AvatarURL
} else if name != "" {
login = name
}
if _, ok := authorMap[login]; !ok {
authorMap[login] = &AuthorStat{Login: login, Name: name, AvatarURL: avatar}
}
authorMap[login].Commits++
authorMap[login].Additions += n.Additions
authorMap[login].Deletions += n.Deletions
if t, err := time.Parse(time.RFC3339, n.CommittedDate); err == nil {
week := t.Format("2006-01-02")
weekMap[week]++
}
}
var totalChanges int
var authors []AuthorStat
for _, a := range authorMap {
authors = append(authors, *a)
totalChanges += a.Additions + a.Deletions
}
sort.Slice(authors, func(i, j int) bool {
return authors[i].Commits > authors[j].Commits
})
for i := range authors {
if totalChanges > 0 {
authors[i].Pct = float64(authors[i].Additions+authors[i].Deletions) / float64(totalChanges) * 100
}
}
result.Stats.Authors = authors
var weeks []WeekBucket
for w, c := range weekMap {
weeks = append(weeks, WeekBucket{Week: w, Commits: c})
}
sort.Slice(weeks, func(i, j int) bool { return weeks[i].Week < weeks[j].Week })
result.Stats.WeeklyCommits = weeks
// bus factor: minimum authors to reach 50% of total changes
sorted := make([]AuthorStat, len(authors))
copy(sorted, authors)
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].Additions+sorted[i].Deletions > sorted[j].Additions+sorted[j].Deletions
})
half := totalChanges / 2
cumulative := 0
for i, a := range sorted {
cumulative += a.Additions + a.Deletions
if cumulative >= half {
result.Stats.BusFactor = i + 1
result.Stats.BusFactorPct = float64(cumulative) / float64(totalChanges) * 100
break
}
}
}
for _, n := range r.Releases.Nodes {
result.Releases = append(result.Releases, ReleaseEntry{
Name: n.Name, TagName: n.TagName,
Published: n.PublishedAt, URL: n.URL,
})
}
return result, nil
}