Skip to content

Commit 2bf19e5

Browse files
committed
Update from upstream then re-add the global actions
Signed-off-by: Alex Lau(AvengerMoJo) <[email protected]>
1 parent a008486 commit 2bf19e5

File tree

9 files changed

+311
-0
lines changed

9 files changed

+311
-0
lines changed

models/actions/require_action.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
// WIP RequireAction
5+
6+
package actions
7+
8+
import (
9+
"context"
10+
//"errors"
11+
//"fmt"
12+
13+
// org_model "code.gitea.io/gitea/models/organization"
14+
// repo_model "code.gitea.io/gitea/models/repo"
15+
16+
"code.gitea.io/gitea/models/db"
17+
//"code.gitea.io/gitea/models/unit"
18+
"code.gitea.io/gitea/modules/timeutil"
19+
20+
"xorm.io/builder"
21+
)
22+
23+
type RequireAction struct {
24+
ID int64 `xorm:"pk autoincr"`
25+
OrgID int64 `xorm:"index"`
26+
RepoID int64 `xorm:"index"`
27+
Name string `xorm:"UNIQUE(owner_repo_name) NOT NULL"`
28+
Data string `xorm:"LONGTEXT NOT NULL"`
29+
CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
30+
UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
31+
RepoRange string // glob match which repositories could use this runner
32+
}
33+
34+
func init() {
35+
db.RegisterModel(new(RequireAction))
36+
}
37+
38+
type FindRequireActionOptions struct {
39+
db.ListOptions
40+
RequireActionID int64
41+
OrgID int64
42+
}
43+
44+
func (opts FindRequireActionOptions) ToConds() builder.Cond {
45+
cond := builder.NewCond()
46+
if opts.OrgID > 0 {
47+
cond = cond.And(builder.Eq{"org_id": opts.OrgID})
48+
}
49+
if opts.RequireActionID > 0 {
50+
cond = cond.And(builder.Eq{"id": opts.RequireActionID})
51+
}
52+
return cond
53+
}
54+
55+
// LoadAttributes loads the attributes of the require action
56+
func (r *RequireAction) LoadAttributes(ctx context.Context) error {
57+
// place holder for now.
58+
return nil
59+
}
60+
61+
func ListAvailableWorkflows(ctx context.Context, orgID int64) ([]*RequireAction, error) {
62+
requireActionList := []*RequireAction{}
63+
/*
64+
orgRepos, err := org_model.GetOrgRepositories(ctx, orgID)
65+
if err != nil {
66+
return nil, fmt.Errorf("ListAvailableWorkflows get org repos: %w", err)
67+
}
68+
for _, repo := range orgRepos {
69+
repo.LoadUnits(ctx)
70+
actionsConfig := repo.MustGetUnit(ctx, unit.TypeActions).ActionsConfig()
71+
enabledWorkflows := actionsConfig.GetGlobalWorkflow()
72+
for _, workflow := range enabledWorkflows {
73+
requireAction := &RequireAction{
74+
OrgID: orgID,
75+
RepoID: repo.ID,
76+
Repo: repo,
77+
Data: workflow,
78+
Link: repo.APIURL(),
79+
}
80+
requireActionList = append(requireActionList, requireAction)
81+
}
82+
}
83+
for _, require_action := range requireActionList {
84+
if err := require_action.Validate(); err != nil {
85+
return requireActionList, err
86+
}
87+
}
88+
*/
89+
return requireActionList, nil
90+
}

