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 ( 7c022c...ccc1a5 )
by Fedir
02:02
created

main.getClosedIssuesPercentage   A

Complexity

Conditions 3

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nop 2
dl 0
loc 10
rs 9.95
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
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