From 5742459f5e4184fddef5d200e0cba0f779f869ff Mon Sep 17 00:00:00 2001
From: Humaid AlQassimi
Date: Sun, 7 Jun 2020 12:44:37 +0400
Subject: [PATCH] admin: implement adding adding of criteria in steps
---
admin.go | 92 +++++++++++++++++++++++++++++++++++--
admin_forms.go | 19 +++++---
templates/edit-step.tmpl | 4 ++
templates/new-criteria.tmpl | 24 ++++++++++
templates/new-step.tmpl | 4 ++
templates/step.tmpl | 29 ++++++------
6 files changed, 148 insertions(+), 24 deletions(-)
create mode 100644 templates/new-criteria.tmpl
diff --git a/admin.go b/admin.go
index 1ad9d53..4298b38 100644
--- a/admin.go
+++ b/admin.go
@@ -83,7 +83,13 @@ func (e *Engine) RunWebInterface() {
m.Get("/dash", dashboardHandler)
m.Get("/s/:service", serviceHandler)
m.Get("/s/:service/:channel", channelHandler)
- m.Get("/s/:service/:channel/:step", stepHandler)
+ m.Group("/s/:service/:channel/:step", func() {
+ m.Get("", stepHandler)
+ m.Get("/new/criteria/exec", newExecCriteriaHandler)
+ m.Post("/new/criteria/exec", binding.BindIgnErr(newCriteriaForm{}), postNewExecCriteriaHandler)
+ m.Get("/new/criteria/final", newFinalCriteriaHandler)
+ m.Post("/new/criteria/final", binding.BindIgnErr(newCriteriaForm{}), postNewFinalCriteriaHandler)
+ })
m.Group("/new", func() {
m.Get("/service", newServiceHandler)
m.Post("/service", binding.BindIgnErr(newServiceForm{}), postNewServiceHandler)
@@ -106,6 +112,82 @@ func (e *Engine) RunWebInterface() {
log.Fatal(http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", e.Options.AdminPort), m))
}
+func postNewExecCriteriaHandler(ctx *macaron.Context, f *session.Flash, errs binding.Errors, form newCriteriaForm) {
+ if errs.Len() > 0 {
+ f.Error("Make sure to fill required fields")
+ ctx.Redirect("/s/" + ctx.Params("service") + "/" + ctx.Params("channel") + "/" + ctx.Params("step") + "/new/criteria/exec")
+ return
+ }
+ e := ctx.Data["Engine"].(*Engine)
+ s := e.getStep(ctx.Params("service"), ctx.Params("channel"), ctx.Params("step"))
+ if s == nil {
+ f.Error("Step not found")
+ ctx.Redirect("/dash")
+ return
+ }
+ // TODO check if name exists
+ crit := Criteria{
+ CriteriaName: form.Name,
+ Expression: form.Expression, //TODO check this first
+ FailMessage: form.FailMessage,
+ }
+
+ s.CriteriaExec = append(s.CriteriaExec, crit)
+ ctx.Redirect("/s/" + ctx.Params("service") + "/" + ctx.Params("channel") + "/" +
+ ctx.Params("step"))
+}
+
+func newFinalCriteriaHandler(ctx *macaron.Context, f *session.Flash) {
+ e := ctx.Data["Engine"].(*Engine)
+ s := e.getStep(ctx.Params("service"), ctx.Params("channel"), ctx.Params("step"))
+ if s == nil {
+ f.Error("Step not found")
+ ctx.Redirect("/dash")
+ return
+ }
+ ctx.Data["Step"] = s
+ ctx.Data["Type"] = "final"
+ ctx.HTML(http.StatusOK, "new-criteria")
+}
+
+func postNewFinalCriteriaHandler(ctx *macaron.Context, f *session.Flash, errs binding.Errors, form newCriteriaForm) {
+ if errs.Len() > 0 {
+ f.Error("Make sure to fill required fields")
+ ctx.Redirect("/s/" + ctx.Params("service") + "/" + ctx.Params("channel") + "/" + ctx.Params("step") + "/new/criteria/final")
+ return
+ }
+ e := ctx.Data["Engine"].(*Engine)
+ s := e.getStep(ctx.Params("service"), ctx.Params("channel"), ctx.Params("step"))
+ if s == nil {
+ f.Error("Step not found")
+ ctx.Redirect("/dash")
+ return
+ }
+ // TODO check if name exists
+ crit := Criteria{
+ CriteriaName: form.Name,
+ Expression: form.Expression, //TODO check this first
+ FailMessage: form.FailMessage,
+ }
+
+ s.CriteriaFinal = append(s.CriteriaFinal, crit)
+ ctx.Redirect("/s/" + ctx.Params("service") + "/" + ctx.Params("channel") + "/" +
+ ctx.Params("step"))
+}
+
+func newExecCriteriaHandler(ctx *macaron.Context, f *session.Flash) {
+ e := ctx.Data["Engine"].(*Engine)
+ s := e.getStep(ctx.Params("service"), ctx.Params("channel"), ctx.Params("step"))
+ if s == nil {
+ f.Error("Step not found")
+ ctx.Redirect("/dash")
+ return
+ }
+ ctx.Data["Step"] = s
+ ctx.Data["Type"] = "exec"
+ ctx.HTML(http.StatusOK, "new-criteria")
+}
+
func errorsHandler(ctx *macaron.Context) {
ctx.HTML(http.StatusOK, "errors")
}
@@ -137,6 +219,7 @@ func postEditStepHandler(ctx *macaron.Context, f *session.Flash, errs binding.Er
}
s.Status = form.Status
s.Page = form.Page
+ s.EndApplication = form.EndApplication
s.PostTo = strings.Split(form.PostTo, ",")
s.DataIn = strings.Split(form.DataIn, ",")
s.DataOut = strings.Split(form.DataOut, ",")
@@ -284,9 +367,10 @@ func postNewStepHandler(ctx *macaron.Context, errs binding.Errors, form newStepF
return
}
st := WorkflowStep{
- StepID: form.StepID,
- Status: form.Status,
- Page: form.Page,
+ StepID: form.StepID,
+ Status: form.Status,
+ Page: form.Page,
+ EndApplication: form.EndApplication,
}
if len(form.DataIn) > 0 {
st.DataIn = strings.Split(form.DataIn, ",")
diff --git a/admin_forms.go b/admin_forms.go
index d4b8246..4d6e576 100644
--- a/admin_forms.go
+++ b/admin_forms.go
@@ -13,10 +13,17 @@ type newChannelForm struct {
}
type newStepForm struct {
- StepID string `form:"id" binding:"Required"`
- Status string `form:"desc"`
- Page string `form:"page"`
- PostTo string `form:"post"`
- DataIn string `form:"data-in"`
- DataOut string `form:"data-out"`
+ StepID string `form:"id" binding:"Required"`
+ Status string `form:"desc"`
+ Page string `form:"page"`
+ PostTo string `form:"post"`
+ DataIn string `form:"data-in"`
+ DataOut string `form:"data-out"`
+ EndApplication bool `form:"end"`
+}
+
+type newCriteriaForm struct {
+ Name string `form:"name" binding:"Required"`
+ Expression string `form:"expr" binding:"Required"`
+ FailMessage string `form:"fail" binding:"Required"`
}
diff --git a/templates/edit-step.tmpl b/templates/edit-step.tmpl
index eaa0e82..60bb3c4 100644
--- a/templates/edit-step.tmpl
+++ b/templates/edit-step.tmpl
@@ -31,6 +31,10 @@
+
diff --git a/templates/new-criteria.tmpl b/templates/new-criteria.tmpl
new file mode 100644
index 0000000..b4fabd6
--- /dev/null
+++ b/templates/new-criteria.tmpl
@@ -0,0 +1,24 @@
+{{template "base/head" .}}
+
+ {{template "partials/flash" .}}
+
New {{.Type}} criteria for {{.Step.StepID}}
+
+
+
+{{template "base/footer" .}}
diff --git a/templates/new-step.tmpl b/templates/new-step.tmpl
index f5c88e2..f396ed4 100644
--- a/templates/new-step.tmpl
+++ b/templates/new-step.tmpl
@@ -28,6 +28,10 @@
+
You will be able to add pull data, criterias, and commits later.
diff --git a/templates/step.tmpl b/templates/step.tmpl
index f5d7e10..babfad2 100644
--- a/templates/step.tmpl
+++ b/templates/step.tmpl
@@ -19,35 +19,39 @@
message.
Edit attributes
- Execution Criteria
+ Execution Criteria +
+
These are criterion required to pass before the step is allowed to
start.
{{range .Step.CriteriaExec}}
{{end}}
- Finalisation Criteria
+ Finalisation Criteria +
+
These are criterion required to pass before the step is allowed to be
marked as complete.
{{range .Step.CriteriaFinal}}
{{end}}
- Pull Data
+ Pull Data +
This is the external services called via HTTP POST.
- Commit Objects
+ Commit Objects +
These are objects which are created before committing, and are loaded
in the commit scripting environment.
{{range .Step.CommitObjects}}
@@ -55,7 +59,7 @@
{{.Name}}
(type: {{.Type}})
@@ -72,9 +76,6 @@
{{end}}
-
-
{{template "base/footer" .}}
-
--
2.30.1