|
| 1 | +// Copyright 2026 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package actions |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "slices" |
| 10 | + |
| 11 | + "gitea.dev/models/db" |
| 12 | + repo_model "gitea.dev/models/repo" |
| 13 | + "gitea.dev/modules/timeutil" |
| 14 | + "gitea.dev/modules/util" |
| 15 | + |
| 16 | + "xorm.io/builder" |
| 17 | +) |
| 18 | + |
| 19 | +// ActionScopedWorkflowSource registers a repository as a source of scoped workflows, either for an owner (user/org) or for the whole instance. |
| 20 | +type ActionScopedWorkflowSource struct { |
| 21 | + ID int64 `xorm:"pk autoincr"` |
| 22 | + |
| 23 | + // OwnerID is the scope the source applies to: a user/org ID (applies to that owner's repos), or 0 for instance-level (applies to every repo). |
| 24 | + OwnerID int64 `xorm:"UNIQUE(owner_repo) NOT NULL DEFAULT 0"` |
| 25 | + // SourceRepoID is the source repository providing the workflow files; always non-zero. |
| 26 | + SourceRepoID int64 `xorm:"INDEX UNIQUE(owner_repo) NOT NULL DEFAULT 0"` |
| 27 | + |
| 28 | + // RequiredWorkflowIDs are the workflow IDs marked as required. |
| 29 | + RequiredWorkflowIDs []string `xorm:"JSON TEXT 'required_workflow_ids'"` |
| 30 | + |
| 31 | + CreatedUnix timeutil.TimeStamp `xorm:"created"` |
| 32 | + UpdatedUnix timeutil.TimeStamp `xorm:"updated"` |
| 33 | +} |
| 34 | + |
| 35 | +func init() { |
| 36 | + db.RegisterModel(new(ActionScopedWorkflowSource)) |
| 37 | +} |
| 38 | + |
| 39 | +// IsWorkflowRequired reports whether the given workflow ID (entry name) is marked required in this source. |
| 40 | +func (s *ActionScopedWorkflowSource) IsWorkflowRequired(workflowID string) bool { |
| 41 | + return slices.Contains(s.RequiredWorkflowIDs, workflowID) |
| 42 | +} |
| 43 | + |
| 44 | +type FindScopedWorkflowSourceOpts struct { |
| 45 | + db.ListOptions |
| 46 | + OwnerIDs []int64 |
| 47 | + SourceRepoID int64 |
| 48 | +} |
| 49 | + |
| 50 | +func (opts FindScopedWorkflowSourceOpts) ToConds() builder.Cond { |
| 51 | + cond := builder.NewCond() |
| 52 | + if len(opts.OwnerIDs) > 0 { |
| 53 | + cond = cond.And(builder.In("owner_id", opts.OwnerIDs)) |
| 54 | + } |
| 55 | + if opts.SourceRepoID != 0 { |
| 56 | + cond = cond.And(builder.Eq{"source_repo_id": opts.SourceRepoID}) |
| 57 | + } |
| 58 | + return cond |
| 59 | +} |
| 60 | + |
| 61 | +// GetEffectiveScopedWorkflowSources returns the scoped-workflow sources effective for a repo owned by repoOwnerID: |
| 62 | +// the owner's own sources plus instance-level (owner_id=0) sources. |
| 63 | +func GetEffectiveScopedWorkflowSources(ctx context.Context, repoOwnerID int64) ([]*ActionScopedWorkflowSource, error) { |
| 64 | + owners := []int64{0} |
| 65 | + if repoOwnerID != 0 { |
| 66 | + owners = append(owners, repoOwnerID) |
| 67 | + } |
| 68 | + return db.Find[ActionScopedWorkflowSource](ctx, FindScopedWorkflowSourceOpts{OwnerIDs: owners}) |
| 69 | +} |
| 70 | + |
| 71 | +// IsScopedWorkflowSourceEffective reports whether sourceRepoID is a scoped-workflow source effective for a repo owned by repoOwnerID. |
| 72 | +func IsScopedWorkflowSourceEffective(ctx context.Context, repoOwnerID, sourceRepoID int64) (bool, error) { |
| 73 | + owners := []int64{0} |
| 74 | + if repoOwnerID != 0 { |
| 75 | + owners = append(owners, repoOwnerID) |
| 76 | + } |
| 77 | + return db.Exist[ActionScopedWorkflowSource](ctx, FindScopedWorkflowSourceOpts{OwnerIDs: owners, SourceRepoID: sourceRepoID}.ToConds()) |
| 78 | +} |
| 79 | + |
| 80 | +// IsWorkflowRequiredInSources reports whether workflowID from sourceRepoID is required by any of the given sources. |
| 81 | +func IsWorkflowRequiredInSources(sources []*ActionScopedWorkflowSource, sourceRepoID int64, workflowID string) bool { |
| 82 | + for _, s := range sources { |
| 83 | + if s.SourceRepoID == sourceRepoID && s.IsWorkflowRequired(workflowID) { |
| 84 | + return true |
| 85 | + } |
| 86 | + } |
| 87 | + return false |
| 88 | +} |
| 89 | + |
| 90 | +// ScopedStatusContextPrefix returns the source-repo prefix that makes a scoped run's commit-status context distinct from same-named workflows. |
| 91 | +func ScopedStatusContextPrefix(ctx context.Context, sourceRepoID int64) string { |
| 92 | + if sourceRepo, err := repo_model.GetRepositoryByID(ctx, sourceRepoID); err == nil { |
| 93 | + return sourceRepo.FullName() |
| 94 | + } |
| 95 | + return fmt.Sprintf("scoped:%d", sourceRepoID) |
| 96 | +} |
| 97 | + |
| 98 | +// IsScopedWorkflowRequired reports whether workflowID from sourceRepoID is required for a repo owned by consumerOwnerID. |
| 99 | +func IsScopedWorkflowRequired(ctx context.Context, consumerOwnerID, sourceRepoID int64, workflowID string) (bool, error) { |
| 100 | + sources, err := GetEffectiveScopedWorkflowSources(ctx, consumerOwnerID) |
| 101 | + if err != nil { |
| 102 | + return false, err |
| 103 | + } |
| 104 | + return IsWorkflowRequiredInSources(sources, sourceRepoID, workflowID), nil |
| 105 | +} |
| 106 | + |
| 107 | +// GetScopedWorkflowSourcesByOwner returns the sources an owner (user/org, or 0 for instance) registered. |
| 108 | +func GetScopedWorkflowSourcesByOwner(ctx context.Context, ownerID int64) ([]*ActionScopedWorkflowSource, error) { |
| 109 | + return db.Find[ActionScopedWorkflowSource](ctx, FindScopedWorkflowSourceOpts{OwnerIDs: []int64{ownerID}}) |
| 110 | +} |
| 111 | + |
| 112 | +// GetScopedWorkflowSource returns the (owner, repo) source registration or a NotExist error. |
| 113 | +func GetScopedWorkflowSource(ctx context.Context, ownerID, repoID int64) (*ActionScopedWorkflowSource, error) { |
| 114 | + src := &ActionScopedWorkflowSource{} |
| 115 | + has, err := db.GetEngine(ctx).Where("owner_id = ? AND source_repo_id = ?", ownerID, repoID).Get(src) |
| 116 | + if err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + if !has { |
| 120 | + return nil, util.NewNotExistErrorf("scoped workflow source (owner %d, repo %d) does not exist", ownerID, repoID) |
| 121 | + } |
| 122 | + return src, nil |
| 123 | +} |
| 124 | + |
| 125 | +// AddScopedWorkflowSource registers repoID as a source for ownerID (no-op if already registered). |
| 126 | +func AddScopedWorkflowSource(ctx context.Context, ownerID, repoID int64) error { |
| 127 | + exists, err := db.GetEngine(ctx).Where("owner_id = ? AND source_repo_id = ?", ownerID, repoID).Exist(new(ActionScopedWorkflowSource)) |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + if exists { |
| 132 | + return nil |
| 133 | + } |
| 134 | + return db.Insert(ctx, &ActionScopedWorkflowSource{OwnerID: ownerID, SourceRepoID: repoID}) |
| 135 | +} |
| 136 | + |
| 137 | +// SetScopedWorkflowSourceRequired replaces the required-workflow set (the workflows' entry names / run WorkflowIDs). |
| 138 | +func SetScopedWorkflowSourceRequired(ctx context.Context, ownerID, repoID int64, workflowIDs []string) error { |
| 139 | + _, err := db.GetEngine(ctx).Where("owner_id = ? AND source_repo_id = ?", ownerID, repoID). |
| 140 | + Cols("required_workflow_ids"). |
| 141 | + Update(&ActionScopedWorkflowSource{RequiredWorkflowIDs: workflowIDs}) |
| 142 | + return err |
| 143 | +} |
| 144 | + |
| 145 | +// RemoveScopedWorkflowSource removes the (owner, repo) source registration. |
| 146 | +func RemoveScopedWorkflowSource(ctx context.Context, ownerID, repoID int64) error { |
| 147 | + _, err := db.GetEngine(ctx).Where("owner_id = ? AND source_repo_id = ?", ownerID, repoID).Delete(new(ActionScopedWorkflowSource)) |
| 148 | + return err |
| 149 | +} |
0 commit comments