-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgithub.go
147 lines (129 loc) · 3.83 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
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/google/go-github/v53/github"
"github.com/patrickmn/go-cache"
"golang.org/x/oauth2"
)
func getJobs(c *cache.Cache, owner, repo string) Dashboard {
data, found := c.Get(fmt.Sprintf("%s-%s", owner, repo))
if found {
log.Println("cache found")
return data.(Dashboard)
}
log.Println("cache not found")
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
windowStart := time.Now().Add(time.Duration(-12) * time.Hour).UTC().Format(time.RFC3339)
opt := &github.ListWorkflowRunsOptions{
ListOptions: github.ListOptions{PerPage: 100},
Created: ">=" + windowStart,
}
var runs []*github.WorkflowRun
for {
resp, rr, err := client.Actions.ListRepositoryWorkflowRuns(context.Background(), owner, repo, opt)
if rlErr, ok := err.(*github.RateLimitError); ok { //nolint: errorlint
log.Printf("ListRepositoryWorkflowRuns ratelimited. Pausing until %s", rlErr.Rate.Reset.Time.String())
time.Sleep(time.Until(rlErr.Rate.Reset.Time))
continue
} else if err != nil {
log.Printf("ListRepositoryWorkflowRuns error for repo %s/%s: %s", owner, repo, err.Error())
os.Exit(1)
}
runs = append(runs, resp.WorkflowRuns...)
if rr.NextPage == 0 {
break
}
opt.Page = rr.NextPage
}
var report = make(map[string][]Status)
for _, run := range runs {
conclusion := run.GetConclusion()
if run.GetStatus() == "in_progress" {
conclusion = "progress"
} else if run.GetStatus() == "queued" {
conclusion = "queued"
}
tableStatus := getTableStatus(conclusion)
prInfo := ""
if run.GetEvent() == "pull_request" || run.GetEvent() == "pull_request_target" {
owner = run.GetRepository().GetOwner().GetLogin()
repo = run.GetRepository().GetName()
opts := &github.PullRequestListOptions{
State: "all",
}
pull, _, err := client.PullRequests.ListPullRequestsWithCommit(context.Background(), owner, repo, run.GetHeadSHA(), opts)
if rlErr, ok := err.(*github.RateLimitError); ok { //nolint: errorlint
log.Printf("ListRepositoryWorkflowRuns ratelimited. Pausing until %s", rlErr.Rate.Reset.Time.String())
time.Sleep(time.Until(rlErr.Rate.Reset.Time))
continue
} else if err != nil {
log.Printf("ListPullRequestsWithCommit error for repo %s/%s: %s", owner, repo, err.Error())
os.Exit(1)
}
prInfo = ""
if len(pull) == 1 {
prInfo = pull[0].GetHTMLURL()
}
}
_, ok := report[run.GetName()]
if ok {
report[run.GetName()] = append(report[run.GetName()], Status{
SHA: run.GetHeadSHA(),
Conclusion: conclusion,
TableStatus: tableStatus,
Status: run.GetStatus(),
JobHTML: run.GetHTMLURL(),
WorkflowID: run.GetWorkflowID(),
CreatedAt: run.GetCreatedAt().Time,
Event: run.GetEvent(),
PRUrl: prInfo,
})
} else {
report[run.GetName()] = []Status{
{
SHA: run.GetHeadSHA(),
Conclusion: conclusion,
TableStatus: tableStatus,
Status: run.GetStatus(),
JobHTML: run.GetHTMLURL(),
WorkflowID: run.GetWorkflowID(),
CreatedAt: run.GetCreatedAt().Time,
Event: run.GetEvent(),
PRUrl: prInfo,
},
}
}
}
dash := Dashboard{
Owner: owner,
Repo: repo,
DateGenerated: time.Now().Local().Format(time.RFC3339),
NextGeneration: time.Now().Add(15 * time.Minute).Local().Format(time.RFC3339),
Data: report,
}
c.Set(fmt.Sprintf("%s-%s", owner, repo), dash, 15*time.Minute)
return dash
}
func getTableStatus(conclusion string) string {
switch conclusion {
case "success":
return "success"
case "failure":
return "danger"
case "queued":
return "info"
case "cancelled":
return "secondary"
default:
return "warning"
}
}