|
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
|
|
|
"log" |
|
10
|
|
|
"regexp" |
|
11
|
|
|
"strconv" |
|
12
|
|
|
"time" |
|
13
|
|
|
|
|
14
|
|
|
"github.com/fedir/ghstat/httpcache" |
|
15
|
|
|
"github.com/tidwall/gjson" |
|
16
|
|
|
) |
|
17
|
|
|
|
|
18
|
|
|
// Repository structure with selcted data keys for JSON processing |
|
19
|
|
|
type Repository struct { |
|
20
|
|
|
Name string `json:"name"` |
|
21
|
|
|
FullName string `json:"full_name"` |
|
22
|
|
|
Language string `json:"language"` |
|
23
|
|
|
Watchers int `json:"watchers"` |
|
24
|
|
|
Forks int `json:"forks"` |
|
25
|
|
|
OpenIssues int `json:"open_issues"` |
|
26
|
|
|
CreatedAt time.Time `json:"created_at"` |
|
27
|
|
|
HasIssues bool `json:"has_issues"` |
|
28
|
|
|
License struct { |
|
29
|
|
|
SPDXID string `json:"spdx_id"` |
|
30
|
|
|
} `json:"license"` |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
// Tag structure with selcted data keys for JSON processing |
|
34
|
|
|
type Tag struct { |
|
35
|
|
|
Name string `json:"name"` |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// GetRepositoryClosedIssues gets number of closed issues of a repository |
|
39
|
|
|
func GetRepositoryClosedIssues(repoKey string, tmpFolder string, debug bool) int { |
|
40
|
|
|
url := "https://api.github.com/search/issues?q=repo:" + repoKey + "+type:issue+state:closed" |
|
41
|
|
|
fullResp := httpcache.MakeCachedHTTPRequest(url, tmpFolder, debug) |
|
42
|
|
|
jsonResponse, _, _ := httpcache.ReadResp(fullResp) |
|
43
|
|
|
closedIssuesResult := gjson.Get(string(jsonResponse), "total_count") |
|
44
|
|
|
return int(closedIssuesResult.Int()) |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// GetRepositoryStatistics gets repository common statistics |
|
48
|
|
|
func GetRepositoryStatistics(RepoKey string, tmpFolder string, debug bool) *Repository { |
|
49
|
|
|
repositoryStatistics := ParseRepositoryData(getRepositoryData(RepoKey, tmpFolder, debug)) |
|
50
|
|
|
if repositoryStatistics.HasIssues == false { |
|
51
|
|
|
repositoryStatistics.OpenIssues = 0 |
|
52
|
|
|
} |
|
53
|
|
|
return repositoryStatistics |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// GetRepositoryLanguages gets repository language statistics |
|
57
|
|
|
func GetRepositoryLanguages(RepoKey string, tmpFolder string, debug bool) (string, int) { |
|
58
|
|
|
|
|
59
|
|
|
url := "https://api.github.com/repos/" + RepoKey + "/languages" |
|
60
|
|
|
fullResp := httpcache.MakeCachedHTTPRequest(url, tmpFolder, debug) |
|
61
|
|
|
jsonResponse, _, _ := httpcache.ReadResp(fullResp) |
|
62
|
|
|
|
|
63
|
|
|
jsonMap := make(map[string]interface{}) |
|
64
|
|
|
err := json.Unmarshal([]byte(jsonResponse), &jsonMap) |
|
65
|
|
|
if err != nil { |
|
66
|
|
|
log.Fatal(err) |
|
67
|
|
|
} |
|
68
|
|
|
languages := repositoryLanguagesBySize(jsonMap) |
|
69
|
|
|
totalSize := repositoryTotalSize(jsonMap) |
|
70
|
|
|
|
|
71
|
|
|
return languages, totalSize |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
func repositoryTotalSize(m map[string]interface{}) int { |
|
75
|
|
|
totalSize := 0 |
|
76
|
|
|
for _, v := range m { |
|
77
|
|
|
totalSize = totalSize + int(v.(float64)) |
|
78
|
|
|
} |
|
79
|
|
|
return totalSize |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
func getRepositoryData(repoKey string, tmpFolder string, debug bool) []byte { |
|
83
|
|
|
url := "https://api.github.com/repos/" + repoKey |
|
84
|
|
|
fullResp := httpcache.MakeCachedHTTPRequest(url, tmpFolder, debug) |
|
85
|
|
|
jsonResponse, _, _ := httpcache.ReadResp(fullResp) |
|
86
|
|
|
return jsonResponse |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// ParseRepositoryData is used to parse repository common statistics |
|
90
|
|
|
func ParseRepositoryData(jsonResponse []byte) *Repository { |
|
91
|
|
|
result := &Repository{} |
|
92
|
|
|
err := json.Unmarshal([]byte(jsonResponse), result) |
|
93
|
|
|
if err != nil { |
|
94
|
|
|
log.Fatal(err) |
|
95
|
|
|
} |
|
96
|
|
|
return result |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// GetIssueByDay calculates the rate of issues by day of the repository |
|
100
|
|
|
func GetIssueByDay(totalIssues int, age int) float64 { |
|
101
|
|
|
var issueByDay float64 |
|
102
|
|
|
totalIssuesFloat := float64(totalIssues) |
|
103
|
|
|
ageFloat := float64(age) |
|
104
|
|
|
if totalIssuesFloat != 0 && ageFloat != 0 { |
|
105
|
|
|
issueByDay = totalIssuesFloat / ageFloat |
|
106
|
|
|
} else { |
|
107
|
|
|
issueByDay = 0 |
|
108
|
|
|
} |
|
109
|
|
|
return issueByDay |
|
110
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// GetClosedIssuesPercentage calculates the percentage of closed issues of the repository |
|
114
|
|
|
func GetClosedIssuesPercentage(openIssues int, closedIssues int) float64 { |
|
115
|
|
|
var closedIssuesPercentage float64 |
|
116
|
|
|
openIssuesFloat := float64(openIssues) |
|
117
|
|
|
closedIssuesFloat := float64(closedIssues) |
|
118
|
|
|
if closedIssuesFloat != 0 { |
|
119
|
|
|
closedIssuesPercentage = closedIssuesFloat / (closedIssuesFloat + openIssuesFloat) * 100 |
|
120
|
|
|
} else { |
|
121
|
|
|
closedIssuesPercentage = 0 |
|
122
|
|
|
} |
|
123
|
|
|
return closedIssuesPercentage |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
// GetRepositoryTagsNumber gets information about tags of the repository |
|
127
|
|
|
func GetRepositoryTagsNumber(repoKey string, tmpFolder string, debug bool) int { |
|
128
|
|
|
var totalTags int |
|
129
|
|
|
url := "https://api.github.com/repos/" + repoKey + "/tags" |
|
130
|
|
|
fullResp := httpcache.MakeCachedHTTPRequest(url, tmpFolder, debug) |
|
131
|
|
|
jsonResponse, linkHeader, _ := httpcache.ReadResp(fullResp) |
|
132
|
|
|
var compRegEx = regexp.MustCompile(regexpPageIndexes) |
|
133
|
|
|
match := compRegEx.FindStringSubmatch(linkHeader) |
|
134
|
|
|
nextPage := 0 |
|
135
|
|
|
lastPage := 0 |
|
136
|
|
|
for range compRegEx.SubexpNames() { |
|
137
|
|
|
if len(match) == 3 { |
|
138
|
|
|
nextPage, _ = strconv.Atoi(match[1]) |
|
139
|
|
|
lastPage, _ = strconv.Atoi(match[2]) |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
tags := make([]Tag, 0) |
|
143
|
|
|
json.Unmarshal(jsonResponse, &tags) |
|
144
|
|
|
if nextPage != 0 { |
|
145
|
|
|
tagsOnLastPage := getRepositoryTagsNumberLastPage(linkHeader, tmpFolder, debug) |
|
146
|
|
|
totalTags = (lastPage-1)*30 + tagsOnLastPage |
|
147
|
|
|
} else { |
|
148
|
|
|
totalTags = len(tags) |
|
149
|
|
|
} |
|
150
|
|
|
return totalTags |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
func getRepositoryTagsNumberLastPage(linkHeader string, tmpFolder string, debug bool) int { |
|
154
|
|
|
jsonResponse := getJSONResponse(linkHeader, tmpFolder, debug) |
|
155
|
|
|
tags := make([]Tag, 0) |
|
156
|
|
|
json.Unmarshal(jsonResponse, &tags) |
|
157
|
|
|
tagsOnLastPage := len(tags) |
|
158
|
|
|
return tagsOnLastPage |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
func getJSONResponse(linkHeader string, tmpFolder string, debug bool) []byte { |
|
162
|
|
|
compRegExLastURL := regexp.MustCompile(regexpLastPageURL) |
|
163
|
|
|
matchLastURL := compRegExLastURL.FindStringSubmatch(linkHeader) |
|
164
|
|
|
fullResp := httpcache.MakeCachedHTTPRequest(matchLastURL[1], tmpFolder, debug) |
|
165
|
|
|
jsonResponse, _, err := httpcache.ReadResp(fullResp) |
|
166
|
|
|
if err != nil { |
|
167
|
|
|
log.Fatalf("%s", linkHeader) |
|
168
|
|
|
} |
|
169
|
|
|
return jsonResponse |
|
170
|
|
|
} |
|
171
|
|
|
|