This repository has been archived by the owner on Apr 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
251 lines (224 loc) · 7.42 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
package main
// TODO use Github Languages feature to figure out what code repositories are made of
// https://api.github.com/repos/libp2p/js-libp2p-half-closed-connection-upgrade/languages
// https://api.github.com/repos/libp2p/js-libp2p/languages
// TODO check and apply Labels as well
import (
"context"
"fmt"
"html/template"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/bradfitz/slice"
"github.com/google/go-github/github"
"github.com/gregjones/httpcache"
"github.com/gregjones/httpcache/leveldbcache"
"github.com/ipfs/ci-sync/checks"
"golang.org/x/oauth2"
)
type RepositoryStatus struct {
ID int64
FullName string
Links struct {
Jenkins string
JenkinsMaster string
GithubCollaborators string
GithubBranchProtection string
}
HasJenkins bool
IsJenkinsMasterPassing bool
HasCircle bool
HasTravis bool
HasAppVeyor bool
HasCITeam bool
CITeamRightPermissions bool
HasBranchProtection bool
PassesChecks bool
LastChecked time.Time
LastCheckedPretty string
}
type TemplateList struct {
Repositories []RepositoryStatus
}
func GetRateLimitStatus(client *github.Client) string {
ctx := context.Background()
rateLimits, _, err := client.RateLimits(ctx)
if err != nil {
panic(err)
}
core := rateLimits.GetCore()
limit := strconv.Itoa(core.Limit)
remaining := strconv.Itoa(core.Remaining)
reset := core.Reset.UTC().Format(time.RFC3339)
return "Rate limit status: " + limit + "/" + remaining + " Resets at " + reset
}
func CheckRepo(client *github.Client, repo *github.Repository) RepositoryStatus {
status := RepositoryStatus{}
status.FullName = repo.GetFullName()
status.ID = repo.GetID()
// status.Links
orgName := repo.GetOwner().GetLogin()
repoName := repo.GetName()
// TODO replace with blueocean UI, but doesn't work because of casing mismatch Github <> Jenkins
// status.Links.Jenkins = rootURL + orgName + "%2F" + repoName + "/activity"
// status.Links.JenkinsMaster = status.Links.Jenkins + "?branch=master"
status.Links.Jenkins = "https://ci.ipfs.team/job/" + orgName + "/job/" + repoName
status.Links.JenkinsMaster = "https://ci.ipfs.team/job/" + orgName + "/job/" + repoName + "/job/master/lastBuild"
status.Links.GithubCollaborators = "https://github.com/" + status.FullName + "/settings/collaboration"
status.Links.GithubBranchProtection = "https://github.com/" + status.FullName + "/settings/branches"
status.HasJenkins = checks.GithubRepositoryHasFile(client, repo, "ci/Jenkinsfile")
status.HasCircle = checks.GithubRepositoryHasFile(client, repo, "circle.yml")
status.HasTravis = checks.GithubRepositoryHasFile(client, repo, ".travis.yml")
status.HasAppVeyor = checks.GithubRepositoryHasFile(client, repo, "appveyor.yml")
if status.HasJenkins {
status.IsJenkinsMasterPassing = checks.JenkinsMasterPassing(repo)
}
hasCITeam, rightPermissions := checks.GithubRepositoryHasCITeam(client, repo)
status.HasCITeam = hasCITeam
status.CITeamRightPermissions = rightPermissions
status.HasBranchProtection = checks.GithubBranchProtection(client, repo)
status.LastChecked = time.Now()
status.LastCheckedPretty = status.LastChecked.UTC().Format(time.RFC3339)
if status.HasJenkins &&
status.IsJenkinsMasterPassing &&
!status.HasCircle &&
!status.HasTravis &&
!status.HasAppVeyor &&
status.HasCITeam &&
status.CITeamRightPermissions {
status.PassesChecks = true
} else {
status.PassesChecks = false
}
return status
}
var repositories = []RepositoryStatus{}
func main() {
// Make sure we have Github token in env
accessToken := os.Getenv("GITHUB_TOKEN")
if accessToken == "" {
panic("Missing environment variable GITHUB_TOKEN")
}
httpPort := os.Getenv("PORT")
if httpPort == "" {
panic("Missing environment variable PORT")
}
// cached client to relive some of the requests to Github
cache, err := leveldbcache.New("./cache")
if err != nil {
panic(err)
}
transport := httpcache.NewTransport(cache)
ctx := context.Background()
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: accessToken})
tc := &http.Client{
Transport: &oauth2.Transport{
Source: ts,
Base: transport,
},
}
client := github.NewClient(tc)
// HTTP server for index + healthcheck
http.HandleFunc("/", listHandler)
http.HandleFunc("/alive", aliveHandler)
go func() {
log.Fatal(http.ListenAndServe(":"+httpPort, nil))
}()
// Print out rate limit status once per minute
go func() {
for {
log.Println(GetRateLimitStatus(client))
time.Sleep(time.Second * 60) // TODO make configurable
}
}()
for {
log.Println("Updating")
repositories = []RepositoryStatus{}
var allRepos []*github.Repository
// Organizations to get repositories from
orgs := []string{"ipfs", "libp2p", "ipld", "multiformats", "ipfs-shipyard"}
// orgs := []string{"multiformats"}
for _, org := range orgs {
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
for {
log.Println("Fetching repos for " + org)
repos, resp, err := client.Repositories.ListByOrg(ctx, org, opt)
if err != nil {
panic(err)
}
for _, repo := range repos {
// Don't check archived repos
// Only use javascript repos for now
// if !repo.GetArchived() && strings.HasPrefix(repo.GetName(), "js-") {
// if !repo.GetArchived() {
fmt.Printf("https://github.com/%s/%s\n", org, repo.GetName())
// isCodeRepo := strings.HasPrefix(repo.GetName(), "go-") || strings.HasPrefix(repo.GetName(), "js-")
isCodeRepo := true
if !repo.GetArchived() && isCodeRepo {
allRepos = append(allRepos, repo)
}
}
// allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
}
// TODO attempt at concurrency, but need to add buffering so we don't get
// rate limited by Github while running
// wg := sync.WaitGroup{}
// statusCh := make(chan RepositoryStatus)
// go func() {
// for {
// status := <-statusCh
// numberOfRepos := strconv.Itoa(len(repositories))
// log.Println("Checked " + numberOfRepos + " repositories so far")
// repositories = append(repositories, status)
// wg.Done()
// }
// }()
// for _, repo := range allRepos {
// time.Sleep(time.Millisecond * 100)
// wg.Add(1)
// go func(client *github.Client, repo *github.Repository) {
// statusCh <- CheckRepo(client, repo)
// }(client, repo)
// }
// wg.Wait()
numberOfAllRepos := strconv.Itoa(len(allRepos))
for _, repo := range allRepos {
log.Println("## Checking " + repo.GetFullName())
// Actually perform the check
repositories = append(repositories, CheckRepo(client, repo))
numberOfRepos := strconv.Itoa(len(repositories))
log.Println("Checked " + numberOfRepos + "/" + numberOfAllRepos + " repositories so far")
}
log.Println("Done checking")
// Now lets fix repositories if we can
time.Sleep(time.Hour * 1) // TODO make configurable, currently once per hour
}
}
func aliveHandler(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("alive"))
if err != nil {
panic(err)
}
}
func listHandler(w http.ResponseWriter, r *http.Request) {
t, _ := template.ParseFiles("./templates/list.html")
// Sort repos
slice.Sort(repositories[:], func(i, j int) bool {
return repositories[i].FullName < repositories[j].FullName
})
list := TemplateList{Repositories: repositories}
err := t.Execute(w, list)
if err != nil {
panic(err)
}
}