models/repo/repo_unit.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,23 @@ func (cfg *PullRequestsConfig) GetDefaultMergeStyle() MergeStyle {
168168

169169
type ActionsConfig struct {
170170
DisabledWorkflows []string
171+
EnabledGlobalWorkflows []string
171172
}
172173

173174
func (cfg *ActionsConfig) EnableWorkflow(file string) {
174175
cfg.DisabledWorkflows = util.SliceRemoveAll(cfg.DisabledWorkflows, file)
176+
}
177+
178+
func (cfg *ActionsConfig) DisableGlobalWorkflow(file string) {
179+
cfg.EnabledGlobalWorkflows = util.SliceRemoveAll(cfg.EnabledGlobalWorkflows, file)
180+
}
181+
182+
func (cfg *ActionsConfig) IsGlobalWorkflowEnabled(file string) bool {
183+
return slices.Contains(cfg.EnabledGlobalWorkflows, file)
184+
}
185+
186+
func (cfg *ActionsConfig) EnableGlobalWorkflow(file string) {
187+
cfg.EnabledGlobalWorkflows = append(cfg.EnabledGlobalWorkflows, file)
175188
}
176189

177190
func (cfg *ActionsConfig) ToString() string {

options/locale/locale_en-US.ini

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3638,11 +3638,28 @@ runs.no_workflows.documentation = For more information on Gitea Actions, see <a
36383638
runs.no_runs = The workflow has no runs yet.
36393639
runs.empty_commit_message = (empty commit message)
36403640
3641+
require_actions = Require Actions
3642+
require_actions.require_actions_manage_panel = Require Actions Management Panel
3643+
require_actions.id = ID
3644+
require_actions.name = Name
3645+
require_actions.new = Create New
3646+
require_actions.status = Status
3647+
require_actions.version = Version
3648+
require_actions.repo = Repo
3649+
require_actions.link = Link
3650+
require_actions.none = No Require Actions Available
3651+
36413652
workflow.disable = Disable Workflow
36423653
workflow.disable_success = Workflow '%s' disabled successfully.
36433654
workflow.enable = Enable Workflow
36443655
workflow.enable_success = Workflow '%s' enabled successfully.
36453656
workflow.disabled = Workflow is disabled.
3657+
workflow.global = Global
3658+
workflow.global_disable = Disable Global Require
3659+
workflow.global_disable_success = Global Require '%s' disabled successfully.
3660+
workflow.global_enable = Enable Global Require
3661+
workflow.global_enable_success = Global Require '%s' enabled successfully.
3662+
workflow.global_enabled = Global Require is disabled.
36463663
36473664
need_approval_desc = Need approval to run workflows for fork pull request.
36483665

routers/web/repo/actions/actions.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ func List(ctx *context.Context) {
145145
workflow := ctx.FormString("workflow")
146146
actorID := ctx.FormInt64("actor")
147147
status := ctx.FormInt("status")
148+
isGlobal := false
148149
ctx.Data["CurWorkflow"] = workflow
149150

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

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

routers/web/repo/actions/view.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,3 +714,38 @@ func disableOrEnableWorkflowFile(ctx *context_module.Context, isEnable bool) {
714714
url.QueryEscape(ctx.FormString("actor")), url.QueryEscape(ctx.FormString("status")))
715715
ctx.JSONRedirect(redirectURL)
716716
}
717+
718+
func DisableGlobalWorkflowFile(ctx *context_module.Context) {
719+
disableOrEnableGlobalWorkflowFile(ctx, true)
720+
}
721+
722+
func EnableGlobalWorkflowFile(ctx *context_module.Context) {
723+
disableOrEnableGlobalWorkflowFile(ctx, false)
724+
}
725+
726+
func disableOrEnableGlobalWorkflowFile(ctx *context_module.Context, isGlobalEnable bool) {
727+
workflow := ctx.FormString("workflow")
728+
if len(workflow) == 0 {
729+
ctx.ServerError("workflow", nil)
730+
return
731+
}
732+
cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions)
733+
cfg := cfgUnit.ActionsConfig()
734+
if isGlobalEnable {
735+
cfg.DisableGlobalWorkflow(workflow)
736+
} else {
737+
cfg.EnableGlobalWorkflow(workflow)
738+
}
739+
if err := repo_model.UpdateRepoUnit(ctx, cfgUnit); err != nil {
740+
ctx.ServerError("UpdateRepoUnit", err)
741+
return
742+
}
743+
if isGlobalEnable {
744+
ctx.Flash.Success(ctx.Tr("actions.workflow.global_disable_success", workflow))
745+
} else {
746+
ctx.Flash.Success(ctx.Tr("actions.workflow.global_enable_success", workflow))
747+
}
748+
redirectURL := fmt.Sprintf("%s/actions?workflow=%s&actor=%s&status=%s", ctx.Repo.RepoLink, url.QueryEscape(workflow),
749+
url.QueryEscape(ctx.FormString("actor")), url.QueryEscape(ctx.FormString("status")))
750+
ctx.JSONRedirect(redirectURL)
751+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
// WIP RequireAction
5+
6+
package setting
7+
8+
import (
9+
"errors"
10+
"net/http"
11+
12+
"code.gitea.io/gitea/models/db"
13+
"code.gitea.io/gitea/modules/base"
14+
// "code.gitea.io/gitea/modules/log"
15+
16+
"code.gitea.io/gitea/services/context"
17+
18+
//"code.gitea.io/gitea/modules/setting"
19+
shared "code.gitea.io/gitea/routers/web/shared/actions"
20+
actions_model "code.gitea.io/gitea/models/actions"
21+
)
22+
23+
const (
24+
// let start with org WIP
25+
tplOrgRequireActions base.TplName = "org/settings/actions"
26+
)
27+
28+
type requireActionsCtx struct {
29+
OrgID int64
30+
IsOrg bool
31+
RequireActionsTemplate base.TplName
32+
RedirectLink string
33+
}
34+
35+
func getRequireActionsCtx(ctx *context.Context) (*requireActionsCtx, error) {
36+
if ctx.Data["PageIsOrgSettings"] == true {
37+
return &requireActionsCtx{
38+
OrgID: ctx.Org.Organization.ID,
39+
IsOrg: true,
40+
RequireActionsTemplate: tplOrgRequireActions,
41+
RedirectLink: ctx.Org.OrgLink + "/settings/actions/require_action",
42+
}, nil
43+
}
44+
return nil, errors.New("unable to set Require Actions context")
45+
}
46+
47+
// Listing all RequireActions
48+
func RequireActions(ctx *context.Context) {
49+
ctx.Data["ActionsTitle"] = ctx.Tr("actions.requires")
50+
ctx.Data["PageType"] = "require_actions"
51+
ctx.Data["PageIsSharedSettingsRequireActions"] = true
52+
53+
vCtx, err := getRequireActionsCtx(ctx)
54+
if err != nil {
55+
ctx.ServerError("getRequireActionsCtx", err)
56+
return
57+
}
58+
shared.SetRequireActionsContext(ctx)
59+
if ctx.Written() {
60+
return
61+
}
62+
opts := actions_model.FindRequireActionOptions{
63+
ListOptions: db.ListOptions{
64+
Page: 1,
65+
PageSize: 10,
66+
},
67+
OrgID: ctx.Org.Organization.ID,
68+
}
69+
shared.RequireActionsList(ctx, opts)
70+
ctx.HTML(http.StatusOK, vCtx.RequireActionsTemplate)
71+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
// WIP RequireAction
5+
6+
package actions
7+
8+
import (
9+
//"errors"
10+
11+
actions_model "code.gitea.io/gitea/models/actions"
12+
"code.gitea.io/gitea/models/db"
13+
//"code.gitea.io/gitea/modules/log"
14+
//"code.gitea.io/gitea/modules/util"
15+
//"code.gitea.io/gitea/modules/web"
16+
//"code.gitea.io/gitea/services/forms"
17+
// action_service "code.gitea.io/gitea/services/actions"
18+
19+
"code.gitea.io/gitea/services/context"
20+
)
21+
22+
// RequireActionsList prepares data for workflow actions list
23+
func RequireActionsList(ctx *context.Context, opts actions_model.FindRequireActionOptions) {
24+
//all_opts := repo_model.FindEnabledGlobalWorkflowsOptions {}
25+
//avalible_workflows, count, err := db.FindAndCount[repo_model.RepoUnit](ctx, all_opts)
26+
require_actions, count, err := db.FindAndCount[actions_model.RequireAction](ctx, opts)
27+
if err != nil {
28+
ctx.ServerError("CountRequireAction", err)
29+
return
30+
}
31+
/*
32+
if err := actions_model.RequireActionList(require_actions).LoadAttributes(ctx); err != nil {
33+
ctx.ServerError("RequireActionLoadAttributes", err)
34+
return
35+
}
36+
*/
37+
ctx.Data["RequireActions"] = require_actions
38+
ctx.Data["Total"] = count
39+
ctx.Data["RequireActionsOrgID"] = opts.OrgID
40+
//ctx.Data["Sort"] = opts.Sort
41+
pager := context.NewPagination(int(count), opts.PageSize, opts.Page, 5)
42+
ctx.Data["Page"] = pager
43+
workflow_list, err := actions_model.ListAvailableWorkflows(ctx, opts.OrgID)
44+
if err != nil {
45+
ctx.ServerError("ListAvailableWorkflows", err)
46+
return
47+
}
48+
/*
49+
var global_workflow []string
50+
for _, workflow := range workflow_list {
51+
global_workflow = append(global_workflow, workflow.Data)
52+
}
53+
ctx.Data["ListAvailableWorkflows"] = global_workflow */
54+
ctx.Data["ListAvailableWorkflows"] = workflow_list
55+
}
56+
57+
// SetRequireActionDeletePost response for deleting a require action workflow
58+
func SetRequireActionsContext(ctx *context.Context) {
59+
}

routers/web/web.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,13 @@ func registerRoutes(m *web.Route) {
458458
})
459459
}
460460

461+
// WIP RequireAction
462+
addSettingsRequireActionsRoutes := func() {
463+
m.Group("/require_actions", func() {
464+
m.Get("", repo_setting.RequireActions)
465+
})
466+
}
467+
461468
// FIXME: not all routes need go through same middleware.
462469
// Especially some AJAX requests, we can reduce middleware number to improve performance.
463470

@@ -925,6 +932,7 @@ func registerRoutes(m *web.Route) {
925932

926933
m.Group("/actions", func() {
927934
m.Get("", org_setting.RedirectToDefaultSetting)
935+
addSettingsRequireActionsRoutes()
928936
addSettingsRunnersRoutes()
929937
addSettingsSecretsRoutes()
930938
addSettingsVariablesRoutes()
@@ -1358,6 +1366,8 @@ func registerRoutes(m *web.Route) {
13581366
m.Get("", actions.List)
13591367
m.Post("/disable", reqRepoAdmin, actions.DisableWorkflowFile)
13601368
m.Post("/enable", reqRepoAdmin, actions.EnableWorkflowFile)
1369+
m.Post("/global_disable", reqRepoAdmin, actions.DisableGlobalWorkflowFile)
1370+
m.Post("/global_enable", reqRepoAdmin, actions.EnableGlobalWorkflowFile)
13611371

13621372
m.Group("/runs/{run}", func() {
13631373
m.Combo("").

0 commit comments

Comments
 (0)