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-11-18 16:29:57 +01:00
|
|
|
"html/template"
|
|
|
|
|
2019-11-18 15:40:46 +01:00
|
|
|
"github.com/gin-gonic/gin"
|
2019-11-18 16:29:57 +01:00
|
|
|
"github.com/rakyll/statik/fs"
|
|
|
|
|
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-11-21 16:12:04 +01:00
|
|
|
router := gin.Default()
|
|
|
|
|
2019-12-02 10:00:32 +01:00
|
|
|
projectName := gerritClient.GetProjectName()
|
|
|
|
branchName := gerritClient.GetBranchName()
|
2019-11-18 15:40:46 +01:00
|
|
|
|
|
|
|
router.GET("/", func(c *gin.Context) {
|
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-11-27 11:38:54 +01:00
|
|
|
tmpl := template.Must(loadTemplate([]string{
|
|
|
|
"submit-queue.tmpl.html",
|
|
|
|
"serie.tmpl.html",
|
|
|
|
"changeset.tmpl.html",
|
|
|
|
}, funcMap))
|
2019-11-25 18:12:38 +01:00
|
|
|
|
|
|
|
tmpl.ExecuteTemplate(c.Writer, "submit-queue.tmpl.html", gin.H{
|
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
|
|
|
})
|
|
|
|
})
|
2019-11-25 18:12:38 +01:00
|
|
|
return router
|
2019-11-18 15:40:46 +01:00
|
|
|
}
|