-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor.go
186 lines (163 loc) · 4.15 KB
/
executor.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
package cultivator
import (
"context"
"fmt"
"io/fs"
"net/http"
"os"
"path"
"path/filepath"
"github.com/bradleyfalzon/ghinstallation"
"github.com/google/go-github/v52/github"
)
// Executor defines a cultivator instance
type Executor struct {
Config Config
}
// NewFromFile creates a new Executor from a config file
func NewFromFile(fileArg string) (Executor, error) {
c, err := loadConfig(fileArg)
if err != nil {
return Executor{}, err
}
logger.DebugMsg(fmt.Sprintf("creating executor with config: %+v", c))
return Executor{Config: c}, err
}
// Execute checks on all visible repos
func (e *Executor) Execute() error {
targets, err := e.targets()
if err != nil {
return err
}
logger.DebugMsg(fmt.Sprintf("found %d targets", len(targets)))
checks, err := e.checks()
if err != nil {
return err
}
logger.DebugMsg(fmt.Sprintf("running %d checks", len(checks)))
for _, c := range checks {
err := e.runCheck(c, targets)
if err != nil {
return err
}
}
return nil
}
func (e *Executor) checks() ([]string, error) {
var res []string
logger.DebugMsg(fmt.Sprintf("loading checks from %s", e.Config.CheckDir))
err := filepath.Walk(e.Config.CheckDir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
logger.DebugMsg(fmt.Sprintf("failed to walk %s", path))
return err
}
if !info.IsDir() && info.Mode()&0100 != 0 {
logger.DebugMsg(fmt.Sprintf("found check %s", path))
res = append(res, path)
}
return nil
})
if err != nil {
return []string{}, err
}
return res, nil
}
func (e *Executor) runCheck(c string, targets []target) error {
dir, err := os.MkdirTemp("", "cultivator-")
if err != nil {
return err
}
logger.DebugMsg(fmt.Sprintf("created tmpdir for %s at %s", c, dir))
defer os.RemoveAll(dir)
for _, t := range targets {
logger.DebugMsg(fmt.Sprintf("running %s on %s", c, t.Path))
if err := t.sync(); err != nil {
return err
}
if err := t.runCheck(c, dir); err != nil {
return err
}
}
return nil
}
func (e *Executor) appClient() (*github.Client, error) {
logger.DebugMsg("creating app client")
itr, err := ghinstallation.NewAppsTransportKeyFromFile(
http.DefaultTransport,
int64(e.Config.IntegrationID),
e.Config.PrivateKeyFile,
)
if err != nil {
return nil, err
}
return github.NewClient(&http.Client{Transport: itr}), nil
}
func (e *Executor) installClient(installID int64) (*github.Client, error) {
logger.DebugMsg(fmt.Sprintf("creating install client for %d", installID))
itr, err := ghinstallation.NewKeyFromFile(
http.DefaultTransport,
int64(e.Config.IntegrationID),
installID,
e.Config.PrivateKeyFile,
)
if err != nil {
return nil, err
}
return github.NewClient(&http.Client{Transport: itr}), nil
}
func (e *Executor) basicAuth(installID int64) (string, error) {
logger.DebugMsg(fmt.Sprintf("creating basic auth token for %d", installID))
appClient, err := e.appClient()
if err != nil {
return "", err
}
token, _, err := appClient.Apps.CreateInstallationToken(context.Background(), installID, nil)
if err != nil {
return "", err
}
return "x-access-token:" + *token.Token, nil
}
func (e *Executor) targets() ([]target, error) {
var t []target
appClient, err := e.appClient()
if err != nil {
return t, err
}
user, _, err := appClient.Apps.Get(context.Background(), "")
if err != nil {
return t, err
}
installations, _, err := appClient.Apps.ListInstallations(context.Background(), nil)
logger.DebugMsg(fmt.Sprintf("found %d installations", len(installations)))
for _, i := range installations {
installClient, err := e.installClient(*i.ID)
if err != nil {
return t, err
}
ba, err := e.basicAuth(*i.ID)
if err != nil {
return t, err
}
opt := &github.ListOptions{PerPage: 100}
for {
repos, resp, err := installClient.Apps.ListRepos(context.Background(), opt)
if err != nil {
return t, err
}
for _, r := range repos.Repositories {
t = append(t, target{
Data: r,
Path: path.Join(e.Config.CacheDir, *r.FullName),
Client: installClient,
Slug: *user.Slug,
BasicAuth: ba,
})
}
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
}
return t, nil
}