1
|
|
|
// Copyright 2018 Fedir RYKHTIK. All rights reserved. |
2
|
|
|
// Use of this source code is governed by the GNU GPL 3.0 |
3
|
|
|
// license that can be found in the LICENSE file. |
4
|
|
|
|
5
|
|
|
package github |
6
|
|
|
|
7
|
|
|
import ( |
8
|
|
|
"encoding/json" |
9
|
|
|
|
10
|
|
|
"github.com/fedir/ghstat/httpcache" |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
// StatsContributor contains statistical data for contribution |
14
|
|
|
type StatsContributor struct { |
15
|
|
|
Author struct { |
16
|
|
|
Login string `json:"login"` |
17
|
|
|
} `json:"author"` |
18
|
|
|
TotalCommits int `json:"total"` |
19
|
|
|
Weeks []struct { |
20
|
|
|
Week int `json:"w"` |
21
|
|
|
Additions int `json:"a"` |
22
|
|
|
Deletions int `json:"d"` |
23
|
|
|
Commits int `json:"c"` |
24
|
|
|
} `json:"weeks"` |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
// ContributionStatistics contains multiple statistics about contribution into the repository |
28
|
|
|
type ContributionStatistics struct { |
29
|
|
|
TotalCommits int |
30
|
|
|
TotalAdditions int |
31
|
|
|
TotalDeletions int |
32
|
|
|
TotalCodeChanges int |
33
|
|
|
MediumCommitSize int |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
func GetContributionStatistics(repoKey string, tmpFolder string, debug bool) ContributionStatistics { |
|
|
|
|
37
|
|
|
url := "https://api.github.com/repos/" + repoKey + "/stats/contributors" |
38
|
|
|
fullResp := httpcache.MakeCachedHTTPRequest(url, tmpFolder, debug) |
39
|
|
|
jsonResponse, _, _ := httpcache.ReadResp(fullResp) |
40
|
|
|
cs := extractContributionStatisticsFromJSON(jsonResponse) |
41
|
|
|
return cs |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
func GetCommitsByDay(totalCommits int, repositoryAge int) float64 { |
|
|
|
|
45
|
|
|
var commitsByDay float64 |
46
|
|
|
totalCommitsFloat := float64(totalCommits) |
47
|
|
|
repositoryAgeFloat := float64(repositoryAge) |
48
|
|
|
if totalCommitsFloat != 0 && repositoryAgeFloat != 0 { |
49
|
|
|
commitsByDay = totalCommitsFloat / repositoryAgeFloat |
50
|
|
|
} else { |
51
|
|
|
commitsByDay = 0 |
52
|
|
|
} |
53
|
|
|
return commitsByDay |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
func extractContributionStatisticsFromJSON(jsonResponse []byte) ContributionStatistics { |
57
|
|
|
var cs ContributionStatistics |
58
|
|
|
cs.TotalCommits = 0 |
59
|
|
|
cs.TotalAdditions = 0 |
60
|
|
|
cs.TotalDeletions = 0 |
61
|
|
|
cs.TotalCodeChanges = 0 |
62
|
|
|
contributionStatistics := make([]StatsContributor, 0) |
63
|
|
|
json.Unmarshal(jsonResponse, &contributionStatistics) |
64
|
|
|
for _, c := range contributionStatistics { |
65
|
|
|
cs.TotalCommits += c.TotalCommits |
66
|
|
|
for _, cw := range c.Weeks { |
67
|
|
|
cs.TotalAdditions += cw.Additions |
68
|
|
|
cs.TotalDeletions += cw.Deletions |
69
|
|
|
cs.TotalCodeChanges += cw.Additions |
70
|
|
|
cs.TotalCodeChanges += cw.Deletions |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
cs.MediumCommitSize = calculateMediumCommitSize(cs.TotalCommits, cs.TotalCodeChanges) |
74
|
|
|
return cs |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
func calculateMediumCommitSize(totalCommits int, totalCodeChanges int) int { |
78
|
|
|
return int(float64(totalCodeChanges) / float64(totalCommits)) |
79
|
|
|
} |
80
|
|
|
|