2019-11-18 15:40:46 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2019-11-21 16:12:04 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"net/http"
|
2019-11-18 15:40:46 +01:00
|
|
|
|
|
|
|
"github.com/tweag/gerrit-queue/frontend"
|
|
|
|
"github.com/tweag/gerrit-queue/gerrit"
|
2019-12-02 12:03:46 +01:00
|
|
|
"github.com/tweag/gerrit-queue/misc"
|
2019-11-18 15:40:46 +01:00
|
|
|
"github.com/tweag/gerrit-queue/submitqueue"
|
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
|
|
|
|
2019-12-02 10:00:32 +01:00
|
|
|
"github.com/apex/log"
|
|
|
|
"github.com/apex/log/handlers/multi"
|
|
|
|
"github.com/apex/log/handlers/text"
|
2019-11-18 15:40:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-12-09 11:49:16 +01:00
|
|
|
var URL, username, password, projectName, branchName string
|
2019-11-18 15:40:46 +01:00
|
|
|
var fetchOnly bool
|
2019-12-03 10:44:29 +01:00
|
|
|
var triggerInterval int
|
2019-11-18 15:40:46 +01:00
|
|
|
|
|
|
|
app := cli.NewApp()
|
|
|
|
app.Name = "gerrit-queue"
|
|
|
|
|
|
|
|
app.Flags = []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "url",
|
|
|
|
Usage: "URL to the gerrit instance",
|
|
|
|
EnvVar: "GERRIT_URL",
|
|
|
|
Destination: &URL,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "username",
|
|
|
|
Usage: "Username to use to login to gerrit",
|
|
|
|
EnvVar: "GERRIT_USERNAME",
|
|
|
|
Destination: &username,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "password",
|
|
|
|
Usage: "Password to use to login to gerrit",
|
|
|
|
EnvVar: "GERRIT_PASSWORD",
|
|
|
|
Destination: &password,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "project",
|
|
|
|
Usage: "Gerrit project name to run the submit queue for",
|
|
|
|
EnvVar: "GERRIT_PROJECT",
|
|
|
|
Destination: &projectName,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "branch",
|
|
|
|
Usage: "Destination branch",
|
|
|
|
EnvVar: "GERRIT_BRANCH",
|
|
|
|
Destination: &branchName,
|
|
|
|
Value: "master",
|
|
|
|
},
|
2019-12-03 10:44:29 +01:00
|
|
|
cli.IntFlag{
|
|
|
|
Name: "trigger-interval",
|
|
|
|
Usage: "How often we should trigger ourselves (interval in seconds)",
|
|
|
|
EnvVar: "SUBMIT_QUEUE_TRIGGER_INTERVAL",
|
|
|
|
Destination: &triggerInterval,
|
|
|
|
Value: 600,
|
|
|
|
},
|
2019-11-18 15:40:46 +01:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "fetch-only",
|
|
|
|
Usage: "Only fetch changes and assemble queue, but don't actually write",
|
2019-11-21 19:01:08 +01:00
|
|
|
EnvVar: "SUBMIT_QUEUE_FETCH_ONLY",
|
2019-11-18 15:40:46 +01:00
|
|
|
Destination: &fetchOnly,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-12-02 12:03:46 +01:00
|
|
|
rotatingLogHandler := misc.NewRotatingLogHandler(10000)
|
2019-12-02 10:00:32 +01:00
|
|
|
l := &log.Logger{
|
|
|
|
Handler: multi.New(
|
|
|
|
text.New(os.Stderr),
|
2019-12-02 12:03:46 +01:00
|
|
|
rotatingLogHandler,
|
2019-12-02 10:00:32 +01:00
|
|
|
),
|
|
|
|
Level: log.DebugLevel,
|
|
|
|
}
|
|
|
|
|
2019-11-18 15:40:46 +01:00
|
|
|
app.Action = func(c *cli.Context) error {
|
2019-12-02 10:00:32 +01:00
|
|
|
gerrit, err := gerrit.NewClient(l, URL, username, password, projectName, branchName)
|
2019-11-18 15:40:46 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-02 10:00:32 +01:00
|
|
|
log.Infof("Successfully connected to gerrit at %s", URL)
|
2019-11-18 15:40:46 +01:00
|
|
|
|
2021-12-09 11:49:16 +01:00
|
|
|
runner := submitqueue.NewRunner(l, gerrit)
|
2019-11-18 15:40:46 +01:00
|
|
|
|
2019-12-02 12:03:46 +01:00
|
|
|
handler := frontend.MakeFrontend(rotatingLogHandler, gerrit, runner)
|
2019-11-18 15:40:46 +01:00
|
|
|
|
2019-11-21 16:12:04 +01:00
|
|
|
// fetch only on first run
|
2019-12-03 11:07:44 +01:00
|
|
|
err = runner.Trigger(fetchOnly)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
}
|
2019-11-18 15:40:46 +01:00
|
|
|
|
2019-11-21 16:12:04 +01:00
|
|
|
// ticker
|
|
|
|
go func() {
|
|
|
|
for {
|
2019-12-03 10:58:39 +01:00
|
|
|
time.Sleep(time.Duration(triggerInterval) * time.Second)
|
2019-12-03 11:07:44 +01:00
|
|
|
err = runner.Trigger(fetchOnly)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
}
|
2019-11-18 15:40:46 +01:00
|
|
|
}
|
2019-11-21 16:12:04 +01:00
|
|
|
}()
|
2019-11-18 15:40:46 +01:00
|
|
|
|
2019-11-21 16:12:04 +01:00
|
|
|
server := http.Server{
|
|
|
|
Addr: ":8080",
|
|
|
|
Handler: handler,
|
|
|
|
}
|
2019-11-18 15:40:46 +01:00
|
|
|
|
2019-11-21 16:12:04 +01:00
|
|
|
server.ListenAndServe()
|
|
|
|
if err != nil {
|
2019-12-02 10:00:32 +01:00
|
|
|
log.Fatalf(err.Error())
|
2019-11-18 15:40:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := app.Run(os.Args)
|
|
|
|
if err != nil {
|
2019-12-02 10:00:32 +01:00
|
|
|
log.Fatal(err.Error())
|
2019-11-18 15:40:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODOS:
|
|
|
|
// - handle event log, either by accepting webhooks, or by streaming events?
|
|
|
|
}
|