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 ( c9b528...85ea38 )
by Fedir
02:26
created

github.GetRepositoryCommitsData   B

Complexity

Conditions 5

Size

Total Lines 24
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 21
nop 3
dl 0
loc 24
rs 8.9093
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
	"regexp"
10
	"strconv"
11
	"time"
12
13
	"github.com/fedir/ghstat/httpcache"
14
)
15
16
// Commit structure with selcted data keys for JSON processing
17
type Commit struct {
18
	Author struct {
19
		Login string `json:"login"`
20
		Date  string `json:"date"`
21
	} `json:"author"`
22
	Commit struct {
23
		Author struct {
24
			Name  string    `json:"name"`
25
			Email string    `json:"email"`
26
			Date  time.Time `json:"date"`
27
		} `json:"author"`
28
	} `json:"commit"`
29
}
30
31
func GetRepositoryCommitsData(repoKey string, tmpFolder string, debug bool) (string, time.Time) {
0 ignored issues
show
introduced by
exported function GetRepositoryCommitsData should have comment or be unexported
Loading history...
32
	var total int
33
	var commitAuthorLogin string
34
	url := "https://api.github.com/repos/" + repoKey + "/commits"
35
	fullResp := httpcache.MakeCachedHTTPRequest(url, tmpFolder, debug)
36
	jsonResponse, linkHeader, _ := httpcache.ReadResp(fullResp)
37
	var compRegEx = regexp.MustCompile(regexpPageIndexes)
38
	match := compRegEx.FindStringSubmatch(linkHeader)
39
	nextPage := 0
40
	for range compRegEx.SubexpNames() {
41
		if len(match) == 3 {
42
			nextPage, _ = strconv.Atoi(match[1])
43
		}
44
	}
45
	lastCommitDate := getRepositoryLastCommitDate(jsonResponse)
46
	if nextPage == 0 {
47
		commits := make([]Commit, 0)
48
		json.Unmarshal(jsonResponse, &commits)
49
		total = len(commits)
50
		commitAuthorLogin = commits[total-1].Author.Login
51
	} else {
52
		commitAuthorLogin = getRepositoryFirstCommitAuthorLogin(linkHeader, tmpFolder, debug)
53
	}
54
	return commitAuthorLogin, lastCommitDate
55
}
56
57
func getRepositoryFirstCommitAuthorLogin(linkHeader string, tmpFolder string, debug bool) string {
58
	var commitAuthorLogin string
59
	compRegExLastURL := regexp.MustCompile(regexpLastPageURL)
60
	matchLastURL := compRegExLastURL.FindStringSubmatch(linkHeader)
61
	lastPageURL := matchLastURL[1]
62
	fullResp := httpcache.MakeCachedHTTPRequest(lastPageURL, tmpFolder, debug)
63
	jsonResponse, _, _ := httpcache.ReadResp(fullResp)
64
	commits := make([]Commit, 0)
65
	json.Unmarshal(jsonResponse, &commits)
66
	commitsOnLastPage := len(commits)
67
	commitAuthorLogin = commits[commitsOnLastPage-1].Author.Login
68
	return commitAuthorLogin
69
}
70
71
func getRepositoryLastCommitDate(jsonResponse []byte) time.Time {
72
	commits := make([]Commit, 0)
73
	json.Unmarshal(jsonResponse, &commits)
74
	return commits[0].Commit.Author.Date
75
}
76
77
func GetUserFollowers(username string, tmpFolder string, debug bool) int {
0 ignored issues
show
introduced by
exported function GetUserFollowers should have comment or be unexported
Loading history...
78
	var total int
79
	url := "https://api.github.com/users/" + username + "/followers"
80
	fullResp := httpcache.MakeCachedHTTPRequest(url, tmpFolder, debug)
81
	jsonResponse, linkHeader, _ := httpcache.ReadResp(fullResp)
82
	var compRegEx = regexp.MustCompile(regexpPageIndexes)
83
	match := compRegEx.FindStringSubmatch(linkHeader)
84
	nextPage := 0
85
	lastPage := 0
86
	for range compRegEx.SubexpNames() {
87
		if len(match) == 3 {
88
			nextPage, _ = strconv.Atoi(match[1])
89
			lastPage, _ = strconv.Atoi(match[2])
90
		}
91
	}
92
	if nextPage == 0 {
93
		contributors := make([]Contributor, 0)
94
		json.Unmarshal(jsonResponse, &contributors)
95
		total = len(contributors)
96
	} else {
97
		itemsNumberOnLastPage := getItemsNumberOnLastPage(linkHeader, tmpFolder, debug)
98
		total = (lastPage-1)*30 + itemsNumberOnLastPage
99
	}
100
	return total
101
}
102
103
func getItemsNumberOnLastPage(linkHeader string, tmpFolder string, debug bool) int {
104
	compRegExLastURL := regexp.MustCompile(regexpLastPageURL)
105
	matchLastURL := compRegExLastURL.FindStringSubmatch(linkHeader)
106
	lastPageURL := matchLastURL[1]
107
	fullResp := httpcache.MakeCachedHTTPRequest(lastPageURL, tmpFolder, debug)
108
	jsonResponse, _, _ := httpcache.ReadResp(fullResp)
109
	items := make([]Contributor, 0)
110
	json.Unmarshal(jsonResponse, &items)
111
	itemsNumberOnLastPage := len(items)
112
	return itemsNumberOnLastPage
113
}
114