|
| 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 | +} |
0 commit comments