c572fcfe04
Rename the Go module path from code.gitea.io/gitea to git.mokoconsulting.tech/MokoConsulting/MokoGitea across the entire codebase. Scope: - go.mod module declaration - 2,235 Go source files (import paths) - Dockerfile WORKDIR and COPY paths - Swagger API templates - golangci.yml linter config External dependencies (code.gitea.io/gitea-vet, code.gitea.io/sdk/gitea, gitea.com/gitea/act, etc.) are intentionally NOT renamed — they are separate upstream modules. Closes #132 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
246 lines
6.9 KiB
Go
246 lines
6.9 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package actions
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
actions_model "git.mokoconsulting.tech/MokoConsulting/MokoGitea/models/actions"
|
|
"git.mokoconsulting.tech/MokoConsulting/MokoGitea/models/db"
|
|
"git.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/log"
|
|
"git.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/setting"
|
|
"git.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/templates"
|
|
"git.mokoconsulting.tech/MokoConsulting/MokoGitea/modules/web"
|
|
shared_user "git.mokoconsulting.tech/MokoConsulting/MokoGitea/routers/web/shared/user"
|
|
actions_service "git.mokoconsulting.tech/MokoConsulting/MokoGitea/services/actions"
|
|
"git.mokoconsulting.tech/MokoConsulting/MokoGitea/services/context"
|
|
"git.mokoconsulting.tech/MokoConsulting/MokoGitea/services/forms"
|
|
)
|
|
|
|
const (
|
|
tplRepoVariables templates.TplName = "repo/settings/actions"
|
|
tplOrgVariables templates.TplName = "org/settings/actions"
|
|
tplUserVariables templates.TplName = "user/settings/actions"
|
|
tplAdminVariables templates.TplName = "admin/actions"
|
|
)
|
|
|
|
type variablesCtx struct {
|
|
OwnerID int64
|
|
RepoID int64
|
|
IsRepo bool
|
|
IsOrg bool
|
|
IsUser bool
|
|
IsGlobal bool
|
|
VariablesTemplate templates.TplName
|
|
RedirectLink string
|
|
}
|
|
|
|
func getVariablesCtx(ctx *context.Context) (*variablesCtx, error) {
|
|
if ctx.Data["PageIsRepoSettings"] == true {
|
|
return &variablesCtx{
|
|
OwnerID: 0,
|
|
RepoID: ctx.Repo.Repository.ID,
|
|
IsRepo: true,
|
|
VariablesTemplate: tplRepoVariables,
|
|
RedirectLink: ctx.Repo.RepoLink + "/settings/actions/variables",
|
|
}, nil
|
|
}
|
|
|
|
if ctx.Data["PageIsOrgSettings"] == true {
|
|
if _, err := shared_user.RenderUserOrgHeader(ctx); err != nil {
|
|
ctx.ServerError("RenderUserOrgHeader", err)
|
|
return nil, nil //nolint:nilnil // error is already handled by ctx.ServerError
|
|
}
|
|
return &variablesCtx{
|
|
OwnerID: ctx.ContextUser.ID,
|
|
RepoID: 0,
|
|
IsOrg: true,
|
|
VariablesTemplate: tplOrgVariables,
|
|
RedirectLink: ctx.Org.OrgLink + "/settings/actions/variables",
|
|
}, nil
|
|
}
|
|
|
|
if ctx.Data["PageIsUserSettings"] == true {
|
|
return &variablesCtx{
|
|
OwnerID: ctx.Doer.ID,
|
|
RepoID: 0,
|
|
IsUser: true,
|
|
VariablesTemplate: tplUserVariables,
|
|
RedirectLink: setting.AppSubURL + "/user/settings/actions/variables",
|
|
}, nil
|
|
}
|
|
|
|
if ctx.Data["PageIsAdmin"] == true {
|
|
return &variablesCtx{
|
|
OwnerID: 0,
|
|
RepoID: 0,
|
|
IsGlobal: true,
|
|
VariablesTemplate: tplAdminVariables,
|
|
RedirectLink: setting.AppSubURL + "/-/admin/actions/variables",
|
|
}, nil
|
|
}
|
|
|
|
return nil, errors.New("unable to set Variables context")
|
|
}
|
|
|
|
func Variables(ctx *context.Context) {
|
|
ctx.Data["Title"] = ctx.Tr("actions.variables")
|
|
ctx.Data["PageType"] = "variables"
|
|
ctx.Data["PageIsSharedSettingsVariables"] = true
|
|
|
|
vCtx, err := getVariablesCtx(ctx)
|
|
if err != nil {
|
|
ctx.ServerError("getVariablesCtx", err)
|
|
return
|
|
}
|
|
|
|
variables, err := db.Find[actions_model.ActionVariable](ctx, actions_model.FindVariablesOpts{
|
|
OwnerID: vCtx.OwnerID,
|
|
RepoID: vCtx.RepoID,
|
|
})
|
|
if err != nil {
|
|
ctx.ServerError("FindVariables", err)
|
|
return
|
|
}
|
|
ctx.Data["Variables"] = variables
|
|
ctx.Data["DataMaxLength"] = actions_model.VariableDataMaxLength
|
|
ctx.Data["DescriptionMaxLength"] = actions_model.VariableDescriptionMaxLength
|
|
|
|
// MokoGitea: when viewing repo variables, also show inherited org variables
|
|
if vCtx.IsRepo && ctx.Repo != nil && ctx.Repo.Repository != nil {
|
|
orgVars, err := db.Find[actions_model.ActionVariable](ctx, actions_model.FindVariablesOpts{
|
|
OwnerID: ctx.Repo.Repository.OwnerID,
|
|
})
|
|
if err != nil {
|
|
log.Error("FindOrgVariables failed: %v", err)
|
|
} else if len(orgVars) > 0 {
|
|
repoVarNames := make(map[string]bool, len(variables))
|
|
for _, v := range variables {
|
|
repoVarNames[v.Name] = true
|
|
}
|
|
ctx.Data["OrgVariables"] = orgVars
|
|
ctx.Data["RepoVariableNames"] = repoVarNames
|
|
if err := ctx.Repo.Repository.LoadOwner(ctx); err == nil {
|
|
ctx.Data["OrgVariablesLink"] = ctx.Repo.Repository.Owner.HTMLURL(ctx) + "/settings/actions/variables"
|
|
}
|
|
}
|
|
}
|
|
|
|
ctx.HTML(http.StatusOK, vCtx.VariablesTemplate)
|
|
}
|
|
|
|
func VariableCreate(ctx *context.Context) {
|
|
vCtx, err := getVariablesCtx(ctx)
|
|
if err != nil {
|
|
ctx.ServerError("getVariablesCtx", err)
|
|
return
|
|
}
|
|
|
|
if ctx.HasError() { // form binding validation error
|
|
ctx.JSONError(ctx.GetErrMsg())
|
|
return
|
|
}
|
|
|
|
form := web.GetForm(ctx).(*forms.EditVariableForm)
|
|
|
|
v, err := actions_service.CreateVariable(ctx, vCtx.OwnerID, vCtx.RepoID, form.Name, form.Data, form.Description)
|
|
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(vCtx.RedirectLink)
|
|
}
|
|
|
|
func VariableUpdate(ctx *context.Context) {
|
|
vCtx, err := getVariablesCtx(ctx)
|
|
if err != nil {
|
|
ctx.ServerError("getVariablesCtx", err)
|
|
return
|
|
}
|
|
|
|
if ctx.HasError() { // form binding validation error
|
|
ctx.JSONError(ctx.GetErrMsg())
|
|
return
|
|
}
|
|
|
|
id := ctx.PathParamInt64("variable_id")
|
|
|
|
variable := findActionsVariable(ctx, id, vCtx)
|
|
if ctx.Written() {
|
|
return
|
|
}
|
|
|
|
form := web.GetForm(ctx).(*forms.EditVariableForm)
|
|
variable.Name = form.Name
|
|
variable.Data = form.Data
|
|
variable.Description = form.Description
|
|
|
|
if ok, err := actions_service.UpdateVariableNameData(ctx, variable); 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(vCtx.RedirectLink)
|
|
}
|
|
|
|
func findActionsVariable(ctx *context.Context, id int64, vCtx *variablesCtx) *actions_model.ActionVariable {
|
|
opts := actions_model.FindVariablesOpts{
|
|
IDs: []int64{id},
|
|
}
|
|
switch {
|
|
case vCtx.IsRepo:
|
|
opts.RepoID = vCtx.RepoID
|
|
if opts.RepoID == 0 {
|
|
panic("RepoID is 0")
|
|
}
|
|
case vCtx.IsOrg, vCtx.IsUser:
|
|
opts.OwnerID = vCtx.OwnerID
|
|
if opts.OwnerID == 0 {
|
|
panic("OwnerID is 0")
|
|
}
|
|
case vCtx.IsGlobal:
|
|
// do nothing
|
|
default:
|
|
panic("invalid actions variable")
|
|
}
|
|
|
|
got, err := actions_model.FindVariables(ctx, opts)
|
|
if err != nil {
|
|
ctx.ServerError("FindVariables", err)
|
|
return nil
|
|
} else if len(got) == 0 {
|
|
ctx.NotFound(nil)
|
|
return nil
|
|
}
|
|
return got[0]
|
|
}
|
|
|
|
func VariableDelete(ctx *context.Context) {
|
|
vCtx, err := getVariablesCtx(ctx)
|
|
if err != nil {
|
|
ctx.ServerError("getVariablesCtx", err)
|
|
return
|
|
}
|
|
|
|
id := ctx.PathParamInt64("variable_id")
|
|
|
|
variable := findActionsVariable(ctx, id, vCtx)
|
|
if ctx.Written() {
|
|
return
|
|
}
|
|
|
|
if err := actions_service.DeleteVariableByID(ctx, variable.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(vCtx.RedirectLink)
|
|
}
|