-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
265 lines (229 loc) · 6.55 KB
/
main.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"sort"
"strconv"
"strings"
"time"
)
var user string
var pass string
var port string
var org string
var topic string
var include string
var exclude string
var divWidth string
var httpClient http.Client
type repoInfo struct {
Name string `json:"name"`
FullName string `json:"full_name"`
HTMLUrl string `json:"html_url"`
Topics []string `json:"topics"`
}
type commitInfo struct {
Sha string `json:"sha"`
}
type checkRunResponse struct {
TotalCount int `json:"total_count"`
CheckRuns []checkRunInfo `json:"check_runs"`
}
type checkRunInfo struct {
Name string `json:"name"`
Status string `json:"status"`
Conclusion string `json:"conclusion"`
CompletedAt string `json:"completed_at"`
}
type repoStatus struct {
Name string
Status string
URL string
Time string
}
func main() {
flag.StringVar(&user, "user", "", "github username")
flag.StringVar(&pass, "pass", "", "github password or access token")
flag.StringVar(&port, "port", "8080", "port to listen on")
flag.StringVar(&org, "org", "", "organization to pull repos from, blank for your own")
flag.StringVar(&topic, "topics", "", "topics (csv) to include from repo list, this is an or, so any match will include the repo")
flag.StringVar(&include, "include", "", "repositories (csv) to look at for check-run status")
flag.StringVar(&exclude, "exclude", "", "repositories (csv) to exclude looking at (useful if you are getting repos by topic)")
flag.StringVar(&divWidth, "width", "200px", "the repo status div min-width style")
flag.Parse()
httpClient = http.Client{Timeout: 10 * time.Second}
c := make(chan repoStatus)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
repos := getRepos()
for _, repo := range repos {
go doRepoWork(repo, c)
}
s := []repoStatus{}
for i := 0; i < len(repos); i++ {
resp := <-c
s = append(s, resp)
}
sort.Slice(s, func(i, j int) bool {
return s[i].Name < s[j].Name
})
fmt.Fprintf(w, "<html><head>")
fmt.Fprintf(w, "<style>")
fmt.Fprintf(w, "body { background-color: black; color: white; }")
fmt.Fprintf(w, "a { color: white; text-decoration: none; }")
fmt.Fprintf(w, "div { margin: 5px; padding: 15px; float: left; min-width: %v }", divWidth)
fmt.Fprintf(w, ".success { background-color: #259225; }")
fmt.Fprintf(w, ".unknown { background-color: #9d9d9d; }")
fmt.Fprintf(w, ".failure { background-color: #eb0000; }")
fmt.Fprintf(w, ".inprogress { background-color: #6492a3; }")
fmt.Fprintf(w, "</style>")
fmt.Fprintf(w, "</head><body>")
for _, v := range s {
fmt.Fprintf(w, `<a href="%s" target="_blank"><div class="%s"><p>%s</p><p>%s</p><p>%s</p></div></a>`, v.URL, v.Status, v.Name, timeSince(v.Time), v.Time)
}
fmt.Fprintf(w, "</body></html>")
})
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func getRepos() []repoInfo {
var body []byte
if org != "" {
body = httpRequest("https://api.github.com/orgs/" + org + "/repos?per_page=100")
} else if user != "" {
body = httpRequest("https://api.github.com/users/" + user + "/repos?per_page=100")
} else {
log.Fatal("Either user or org is required")
}
// change our CSVs into maps for easier lookup
includes := make(map[string]bool)
excludes := make(map[string]bool)
topics := make(map[string]bool)
for _, v := range strings.Split(include, ",") {
includes[v] = true
}
for _, v := range strings.Split(exclude, ",") {
excludes[v] = true
}
for _, v := range strings.Split(topic, ",") {
topics[v] = true
}
allRepos := []repoInfo{}
err := json.Unmarshal(body, &allRepos)
if err != nil {
log.Fatalf("Error parsing repo list: %v", err)
}
// loop over the repos and only return the ones we care about
list := []repoInfo{}
for _, repo := range allRepos {
// skip excludes
if !excludes[repo.Name] {
// include if its on the include list
if includes[repo.Name] {
list = append(list, repo)
} else {
// include if it has a matching topic
for _, t := range repo.Topics {
if topics[t] {
list = append(list, repo)
}
}
}
}
}
return list
}
func doRepoWork(repo repoInfo, c chan<- repoStatus) {
sha := getShaOfLastCommit(repo.FullName)
runInfo := getCheckRunStatusOfLastCommit(repo.FullName, sha)
status := repoStatus{
Name: repo.Name,
Status: "unknown",
URL: repo.HTMLUrl,
Time: "",
}
if runInfo != nil {
if runInfo.CompletedAt != "" {
status.Status = runInfo.Conclusion
status.Time = runInfo.CompletedAt
}
if runInfo.Status == "in_progress" {
status.Status = "inprogress"
status.Time = "in progress"
}
}
c <- status
}
func getShaOfLastCommit(repo string) string {
body := httpRequest("https://api.github.com/repos/" + repo + "/commits")
commits := []commitInfo{}
err := json.Unmarshal(body, &commits)
if err != nil {
log.Fatalf("Error parsing commit list: %v", err)
}
return commits[0].Sha
}
func getCheckRunStatusOfLastCommit(repo string, sha string) *checkRunInfo {
body := httpRequest("https://api.github.com/repos/" + repo + "/commits/" + sha + "/check-runs")
info := checkRunResponse{}
err := json.Unmarshal(body, &info)
if err != nil {
log.Fatalf("Error parsing check-run info: %v", err)
}
if info.TotalCount > 0 {
return &info.CheckRuns[0]
}
return nil
}
func httpRequest(url string) []byte {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Fatalf("%v", err)
}
if user != "" && pass != "" {
req.SetBasicAuth(user, pass)
}
// needed for repo topics, check-run status
req.Header.Add("Accept", "application/vnd.github.mercy-preview+json,application/vnd.github.antiope-preview+json")
resp, err := httpClient.Do(req)
if err != nil {
log.Fatalf("Error performing get %v: %v", url, err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Error reading %v response body: %v", url, err)
}
return body
}
func timeSince(t string) string {
if t == "" || t == "in progress" {
return ""
}
count := 0
unit := ""
ts, _ := time.Parse(time.RFC3339, t)
since := time.Since(ts)
if since.Seconds() >= 86400*30 {
count = int(since.Seconds() / (86400 * 30))
unit = "month"
} else if since.Seconds() >= 86400 {
count = int(since.Seconds() / 86400)
unit = "day"
} else if since.Seconds() >= 3600 {
count = int(since.Seconds() / 3600)
unit = "hour"
} else if since.Seconds() >= 60 {
count = int(since.Seconds() / 60)
unit = "minute"
} else {
count = int(since.Seconds())
unit = "second"
}
if count > 1 {
unit += "s"
}
return strconv.Itoa(count) + " " + unit + " ago"
}