This repository has been archived by the owner on Mar 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
github.go
192 lines (149 loc) · 5.22 KB
/
github.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//nolint:tagliatelle
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"os"
"strings"
)
type GHFile struct {
Name string
Path string
URL string
Repo string
}
func sendGithubRequest(url string) (string, http.Header) {
tokenLen := len(config.Github.Token)
if tokenLen >= 4 && config.Github.Username != "" { // Check if the token & username are set properly
debugLog(
"Sending request to %s with username %s and token (last 4 digits) %s",
bolden(url), bolden(config.Github.Username),
bolden(
config.Github.Token[len(config.Github.Token)-4:], // Get last 4 digits
),
)
} else {
debugLog("Invalid/default credentials.")
}
errMsgAdded := "An error occurred while getting information from the github API. URL: " + bolden(url)
resp, err := makeGithubReq(url)
errorLog(err, errMsgAdded)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
errorLogRaw("The Github API returned an error. Status code: %s", bolden(resp.StatusCode))
return "", nil
}
final, err := ioutil.ReadAll(resp.Body)
errorLog(err, errMsgAdded)
if string(final) == "" {
errorLogRaw("The Github API returned an empty response. This may be because you are getting rate limited. If you would like to improve the amount of requests you have, specify the [github] fields. URL: %s", bolden(url))
return "", nil
}
debugLog("Github API requests limit: %s.", resp.Header.Get("x-ratelimit-limit"))
debugLog("Github API requests remaining: %s.", resp.Header.Get("x-ratelimit-remaining"))
return string(final), resp.Header
}
func getAllPkgsFromGh() ([]GHFile, http.Header) {
urls := parseSources()
final := make([]GHFile, 0)
var headers http.Header
convertURL := func(url string) string {
apiLink := strings.ReplaceAll(url, "raw.githubusercontent.com", "api.github.com/repos")
split := strings.Split(apiLink, "/")
index := 6
inserted := split[:index]
inserted = append(inserted, split[index:]...)
branch := split[index]
inserted[index] = "contents"
trimmed := strings.TrimSuffix(strings.Join(inserted, "/"), "/")
return trimmed + "?ref=" + branch
}
for _, url := range urls {
url = parseURL(url, true)
if !strings.HasPrefix(url, "http://raw.githubusercontent.com") {
log(3, "Non-github repositories can't be queried. Repo: %s", url)
continue
}
convURL := convertURL(url)
debugLog("URL: %s", convURL)
response, h := sendGithubRequest(convURL)
headers = h
var files []GHFile
err := json.Unmarshal([]byte(response), &files)
errorLog(err, "An error occurred while parsing package list")
for _, file := range files {
if !strings.HasSuffix(file.Name, ".json") {
continue
}
file.Name = strings.TrimSuffix(file.Name, ".json")
file.Repo = url
final = append(final, file)
}
}
return final, headers
}
func getPkgFromGh(query string) ([]GHFile, http.Header) {
files, headers := getAllPkgsFromGh()
matches := make([]GHFile, 0)
for _, file := range files {
if strings.Contains(file.Name, query) {
file.Repo = repoLabel(file.Repo, true)
matches = append(matches, file)
}
}
if len(matches) == 0 {
log(4, "No matches found.")
os.Exit(1)
}
return matches, headers
}
func getRepoInfo(author string, repo string) {
type GithubRepoInfo struct {
Name string
Description string
Owner struct {
Login string
}
CloneURL string `json:"clone_url"`
Language string
License struct {
SpdxID string `json:"spdx_id"`
}
}
log(1, "Getting repository info from the Github API...")
url := "https://api.github.com/repos/" + author + "/" + repo
response, _ := sendGithubRequest(url)
if response == "" {
errorLogRaw("The Github API returned an empty response. This may be because you are getting rate limited. URL: %s", url)
os.Exit(1)
}
debugLog("Response:\n%s", response)
log(1, "Parsing response...")
var repoInfo GithubRepoInfo
err := json.Unmarshal([]byte(response), &repoInfo)
errorLog(err, "An error occurred while parsing the response.")
path := "samples/templates/" + strings.ToLower(repoInfo.Language) + ".json"
if pathExists(path, "An error occurred while checking for language template.") {
log(1, "Using language template: %s", path)
} else {
path = "samples/basic.json"
log(1, "Using default language template")
}
log(1, "Parsing description...")
repoInfo.Description = strings.ToUpper(string(repoInfo.Description[0])) + repoInfo.Description[1:]
if !strings.HasSuffix(repoInfo.Description, ".") {
repoInfo.Description += "."
}
file := readFile(path, "An error occurred while reading the sample file for %s.", repoInfo.Language)
finalFile := file
finalFile = strings.ReplaceAll(finalFile, "nameofpkg", repoInfo.Name)
finalFile = strings.ReplaceAll(finalFile, "mypkglicense", repoInfo.License.SpdxID)
finalFile = strings.ReplaceAll(finalFile, "mypkgdescription", repoInfo.Description)
finalFile = strings.ReplaceAll(finalFile, "mypkgauthor", repoInfo.Owner.Login)
finalFile = strings.ReplaceAll(finalFile, "git url", repoInfo.CloneURL)
finalFile = strings.ReplaceAll(finalFile, "nameoflang", repoInfo.Language)
log(1, "Writing generated package info...")
newFile(repoInfo.Name+".json", finalFile, "An error occurred while writing generated package info file")
log(0, "Successfully generated package info for %s.", repoInfo.Name)
}