2019-11-18 15:40:46 +01:00
|
|
|
package frontend
|
|
|
|
|
|
|
|
import (
|
2019-11-27 11:23:12 +01:00
|
|
|
"fmt"
|
2019-11-18 16:29:57 +01:00
|
|
|
"io/ioutil"
|
2019-11-18 15:40:46 +01:00
|
|
|
"net/http"
|
2019-12-04 15:21:17 +01:00
|
|
|
"encoding/json"
|
2019-11-18 15:40:46 +01:00
|
|
|
|
2019-11-18 16:29:57 +01:00
|
|
|
"html/template"
|
|
|
|
|
|
|
|
"github.com/rakyll/statik/fs"
|
|
|
|
|
2019-12-04 15:21:17 +01:00
|
|
|
"github.com/apex/log"
|
|
|
|
|
2019-11-21 16:12:04 +01:00
|
|
|
"github.com/tweag/gerrit-queue/gerrit"
|
2019-12-02 12:03:46 +01:00
|
|
|
"github.com/tweag/gerrit-queue/misc"
|
2019-11-18 16:29:57 +01:00
|
|
|
_ "github.com/tweag/gerrit-queue/statik" // register static assets
|
2019-11-18 15:40:46 +01:00
|
|
|
"github.com/tweag/gerrit-queue/submitqueue"
|
2019-12-02 10:00:32 +01:00
|
|
|
)
|
2019-11-25 17:43:56 +01:00
|
|
|
|
2019-11-27 11:23:12 +01:00
|
|
|
//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")
|
2019-11-18 16:29:57 +01:00
|
|
|
}
|
2019-11-27 11:23:12 +01:00
|
|
|
tmpl := template.New(templateNames[0]).Funcs(funcMap)
|
|
|
|
statikFS, err := fs.New()
|
2019-11-18 16:29:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-27 11:23:12 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-11-18 16:29:57 +01:00
|
|
|
}
|
2019-11-27 11:23:12 +01:00
|
|
|
|
|
|
|
return tmpl, nil
|
2019-11-18 16:29:57 +01:00
|
|
|
}
|
|
|
|
|
2019-12-02 10:00:32 +01:00
|
|
|
// MakeFrontend returns a http.Handler
|
2019-12-02 12:03:46 +01:00
|
|
|
func MakeFrontend(rotatingLogHandler *misc.RotatingLogHandler, gerritClient *gerrit.Client, runner *submitqueue.Runner) http.Handler {
|
2019-12-02 10:00:32 +01:00
|
|
|
projectName := gerritClient.GetProjectName()
|
|
|
|
branchName := gerritClient.GetBranchName()
|
2019-11-18 15:40:46 +01:00
|
|
|
|
2021-12-10 10:15:15 +01:00
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
|
2019-12-02 10:00:32 +01:00
|
|
|
var wipSerie *gerrit.Serie = nil
|
|
|
|
HEAD := ""
|
|
|
|
currentlyRunning := runner.IsCurrentlyRunning()
|
|
|
|
|
|
|
|
// don't trigger operations requiring a lock
|
|
|
|
if !currentlyRunning {
|
|
|
|
wipSerie = runner.GetWIPSerie()
|
|
|
|
HEAD = gerritClient.GetHEAD()
|
|
|
|
}
|
2019-11-25 18:12:38 +01:00
|
|
|
|
|
|
|
funcMap := template.FuncMap{
|
|
|
|
"changesetURL": func(changeset *gerrit.Changeset) string {
|
2019-12-02 10:00:32 +01:00
|
|
|
return gerritClient.GetChangesetURL(changeset)
|
2019-11-25 18:12:38 +01:00
|
|
|
},
|
2019-12-04 15:21:17 +01:00
|
|
|
"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)
|
|
|
|
},
|
2019-11-25 18:12:38 +01:00
|
|
|
}
|
|
|
|
|
2019-11-27 11:38:54 +01:00
|
|
|
tmpl := template.Must(loadTemplate([]string{
|
2019-12-04 14:51:04 +01:00
|
|
|
"index.tmpl.html",
|
2019-11-27 11:38:54 +01:00
|
|
|
"serie.tmpl.html",
|
|
|
|
"changeset.tmpl.html",
|
|
|
|
}, funcMap))
|
2019-11-25 18:12:38 +01:00
|
|
|
|
2021-12-10 10:15:15 +01:00
|
|
|
tmpl.ExecuteTemplate(w, "index.tmpl.html", map[string]interface{}{
|
2019-11-27 13:00:50 +01:00
|
|
|
// Config
|
2019-12-02 10:00:32 +01:00
|
|
|
"projectName": projectName,
|
|
|
|
"branchName": branchName,
|
2019-11-27 13:00:50 +01:00
|
|
|
|
|
|
|
// State
|
2019-11-25 18:22:55 +01:00
|
|
|
"currentlyRunning": currentlyRunning,
|
2019-12-02 10:00:32 +01:00
|
|
|
"wipSerie": wipSerie,
|
|
|
|
"HEAD": HEAD,
|
2019-11-27 13:00:50 +01:00
|
|
|
|
|
|
|
// History
|
2019-12-02 12:03:46 +01:00
|
|
|
"memory": rotatingLogHandler,
|
2019-11-18 15:40:46 +01:00
|
|
|
})
|
|
|
|
})
|
2021-12-10 10:15:15 +01:00
|
|
|
return mux
|
2019-11-18 15:40:46 +01:00
|
|
|
}
|