GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( f03d51...500dda )
by Fedir
02:25
created

github.getJSONResponse   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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