2019-11-18 15:40:46 +01:00
|
|
|
package frontend
|
|
|
|
|
|
|
|
import (
|
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-18 16:29:57 +01:00
|
|
|
//loadTemplate loads a single template from statikFS and returns a template object
|
2019-11-21 16:12:04 +01:00
|
|
|
func loadTemplate(templateName string, funcMap template.FuncMap) (*template.Template, error) {
|
2019-11-25 18:12:38 +01:00
|
|
|
tmpl := template.New(templateName).Funcs(funcMap)
|
2019-11-18 16:29:57 +01:00
|
|
|
statikFS, err := fs.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return tmpl.Parse(string(contents))
|
|
|
|
}
|
|
|
|
|
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:12:38 +01:00
|
|
|
submitQueue, _, _ := runner.GetState()
|
|
|
|
|
|
|
|
funcMap := template.FuncMap{
|
|
|
|
"isAutoSubmittable": func(serie *submitqueue.Serie) bool {
|
|
|
|
return submitQueue.IsAutoSubmittable(serie)
|
|
|
|
},
|
|
|
|
"changesetURL": func(changeset *gerrit.Changeset) string {
|
|
|
|
return submitQueue.GetChangesetURL(changeset)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl := template.Must(loadTemplate("submit-queue.tmpl.html", funcMap))
|
|
|
|
|
|
|
|
tmpl.ExecuteTemplate(c.Writer, "submit-queue.tmpl.html", gin.H{
|
2019-11-18 15:40:46 +01:00
|
|
|
"series": submitQueue.Series,
|
|
|
|
"projectName": submitQueue.ProjectName,
|
|
|
|
"branchName": submitQueue.BranchName,
|
|
|
|
"HEAD": submitQueue.HEAD,
|
|
|
|
})
|
|
|
|
})
|
2019-11-25 18:12:38 +01:00
|
|
|
return router
|
2019-11-18 15:40:46 +01:00
|
|
|
}
|