Skip to content

Commit 7141df2

Browse files
committed
Adding the new org require action files
1 parent 5c85d88 commit 7141df2

File tree

7 files changed

+745
-0
lines changed

7 files changed

+745
-0
lines changed

models/actions/require_action.go

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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+
"code.gitea.io/gitea/modules/util"
20+
21+
"xorm.io/builder"
22+
)
23+
24+
type RequireAction struct {
25+
ID int64 `xorm:"pk autoincr"`
26+
OwnerID int64 `xorm:"index"` // should be the Org, Global?
27+
Org *org_model.Organization `xorm:"-"`
28+
OrgID int64 `xorm:"index"`
29+
Repo *repo_model.Repository `xorm:"-"`
30+
RepoID int64 `xorm:"index"`
31+
Name string `xorm:"UNIQUE(owner_repo_name) NOT NULL"`
32+
Data string `xorm:"LONGTEXT NOT NULL"`
33+
Link string `xorm:"LONGTEXT NOT NULL"`
34+
CreatedUnix timeutil.TimeStamp `xorm:"created NOT NULL"`
35+
UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
36+
37+
RepoRange string // glob match which repositories could use this runner
38+
}
39+
40+
func (v *RequireAction) Validate() error {
41+
if v.RepoID != 0 && v.OrgID != 0 && v.Data != "" {
42+
return nil
43+
} else {
44+
return errors.New("the action workflow need repo id , org id and the name, something missing")
45+
}
46+
}
47+
48+
func init() {
49+
db.RegisterModel(new(RequireAction))
50+
}
51+
52+
// ErrRequireActionNotFound represents a "require action not found" error.
53+
type ErrRequireActionNotFound struct {
54+
Name string
55+
}
56+
57+
func (err ErrRequireActionNotFound) Error() string {
58+
return fmt.Sprintf("require action was not found [name: %s]", err.Name)
59+
}
60+
61+
func (err ErrRequireActionNotFound) Unwrap() error {
62+
return util.ErrNotExist
63+
}
64+
65+
66+
67+
type FindRequireActionOptions struct {
68+
db.ListOptions
69+
RequireActionID int64
70+
OrgID int64
71+
}
72+
73+
func (opts FindRequireActionOptions) ToConds() builder.Cond {
74+
cond := builder.NewCond()
75+
if opts.OrgID > 0 {
76+
cond = cond.And(builder.Eq{"org_id": opts.OrgID})
77+
}
78+
if opts.RequireActionID > 0 {
79+
cond = cond.And(builder.Eq{"id": opts.RequireActionID})
80+
}
81+
return cond
82+
}
83+
84+
// LoadAttributes loads the attributes of the require action
85+
func (r *RequireAction) LoadAttributes(ctx context.Context) error {
86+
// place holder for now.
87+
return nil
88+
}
89+
90+
func InsertRequireAction(ctx context.Context, orgID int64, repoID int64, data string, link string) (*RequireAction, error) {
91+
require_action := &RequireAction{
92+
OrgID: orgID,
93+
RepoID: repoID,
94+
Data: data,
95+
Link: link,
96+
}
97+
if err := require_action.Validate(); err != nil {
98+
return require_action, err
99+
}
100+
return require_action, db.Insert(ctx, require_action)
101+
}
102+
103+
// Editable checks if the require action is editable by the user
104+
func (r *RequireAction) Editable(orgID int64, repoID int64) bool {
105+
if orgID == 0 && repoID == 0 {
106+
return true
107+
}
108+
if orgID > 0 && r.OrgID == orgID {
109+
return true
110+
}
111+
return repoID > 0 && r.RepoID == repoID
112+
}
113+
114+
func GetRequireActionByID(ctx context.Context, require_actionID int64) (*RequireAction, error) {
115+
var require_action RequireAction
116+
has, err := db.GetEngine(ctx).Where("id=?", require_actionID).Get(&require_action)
117+
if err != nil {
118+
return nil, err
119+
} else if !has {
120+
return nil, fmt.Errorf("require action with id %d: %w", require_actionID, util.ErrNotExist)
121+
}
122+
return &require_action, nil
123+
}
124+
125+
func UpdateRequireAction(ctx context.Context, require_action *RequireAction) (bool, error) {
126+
count, err := db.GetEngine(ctx).ID(require_action.ID).Cols("name", "data", "link").
127+
Update(&RequireAction{
128+
Name: require_action.Name,
129+
Data: require_action.Data,
130+
Link: require_action.Link,
131+
})
132+
return count != 0, err
133+
}
134+
135+
func ListAvailableWorkflows(ctx context.Context, orgID int64) ([]*RequireAction, error) {
136+
requireActionList := []*RequireAction{}
137+
orgRepos, err := org_model.GetOrgRepositories(ctx, orgID)
138+
if err != nil {
139+
return nil, fmt.Errorf("ListAvailableWorkflows get org repos: %w", err)
140+
}
141+
for _, repo := range orgRepos {
142+
repo.LoadUnits(ctx)
143+
actionsConfig := repo.MustGetUnit(ctx, unit.TypeActions).ActionsConfig()
144+
enabledWorkflows := actionsConfig.GetGlobalWorkflow()
145+
for _, workflow := range enabledWorkflows {
146+
requireAction := &RequireAction{
147+
OrgID: orgID,
148+
RepoID: repo.ID,
149+
Repo: repo,
150+
Data: workflow,
151+
Link: repo.APIURL(),
152+
}
153+
requireActionList = append(requireActionList, requireAction)
154+
}
155+
}
156+
for _, require_action := range requireActionList {
157+
if err := require_action.Validate(); err != nil {
158+
return requireActionList, err
159+
}
160+
}
161+
return requireActionList, nil
162+
}
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/modules/context"
10+
)
11+
12+
func RedirectToRepoSetting(ctx *context.Context) {
13+
ctx.Redirect(ctx.Org.OrgLink + "/settings/actions/require_actions")
14+
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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/context"
15+
"code.gitea.io/gitea/modules/log"
16+
17+
//"code.gitea.io/gitea/modules/setting"
18+
shared "code.gitea.io/gitea/routers/web/shared/actions"
19+
//shared_user "code.gitea.io/gitea/routers/web/shared/user"
20+
actions_model "code.gitea.io/gitea/models/actions"
21+
actions_shared "code.gitea.io/gitea/routers/web/shared/actions"
22+
)
23+
24+
const (
25+
// let start with org WIP
26+
//tplRepoReqireActions base.TplName = "repo/settings/actions"
27+
//tplUserRequireActions base.TplName = "user/settings/actions"
28+
//tplAdminRequireActions base.TplName = "admin/actions"
29+
tplOrgRequireActions base.TplName = "org/settings/actions"
30+
)
31+
32+
type requireActionsCtx struct {
33+
OrgID int64
34+
IsOrg bool
35+
RequireActionsTemplate base.TplName
36+
RedirectLink string
37+
}
38+
39+
func getRequireActionsCtx(ctx *context.Context) (*requireActionsCtx, error) {
40+
if ctx.Data["PageIsOrgSettings"] == true {
41+
/*
42+
err := shared_user.LoadHeaderCount(ctx)
43+
if err != nil {
44+
ctx.ServerError("LoadHeaderCount", err)
45+
return nil, nil
46+
}
47+
*/
48+
return &requireActionsCtx{
49+
OrgID: ctx.Org.Organization.ID,
50+
IsOrg: true,
51+
RequireActionsTemplate: tplOrgRequireActions,
52+
RedirectLink: ctx.Org.OrgLink + "/settings/actions/require_action",
53+
}, nil
54+
}
55+
/*
56+
if ctx.Data["PageIsRepoSettings"] == true {
57+
return &requireActionsCtx{
58+
OwnerID: 0,
59+
RepoID: ctx.Repo.Repository.ID,
60+
RequireActionsTemplate: tplRepoReqireActions,
61+
// RedirectLink: ctx.Repo.RepoLink + "/settings/actions/require_action_list",
62+
RedirectLink: ctx.Repo.RepoLink + "/settings/actions/require_action",
63+
}, nil
64+
}
65+
66+
if ctx.Data["PageIsUserSettings"] == true {
67+
return &requireActionsCtx{
68+
OwnerID: ctx.Doer.ID,
69+
RepoID: 0,
70+
RequireActionsTemplate: tplUserVariables,
71+
RedirectLink: setting.AppSubURL + "/user/settings/actions/require_action",
72+
}, nil
73+
}
74+
75+
if ctx.Data["PageIsAdmin"] == true {
76+
return &requireActionsCtx{
77+
OwnerID: 0,
78+
RepoID: 0,
79+
IsGlobal: true,
80+
RequireActionsTemplate: tplAdminVariables,
81+
RedirectLink: setting.AppSubURL + "/admin/actions/require_action",
82+
}, nil
83+
}
84+
*/
85+
return nil, errors.New("unable to set Require Actions context")
86+
}
87+
88+
func RequireActionsList(ctx *context.Context) {
89+
ctx.Data["ActionsTitle"] = ctx.Tr("actions.requires")
90+
ctx.Data["PageType"] = "require_actions"
91+
ctx.Data["PageIsSharedSettingsRequireActions"] = true
92+
93+
vCtx, err := getRequireActionsCtx(ctx)
94+
if err != nil {
95+
ctx.ServerError("getRequireActionsCtx", err)
96+
return
97+
}
98+
99+
shared.SetRequireActionsContext(ctx)
100+
if ctx.Written() {
101+
return
102+
}
103+
opts := actions_model.FindRequireActionOptions{
104+
ListOptions: db.ListOptions{
105+
Page: 1,
106+
PageSize: 10,
107+
},
108+
OrgID: ctx.Org.Organization.ID,
109+
}
110+
actions_shared.RequireActionsList(ctx, opts)
111+
ctx.HTML(http.StatusOK, vCtx.RequireActionsTemplate)
112+
}
113+
114+
func RequireActionsCreate(ctx *context.Context) {
115+
log.Trace("Require Action Trace")
116+
vCtx, err := getRequireActionsCtx(ctx)
117+
if err != nil {
118+
ctx.ServerError("getRequireActionsCtx", err)
119+
return
120+
}
121+
122+
if ctx.HasError() { // form binding validation error
123+
ctx.JSONError(ctx.GetErrMsg())
124+
return
125+
}
126+
127+
shared.CreateRequireAction(
128+
ctx,
129+
vCtx.OrgID,
130+
vCtx.RedirectLink,
131+
)
132+
}
133+
134+
/*
135+
func RequireActionsUpdate(ctx *context.Context) {
136+
vCtx, err := getRequireActionsCtx(ctx)
137+
if err != nil {
138+
ctx.ServerError("getRequireActionsCtx", err)
139+
return
140+
}
141+
if ctx.HasError() { // form binding validation error
142+
ctx.JSONError(ctx.GetErrMsg())
143+
return
144+
}
145+
shared.UpdateRequireAction(ctx, vCtx.RedirectLink)
146+
}
147+
148+
func RequireActionsUpdatePost(ctx *context.Context) {
149+
vCtx, err := getRequireActionsCtx(ctx)
150+
if err != nil {
151+
ctx.ServerError("getRequireActionsCtx", err)
152+
return
153+
}
154+
shared.UpdateRequireAction(ctx, vCtx.RedirectLink)
155+
}
156+
157+
func RequireActionsDelete(ctx *context.Context) {
158+
vCtx, err := getRequireActionsCtx(ctx)
159+
if err != nil {
160+
ctx.ServerError("getRequireActionsCtx", err)
161+
return
162+
}
163+
shared.DeleteRequireAction(ctx, vCtx.RedirectLink)
164+
}
165+
func RequireActionsDeletePost(ctx *context.Context) {
166+
vCtx, err := getRequireActionsCtx(ctx)
167+
if err != nil {
168+
ctx.ServerError("getRequireActionsCtx", err)
169+
return
170+
}
171+
shared.DeleteRequireAction(ctx, vCtx.RedirectLink)
172+
}
173+
*/

0 commit comments

Comments
 (0)