/
handler.go
233 lines (200 loc) · 7.44 KB
/
handler.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
package app
import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"time"
"github.com/go-logr/zapr"
"github.com/google/go-github/v52/github"
"github.com/jlewi/hydros/api/v1alpha1"
hGithub "github.com/jlewi/hydros/pkg/github"
"github.com/jlewi/hydros/pkg/github/ghrepo"
"github.com/jlewi/hydros/pkg/gitops"
"github.com/jlewi/hydros/pkg/util"
"github.com/palantir/go-githubapp/appconfig"
"github.com/palantir/go-githubapp/githubapp"
"github.com/pkg/errors"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
)
const (
// HydrosConfigPath default path to look for the hydros repository configuration file.
// TODO(jeremy): We should expose this as a configuration option for hydros.
HydrosConfigPath = "hydros.yaml"
// SharedRepository is the name of the repository containing the shared hydros configurationf or all repositories
SharedRepository = ".github"
)
// HydrosHandler is a handler for certain GitHub events. It currently handles PushEvents by sending them to
// Renderer which knows how to do in place modification using KRMs.
// TODO(jeremy): Also handle syncer.
type HydrosHandler struct {
githubapp.ClientCreator
Manager *gitops.Manager
// TODO(jeremy): ClientCreator and TransportManager are somewhat redundant.
transports *hGithub.TransportManager
workDir string
fetcher *ConfigFetcher
}
// NewHandler starts a new HydrosHandler for GitHub.
// cc: ClientCreator for creating GitHub clients.
// transports: Manage GitHub transports
// workDir: The directory to use for storing temporary files and checking out repositories
// numWorkers: The number of workers to use for processing events
func NewHandler(cc githubapp.ClientCreator, transports *hGithub.TransportManager, workDir string, numWorkers int) (*HydrosHandler, error) {
log := zapr.NewLogger(zap.L())
fetcher := &ConfigFetcher{Loader: appconfig.NewLoader(
[]string{HydrosConfigPath},
appconfig.WithOwnerDefault(SharedRepository, []string{
HydrosConfigPath,
}),
)}
if workDir == "" {
tDir, err := os.MkdirTemp("", "hydros")
if err != nil {
return nil, errors.Wrapf(err, "Failed to create temporary directory")
}
log.Info("Setting workDir to temporary directory", "workDir", tDir)
workDir = tDir
}
manager, err := gitops.NewManager([]gitops.Reconciler{})
if err != nil {
return nil, err
}
if err := manager.Start(numWorkers, 1*time.Hour); err != nil {
return nil, err
}
handler := &HydrosHandler{
ClientCreator: cc,
transports: transports,
fetcher: fetcher,
workDir: workDir,
Manager: manager,
}
return handler, nil
}
func (h *HydrosHandler) Handles() []string {
return []string{"push"}
}
func (h *HydrosHandler) Handle(ctx context.Context, eventType, deliveryID string, payload []byte) error {
log := zapr.NewLogger(zap.L()).WithValues("eventType", eventType, "deliverID", deliveryID)
log.V(util.Debug).Info("Got github webhook")
r := bytes.NewBuffer(payload)
d := json.NewDecoder(r)
// Handle push events.
// GitHub push events are sent whenever a branch is pushed. I believe a single push could contain multiple commits.
// When a PR is merged, that will trigger a push with the commits pushed to the target branch.
event := &github.PushEvent{}
if err := d.Decode(event); err != nil {
log.Error(err, "Failed to decode a PushEvent")
return err
}
repo := event.GetRepo()
action := event.GetAction()
log.Info("Got push event", "url", repo.GetURL(), "action", action)
repoName, err := ghrepo.FromFullName(repo.GetFullName())
if err != nil {
return err
}
installationID := githubapp.GetInstallationIDFromEvent(event)
client, err := h.NewInstallationClient(installationID)
if err != nil {
return err
}
refsPrefix := "refs/heads/"
branch := event.GetRef()[len(refsPrefix):]
config := h.fetcher.ConfigForRepositoryBranch(context.Background(), client, repoName.RepoOwner(), repoName.RepoName(), branch)
if config.LoadError != nil {
log.Error(config.LoadError, "Error loading config")
return config.LoadError
}
if config.Config == nil {
log.V(util.Debug).Info("No config found", "owner", repoName.RepoOwner(), "repo", repoName.RepoName(), "branch", branch)
return nil
}
if msg, ok := v1alpha1.IsValid(config.Config); !ok {
log.Error(errors.Errorf(msg), "Invalid configuration", repoName.RepoOwner(), "repo", repoName.RepoName(), "branch", branch)
_, _, err := client.Checks.CreateCheckRun(ctx, repoName.RepoOwner(), repoName.RepoName(), github.CreateCheckRunOptions{
Name: "hydros",
HeadSHA: event.GetAfter(),
DetailsURL: proto.String("https://url.not.set.yet"),
Status: proto.String("completed"),
Conclusion: proto.String("failure"),
Output: &github.CheckRunOutput{
Title: proto.String("Hydros failed"),
Summary: proto.String("Hydros invalid configuration"),
Text: proto.String(fmt.Sprintf("Hydros failed because config file, %v, is invalid. %s", HydrosConfigPath, msg)),
},
})
if err != nil {
log.Error(err, "Failed to create check run")
}
return errors.Errorf(msg)
}
// TODO(jeremy): Payload in push_event contains a list of added/removed/modified files. We could use that
// to determine whether hydros needs to run.
// Check if its a branch for which we do in place configuration.
var inPlaceConfig *v1alpha1.InPlaceConfig
for _, inPlace := range config.Config.Spec.InPlaceConfigs {
if inPlace.BaseBranch == branch {
inPlaceConfig = &inPlace
break
}
}
if inPlaceConfig == nil {
log.Info("branch isn't configured for inplace changes. Skipping", "ref", event.GetRef())
// Update the PR with a CreateCheckRun
check, response, err := client.Checks.CreateCheckRun(ctx, repoName.RepoOwner(), repoName.RepoName(), github.CreateCheckRunOptions{
Name: "hydros",
HeadSHA: event.GetAfter(),
DetailsURL: proto.String("https://url.not.set.yet"),
Status: proto.String("completed"),
Conclusion: proto.String("skipped"),
Output: &github.CheckRunOutput{
Title: proto.String("Hydros skipped"),
Summary: proto.String("Hydros skipped"),
Text: proto.String("Hydros skipped because this is not the main branch."),
},
})
if err != nil {
log.Error(err, "Failed to create check")
return err
}
log.Info("Created check", "check", check, "response", response)
return nil
}
// Determine the name for the reconciler
// It should be unique for each repo and also particular type of reconciler.
rName := gitops.RendererName(repoName.RepoOwner(), repoName.RepoName())
if !h.Manager.HasReconciler(rName) {
log.Info("Creating reconciler", "name", rName)
// Make sure workdir is unique for each reconciler.
workDir := filepath.Join(h.workDir, rName)
r, err := gitops.NewRenderer(repoName.RepoOwner(), repoName.RepoName(), workDir, h.transports)
if err != nil {
return err
}
if err := h.Manager.AddReconciler(r); err != nil {
if !gitops.IsDuplicateReconciler(err) {
return err
}
log.Info("Ignoring AddReconciler DuplicateReconciler error; assuming its a race condition caused by simultaneous webhooks", "name", rName)
}
}
// Enqueue a sync event.
h.Manager.Enqueue(rName, gitops.RenderEvent{
// https://docs.github.com/en/webhooks-and-events/webhooks/webhook-events-and-payloads#push
// "After" is the commit after the push.
Commit: event.GetAfter(),
// HydrosConfig could potentially be different for different commits
// So we pass it along with the event
BranchConfig: inPlaceConfig,
})
if err != nil {
log.Error(err, "Failed to create check")
return err
}
return nil
}