Skip to content

Commit 346bc6c

Browse files
committed
Adding Global Require flag for workflow
1 parent 2e31a28 commit 346bc6c

File tree

6 files changed

+103
-2
lines changed

6 files changed

+103
-2
lines changed

models/repo/repo_unit.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ func (cfg *PullRequestsConfig) GetDefaultMergeStyle() MergeStyle {
167167
}
168168

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

173174
func (cfg *ActionsConfig) EnableWorkflow(file string) {
@@ -192,6 +193,18 @@ func (cfg *ActionsConfig) DisableWorkflow(file string) {
192193
cfg.DisabledWorkflows = append(cfg.DisabledWorkflows, file)
193194
}
194195

196+
func (cfg *ActionsConfig) DisableGlobalWorkflow(file string) {
197+
cfg.EnabledGlobalWorkflows = util.SliceRemoveAll(cfg.EnabledGlobalWorkflows, file)
198+
}
199+
200+
func (cfg *ActionsConfig) IsGlobalWorkflowEnabled(file string) bool {
201+
return slices.Contains(cfg.EnabledGlobalWorkflows, file)
202+
}
203+
204+
func (cfg *ActionsConfig) EnableGlobalWorkflow(file string) {
205+
cfg.EnabledGlobalWorkflows = append(cfg.EnabledGlobalWorkflows, file)
206+
}
207+
195208
// FromDB fills up a ActionsConfig from serialized format.
196209
func (cfg *ActionsConfig) FromDB(bs []byte) error {
197210
return json.UnmarshalHandleDoubleEncode(bs, &cfg)

options/locale/locale_en-US.ini

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3621,6 +3621,17 @@ runners.version = Version
36213621
runners.reset_registration_token = Reset registration token
36223622
runners.reset_registration_token_success = Runner registration token reset successfully
36233623

3624+
require_actions = Require Actions
3625+
require_actions.require_actions_manage_panel = Require Actions Management Panel
3626+
require_actions.id = ID
3627+
require_actions.name = Name
3628+
require_actions.new = Create New
3629+
require_actions.status = Status
3630+
require_actions.version = Version
3631+
require_actions.repo = Repo
3632+
require_actions.link = Link
3633+
require_actions.none = No Require Actions Available
3634+
36243635
runs.all_workflows = All Workflows
36253636
runs.commit = Commit
36263637
runs.scheduled = Scheduled
@@ -3644,6 +3655,12 @@ workflow.disable_success = Workflow '%s' disabled successfully.
36443655
workflow.enable = Enable Workflow
36453656
workflow.enable_success = Workflow '%s' enabled successfully.
36463657
workflow.disabled = Workflow is disabled.
3658+
workflow.global = Global
3659+
workflow.global_disable = Disable Global Require
3660+
workflow.global_disable_success = Global Require '%s' disabled successfully.
3661+
workflow.global_enable = Enable Global Require
3662+
workflow.global_enable_success = Global Require '%s' enabled successfully.
3663+
workflow.global_enabled = Global Require is disabled.
36473664
36483665
need_approval_desc = Need approval to run workflows for fork pull request.
36493666

routers/web/repo/actions/actions.go

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

150152
actionsConfig := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions).ActionsConfig()
@@ -153,6 +155,9 @@ func List(ctx *context.Context) {
153155
if len(workflow) > 0 && ctx.Repo.IsAdmin() {
154156
ctx.Data["AllowDisableOrEnableWorkflow"] = true
155157
ctx.Data["CurWorkflowDisabled"] = actionsConfig.IsWorkflowDisabled(workflow)
158+
ctx.Data["CurGlobalWorkflowEnable"] = actionsConfig.IsGlobalWorkflowEnabled(workflow)
159+
160+
isGlobal = actionsConfig.IsGlobalWorkflowEnabled(workflow)
156161
}
157162

158163
// if status or actor query param is not given to frontend href, (href="/<repoLink>/actions")
@@ -209,6 +214,9 @@ func List(ctx *context.Context) {
209214
pager.AddParamString("workflow", workflow)
210215
pager.AddParamString("actor", fmt.Sprint(actorID))
211216
pager.AddParamString("status", fmt.Sprint(status))
217+
if isGlobal {
218+
pager.AddParamString("global", fmt.Sprint(isGlobal))
219+
}
212220
ctx.Data["Page"] = pager
213221
ctx.Data["HasWorkflowsOrRuns"] = len(workflows) > 0 || len(runs) > 0
214222

routers/web/repo/actions/view.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,3 +714,43 @@ 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+
733+
cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions)
734+
cfg := cfgUnit.ActionsConfig()
735+
736+
if isGlobalEnable {
737+
cfg.DisableGlobalWorkflow(workflow)
738+
} else {
739+
cfg.EnableGlobalWorkflow(workflow)
740+
}
741+
742+
if err := repo_model.UpdateRepoUnit(ctx, cfgUnit); err != nil {
743+
ctx.ServerError("UpdateRepoUnit", err)
744+
return
745+
}
746+
747+
if isGlobalEnable {
748+
ctx.Flash.Success(ctx.Tr("actions.workflow.global_disable_success", workflow))
749+
} else {
750+
ctx.Flash.Success(ctx.Tr("actions.workflow.global_enable_success", workflow))
751+
}
752+
753+
redirectURL := fmt.Sprintf("%s/actions?workflow=%s&actor=%s&status=%s", ctx.Repo.RepoLink, url.QueryEscape(workflow),
754+
url.QueryEscape(ctx.FormString("actor")), url.QueryEscape(ctx.FormString("status")))
755+
ctx.JSONRedirect(redirectURL)
756+
}

routers/web/web.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,17 @@ func registerRoutes(m *web.Route) {
448448
})
449449
}
450450

