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 ( 6ed052...488927 )
by Fedir
02:36
created

github.GetUserData   A

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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