aboutsummaryrefslogtreecommitdiffstats
path: root/models/repo
diff options
context:
space:
mode:
authora1012112796 <1012112796@qq.com>2023-08-14 23:14:30 +0800
committerGitHub <noreply@github.com>2023-08-14 15:14:30 +0000
commit19872063a3c14256a1d89b2a104d63e7538a3a28 (patch)
treedcdfb5ffa923fb0f66d5e0b4b94f890be3e9fdf5 /models/repo
parent253737eb364192e31c8be1fc9bd9581331ca37a9 (diff)
downloadgitea-19872063a3c14256a1d89b2a104d63e7538a3a28.tar.gz
gitea-19872063a3c14256a1d89b2a104d63e7538a3a28.zip
add disable workflow feature (#26413)
As title, that's simmilar with github. ![image](https://github.com/go-gitea/gitea/assets/25342410/9e8b2444-63e0-4e87-80da-730c1e4d09d6) ![image](https://github.com/go-gitea/gitea/assets/25342410/6c3a3345-3ba7-48c9-9acd-3e621632491b) --------- Signed-off-by: a1012112796 <1012112796@qq.com> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Jason Song <i@wolfogre.com>
Diffstat (limited to 'models/repo')
-rw-r--r--models/repo/repo.go6
-rw-r--r--models/repo/repo_unit.go46
-rw-r--r--models/repo/repo_unit_test.go30
3 files changed, 81 insertions, 1 deletions
diff --git a/models/repo/repo.go b/models/repo/repo.go
index 3d1f2dcfa8..b37948fea7 100644
--- a/models/repo/repo.go
+++ b/models/repo/repo.go
@@ -391,7 +391,13 @@ func (repo *Repository) MustGetUnit(ctx context.Context, tp unit.Type) *RepoUnit
Type: tp,
Config: new(IssuesConfig),
}
+ } else if tp == unit.TypeActions {
+ return &RepoUnit{
+ Type: tp,
+ Config: new(ActionsConfig),
+ }
}
+
return &RepoUnit{
Type: tp,
Config: new(UnitConfig),
diff --git a/models/repo/repo_unit.go b/models/repo/repo_unit.go
index 7c1af95bf0..cf9ff93d32 100644
--- a/models/repo/repo_unit.go
+++ b/models/repo/repo_unit.go
@@ -6,6 +6,7 @@ package repo
import (
"context"
"fmt"
+ "strings"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/unit"
@@ -162,6 +163,42 @@ func (cfg *PullRequestsConfig) GetDefaultMergeStyle() MergeStyle {
return MergeStyleMerge
}
+type ActionsConfig struct {
+ DisabledWorkflows []string
+}
+
+func (cfg *ActionsConfig) EnableWorkflow(file string) {
+ cfg.DisabledWorkflows = util.SliceRemoveAll(cfg.DisabledWorkflows, file)
+}
+
+func (cfg *ActionsConfig) ToString() string {
+ return strings.Join(cfg.DisabledWorkflows, ",")
+}
+
+func (cfg *ActionsConfig) IsWorkflowDisabled(file string) bool {
+ return util.SliceContains(cfg.DisabledWorkflows, file)
+}
+
+func (cfg *ActionsConfig) DisableWorkflow(file string) {
+ for _, workflow := range cfg.DisabledWorkflows {
+ if file == workflow {
+ return
+ }
+ }
+
+ cfg.DisabledWorkflows = append(cfg.DisabledWorkflows, file)
+}
+
+// FromDB fills up a ActionsConfig from serialized format.
+func (cfg *ActionsConfig) FromDB(bs []byte) error {
+ return json.UnmarshalHandleDoubleEncode(bs, &cfg)
+}
+
+// ToDB exports a ActionsConfig to a serialized format.
+func (cfg *ActionsConfig) ToDB() ([]byte, error) {
+ return json.Marshal(cfg)
+}
+
// BeforeSet is invoked from XORM before setting the value of a field of this object.
func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
switch colName {
@@ -175,7 +212,9 @@ func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
r.Config = new(PullRequestsConfig)
case unit.TypeIssues:
r.Config = new(IssuesConfig)
- case unit.TypeCode, unit.TypeReleases, unit.TypeWiki, unit.TypeProjects, unit.TypePackages, unit.TypeActions:
+ case unit.TypeActions:
+ r.Config = new(ActionsConfig)
+ case unit.TypeCode, unit.TypeReleases, unit.TypeWiki, unit.TypeProjects, unit.TypePackages:
fallthrough
default:
r.Config = new(UnitConfig)
@@ -218,6 +257,11 @@ func (r *RepoUnit) ExternalTrackerConfig() *ExternalTrackerConfig {
return r.Config.(*ExternalTrackerConfig)
}
+// ActionsConfig returns config for unit.ActionsConfig
+func (r *RepoUnit) ActionsConfig() *ActionsConfig {
+ return r.Config.(*ActionsConfig)
+}
+
func getUnitsByRepoID(ctx context.Context, repoID int64) (units []*RepoUnit, err error) {
var tmpUnits []*RepoUnit
if err := db.GetEngine(ctx).Where("repo_id = ?", repoID).Find(&tmpUnits); err != nil {
diff --git a/models/repo/repo_unit_test.go b/models/repo/repo_unit_test.go
new file mode 100644
index 0000000000..a760594013
--- /dev/null
+++ b/models/repo/repo_unit_test.go
@@ -0,0 +1,30 @@
+// Copyright 2023 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package repo
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestActionsConfig(t *testing.T) {
+ cfg := &ActionsConfig{}
+ cfg.DisableWorkflow("test1.yaml")
+ assert.EqualValues(t, []string{"test1.yaml"}, cfg.DisabledWorkflows)
+
+ cfg.DisableWorkflow("test1.yaml")
+ assert.EqualValues(t, []string{"test1.yaml"}, cfg.DisabledWorkflows)
+
+ cfg.EnableWorkflow("test1.yaml")
+ assert.EqualValues(t, []string{}, cfg.DisabledWorkflows)
+
+ cfg.EnableWorkflow("test1.yaml")
+ assert.EqualValues(t, []string{}, cfg.DisabledWorkflows)
+
+ cfg.DisableWorkflow("test1.yaml")
+ cfg.DisableWorkflow("test2.yaml")
+ cfg.DisableWorkflow("test3.yaml")
+ assert.EqualValues(t, "test1.yaml,test2.yaml,test3.yaml", cfg.ToString())
+}