Skip to content

Commit

Permalink
Update from upstream then re-add the global actions
Browse files Browse the repository at this point in the history
Remove the cycle call function
Create Org Setting Actions RequireAction

Signed-off-by: Alex Lau(AvengerMoJo) <[email protected]>
  • Loading branch information
AvengerMoJo committed Apr 8, 2024
1 parent d872ce0 commit 345b1d7
Show file tree
Hide file tree
Showing 13 changed files with 450 additions and 1 deletion.
65 changes: 65 additions & 0 deletions models/actions/require_action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

// WIP RequireAction

package actions

import (
"context"
//"errors"
//"fmt"

// org_model "code.gitea.io/gitea/models/organization"
// repo_model "code.gitea.io/gitea/models/repo"

"code.gitea.io/gitea/models/db"
//"code.gitea.io/gitea/models/unit"
"code.gitea.io/gitea/modules/timeutil"

"xorm.io/builder"
)

type RequireAction struct {
ID int64 `xorm:"pk autoincr"`
OrgID int64 `xorm:"index"`
RepoID int64 `xorm:"index"`
Name string `xorm:"UNIQUE(owner_repo_name) NOT NULL"`
Description string `xorm:"LONGTEXT NOT NULL"`
CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
RepoRange string // glob match which repositories could use this runner
}

type GlobalWorkflow struct {
RepoName string
Filename string
}

func init() {
db.RegisterModel(new(RequireAction))
}

type FindRequireActionOptions struct {
db.ListOptions
RequireActionID int64
OrgID int64
}

func (opts FindRequireActionOptions) ToConds() builder.Cond {
cond := builder.NewCond()
if opts.OrgID > 0 {
cond = cond.And(builder.Eq{"org_id": opts.OrgID})
}
if opts.RequireActionID > 0 {
cond = cond.And(builder.Eq{"id": opts.RequireActionID})
}
return cond
}

// LoadAttributes loads the attributes of the require action
func (r *RequireAction) LoadAttributes(ctx context.Context) error {
// place holder for now.
return nil
}