451+
// WIP RequireAction
452+
addSettingsRequireActionsRoutes := func() {
453+
m.Group("/require_actions", func() {
454+
m.Get("", repo_setting.RequireActions)
455+
m.Combo("/{require_action_id}").Get(repo_setting.RequireActionsUpdate).
456+
Post(web.Bind(forms.EditRequireActionForm{}), repo_setting.RequireActionsUpdatePost)
457+
m.Post("/require_action_id}/delete", repo_setting.RequireActionsDeletePost)
458+
459+
})
460+
}
461+
451462
addSettingsRunnersRoutes := func() {
452463
m.Group("/runners", func() {
453464
m.Get("", repo_setting.Runners)
@@ -925,6 +936,7 @@ func registerRoutes(m *web.Route) {
925936

926937
m.Group("/actions", func() {
927938
m.Get("", org_setting.RedirectToDefaultSetting)
939+
addSettingsRequireActionsRoutes()
928940
addSettingsRunnersRoutes()
929941
addSettingsSecretsRoutes()
930942
addSettingsVariablesRoutes()
@@ -1360,6 +1372,8 @@ func registerRoutes(m *web.Route) {
13601372
m.Get("", actions.List)
13611373
m.Post("/disable", reqRepoAdmin, actions.DisableWorkflowFile)
13621374
m.Post("/enable", reqRepoAdmin, actions.EnableWorkflowFile)
1375+
m.Post("/global_disable", reqRepoAdmin, actions.DisableGlobalWorkflowFile)
1376+
m.Post("/global_enable", reqRepoAdmin, actions.EnableGlobalWorkflowFile)
13631377

13641378
m.Group("/runs/{run}", func() {
13651379
m.Combo("").

templates/repo/actions/list.tmpl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
{{if $.ActionsConfig.IsWorkflowDisabled .Entry.Name}}
2121
<div class="ui red label">{{ctx.Locale.Tr "disabled"}}</div>
2222
{{end}}
23+
{{if $.ActionsConfig.IsGlobalWorkflowEnabled .Entry.Name}}
24+
<div class="ui red label">{{ctx.Locale.Tr "Global Enabled"}}</div>
25+
{{end}}
2326
</a>
2427
{{end}}
2528
</div>
@@ -64,14 +67,20 @@
6467
{{end}}
6568
</div>
6669
</div>
67-
70+
<!-- IsGlobalWorkflowEnabled -->
71+
<div class="ui dropdown jump item">
72+
<span class="text">{{ctx.Locale.Tr "actions.workflow.global"}}</span>
73+
</div>
6874
{{if .AllowDisableOrEnableWorkflow}}
6975
<button class="ui jump dropdown btn interact-bg tw-p-2">
7076
{{svg "octicon-kebab-horizontal"}}
7177
<div class="menu">
7278
<a class="item link-action" data-url="{{$.Link}}/{{if .CurWorkflowDisabled}}enable{{else}}disable{{end}}?workflow={{$.CurWorkflow}}&actor={{.CurActor}}&status={{$.CurStatus}}">
7379
{{if .CurWorkflowDisabled}}{{ctx.Locale.Tr "actions.workflow.enable"}}{{else}}{{ctx.Locale.Tr "actions.workflow.disable"}}{{end}}
7480
</a>
81+
<a class="item link-action" data-url="{{$.Link}}/{{if .CurGlobalWorkflowEnable}}global_disable{{else}}global_enable{{end}}?workflow={{$.CurWorkflow}}&actor={{.CurActor}}&status={{$.CurStatus}}">
82+
{{if .CurGlobalWorkflowEnable}}{{ctx.Locale.Tr "actions.workflow.global_disable"}}{{else}}{{ctx.Locale.Tr "actions.workflow.global_enable"}}{{end}}
83+
</a>
7584
</div>
7685
</button>
7786
{{end}}

0 commit comments

Comments
 (0)