|
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
|
|
|
"regexp" |
|
10
|
|
|
"strconv" |
|
11
|
|
|
|
|
12
|
|
|
"github.com/fedir/ghstat/httpcache" |
|
13
|
|
|
) |
|
14
|
|
|
|
|
15
|
|
|
const ( |
|
16
|
|
|
regexpPageIndexes = `.*page=(\d+).*page=(\d+).*` |
|
17
|
|
|
regexpLastPageURL = `.* rel="next", <(.*)>;.*` |
|
18
|
|
|
) |
|
19
|
|
|
|
|
20
|
|
|
// Contributor structure with selcted data keys for JSON processing |
|
21
|
|
|
type Contributor struct { |
|
22
|
|
|
Login string `json:"login"` |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
// GetRepositoryContributors gets information about contributors of the repository |
|
26
|
|
|
func GetRepositoryContributors(repoKey string, tmpFolder string, debug bool) (int, int) { |
|
27
|
|
|
var totalContributors int |
|
28
|
|
|
var topContributorsFollowers = 0 |
|
29
|
|
|
url := "https://api.github.com/repos/" + repoKey + "/contributors" |
|
30
|
|
|
fullResp := httpcache.MakeCachedHTTPRequest(url, tmpFolder, debug) |
|
31
|
|
|
jsonResponse, linkHeader, _ := httpcache.ReadResp(fullResp) |
|
32
|
|
|
var compRegEx = regexp.MustCompile(regexpPageIndexes) |
|
33
|
|
|
match := compRegEx.FindStringSubmatch(linkHeader) |
|
34
|
|
|
nextPage := 0 |
|
35
|
|
|
lastPage := 0 |
|
36
|
|
|
for range compRegEx.SubexpNames() { |
|
37
|
|
|
if len(match) == 3 { |
|
38
|
|
|
nextPage, _ = strconv.Atoi(match[1]) |
|
39
|
|
|
lastPage, _ = strconv.Atoi(match[2]) |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
contributors := make([]Contributor, 0) |
|
44
|
|
|
json.Unmarshal(jsonResponse, &contributors) |
|
45
|
|
|
i := 0 |
|
46
|
|
|
for _, contributor := range contributors { |
|
47
|
|
|
topContributorsFollowers = topContributorsFollowers + getContributorFollowers(contributor.Login, tmpFolder, debug) |
|
48
|
|
|
i++ |
|
49
|
|
|
if i == 10 { |
|
50
|
|
|
goto TOTAL_CONTRIBUTORS |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
TOTAL_CONTRIBUTORS: |
|
54
|
|
|
if nextPage != 0 { |
|
55
|
|
|
contributorsOnLastPage := getRepositoryContributorsNumberLastPage(linkHeader, tmpFolder, debug) |
|
56
|
|
|
totalContributors = (lastPage-1)*30 + contributorsOnLastPage |
|
57
|
|
|
} else { |
|
58
|
|
|
totalContributors = len(contributors) |
|
59
|
|
|
} |
|
60
|
|
|
return topContributorsFollowers, totalContributors |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
func getContributorFollowers(login string, tmpFolder string, debug bool) int { |
|
64
|
|
|
totalUsers := 0 |
|
65
|
|
|
url := "https://api.github.com/users/" + login + "/followers" |
|
66
|
|
|
fullResp := httpcache.MakeCachedHTTPRequest(url, tmpFolder, debug) |
|
67
|
|
|
jsonResponse, linkHeader, _ := httpcache.ReadResp(fullResp) |
|
68
|
|
|
var compRegEx = regexp.MustCompile(regexpPageIndexes) |
|
69
|
|
|
match := compRegEx.FindStringSubmatch(linkHeader) |
|
70
|
|
|
nextPage := 0 |
|
71
|
|
|
lastPage := 0 |
|
72
|
|
|
for range compRegEx.SubexpNames() { |
|
73
|
|
|
if len(match) == 3 { |
|
74
|
|
|
nextPage, _ = strconv.Atoi(match[1]) |
|
75
|
|
|
lastPage, _ = strconv.Atoi(match[2]) |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
contributors := make([]Contributor, 0) |
|
79
|
|
|
json.Unmarshal(jsonResponse, &contributors) |
|
80
|
|
|
if nextPage != 0 { |
|
81
|
|
|
contributorsOnLastPage := getRepositoryContributorsNumberLastPage(linkHeader, tmpFolder, debug) |
|
82
|
|
|
totalUsers = (lastPage-1)*30 + contributorsOnLastPage |
|
83
|
|
|
} else { |
|
84
|
|
|
totalUsers = len(contributors) |
|
85
|
|
|
} |
|
86
|
|
|
return totalUsers |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
func getRepositoryContributorsNumberLastPage(linkHeader string, tmpFolder string, debug bool) int { |
|
90
|
|
|
jsonResponse := getJSONResponse(linkHeader, tmpFolder, debug) |
|
91
|
|
|
contributors := make([]Contributor, 0) |
|
92
|
|
|
json.Unmarshal(jsonResponse, &contributors) |
|
93
|
|
|
contributorsOnLastPage := len(contributors) |
|
94
|
|
|
return contributorsOnLastPage |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// GetActiveForkersPercentage calculates the percentage of active forkers of the repository |
|
98
|
|
|
func GetActiveForkersPercentage(contributors int, forkers int) float64 { |
|
99
|
|
|
contributorsFloat := float64(contributors) |
|
100
|
|
|
forkersFloat := float64(forkers) |
|
101
|
|
|
activeForkersPercentage := (contributorsFloat / forkersFloat) * 100 |
|
102
|
|
|
return activeForkersPercentage |
|
103
|
|
|
} |
|
104
|
|
|
|