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
|
|
|
Watchers int `json:"watchers"` |
23
|
|
|
Forks int `json:"forks"` |
24
|
|
|
OpenIssues int `json:"open_issues"` |
25
|
|
|
CreatedAt time.Time `json:"created_at"` |
26
|
|
|
License struct { |
27
|
|
|
SPDXID string `json:"spdx_id"` |
28
|
|
|
} `json:"license"` |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// Tag structure with selcted data keys for JSON processing |
32
|
|
|
type Tag struct { |
33
|
|
|
Name string `json:"name"` |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// GetRepositoryClosedIssues gets number of closed issues of a repository |
37
|
|
|
func GetRepositoryClosedIssues(repoKey string, tmpFolder string, debug bool) int { |
38
|
|
|
url := "https://api.github.com/search/issues?q=repo:" + repoKey + "+type:issue+state:closed" |
39
|
|
|
fullResp := httpcache.MakeCachedHTTPRequest(url, tmpFolder, debug) |
40
|
|
|
jsonResponse, _, _ := httpcache.ReadResp(fullResp) |
41
|
|
|
closedIssuesResult := gjson.Get(string(jsonResponse), "total_count") |
42
|
|
|
return int(closedIssuesResult.Int()) |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// GetRepositoryStatistics gets repository common statistics |
46
|
|
|
func GetRepositoryStatistics(RepoKey string, tmpFolder string, debug bool) *Repository { |
47
|
|
|
return ParseRepositoryData(getRepositoryData(RepoKey, tmpFolder, debug)) |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
func getRepositoryData(repoKey string, tmpFolder string, debug bool) []byte { |
51
|
|
|
url := "https://api.github.com/repos/" + repoKey |
52
|
|
|
fullResp := httpcache.MakeCachedHTTPRequest(url, tmpFolder, debug) |
53
|
|
|
jsonResponse, _, _ := httpcache.ReadResp(fullResp) |
54
|
|
|
return jsonResponse |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// ParseRepositoryData is used to parse repository common statistics |
58
|
|
|
func ParseRepositoryData(jsonResponse []byte) *Repository { |
59
|
|
|
result := &Repository{} |
60
|
|
|
err := json.Unmarshal([]byte(jsonResponse), result) |
61
|
|
|
if err != nil { |
62
|
|
|
log.Fatal(err) |
63
|
|
|
} |
64
|
|
|
return result |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// GetIssueByDay calculates the rate of issues by day of the repository |
68
|
|
|
func GetIssueByDay(totalIssues int, age int) float64 { |
69
|
|
|
var issueByDay float64 |
70
|
|
|
totalIssuesFloat := float64(totalIssues) |
71
|
|
|
ageFloat := float64(age) |
72
|
|
|
if totalIssuesFloat != 0 && ageFloat != 0 { |
73
|
|
|
issueByDay = totalIssuesFloat / ageFloat |
74
|
|
|
} else { |
75
|
|
|
issueByDay = 0 |
76
|
|
|
} |
77
|
|
|
return issueByDay |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// GetClosedIssuesPercentage calculates the percentage of closed issues of the repository |
82
|
|
|
func GetClosedIssuesPercentage(openIssues int, closedIssues int) float64 { |
83
|
|
|
var closedIssuesPercentage float64 |
84
|
|
|
openIssuesFloat := float64(openIssues) |
85
|
|
|
closedIssuesFloat := float64(closedIssues) |
86
|
|
|
if closedIssuesFloat != 0 && openIssuesFloat != 0 { |
87
|
|
|
closedIssuesPercentage = closedIssuesFloat / (closedIssuesFloat + openIssuesFloat) * 100 |
88
|
|
|
} else { |
89
|
|
|
closedIssuesPercentage = 100 |
90
|
|
|
} |
91
|
|
|
return closedIssuesPercentage |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// GetRepositoryTagsNumber gets information about tags of the repository |
95
|
|
|
func GetRepositoryTagsNumber(repoKey string, tmpFolder string, debug bool) int { |
96
|
|
|
var totalTags int |
97
|
|
|
url := "https://api.github.com/repos/" + repoKey + "/tags" |
98
|
|
|
fullResp := httpcache.MakeCachedHTTPRequest(url, tmpFolder, debug) |
99
|
|
|
jsonResponse, linkHeader, _ := httpcache.ReadResp(fullResp) |
100
|
|
|
var compRegEx = regexp.MustCompile(regexpPageIndexes) |
101
|
|
|
match := compRegEx.FindStringSubmatch(linkHeader) |
102
|
|
|
nextPage := 0 |
103
|
|
|
lastPage := 0 |
104
|
|
|
for range compRegEx.SubexpNames() { |
105
|
|
|
if len(match) == 3 { |
106
|
|
|
nextPage, _ = strconv.Atoi(match[1]) |
107
|
|
|
lastPage, _ = strconv.Atoi(match[2]) |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
tags := make([]Tag, 0) |
111
|
|
|
json.Unmarshal(jsonResponse, &tags) |
112
|
|
|
if nextPage != 0 { |
113
|
|
|
tagsOnLastPage := getRepositoryTagsNumberLastPage(linkHeader, tmpFolder, debug) |
114
|
|
|
totalTags = (lastPage-1)*30 + tagsOnLastPage |
115
|
|
|
} else { |
116
|
|
|
totalTags = len(tags) |
117
|
|
|
} |
118
|
|
|
return totalTags |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
func getRepositoryTagsNumberLastPage(linkHeader string, tmpFolder string, debug bool) int { |
122
|
|
|
compRegExLastURL := regexp.MustCompile(regexpLastPageURL) |
123
|
|
|
matchLastURL := compRegExLastURL.FindStringSubmatch(linkHeader) |
124
|
|
|
lastPageURL := matchLastURL[1] |
125
|
|
|
fullResp := httpcache.MakeCachedHTTPRequest(lastPageURL, tmpFolder, debug) |
126
|
|
|
jsonResponse, _, _ := httpcache.ReadResp(fullResp) |
127
|
|
|
tags := make([]Tag, 0) |
128
|
|
|
json.Unmarshal(jsonResponse, &tags) |
129
|
|
|
tagsOnLastPage := len(tags) |
130
|
|
|
return tagsOnLastPage |
131
|
|
|
} |
132
|
|
|
|