17 changes: 17 additions & 0 deletions models/repo/repo_unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,27 @@ func (cfg *PullRequestsConfig) GetDefaultMergeStyle() MergeStyle {

type ActionsConfig struct {
DisabledWorkflows []string
EnabledGlobalWorkflows []string
}

func (cfg *ActionsConfig) EnableWorkflow(file string) {
cfg.DisabledWorkflows = util.SliceRemoveAll(cfg.DisabledWorkflows, file)
}

func (cfg *ActionsConfig) DisableGlobalWorkflow(file string) {
cfg.EnabledGlobalWorkflows = util.SliceRemoveAll(cfg.EnabledGlobalWorkflows, file)
}

func (cfg *ActionsConfig) IsGlobalWorkflowEnabled(file string) bool {
return slices.Contains(cfg.EnabledGlobalWorkflows, file)
}

func (cfg *ActionsConfig) EnableGlobalWorkflow(file string) {
cfg.EnabledGlobalWorkflows = append(cfg.EnabledGlobalWorkflows, file)
}

func (cfg *ActionsConfig) GetGlobalWorkflow() []string {
return cfg.EnabledGlobalWorkflows
}

func (cfg *ActionsConfig) ToString() string {
Expand Down
21 changes: 21 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3640,11 +3640,32 @@ runs.no_workflows.documentation = For more information on Gitea Actions, see <a
runs.no_runs = The workflow has no runs yet.
runs.empty_commit_message = (empty commit message)
require_actions = Require Actions
require_actions.require_actions_manage_panel = Require Actions Management Panel
require_actions.enable_global_workflow = How to Enable Global Workflow
require_actions.id = ID
require_actions.name = Name
require_actions.add = Add Global Workflow
require_actions.add_require_actions = Enable selected Workflow
require_actions.new = Create New
require_actions.status = Status
require_actions.version = Version
require_actions.repo = Repo
require_actions.workflow = Workflow Filename
require_actions.link = Link
require_actions.none = No Require Actions Available
workflow.disable = Disable Workflow
workflow.disable_success = Workflow '%s' disabled successfully.
workflow.enable = Enable Workflow
workflow.enable_success = Workflow '%s' enabled successfully.
workflow.disabled = Workflow is disabled.
workflow.global = Global
workflow.global_disable = Disable Global Require
workflow.global_disable_success = Global Require '%s' disabled successfully.
workflow.global_enable = Enable Global Require
workflow.global_enable_success = Global Require '%s' enabled successfully.
workflow.global_enabled = Global Require is disabled.
need_approval_desc = Need approval to run workflows for fork pull request.
Expand Down
14 changes: 14 additions & 0 deletions routers/web/org/setting/require_actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

// WIP RequireAction

package setting

import (
"code.gitea.io/gitea/services/context"
)

func RedirectToRepoSetting(ctx *context.Context) {
ctx.Redirect(ctx.Org.OrgLink + "/settings/actions/require_actions")
}
6 changes: 6 additions & 0 deletions routers/web/repo/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func List(ctx *context.Context) {
workflow := ctx.FormString("workflow")
actorID := ctx.FormInt64("actor")
status := ctx.FormInt("status")
isGlobal := false
ctx.Data["CurWorkflow"] = workflow

actionsConfig := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions).ActionsConfig()
Expand All @@ -153,6 +154,8 @@ func List(ctx *context.Context) {
if len(workflow) > 0 && ctx.Repo.IsAdmin() {
ctx.Data["AllowDisableOrEnableWorkflow"] = true
ctx.Data["CurWorkflowDisabled"] = actionsConfig.IsWorkflowDisabled(workflow)
ctx.Data["CurGlobalWorkflowEnable"] = actionsConfig.IsGlobalWorkflowEnabled(workflow)
isGlobal = actionsConfig.IsGlobalWorkflowEnabled(workflow)
}

// if status or actor query param is not given to frontend href, (href="/<repoLink>/actions")
Expand Down Expand Up @@ -209,6 +212,9 @@ func List(ctx *context.Context) {
pager.AddParamString("workflow", workflow)
pager.AddParamString("actor", fmt.Sprint(actorID))
pager.AddParamString("status", fmt.Sprint(status))
if isGlobal {
pager.AddParamString("global", fmt.Sprint(isGlobal))
}
ctx.Data["Page"] = pager
ctx.Data["HasWorkflowsOrRuns"] = len(workflows) > 0 || len(runs) > 0

Expand Down
35 changes: 35 additions & 0 deletions routers/web/repo/actions/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,3 +714,38 @@ func disableOrEnableWorkflowFile(ctx *context_module.Context, isEnable bool) {
url.QueryEscape(ctx.FormString("actor")), url.QueryEscape(ctx.FormString("status")))
ctx.JSONRedirect(redirectURL)
}

func DisableGlobalWorkflowFile(ctx *context_module.Context) {
disableOrEnableGlobalWorkflowFile(ctx, true)
}

func EnableGlobalWorkflowFile(ctx *context_module.Context) {
disableOrEnableGlobalWorkflowFile(ctx, false)
}

func disableOrEnableGlobalWorkflowFile(ctx *context_module.Context, isGlobalEnable bool) {
workflow := ctx.FormString("workflow")
if len(workflow) == 0 {
ctx.ServerError("workflow", nil)
return
}
cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions)
cfg := cfgUnit.ActionsConfig()
if isGlobalEnable {
cfg.DisableGlobalWorkflow(workflow)
} else {
cfg.EnableGlobalWorkflow(workflow)
}
if err := repo_model.UpdateRepoUnit(ctx, cfgUnit); err != nil {
ctx.ServerError("UpdateRepoUnit", err)
return
}
if isGlobalEnable {
ctx.Flash.Success(ctx.Tr("actions.workflow.global_disable_success", workflow))
} else {
ctx.Flash.Success(ctx.Tr("actions.workflow.global_enable_success", workflow))
}
redirectURL := fmt.Sprintf("%s/actions?workflow=%s&actor=%s&status=%s", ctx.Repo.RepoLink, url.QueryEscape(workflow),
url.QueryEscape(ctx.FormString("actor")), url.QueryEscape(ctx.FormString("status")))
ctx.JSONRedirect(redirectURL)
}
71 changes: 71 additions & 0 deletions routers/web/repo/setting/require_actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

// WIP RequireAction

package setting

import (
"errors"
"net/http"

"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/modules/base"
// "code.gitea.io/gitea/modules/log"

"code.gitea.io/gitea/services/context"

//"code.gitea.io/gitea/modules/setting"
shared "code.gitea.io/gitea/routers/web/shared/actions"
actions_model "code.gitea.io/gitea/models/actions"
)

const (
// let start with org WIP
tplOrgRequireActions base.TplName = "org/settings/actions"
)

type requireActionsCtx struct {
OrgID int64
IsOrg bool
RequireActionsTemplate base.TplName
RedirectLink string
}

func getRequireActionsCtx(ctx *context.Context) (*requireActionsCtx, error) {
if ctx.Data["PageIsOrgSettings"] == true {
return &requireActionsCtx{
OrgID: ctx.Org.Organization.ID,
IsOrg: true,
RequireActionsTemplate: tplOrgRequireActions,
RedirectLink: ctx.Org.OrgLink + "/settings/actions/require_action",
}, nil
}
return nil, errors.New("unable to set Require Actions context")
}

// Listing all RequireActions
func RequireActions(ctx *context.Context) {
ctx.Data["ActionsTitle"] = ctx.Tr("actions.requires")
ctx.Data["PageType"] = "require_actions"
ctx.Data["PageIsSharedSettingsRequireActions"] = true

vCtx, err := getRequireActionsCtx(ctx)
if err != nil {
ctx.ServerError("getRequireActionsCtx", err)
return
}

page := ctx.FormInt("page")
if page <= 1 { page = 1 }
opts := actions_model.FindRequireActionOptions{
ListOptions: db.ListOptions{
Page: page,
PageSize: 10,
},
}
shared.SetRequireActionsContext(ctx, opts)
ctx.Data["Link"] = vCtx.RedirectLink
shared.GlobalEnableWorkflow(ctx, ctx.Org.Organization.ID)
ctx.HTML(http.StatusOK, vCtx.RequireActionsTemplate)
}
59 changes: 59 additions & 0 deletions routers/web/shared/actions/require_action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

// WIP RequireAction

package actions

import (

actions_model "code.gitea.io/gitea/models/actions"
org_model "code.gitea.io/gitea/models/organization"
"code.gitea.io/gitea/models/db"
//"code.gitea.io/gitea/modules/log"
//"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/models/unit"
//"code.gitea.io/gitea/modules/web"
//"code.gitea.io/gitea/services/forms"
// action_service "code.gitea.io/gitea/services/actions"

"code.gitea.io/gitea/services/context"
)

// SetRequireActionDeletePost response for deleting a require action workflow
func SetRequireActionsContext(ctx *context.Context, opts actions_model.FindRequireActionOptions) {
require_actions, count, err := db.FindAndCount[actions_model.RequireAction](ctx, opts)
if err != nil {
ctx.ServerError("CountRequireAction", err)
return
}
ctx.Data["RequireActions"] = require_actions
ctx.Data["Total"] = count
ctx.Data["OrgID"] = ctx.Org.Organization.ID
ctx.Data["OrgName"] = ctx.Org.Organization.Name
pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
ctx.Data["Page"] = pager
}

// get all the available enable global workflow in the org's repo
func GlobalEnableWorkflow(ctx *context.Context, orgID int64){
var gwfList []actions_model.GlobalWorkflow
orgRepos, err := org_model.GetOrgRepositories(ctx, orgID)
if err != nil {
ctx.ServerError("GlobalEnableWorkflows get org repos: ", err)
return
}
for _, repo := range orgRepos {
repo.LoadUnits(ctx)
actionsConfig := repo.MustGetUnit(ctx, unit.TypeActions).ActionsConfig()
enabledWorkflows := actionsConfig.GetGlobalWorkflow()
for _, workflow := range enabledWorkflows {
gwf := actions_model.GlobalWorkflow{
RepoName: repo.Name,
Filename: workflow,
}
gwfList = append(gwfList, gwf)
}
}
ctx.Data["GlobalEnableWorkflows"] = gwfList
}
11 changes: 11 additions & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,13 @@ func registerRoutes(m *web.Route) {
})
}

