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-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-11-25 17:43:56 +01:00
|
|
|
//TODO: log last update
|
|
|
|
|
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-11-18 15:40:46 +01:00
|
|
|
// MakeFrontend configures the router and returns a new Frontend struct
|
2019-11-25 18:12:38 +01:00
|
|
|
func MakeFrontend(runner *submitqueue.Runner) http.Handler {
|
2019-11-21 16:12:04 +01:00
|
|
|
router := gin.Default()
|
|
|
|
|
2019-11-18 15:40:46 +01:00
|
|
|
router.GET("/submit-queue.json", func(c *gin.Context) {
|
2019-11-25 18:12:38 +01:00
|
|
|
submitQueue, _, _ := runner.GetState()
|
2019-11-18 15:40:46 +01:00
|
|
|
c.JSON(http.StatusOK, submitQueue)
|
|
|
|
})
|
|
|
|
|
|
|
|
router.GET("/", func(c *gin.Context) {
|
2019-11-25 18:22:55 +01:00
|
|
|
submitQueue, currentlyRunning, results := runner.GetState()
|
2019-11-25 18:12:38 +01:00
|
|
|
|
|
|
|
funcMap := template.FuncMap{
|
|
|
|
"isAutoSubmittable": func(serie *submitqueue.Serie) bool {
|
|
|
|
return submitQueue.IsAutoSubmittable(serie)
|
|
|
|
},
|
|
|
|
"changesetURL": func(changeset *gerrit.Changeset) string {
|
|
|
|
return submitQueue.GetChangesetURL(changeset)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-11-27 11:38:54 +01:00
|
|
|
tmpl := template.Must(loadTemplate([]string{
|
|
|
|
"submit-queue.tmpl.html",
|
2019-11-27 11:41:45 +01:00
|
|
|
"series.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
|
|
|
|
|
|
|
tmpl.ExecuteTemplate(c.Writer, "submit-queue.tmpl.html", gin.H{
|
2019-11-27 13:00:50 +01:00
|
|
|
// Config
|
2019-11-18 15:40:46 +01:00
|
|
|
"projectName": submitQueue.ProjectName,
|
|
|
|
"branchName": submitQueue.BranchName,
|
2019-11-27 13:00:50 +01:00
|
|
|
|
|
|
|
// State
|
2019-11-25 18:22:55 +01:00
|
|
|
"currentlyRunning": currentlyRunning,
|
2019-11-27 13:00:50 +01:00
|
|
|
"series": submitQueue.Series,
|
|
|
|
"HEAD": submitQueue.HEAD,
|
|
|
|
|
|
|
|
// History
|
2019-11-25 18:22:55 +01:00
|
|
|
"results": results,
|
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
|
|
|
}
|