-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathgithub_fetcher.go
More file actions
111 lines (97 loc) · 2.72 KB
/
github_fetcher.go
File metadata and controls
111 lines (97 loc) · 2.72 KB
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
package metrics
import (
"context"
"log"
"strings"
"time"
"github.com/google/go-github/v45/github"
"github.com/spendesk/github-actions-exporter/pkg/config"
)
var (
repositories []string
workflows map[string]map[int64]github.Workflow
)
func getAllReposForOrg(orga string) []string {
var all_repos []string
opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{
PerPage: 200,
Page: 0,
},
}
for {
repos_page, resp, err := client.Repositories.ListByOrg(context.Background(), orga, opt)
if rl_err, ok := err.(*github.RateLimitError); ok {
log.Printf("ListByOrg ratelimited. Pausing until %s", rl_err.Rate.Reset.Time.String())
time.Sleep(time.Until(rl_err.Rate.Reset.Time))
continue
} else if err != nil {
log.Printf("ListByOrg error for %s: %s", orga, err.Error())
break
}
for _, repo := range repos_page {
all_repos = append(all_repos, *repo.FullName)
}
if resp.NextPage == 0 {
break
}
opt.ListOptions.Page = resp.NextPage
}
return all_repos
}
func getAllWorkflowsForRepo(owner string, repo string) map[int64]github.Workflow {
res := make(map[int64]github.Workflow)
opt := &github.ListOptions{
PerPage: 200,
Page: 0,
}
for {
workflows_page, resp, err := client.Actions.ListWorkflows(context.Background(), owner, repo, opt)
if rl_err, ok := err.(*github.RateLimitError); ok {
log.Printf("ListWorkflows ratelimited. Pausing until %s", rl_err.Rate.Reset.Time.String())
time.Sleep(time.Until(rl_err.Rate.Reset.Time))
continue
} else if err != nil {
log.Printf("ListWorkflows error for %s: %s", repo, err.Error())
return res
}
for _, w := range workflows_page.Workflows {
res[*w.ID] = *w
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
return res
}
func periodicGithubFetcher() {
for {
// Fetch repositories (if dynamic)
var repos_to_fetch []string
if len(config.Github.Repositories.Value()) > 0 {
repos_to_fetch = config.Github.Repositories.Value()
} else {
for _, orga := range config.Github.Organizations.Value() {
repos_to_fetch = append(repos_to_fetch, getAllReposForOrg(orga)...)
}
}
repositories = repos_to_fetch
// Fetch workflows
non_empty_repos := make([]string, 0)
ww := make(map[string]map[int64]github.Workflow)
for _, repo := range repos_to_fetch {
r := strings.Split(repo, "/")
workflows_for_repo := getAllWorkflowsForRepo(r[0], r[1])
if len(workflows_for_repo) == 0 {
continue
}
non_empty_repos = append(non_empty_repos, repo)
ww[repo] = workflows_for_repo
log.Printf("Fetched %d workflows for repository %s", len(ww[repo]), repo)
}
repositories = non_empty_repos
workflows = ww
time.Sleep(time.Duration(config.Github.Refresh) * time.Second)
}
}