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.

github/repository.go   A
last analyzed

Size/Duplication

Total Lines 166
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 24
eloc 115
dl 0
loc 166
rs 10
c 0
b 0
f 0

11 Methods

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