about summary refs log tree commit diff
path: root/third_party/gerrit-queue/frontend/frontend.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/frontend/frontend.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/frontend/frontend.go')
-rw-r--r--third_party/gerrit-queue/frontend/frontend.go117
1 files changed, 117 insertions, 0 deletions
diff --git a/third_party/gerrit-queue/frontend/frontend.go b/third_party/gerrit-queue/frontend/frontend.go
new file mode 100644
index 000000000000..f1c7857ef87c
--- /dev/null
+++ b/third_party/gerrit-queue/frontend/frontend.go
@@ -0,0 +1,117 @@
+package frontend
+
+import (
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"encoding/json"
+
+	"html/template"
+
+	"github.com/gin-gonic/gin"
+	"github.com/rakyll/statik/fs"
+
+	"github.com/apex/log"
+
+	"github.com/tweag/gerrit-queue/gerrit"
+	"github.com/tweag/gerrit-queue/misc"
+	_ "github.com/tweag/gerrit-queue/statik" // register static assets
+	"github.com/tweag/gerrit-queue/submitqueue"
+)
+
+//loadTemplate loads a list of templates, relative to the statikFS root, and a FuncMap, and returns a template object
+func loadTemplate(templateNames []string, funcMap template.FuncMap) (*template.Template, error) {
+	if len(templateNames) == 0 {
+		return nil, fmt.Errorf("templateNames can't be empty")
+	}
+	tmpl := template.New(templateNames[0]).Funcs(funcMap)
+	statikFS, err := fs.New()
+	if err != nil {
+		return nil, err
+	}
+
+	for _, templateName := range templateNames {
+		r, err := statikFS.Open("/" + templateName)
+		if err != nil {
+			return nil, err
+		}
+		defer r.Close()
+		contents, err := ioutil.ReadAll(r)
+		if err != nil {
+			return nil, err
+		}
+		tmpl, err = tmpl.Parse(string(contents))
+		if err != nil {
+			return nil, err
+		}
+	}
+
+	return tmpl, nil
+}
+
+// MakeFrontend returns a http.Handler
+func MakeFrontend(rotatingLogHandler *misc.RotatingLogHandler, gerritClient *gerrit.Client, runner *submitqueue.Runner) http.Handler {
+	router := gin.Default()
+
+	projectName := gerritClient.GetProjectName()
+	branchName := gerritClient.GetBranchName()
+
+	router.GET("/", func(c *gin.Context) {
+		var wipSerie *gerrit.Serie = nil
+		HEAD := ""
+		currentlyRunning := runner.IsCurrentlyRunning()
+
+		// don't trigger operations requiring a lock
+		if !currentlyRunning {
+			wipSerie = runner.GetWIPSerie()
+			HEAD = gerritClient.GetHEAD()
+		}
+
+		funcMap := template.FuncMap{
+			"changesetURL": func(changeset *gerrit.Changeset) string {
+				return gerritClient.GetChangesetURL(changeset)
+			},
+			"levelToClasses": func(level log.Level) string {
+				switch level {
+				case log.DebugLevel:
+					return "text-muted"
+				case log.InfoLevel:
+					return "text-info"
+				case log.WarnLevel:
+					return "text-warning"
+				case log.ErrorLevel:
+					return "text-danger"
+				case log.FatalLevel:
+					return "text-danger"
+				default:
+					return "text-white"
+				}
+			},
+			"fieldsToJSON": func(fields log.Fields) string {
+				jsonData, _ := json.Marshal(fields)
+				return string(jsonData)
+			},
+		}
+
+		tmpl := template.Must(loadTemplate([]string{
+			"index.tmpl.html",
+			"serie.tmpl.html",
+			"changeset.tmpl.html",
+		}, funcMap))
+
+		tmpl.ExecuteTemplate(c.Writer, "index.tmpl.html", gin.H{
+			// Config
+			"projectName": projectName,
+			"branchName":  branchName,
+
+			// State
+			"currentlyRunning": currentlyRunning,
+			"wipSerie":         wipSerie,
+			"HEAD":             HEAD,
+
+			// History
+			"memory": rotatingLogHandler,
+		})
+	})
+	return router
+}