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
|
|
|
package main |
5
|
|
|
|
6
|
|
|
import ( |
7
|
|
|
"encoding/json" |
8
|
|
|
"log" |
9
|
|
|
"time" |
10
|
|
|
|
11
|
|
|
"github.com/fedir/ghstat/httpcache" |
12
|
|
|
"github.com/tidwall/gjson" |
13
|
|
|
) |
14
|
|
|
|
15
|
|
|
// Repository structure with selcted data keys for JSON processing |
16
|
|
|
type Repository struct { |
17
|
|
|
Name string `json:"name"` |
18
|
|
|
FullName string `json:"full_name"` |
19
|
|
|
Watchers int `json:"watchers"` |
20
|
|
|
Forks int `json:"forks"` |
21
|
|
|
OpenIssues int `json:"open_issues"` |
22
|
|
|
CreatedAt time.Time `json:"created_at"` |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
func getRepositoryClosedIssues(repoKey string, tmpFolder string, debug bool) int { |
26
|
|
|
url := "https://api.github.com/search/issues?q=repo:" + repoKey + "+type:issue+state:closed" |
27
|
|
|
fullResp := httpcache.MakeCachedHTTPRequest(url, tmpFolder, debug) |
28
|
|
|
jsonResponse, _, _ := httpcache.ReadResp(fullResp) |
29
|
|
|
closedIssuesResult := gjson.Get(string(jsonResponse), "total_count") |
30
|
|
|
//fmt.Printf("%d\n", closedIssuesResult.Int()) |
31
|
|
|
return int(closedIssuesResult.Int()) |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
func getRepositoryData(repoKey string, tmpFolder string, debug bool) []byte { |
35
|
|
|
url := "https://api.github.com/repos/" + repoKey |
36
|
|
|
fullResp := httpcache.MakeCachedHTTPRequest(url, tmpFolder, debug) |
37
|
|
|
jsonResponse, _, _ := httpcache.ReadResp(fullResp) |
38
|
|
|
return jsonResponse |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
func parseRepositoryData(jsonResponse []byte) *Repository { |
42
|
|
|
result := &Repository{} |
43
|
|
|
err := json.Unmarshal([]byte(jsonResponse), result) |
44
|
|
|
if err != nil { |
45
|
|
|
log.Fatal(err) |
46
|
|
|
} |
47
|
|
|
return result |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
func getRepositoryStatistics(RepoKey string, tmpFolder string, debug bool) *Repository { |
51
|
|
|
return parseRepositoryData(getRepositoryData(RepoKey, tmpFolder, debug)) |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
func getIssueByDay(totalIssues int, age int) float64 { |
55
|
|
|
var issueByDay float64 |
56
|
|
|
totalIssuesFloat := float64(totalIssues) |
57
|
|
|
ageFloat := float64(age) |
58
|
|
|
if totalIssuesFloat != 0 && ageFloat != 0 { |
59
|
|
|
issueByDay = totalIssuesFloat / ageFloat |
60
|
|
|
} else { |
61
|
|
|
issueByDay = 0 |
62
|
|
|
} |
63
|
|
|
return issueByDay |
64
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
func getClosedIssuesPercentage(openIssues int, closedIssues int) float64 { |
68
|
|
|
var closedIssuesPercentage float64 |
69
|
|
|
openIssuesFloat := float64(openIssues) |
70
|
|
|
closedIssuesFloat := float64(closedIssues) |
71
|
|
|
if closedIssuesFloat != 0 && openIssuesFloat != 0 { |
72
|
|
|
closedIssuesPercentage = closedIssuesFloat / (closedIssuesFloat + openIssuesFloat) * 100 |
73
|
|
|
} else { |
74
|
|
|
closedIssuesPercentage = 100 |
75
|
|
|
} |
76
|
|
|
return closedIssuesPercentage |
77
|
|
|
} |
78
|
|
|
|