summaryrefslogtreecommitdiffstats
path: root/routers/web/shared/actions
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--routers/web/shared/actions/runners.go161
-rw-r--r--routers/web/shared/actions/variables.go65
2 files changed, 226 insertions, 0 deletions
diff --git a/routers/web/shared/actions/runners.go b/routers/web/shared/actions/runners.go
new file mode 100644
index 0000000..f389332
--- /dev/null
+++ b/routers/web/shared/actions/runners.go
@@ -0,0 +1,161 @@
+// Copyright 2022 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package actions
+
+import (
+ "errors"
+
+ actions_model "code.gitea.io/gitea/models/actions"
+ "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/util"
+ "code.gitea.io/gitea/modules/web"
+ "code.gitea.io/gitea/services/context"
+ "code.gitea.io/gitea/services/forms"
+)
+
+// RunnersList prepares data for runners list
+func RunnersList(ctx *context.Context, opts actions_model.FindRunnerOptions) {
+ runners, count, err := db.FindAndCount[actions_model.ActionRunner](ctx, opts)
+ if err != nil {
+ ctx.ServerError("CountRunners", err)
+ return
+ }
+
+ if err := actions_model.RunnerList(runners).LoadAttributes(ctx); err != nil {
+ ctx.ServerError("LoadAttributes", err)
+ return
+ }
+
+ // ownid=0,repo_id=0,means this token is used for global
+ var token *actions_model.ActionRunnerToken
+ token, err = actions_model.GetLatestRunnerToken(ctx, opts.OwnerID, opts.RepoID)
+ if errors.Is(err, util.ErrNotExist) || (token != nil && !token.IsActive) {
+ token, err = actions_model.NewRunnerToken(ctx, opts.OwnerID, opts.RepoID)
+ if err != nil {
+ ctx.ServerError("CreateRunnerToken", err)
+ return
+ }
+ } else if err != nil {
+ ctx.ServerError("GetLatestRunnerToken", err)
+ return
+ }
+
+ ctx.Data["Keyword"] = opts.Filter
+ ctx.Data["Runners"] = runners
+ ctx.Data["Total"] = count
+ ctx.Data["RegistrationToken"] = token.Token
+ ctx.Data["RunnerOwnerID"] = opts.OwnerID
+ ctx.Data["RunnerRepoID"] = opts.RepoID
+ ctx.Data["SortType"] = opts.Sort
+
+ pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
+
+ ctx.Data["Page"] = pager
+}
+
+// RunnerDetails prepares data for runners edit page
+func RunnerDetails(ctx *context.Context, page int, runnerID, ownerID, repoID int64) {
+ runner, err := actions_model.GetRunnerByID(ctx, runnerID)
+ if err != nil {
+ ctx.ServerError("GetRunnerByID", err)
+ return
+ }
+ if err := runner.LoadAttributes(ctx); err != nil {
+ ctx.ServerError("LoadAttributes", err)
+ return
+ }
+ if !runner.Editable(ownerID, repoID) {
+ err = errors.New("no permission to edit this runner")
+ ctx.NotFound("RunnerDetails", err)
+ return
+ }
+
+ ctx.Data["Runner"] = runner
+
+ opts := actions_model.FindTaskOptions{
+ ListOptions: db.ListOptions{
+ Page: page,
+ PageSize: 30,
+ },
+ Status: actions_model.StatusUnknown, // Unknown means all
+ RunnerID: runner.ID,
+ }
+
+ tasks, count, err := db.FindAndCount[actions_model.ActionTask](ctx, opts)
+ if err != nil {
+ ctx.ServerError("CountTasks", err)
+ return
+ }
+
+ if err = actions_model.TaskList(tasks).LoadAttributes(ctx); err != nil {
+ ctx.ServerError("TasksLoadAttributes", err)
+ return
+ }
+
+ ctx.Data["Tasks"] = tasks
+ pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
+ ctx.Data["Page"] = pager
+}
+
+// RunnerDetailsEditPost response for edit runner details
+func RunnerDetailsEditPost(ctx *context.Context, runnerID, ownerID, repoID int64, redirectTo string) {
+ runner, err := actions_model.GetRunnerByID(ctx, runnerID)
+ if err != nil {
+ log.Warn("RunnerDetailsEditPost.GetRunnerByID failed: %v, url: %s", err, ctx.Req.URL)
+ ctx.ServerError("RunnerDetailsEditPost.GetRunnerByID", err)
+ return
+ }
+ if !runner.Editable(ownerID, repoID) {
+ ctx.NotFound("RunnerDetailsEditPost.Editable", util.NewPermissionDeniedErrorf("no permission to edit this runner"))
+ return
+ }
+
+ form := web.GetForm(ctx).(*forms.EditRunnerForm)
+ runner.Description = form.Description
+
+ err = actions_model.UpdateRunner(ctx, runner, "description")
+ if err != nil {
+ log.Warn("RunnerDetailsEditPost.UpdateRunner failed: %v, url: %s", err, ctx.Req.URL)
+ ctx.Flash.Warning(ctx.Tr("actions.runners.update_runner_failed"))
+ ctx.Redirect(redirectTo)
+ return
+ }
+
+ log.Debug("RunnerDetailsEditPost success: %s", ctx.Req.URL)
+
+ ctx.Flash.Success(ctx.Tr("actions.runners.update_runner_success"))
+ ctx.Redirect(redirectTo)
+}
+
+// RunnerResetRegistrationToken reset registration token
+func RunnerResetRegistrationToken(ctx *context.Context, ownerID, repoID int64, redirectTo string) {
+ _, err := actions_model.NewRunnerToken(ctx, ownerID, repoID)
+ if err != nil {
+ ctx.ServerError("ResetRunnerRegistrationToken", err)
+ return
+ }
+
+ ctx.Flash.Success(ctx.Tr("actions.runners.reset_registration_token_success"))
+ ctx.Redirect(redirectTo)
+}
+
+// RunnerDeletePost response for deleting a runner
+func RunnerDeletePost(ctx *context.Context, runnerID int64,
+ successRedirectTo, failedRedirectTo string,
+) {
+ if err := actions_model.DeleteRunner(ctx, runnerID); err != nil {
+ log.Warn("DeleteRunnerPost.UpdateRunner failed: %v, url: %s", err, ctx.Req.URL)
+ ctx.Flash.Warning(ctx.Tr("actions.runners.delete_runner_failed"))
+
+ ctx.JSONRedirect(failedRedirectTo)
+ return
+ }
+
+ log.Info("DeleteRunnerPost success: %s", ctx.Req.URL)
+
+ ctx.Flash.Success(ctx.Tr("actions.runners.delete_runner_success"))
+
+ ctx.JSONRedirect(successRedirectTo)
+}
diff --git a/routers/web/shared/actions/variables.go b/routers/web/shared/actions/variables.go
new file mode 100644
index 0000000..79c03e4
--- /dev/null
+++ b/routers/web/shared/actions/variables.go
@@ -0,0 +1,65 @@
+// Copyright 2023 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package actions
+
+import (
+ actions_model "code.gitea.io/gitea/models/actions"
+ "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/web"
+ actions_service "code.gitea.io/gitea/services/actions"
+ "code.gitea.io/gitea/services/context"
+ "code.gitea.io/gitea/services/forms"
+)
+
+func SetVariablesContext(ctx *context.Context, ownerID, repoID int64) {
+ variables, err := db.Find[actions_model.ActionVariable](ctx, actions_model.FindVariablesOpts{
+ OwnerID: ownerID,
+ RepoID: repoID,
+ })
+ if err != nil {
+ ctx.ServerError("FindVariables", err)
+ return
+ }
+ ctx.Data["Variables"] = variables
+}
+
+func CreateVariable(ctx *context.Context, ownerID, repoID int64, redirectURL string) {
+ form := web.GetForm(ctx).(*forms.EditVariableForm)
+
+ v, err := actions_service.CreateVariable(ctx, ownerID, repoID, form.Name, form.Data)
+ if err != nil {
+ log.Error("CreateVariable: %v", err)
+ ctx.JSONError(ctx.Tr("actions.variables.creation.failed"))
+ return
+ }
+
+ ctx.Flash.Success(ctx.Tr("actions.variables.creation.success", v.Name))
+ ctx.JSONRedirect(redirectURL)
+}
+
+func UpdateVariable(ctx *context.Context, redirectURL string) {
+ id := ctx.ParamsInt64(":variable_id")
+ form := web.GetForm(ctx).(*forms.EditVariableForm)
+
+ if ok, err := actions_service.UpdateVariable(ctx, id, form.Name, form.Data); err != nil || !ok {
+ log.Error("UpdateVariable: %v", err)
+ ctx.JSONError(ctx.Tr("actions.variables.update.failed"))
+ return
+ }
+ ctx.Flash.Success(ctx.Tr("actions.variables.update.success"))
+ ctx.JSONRedirect(redirectURL)
+}
+
+func DeleteVariable(ctx *context.Context, redirectURL string) {
+ id := ctx.ParamsInt64(":variable_id")
+
+ if err := actions_service.DeleteVariableByID(ctx, id); err != nil {
+ log.Error("Delete variable [%d] failed: %v", id, err)
+ ctx.JSONError(ctx.Tr("actions.variables.deletion.failed"))
+ return
+ }
+ ctx.Flash.Success(ctx.Tr("actions.variables.deletion.success"))
+ ctx.JSONRedirect(redirectURL)
+}