Skip to content

Commit c0d50fd

Browse files
committed
Combined all RequestActions implementation and merge upstream
Update all formating issues Signed-off-by: Alex Lau(AvengerMoJo) <avengermojo@gmail.com>
1 parent e0c27e5 commit c0d50fd

File tree

19 files changed

+695
-15
lines changed

19 files changed

+695
-15
lines changed

models/actions/require_action.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
11+
"code.gitea.io/gitea/models/db"
12+
"code.gitea.io/gitea/modules/timeutil"
13+
14+
"xorm.io/builder"
15+
)
16+
17+
type RequireAction struct {
18+
ID int64 `xorm:"pk autoincr"`
19+
OrgID int64 `xorm:"INDEX"`
20+
RepoName string `xorm:"VARCHAR(255)"`
21+
WorkflowName string `xorm:"VARCHAR(255) UNIQUE(require_action) NOT NULL"`
22+
CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
23+
UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
24+
}
25+
26+
type GlobalWorkflow struct {
27+
RepoName string
28+
Filename string
29+
}
30+
31+
func init() {
32+
db.RegisterModel(new(RequireAction))
33+
}
34+
35+
type FindRequireActionOptions struct {
36+
db.ListOptions
37+
RequireActionID int64
38+
OrgID int64
39+
RepoName string
40+
}
41+
42+
func (opts FindRequireActionOptions) ToConds() builder.Cond {
43+
cond := builder.NewCond()
44+
if opts.OrgID > 0 {
45+
cond = cond.And(builder.Eq{"org_id": opts.OrgID})
46+
}
47+
if opts.RequireActionID > 0 {
48+
cond = cond.And(builder.Eq{"id": opts.RequireActionID})
49+
}
50+
if opts.RepoName != "" {
51+
cond = cond.And(builder.Eq{"repo_name": opts.RepoName})
52+
}
53+
return cond
54+
}
55+
56+
// LoadAttributes loads the attributes of the require action
57+
func (r *RequireAction) LoadAttributes(ctx context.Context) error {
58+
// place holder for now.
59+
return nil
60+
}
61+
62+
// if the workflow is removable
63+
func (r *RequireAction) Removable(orgID int64) bool {
64+
// everyone can remove for now
65+
return r.OrgID == orgID
66+
}
67+
68+
func AddRequireAction(ctx context.Context, orgID int64, repoName, workflowName string) (*RequireAction, error) {
69+
ra := &RequireAction{
70+
OrgID: orgID,
71+
RepoName: repoName,
72+
WorkflowName: workflowName,
73+
}
74+
return ra, db.Insert(ctx, ra)
75+
}
76+
77+
func DeleteRequireAction(ctx context.Context, requireActionID int64) error {
78+
if _, err := db.DeleteByID[RequireAction](ctx, requireActionID); err != nil {
79+
return err
80+
}
81+
return nil
82+
}

models/repo/repo_unit.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,21 +169,34 @@ func (cfg *PullRequestsConfig) GetDefaultMergeStyle() MergeStyle {
169169
}
170170

171171
type ActionsConfig struct {
172-
DisabledWorkflows []string
172+
DisabledWorkflows []string
173+
EnabledGlobalWorkflows []string
173174
}
174175

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

180+
func (cfg *ActionsConfig) EnableGlobalWorkflow(file string) {
181+
cfg.EnabledGlobalWorkflows = append(cfg.EnabledGlobalWorkflows, file)
182+
}
183+
179184
func (cfg *ActionsConfig) ToString() string {
180185
return strings.Join(cfg.DisabledWorkflows, ",")
181186
}
182187

188+
func (cfg *ActionsConfig) GetGlobalWorkflow() []string {
189+
return cfg.EnabledGlobalWorkflows
190+
}
191+
183192
func (cfg *ActionsConfig) IsWorkflowDisabled(file string) bool {
184193
return slices.Contains(cfg.DisabledWorkflows, file)
185194
}
186195

