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.GetUserFollowers   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 31
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 26
nop 3
dl 0
loc 31
rs 7.856
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
	"fmt"
10
	"log"
11
	"regexp"
12
	"strconv"
13
	"time"
14
15
	"github.com/fedir/ghstat/httpcache"
16
)
17
18
// Commit structure with selcted data keys for JSON processing
19
type Commit struct {
20
	Author struct {
21
		Login string `json:"login"`
22
		Date  string `json:"date"`
23
	} `json:"author"`
24
	Commit struct {
25
		Author struct {
26
			Name  string    `json:"name"`
27
			Email string    `json:"email"`
28
			Date  time.Time `json:"date"`
29
		} `json:"author"`
30
	} `json:"commit"`
31
}
32
33
// User structure with selcted data keys for JSON processing
34
type User struct {
35
	Login     string    `json:"login"`
36
	Name      string    `json:"name"`
37
	Location  string    `json:"location"`
38
	Email     string    `json:"email"`
39
	CreatedAt time.Time `json:"created_at"`
40
}
41
42
// GetRepositoryCommitsData gets information about commits of a repository.
43
// Currerntly used for author login and last commit date
44
func GetRepositoryCommitsData(repoKey string, tmpFolder string, debug bool) (string, time.Time) {
45
	var total int
46
	var authorLogin string
47
	url := "https://api.github.com/repos/" + repoKey + "/commits"
48
	fullResp := httpcache.MakeCachedHTTPRequest(url, tmpFolder, debug)
49
	jsonResponse, linkHeader, _ := httpcache.ReadResp(fullResp)
50
	var compRegEx = regexp.MustCompile(regexpPageIndexes)
51
	match := compRegEx.FindStringSubmatch(linkHeader)
52
	nextPage := 0
53
	for range compRegEx.SubexpNames() {
54
		if len(match) == 3 {
55
			nextPage, _ = strconv.Atoi(match[1])
56
		}
57
	}
58
	lastCommitDate := getRepositoryLastCommitDate(jsonResponse)
59
	if nextPage == 0 {
60
		commits := make([]Commit, 0)
61
		json.Unmarshal(jsonResponse, &commits)
62
		total = len(commits)
63
		authorLogin = commits[total-1].Author.Login
64
	} else {
65
		authorLogin = getRepositoryFirstCommitAuthorLogin(linkHeader, tmpFolder, debug)
66
	}
67
	return authorLogin, lastCommitDate
68
}
69
70
func getRepositoryFirstCommitAuthorLogin(linkHeader string, tmpFolder string, debug bool) string {
71
	jsonResponse := getJSONResponse(linkHeader, tmpFolder, debug)
72
	commits := make([]Commit, 0)
73
	json.Unmarshal(jsonResponse, &commits)
74
	commitsOnLastPage := len(commits)
75
	commitAuthorLogin := commits[commitsOnLastPage-1].Author.Login
76
	return commitAuthorLogin
77
}
78
79
func getRepositoryLastCommitDate(jsonResponse []byte) time.Time {
80
	commits := make([]Commit, 0)
81
	json.Unmarshal(jsonResponse, &commits)
82
	return commits[0].Commit.Author.Date
83
}
84
85
// GetUserFollowers gets information about followers of a user
86
func GetUserFollowers(username string, tmpFolder string, debug bool) int {
87
	var total int
88
	url := "https://api.github.com/users/" + username + "/followers"
89
	fullResp := httpcache.MakeCachedHTTPRequest(url, tmpFolder, debug)
90
	if len(fullResp) == 0 {
91
		filename := httpcache.GetFilename(url)
92
		fmt.Printf("Response for %s is empty, please check %s", url, filename)
93
	}
94
	jsonResponse, linkHeader, err := httpcache.ReadResp(fullResp)
95
	if err != nil {
96
		fmt.Printf("Problem recovering %s", url)
97
	}
98
	var compRegEx = regexp.MustCompile(regexpPageIndexes)
99
	match := compRegEx.FindStringSubmatch(linkHeader)
100
	nextPage := 0
101
	lastPage := 0
102
	for range compRegEx.SubexpNames() {
103
		if len(match) == 3 {
104
			nextPage, _ = strconv.Atoi(match[1])
105
			lastPage, _ = strconv.Atoi(match[2])
106
		}
107
	}
108
	if nextPage == 0 {
109
		contributors := make([]Contributor, 0)
110
		json.Unmarshal(jsonResponse, &contributors)
111
		total = len(contributors)
112
	} else {
113
		itemsNumberOnLastPage := getItemsNumberOnLastPage(linkHeader, tmpFolder, debug)
114
		total = (lastPage-1)*30 + itemsNumberOnLastPage
115
	}
116
	return total
117
}
118
119
// GetUserData gets information about followers of a user
120
func GetUserData(username string, tmpFolder string, debug bool) *User {
121
	url := "https://api.github.com/users/" + username
122
	fullResp := httpcache.MakeCachedHTTPRequest(url, tmpFolder, debug)
123
	jsonResponse, _, _ := httpcache.ReadResp(fullResp)
124
	result := &User{}
125
	err := json.Unmarshal([]byte(jsonResponse), result)
126
	if err != nil {
127
		log.Fatal(err)
128
	}
129
	return result
130
}
131
132
func getItemsNumberOnLastPage(linkHeader string, tmpFolder string, debug bool) int {
133
	jsonResponse := getJSONResponse(linkHeader, tmpFolder, debug)
134
	items := make([]Contributor, 0)
135
	json.Unmarshal(jsonResponse, &items)
136
	itemsNumberOnLastPage := len(items)
137
	return itemsNumberOnLastPage
138
}
139