// WIP RequireAction
addSettingsRequireActionsRoutes := func() {
m.Group("/require_actions", func() {
m.Get("", repo_setting.RequireActions)
})
}

// FIXME: not all routes need go through same middleware.
// Especially some AJAX requests, we can reduce middleware number to improve performance.

Expand Down Expand Up @@ -628,6 +635,7 @@ func registerRoutes(m *web.Route) {

m.Group("/actions", func() {
m.Get("", user_setting.RedirectToDefaultSetting)
addSettingsRequireActionsRoutes()
addSettingsRunnersRoutes()
addSettingsSecretsRoutes()
addSettingsVariablesRoutes()
Expand Down Expand Up @@ -925,6 +933,7 @@ func registerRoutes(m *web.Route) {

m.Group("/actions", func() {
m.Get("", org_setting.RedirectToDefaultSetting)
addSettingsRequireActionsRoutes()
addSettingsRunnersRoutes()
addSettingsSecretsRoutes()
addSettingsVariablesRoutes()
Expand Down Expand Up @@ -1358,6 +1367,8 @@ func registerRoutes(m *web.Route) {
m.Get("", actions.List)
m.Post("/disable", reqRepoAdmin, actions.DisableWorkflowFile)
m.Post("/enable", reqRepoAdmin, actions.EnableWorkflowFile)
m.Post("/global_disable", reqRepoAdmin, actions.DisableGlobalWorkflowFile)
m.Post("/global_enable", reqRepoAdmin, actions.EnableGlobalWorkflowFile)

m.Group("/runs/{run}", func() {
m.Combo("").
Expand Down
4 changes: 3 additions & 1 deletion templates/org/settings/actions.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{{template "org/settings/layout_head" (dict "ctxData" . "pageClass" "organization settings actions")}}
<div class="org-setting-content">
{{if eq .PageType "runners"}}
{{if eq .PageType "require_actions"}}
{{template "shared/actions/require_action_list" .}}
{{else if eq .PageType "runners"}}
{{template "shared/actions/runner_list" .}}
{{else if eq .PageType "secrets"}}
{{template "shared/secrets/add_list" .}}
Expand Down
3 changes: 3 additions & 0 deletions templates/org/settings/navbar.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
<details class="item toggleable-item" {{if or .PageIsSharedSettingsRunners .PageIsSharedSettingsSecrets .PageIsSharedSettingsVariables}}open{{end}}>
<summary>{{ctx.Locale.Tr "actions.actions"}}</summary>
<div class="menu">
<a class="{{if .PageIsSharedSettingsRequireActions}}active {{end}}item" href="{{.OrgLink}}/settings/actions/require_actions">
{{ctx.Locale.Tr "actions.require_actions"}}
</a>
<a class="{{if .PageIsSharedSettingsRunners}}active {{end}}item" href="{{.OrgLink}}/settings/actions/runners">
{{ctx.Locale.Tr "actions.runners"}}
</a>
Expand Down
Loading

0 comments on commit 345b1d7

Please sign in to comment.