From 9d7afb339a0a1939df34db0af28b2abc6bb9d2c1 Mon Sep 17 00:00:00 2001 From: Denis Gukov Date: Sat, 28 Sep 2024 19:05:26 +0500 Subject: [PATCH] feat(ui): create runner from web --- api/router.go | 7 +++++-- api/runners.go | 33 ++++++++++++++++++------------- db/Runner.go | 2 +- db/sql/runner.go | 10 +++++++--- web/src/components/RunnerForm.vue | 17 +++++++++++++++- web/src/views/Runners.vue | 6 +++++- 6 files changed, 53 insertions(+), 22 deletions(-) diff --git a/api/router.go b/api/router.go index d72125c7..bd3fa586 100644 --- a/api/router.go +++ b/api/router.go @@ -80,7 +80,6 @@ func Route() *mux.Router { publicAPIRouter := r.PathPrefix(webPath + "api").Subrouter() publicAPIRouter.Use(StoreMiddleware, JSONMiddleware) - publicAPIRouter.HandleFunc("/runners", runners.RegisterRunner).Methods("POST") publicAPIRouter.HandleFunc("/auth/login", login).Methods("GET", "POST") publicAPIRouter.HandleFunc("/auth/logout", logout).Methods("POST") publicAPIRouter.HandleFunc("/auth/oidc/{provider}/login", oidcLogin).Methods("GET") @@ -88,7 +87,11 @@ func Route() *mux.Router { publicAPIRouter.HandleFunc("/auth/oidc/{provider}/redirect/{redirect_path:.*}", oidcRedirect).Methods("GET") runnersAPI := r.PathPrefix(webPath + "internal").Subrouter() - runnersAPI.Use(StoreMiddleware, JSONMiddleware, runners.RunnerMiddleware) + + runnersAPI.Use(StoreMiddleware, JSONMiddleware) + runnersAPI.HandleFunc("/runners", runners.RegisterRunner).Methods("POST") + + runnersAPI.Use(runners.RunnerMiddleware) runnersAPI.Path("/runners/{runner_id}").HandlerFunc(runners.GetRunner).Methods("GET", "HEAD") runnersAPI.Path("/runners/{runner_id}").HandlerFunc(runners.UpdateRunner).Methods("PUT") runnersAPI.Path("/runners/{runner_id}").HandlerFunc(runners.UnregisterRunner).Methods("DELETE") diff --git a/api/runners.go b/api/runners.go index 3f930e16..30fb0268 100644 --- a/api/runners.go +++ b/api/runners.go @@ -33,23 +33,19 @@ func getGlobalRunners(w http.ResponseWriter, r *http.Request) { helpers.WriteJSON(w, http.StatusOK, result) } +type runnerWithToken struct { + db.Runner + Token string `json:"token"` +} + func addGlobalRunner(w http.ResponseWriter, r *http.Request) { var runner db.Runner if !helpers.Bind(w, r, &runner) { return } - editor := context.Get(r, "user").(*db.User) - if !editor.Admin { - log.Warn(editor.Username + " is not permitted to create users") - w.WriteHeader(http.StatusUnauthorized) - return - } - - newRunner, err := helpers.Store(r).CreateRunner(db.Runner{ - Webhook: runner.Webhook, - MaxParallelTasks: runner.MaxParallelTasks, - }) + runner.ProjectID = nil + newRunner, err := helpers.Store(r).CreateRunner(runner) if err != nil { log.Warn("Runner is not created: " + err.Error()) @@ -57,7 +53,10 @@ func addGlobalRunner(w http.ResponseWriter, r *http.Request) { return } - helpers.WriteJSON(w, http.StatusCreated, newRunner) + helpers.WriteJSON(w, http.StatusCreated, runnerWithToken{ + Runner: newRunner, + Token: newRunner.Token, + }) } func globalRunnerMiddleware(next http.Handler) http.Handler { @@ -94,13 +93,19 @@ func getGlobalRunner(w http.ResponseWriter, r *http.Request) { } func updateGlobalRunner(w http.ResponseWriter, r *http.Request) { - runner := context.Get(r, "runner").(*db.Runner) + oldRunner := context.Get(r, "runner").(*db.Runner) + + var runner db.Runner + if !helpers.Bind(w, r, &runner) { + return + } store := helpers.Store(r) + runner.ID = oldRunner.ID runner.ProjectID = nil - err := store.UpdateRunner(*runner) + err := store.UpdateRunner(runner) if err != nil { helpers.WriteErrorStatus(w, err.Error(), http.StatusBadRequest) diff --git a/db/Runner.go b/db/Runner.go index efc48476..5476cfaa 100644 --- a/db/Runner.go +++ b/db/Runner.go @@ -8,7 +8,7 @@ type RunnerState string //) type Runner struct { - ID int `db:"id" json:"-"` + ID int `db:"id" json:"id"` Token string `db:"token" json:"-"` ProjectID *int `db:"project_id" json:"project_id"` //State RunnerState `db:"state" json:"state"` diff --git a/db/sql/runner.go b/db/sql/runner.go index 414a8902..b097f7bd 100644 --- a/db/sql/runner.go +++ b/db/sql/runner.go @@ -42,7 +42,9 @@ func (d *SqlDb) DeleteGlobalRunner(runnerID int) (err error) { func (d *SqlDb) UpdateRunner(runner db.Runner) (err error) { _, err = d.exec( - "update runner set webhook=?, max_parallel_tasks=? where id=?", + "update runner set name=?, active=?, webhook=?, max_parallel_tasks=? where id=?", + runner.Name, + runner.Active, runner.Webhook, runner.MaxParallelTasks, runner.ID) @@ -55,11 +57,13 @@ func (d *SqlDb) CreateRunner(runner db.Runner) (newRunner db.Runner, err error) insertID, err := d.insert( "id", - "insert into runner (project_id, token, webhook, max_parallel_tasks) values (?, ?, ?, ?)", + "insert into runner (project_id, token, webhook, max_parallel_tasks, name, active) values (?, ?, ?, ?, ?, ?)", runner.ProjectID, token, runner.Webhook, - runner.MaxParallelTasks) + runner.MaxParallelTasks, + runner.Name, + runner.Active) if err != nil { return diff --git a/web/src/components/RunnerForm.vue b/web/src/components/RunnerForm.vue index ab9ae9b0..f62bf31f 100644 --- a/web/src/components/RunnerForm.vue +++ b/web/src/components/RunnerForm.vue @@ -19,9 +19,24 @@ :disabled="formSaving" > + + + + diff --git a/web/src/views/Runners.vue b/web/src/views/Runners.vue index 5d82bf85..ecf0d0d5 100644 --- a/web/src/views/Runners.vue +++ b/web/src/views/Runners.vue @@ -61,6 +61,10 @@ + +