about summary refs log tree commit diff
path: root/third_party/gerrit-queue/main.go
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-12-09T13·11+0300
committerVincent Ambo <mail@tazj.in>2021-12-09T13·13+0300
commit59f97332b3076afe80ed3fe92eb0e1da676e3a99 (patch)
tree07e3cb9b1cf29693e3de3f6a2dfcf22bd58ebf92 /third_party/gerrit-queue/main.go
parentff10b7ab8303d050a8d7d751611da88bc13a75b4 (diff)
parent24f5a642af3aa1627bbff977f0a101907a02c69f (diff)
subtree(3p/gerrit-queue): Vendor at commit '24f5a642' r/3170
Imported from github/tvlfyi/gerrit-queue, originally from
github/tweag/gerrit-queue but that upstream is unmaintained.

git-subtree-dir: third_party/gerrit-queue
git-subtree-mainline: ff10b7ab8303d050a8d7d751611da88bc13a75b4
git-subtree-split: 24f5a642af3aa1627bbff977f0a101907a02c69f
Change-Id: I307cc38185ab9e25eb102c95096298a150ae13a2
Diffstat (limited to 'third_party/gerrit-queue/main.go')
-rw-r--r--third_party/gerrit-queue/main.go139
1 files changed, 139 insertions, 0 deletions
diff --git a/third_party/gerrit-queue/main.go b/third_party/gerrit-queue/main.go
new file mode 100644
index 000000000000..d8577dad507e
--- /dev/null
+++ b/third_party/gerrit-queue/main.go
@@ -0,0 +1,139 @@
+//go:generate statik -f
+
+package main
+
+import (
+	"os"
+	"time"
+
+	"net/http"
+
+	"github.com/tweag/gerrit-queue/frontend"
+	"github.com/tweag/gerrit-queue/gerrit"
+	"github.com/tweag/gerrit-queue/misc"
+	"github.com/tweag/gerrit-queue/submitqueue"
+
+	"github.com/urfave/cli"
+
+	"github.com/apex/log"
+	"github.com/apex/log/handlers/multi"
+	"github.com/apex/log/handlers/text"
+)
+
+func main() {
+	var URL, username, password, projectName, branchName string
+	var fetchOnly bool
+	var triggerInterval int
+
+	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",
+		},
+		cli.IntFlag{
+			Name:        "trigger-interval",
+			Usage:       "How often we should trigger ourselves (interval in seconds)",
+			EnvVar:      "SUBMIT_QUEUE_TRIGGER_INTERVAL",
+			Destination: &triggerInterval,
+			Value:       600,
+		},
+		cli.BoolFlag{
+			Name:        "fetch-only",
+			Usage:       "Only fetch changes and assemble queue, but don't actually write",
+			EnvVar:      "SUBMIT_QUEUE_FETCH_ONLY",
+			Destination: &fetchOnly,
+		},
+	}
+
+	rotatingLogHandler := misc.NewRotatingLogHandler(10000)
+	l := &log.Logger{
+		Handler: multi.New(
+			text.New(os.Stderr),
+			rotatingLogHandler,
+		),
+		Level: log.DebugLevel,
+	}
+
+	app.Action = func(c *cli.Context) error {
+		gerrit, err := gerrit.NewClient(l, URL, username, password, projectName, branchName)
+		if err != nil {
+			return err
+		}
+		log.Infof("Successfully connected to gerrit at %s", URL)
+
+		runner := submitqueue.NewRunner(l, gerrit)
+
+		handler := frontend.MakeFrontend(rotatingLogHandler, gerrit, runner)
+
+		// fetch only on first run
+		err = runner.Trigger(fetchOnly)
+		if err != nil {
+			log.Error(err.Error())
+		}
+
+		// ticker
+		go func() {
+			for {
+				time.Sleep(time.Duration(triggerInterval) * time.Second)
+				err = runner.Trigger(fetchOnly)
+				if err != nil {
+					log.Error(err.Error())
+				}
+			}
+		}()
+
+		server := http.Server{
+			Addr:    ":8080",
+			Handler: handler,
+		}
+
+		server.ListenAndServe()
+		if err != nil {
+			log.Fatalf(err.Error())
+		}
+
+		return nil
+	}
+
+	err := app.Run(os.Args)
+	if err != nil {
+		log.Fatal(err.Error())
+	}
+
+	// TODOS:
+	// - handle event log, either by accepting webhooks, or by streaming events?
+}