-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.go
349 lines (307 loc) · 9.36 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package main
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/signal"
"strings"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/gin-gonic/gin"
"github.com/google/go-github/v28/github"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
clientgithub "github.com/mendersoftware/integration-test-runner/client/github"
"github.com/mendersoftware/integration-test-runner/git"
"github.com/mendersoftware/integration-test-runner/logger"
)
type config struct {
dryRunMode bool
githubSecret []byte
githubProtocol gitProtocol
githubOrganization string
githubToken string
gitlabToken string
gitlabBaseURL string
integrationDirectory string
isProcessPushEvents bool
isProcessPREvents bool
isProcessCommentEvents bool
reposSyncList []string
}
type buildOptions struct {
pr string
repo string
baseBranch string
commitSHA string
makeQEMU bool
}
// Mapping https://github.com/<org> -> https://gitlab.com/Northern.tech/<group>
var gitHubOrganizationToGitLabGroup = map[string]string{
"mendersoftware": "Mender",
"cfengine": "CFEngine",
"NorthernTechHQ": "NorthernTechHQ",
}
// Mapping of special repos that have a custom group/project
var gitHubRepoToGitLabProjectCustom = map[string]string{
"saas": "Northern.tech/MenderSaaS/saas",
}
var qemuBuildRepositories = []string{
"meta-mender",
"mender",
"mender-artifact",
"mender-connect",
"monitor-client",
"mender-auth-azure-iot",
"mender-gateway",
"mender-snapshot",
}
const (
KiB = 1024
MiB = 1024 * KiB
)
const (
gitOperationTimeout = 30
)
const (
featureBranchPrefix = "feature-"
)
const (
githubBotName = "mender-test-bot"
)
const (
commandStartIntegrationPipeline = "start integration pipeline"
commandStartClientPipeline = "start client pipeline"
commandCherryPickBranch = "cherry-pick to:"
commandConventionalCommit = "mark-pr as"
commandSyncRepos = "sync"
)
func getConfig() (*config, error) {
var reposSyncList []string
dryRunMode := os.Getenv("DRY_RUN") != ""
githubSecret := os.Getenv("GITHUB_SECRET")
githubToken := os.Getenv("GITHUB_TOKEN")
gitlabToken := os.Getenv("GITLAB_TOKEN")
gitlabBaseURL := os.Getenv("GITLAB_BASE_URL")
integrationDirectory := "/integration/"
if integrationDirEnv := os.Getenv("INTEGRATION_DIRECTORY"); integrationDirEnv != "" {
integrationDirectory = integrationDirEnv
}
//
// Currently we don't have a distinguishment between GitHub events and features.
// Different features might be implemented across different events, but in future
// it's probability that we might implement proper features selection. For now the
// straight goal is to being able to configure the runner to only sync repos on
// push events and disable all the rest (to be used by the CFEngine team).
//
// default: process push events (sync repos) if not explicitly disabled
isProcessPushEvents := os.Getenv("DISABLE_PUSH_EVENTS_PROCESSING") == ""
// default: process PR events if not explicitly disabled
isProcessPREvents := os.Getenv("DISABLE_PR_EVENTS_PROCESSING") == ""
// default: process comment events if not explicitly disabled
isProcessCommentEvents := os.Getenv("DISABLE_COMMENT_EVENTS_PROCESSING") == ""
logLevel, found := os.LookupEnv("INTEGRATION_TEST_RUNNER_LOG_LEVEL")
logrus.SetLevel(logrus.InfoLevel)
if found {
lvl, err := logrus.ParseLevel(logLevel)
if err != nil {
logrus.Infof(
"Failed to parse the 'INTEGRATION_TEST_RUNNER_LOG_LEVEL' variable, " +
"defaulting to 'InfoLevel'",
)
} else {
logrus.Infof("Set 'LogLevel' to %s", lvl)
logrus.SetLevel(lvl)
}
}
// Comma separated list of repos to sync (GitHub->GitLab)
reposSyncListRaw, found := os.LookupEnv("SYNC_REPOS_LIST")
if found {
reposSyncList = strings.Split(reposSyncListRaw, ",")
}
switch {
case githubSecret == "" && !dryRunMode:
return &config{}, fmt.Errorf("set GITHUB_SECRET")
case githubToken == "":
return &config{}, fmt.Errorf("set GITHUB_TOKEN")
case gitlabToken == "":
return &config{}, fmt.Errorf("set GITLAB_TOKEN")
case gitlabBaseURL == "":
return &config{}, fmt.Errorf("set GITLAB_BASE_URL")
case integrationDirectory == "":
return &config{}, fmt.Errorf("set INTEGRATION_DIRECTORY")
}
return &config{
dryRunMode: dryRunMode,
githubSecret: []byte(githubSecret),
githubProtocol: gitProtocolSSH,
githubToken: githubToken,
gitlabToken: gitlabToken,
gitlabBaseURL: gitlabBaseURL,
integrationDirectory: integrationDirectory,
isProcessPushEvents: isProcessPushEvents,
isProcessPREvents: isProcessPREvents,
isProcessCommentEvents: isProcessCommentEvents,
reposSyncList: reposSyncList,
}, nil
}
func getCustomLoggerFromContext(ctx *gin.Context) *logrus.Entry {
deliveryID, ok := ctx.Get("delivery")
if !ok || !isStringType(deliveryID) {
return logrus.WithField("delivery", "nil")
}
return logrus.WithField("delivery", deliveryID)
}
func isStringType(i interface{}) bool {
switch i.(type) {
case string:
return true
default:
return false
}
}
func processGitHubWebhookRequest(
ctx *gin.Context,
payload []byte,
githubClient clientgithub.Client,
conf *config,
) {
webhookType := github.WebHookType(ctx.Request)
webhookEvent, _ := github.ParseWebHook(webhookType, payload)
_ = processGitHubWebhook(ctx, webhookType, webhookEvent, githubClient, conf)
}
func processGitHubWebhook(
ctx *gin.Context,
webhookType string,
webhookEvent interface{},
githubClient clientgithub.Client,
conf *config,
) error {
githubOrganization, err := getGitHubOrganization(webhookType, webhookEvent)
if err != nil {
logrus.Warnln("ignoring event: ", err.Error())
return nil
}
conf.githubOrganization = githubOrganization
switch webhookType {
case "pull_request":
if conf.isProcessPREvents {
pr := webhookEvent.(*github.PullRequestEvent)
return processGitHubPullRequest(ctx, pr, githubClient, conf)
} else {
logrus.Infof("Webhook event %s processing is skipped", webhookType)
}
case "push":
if conf.isProcessPushEvents {
push := webhookEvent.(*github.PushEvent)
return processGitHubPush(ctx, push, githubClient, conf)
} else {
logrus.Infof("Webhook event %s processing is skipped", webhookType)
}
case "issue_comment":
if conf.isProcessCommentEvents {
comment := webhookEvent.(*github.IssueCommentEvent)
return processGitHubComment(ctx, comment, githubClient, conf)
} else {
logrus.Infof("Webhook event %s processing is skipped", webhookType)
}
}
return nil
}
func setupLogging(conf *config, requestLogger logger.RequestLogger) {
// Log to stdout and with JSON format; suitable for GKE
formatter := &logrus.JSONFormatter{
FieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "time",
logrus.FieldKeyLevel: "level",
logrus.FieldKeyMsg: "message",
},
}
if conf.dryRunMode {
mw := io.MultiWriter(os.Stdout, requestLogger)
logrus.SetOutput(mw)
} else {
logrus.SetOutput(os.Stdout)
}
logrus.SetFormatter(formatter)
}
func main() {
doMain()
}
var githubClient clientgithub.Client
func doMain() {
conf, err := getConfig()
if err != nil {
logrus.Fatalf("failed to load config: %s", err.Error())
}
requestLogger := logger.NewRequestLogger()
logger.SetRequestLogger(requestLogger)
setupLogging(conf, requestLogger)
git.SetDryRunMode(conf.dryRunMode)
logrus.Infoln("using settings: ", spew.Sdump(conf))
githubClient = clientgithub.NewGitHubClient(conf.githubToken, conf.dryRunMode)
r := gin.Default()
filter := "/_health"
if logrus.GetLevel() == logrus.DebugLevel || logrus.GetLevel() == logrus.TraceLevel {
filter = ""
}
r.Use(gin.LoggerWithWriter(gin.DefaultWriter, filter))
r.Use(gin.Recovery())
// webhook for GitHub
r.POST("/", func(context *gin.Context) {
payload, err := github.ValidatePayload(context.Request, conf.githubSecret)
if err != nil {
var mbErr *http.MaxBytesError
if errors.As(err, &mbErr) {
context.Status(http.StatusRequestEntityTooLarge)
return
}
logrus.Warnln("payload failed to validate, ignoring.")
context.Status(http.StatusForbidden)
return
}
context.Set("delivery", github.DeliveryID(context.Request))
if conf.dryRunMode {
processGitHubWebhookRequest(context, payload, githubClient, conf)
} else {
go processGitHubWebhookRequest(context, payload, githubClient, conf)
}
context.Status(http.StatusAccepted)
})
// 200 replay for the loadbalancer
r.GET("/_health", func(_ *gin.Context) {})
r.GET("/", func(_ *gin.Context) {})
// dry-run mode, end-point to retrieve and clear logs
if conf.dryRunMode {
r.GET("/logs", func(context *gin.Context) {
logs := requestLogger.Get()
context.JSON(http.StatusOK, logs)
})
r.DELETE("/logs", func(context *gin.Context) {
requestLogger.Clear()
context.Writer.WriteHeader(http.StatusNoContent)
})
}
srv := &http.Server{
Addr: "0.0.0.0:8080",
Handler: http.MaxBytesHandler(r, 10*MiB),
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logrus.Fatalf("Failed listening: %s\n", err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, unix.SIGINT, unix.SIGTERM)
<-quit
logrus.Info("Shutdown server ...")
ctx := context.Background()
ctxWithTimeout, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctxWithTimeout); err != nil {
logrus.Fatal("Failed to shutdown the server: ", err)
}
}