196+
func (cfg *ActionsConfig) IsGlobalWorkflowEnabled(file string) bool {
197+
return slices.Contains(cfg.EnabledGlobalWorkflows, file)
198+
}
199+
187200
func (cfg *ActionsConfig) DisableWorkflow(file string) {
188201
for _, workflow := range cfg.DisabledWorkflows {
189202
if file == workflow {
@@ -194,6 +207,10 @@ func (cfg *ActionsConfig) DisableWorkflow(file string) {
194207
cfg.DisabledWorkflows = append(cfg.DisabledWorkflows, file)
195208
}
196209

210+
func (cfg *ActionsConfig) DisableGlobalWorkflow(file string) {
211+
cfg.EnabledGlobalWorkflows = util.SliceRemoveAll(cfg.EnabledGlobalWorkflows, file)
212+
}
213+
197214
// FromDB fills up a ActionsConfig from serialized format.
198215
func (cfg *ActionsConfig) FromDB(bs []byte) error {
199216
return json.UnmarshalHandleDoubleEncode(bs, &cfg)

modules/actions/workflows.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ func ListWorkflows(commit *git.Commit) (git.Entries, error) {
6969
return ret, nil
7070
}
7171

72+
/*
73+
func ListGlobalWorkflows() (git.Entries, error) {
74+
tree, err := global.SubTree(".gitea/workflows")
75+
ret := make(git.Entries, 0, len(entries))
76+
return ret, nil
77+
}
78+
*/
79+
7280
func GetContentFromEntry(entry *git.TreeEntry) ([]byte, error) {
7381
f, err := entry.Blob().DataAsync()
7482
if err != nil {
@@ -95,6 +103,17 @@ func GetEventsFromContent(content []byte) ([]*jobparser.Event, error) {
95103
return events, nil
96104
}
97105

106+
func DetectGlobalWorkflows(
107+
gitRepo *git.Repository,
108+
commit *git.Commit,
109+
triggedEvent webhook_module.HookEventType,
110+
payload api.Payloader,
111+
detectSchedule bool,
112+
entries git.Entries,
113+
) ([]*DetectedWorkflow, []*DetectedWorkflow, error) {
114+
return _DetectWorkflows(gitRepo, commit, triggedEvent, payload, detectSchedule, entries)
115+
}
116+
98117
func DetectWorkflows(
99118
gitRepo *git.Repository,
100119
commit *git.Commit,
@@ -106,7 +125,17 @@ func DetectWorkflows(
106125
if err != nil {
107126
return nil, nil, err
108127
}
128+
return _DetectWorkflows(gitRepo, commit, triggedEvent, payload, detectSchedule, entries)
129+
}
109130

131+
func _DetectWorkflows(
132+
gitRepo *git.Repository,
133+
commit *git.Commit,
134+
triggedEvent webhook_module.HookEventType,
135+
payload api.Payloader,
136+
detectSchedule bool,
137+
entries git.Entries,
138+
) ([]*DetectedWorkflow, []*DetectedWorkflow, error) {
110139
workflows := make([]*DetectedWorkflow, 0, len(entries))
111140
schedules := make([]*DetectedWorkflow, 0, len(entries))
112141
for _, entry := range entries {
@@ -146,12 +175,25 @@ func DetectWorkflows(
146175
return workflows, schedules, nil
147176
}
148177

178+
func DetectScheduledGlobalWorkflows(gitRepo *git.Repository, commit *git.Commit, entries git.Entries) ([]*DetectedWorkflow, error) {
179+
return _DetectScheduledWorkflows(gitRepo, commit, entries)
180+
}
181+
149182
func DetectScheduledWorkflows(gitRepo *git.Repository, commit *git.Commit) ([]*DetectedWorkflow, error) {
150183
entries, err := ListWorkflows(commit)
151184
if err != nil {
152185
return nil, err
153186
}
187+
return _DetectScheduledWorkflows(gitRepo, commit, entries)
188+
}
154189

190+
func _DetectScheduledWorkflows(gitRepo *git.Repository, commit *git.Commit, entries git.Entries) ([]*DetectedWorkflow, error) {
191+
if gitRepo != nil {
192+
log.Trace("detect scheduled workflow for gitRepo.Path: %q", gitRepo.Path)
193+
}
194+
if commit != nil {
195+
log.Trace("detect scheduled commit for commit ID: %q", commit.ID)
196+
}
155197
wfs := make([]*DetectedWorkflow, 0, len(entries))
156198
for _, entry := range entries {
157199
content, err := GetContentFromEntry(entry)

options/locale/locale_en-US.ini

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3698,6 +3698,27 @@ runs.no_runs = The workflow has no runs yet.
36983698
runs.empty_commit_message = (empty commit message)
36993699
runs.expire_log_message = Logs have been purged because they were too old.
37003700

3701+
require_action = Require Action
3702+
require_action.require_action_manage_panel = Require Action Management Panel
3703+
require_action.enable_global_workflow = How to Enable Global Workflow
3704+
require_action.id = ID
3705+
require_action.add = Add Global Workflow
3706+
require_action.add_require_action = Enable selected Workflow
3707+
require_action.new = Create New
3708+
require_action.status = Status
3709+
require_action.search = Search...
3710+
require_action.version = Version
3711+
require_action.repo = Repo Name
3712+
require_action.workflow = Workflow Filename
3713+
require_action.link = Link
3714+
require_action.remove = Remove
3715+
require_action.none = No Require Action Available.
3716+
require_action.creation.failed = Create Global Require Action %s Failed.
3717+
require_action.creation.success = Create Global Require Action %s successfully.
3718+
require_action.deletion = Delete
3719+
require_action.deletion.description = Removing the Global Require Action is permanent and cannot be undone. Continue?
3720+
require_action.deletion.success = The Global Require Action has been removed.
3721+
37013722
workflow.disable = Disable Workflow
37023723
workflow.disable_success = Workflow '%s' disabled successfully.
37033724
workflow.enable = Enable Workflow
@@ -3709,6 +3730,13 @@ workflow.run_success = Workflow '%s' run successfully.
37093730
workflow.from_ref = Use workflow from
37103731
workflow.has_workflow_dispatch = This workflow has a workflow_dispatch event trigger.
37113732

3733+
workflow.global = Global
3734+
workflow.global_disable = Disable Global Require
3735+
workflow.global_disable_success = Global Require '%s' disabled successfully.
3736+
workflow.global_enable = Enable Global Require
3737+
workflow.global_enable_success = Global Require '%s' enabled successfully.
3738+
workflow.global_enabled = Global Require is disabled.
3739+
37123740
need_approval_desc = Need approval to run workflows for fork pull request.
37133741

37143742
variables = Variables
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
"code.gitea.io/gitea/services/context"
10+
)
11+
12+
func RedirectToRepoSetting(ctx *context.Context) {
13+
ctx.Redirect(ctx.Org.OrgLink + "/settings/actions/require_action")
14+
}

routers/web/repo/actions/actions.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"code.gitea.io/gitea/modules/base"
2020
"code.gitea.io/gitea/modules/container"
2121
"code.gitea.io/gitea/modules/git"
22+
"code.gitea.io/gitea/modules/gitrepo"
2223
"code.gitea.io/gitea/modules/log"
2324
"code.gitea.io/gitea/modules/optional"
2425
"code.gitea.io/gitea/modules/setting"
@@ -38,6 +39,7 @@ const (
3839

3940
type Workflow struct {
4041
Entry git.TreeEntry
42+
Global bool
4143
ErrMsg string
4244
}
4345

@@ -71,9 +73,19 @@ func List(ctx *context.Context) {
7173

7274
var workflows []Workflow
7375
var curWorkflow *model.Workflow
76+
var globalEntries []*git.TreeEntry
77+
globalWorkflow, err := db.Find[actions_model.RequireAction](ctx, actions_model.FindRequireActionOptions{
78+
OrgID: ctx.Repo.Repository.Owner.ID,
79+
})
80+
if err != nil {
81+
ctx.ServerError("Global Workflow DB find fail", err)
82+
return
83+
}
7484
if empty, err := ctx.Repo.GitRepo.IsEmpty(); err != nil {
7585
ctx.ServerError("IsEmpty", err)
76-
return
86+
if len(globalWorkflow) < 1 {
87+
return
88+
}
7789
} else if !empty {
7890
commit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch)
7991
if err != nil {
@@ -85,6 +97,23 @@ func List(ctx *context.Context) {
8597
ctx.ServerError("ListWorkflows", err)
8698
return
8799
}
100+
for _, gEntry := range globalWorkflow {
101+
if gEntry.RepoName == ctx.Repo.Repository.Name {
102+
log.Trace("Same Repo conflict: %s\n", gEntry.RepoName)
103+
continue
104+
}
105+
gRepo, _ := repo_model.GetRepositoryByName(ctx, gEntry.OrgID, gEntry.RepoName)
106+
gGitRepo, _ := gitrepo.OpenRepository(git.DefaultContext, gRepo)
107+
// it may be a hack for now..... not sure any better way to do this
108+
gCommit, _ := gGitRepo.GetBranchCommit(gRepo.DefaultBranch)
109+
gEntries, _ := actions.ListWorkflows(gCommit)
110+
for _, entry := range gEntries {
111+
if gEntry.WorkflowName == entry.Name() {
112+
globalEntries = append(globalEntries, entry)
113+
entries = append(entries, entry)
114+
}
115+
}
116+
}
88117

89118
// Get all runner labels
90119
runners, err := db.Find[actions_model.ActionRunner](ctx, actions_model.FindRunnerOptions{
@@ -103,7 +132,14 @@ func List(ctx *context.Context) {
103132

104133
workflows = make([]Workflow, 0, len(entries))
105134
for _, entry := range entries {
106-
workflow := Workflow{Entry: *entry}
135+
var workflowIsGlobal bool
136+
workflowIsGlobal = false
137+
for i := range globalEntries {
138+
if globalEntries[i] == entry {
139+
workflowIsGlobal = true
140+
}
141+
}
142+
workflow := Workflow{Entry: *entry, Global: workflowIsGlobal}
107143
content, err := actions.GetContentFromEntry(entry)
108144
if err != nil {
109145
ctx.ServerError("GetContentFromEntry", err)
@@ -165,6 +201,10 @@ func List(ctx *context.Context) {
165201
page = 1
166202
}
167203

204+
workflow := ctx.FormString("workflow")
205+
isGlobal := false
206+
ctx.Data["CurWorkflow"] = workflow
207+
168208
actionsConfig := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions).ActionsConfig()
169209
ctx.Data["ActionsConfig"] = actionsConfig
170210

@@ -205,6 +245,9 @@ func List(ctx *context.Context) {
205245
ctx.Data["Tags"] = tags
206246
}
207247
}
248+
ctx.Data["CurWorkflowDisabled"] = actionsConfig.IsWorkflowDisabled(workflow)
249+
ctx.Data["CurGlobalWorkflowEnable"] = actionsConfig.IsGlobalWorkflowEnabled(workflow)
250+
isGlobal = actionsConfig.IsGlobalWorkflowEnabled(workflow)
208251
}
209252

210253
// if status or actor query param is not given to frontend href, (href="/<repoLink>/actions")
@@ -261,6 +304,9 @@ func List(ctx *context.Context) {
261304
pager.AddParamString("workflow", workflowID)
262305
pager.AddParamString("actor", fmt.Sprint(actorID))
263306
pager.AddParamString("status", fmt.Sprint(status))
307+
if isGlobal {
308+
pager.AddParamString("global", fmt.Sprint(isGlobal))
309+
}
264310
ctx.Data["Page"] = pager
265311
ctx.Data["HasWorkflowsOrRuns"] = len(workflows) > 0 || len(runs) > 0
266312

0 commit comments

Comments
 (0)