mirror of
https://github.com/go-gitea/gitea.git
synced 2026-04-19 02:31:59 +09:00
Add actions.WORKFLOW_DIRS setting (#36619)
Fixes: https://github.com/go-gitea/gitea/issues/36612 This new setting controls which workflow directories are searched. The default value matches the previous hardcoded behaviour. This allows users for example to exclude `.github/workflows` from being picked up by Actions in mirrored repositories by setting `WORKFLOW_DIRS = .gitea/workflows`. Signed-off-by: silverwind <me@silverwind.io> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/glob"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
webhook_module "code.gitea.io/gitea/modules/webhook"
|
||||
@@ -41,22 +42,30 @@ func IsWorkflow(path string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
return strings.HasPrefix(path, ".gitea/workflows") || strings.HasPrefix(path, ".github/workflows")
|
||||
for _, workflowDir := range setting.Actions.WorkflowDirs {
|
||||
if strings.HasPrefix(path, workflowDir+"/") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func ListWorkflows(commit *git.Commit) (string, git.Entries, error) {
|
||||
rpath := ".gitea/workflows"
|
||||
tree, err := commit.SubTree(rpath)
|
||||
if _, ok := err.(git.ErrNotExist); ok {
|
||||
rpath = ".github/workflows"
|
||||
tree, err = commit.SubTree(rpath)
|
||||
var tree *git.Tree
|
||||
var err error
|
||||
var workflowDir string
|
||||
for _, workflowDir = range setting.Actions.WorkflowDirs {
|
||||
tree, err = commit.SubTree(workflowDir)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
if !git.IsErrNotExist(err) {
|
||||
return "", nil, err
|
||||
}
|
||||
}
|
||||
if _, ok := err.(git.ErrNotExist); ok {
|
||||
if tree == nil {
|
||||
return "", nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
|
||||
entries, err := tree.ListEntriesRecursiveFast()
|
||||
if err != nil {
|
||||
@@ -69,7 +78,7 @@ func ListWorkflows(commit *git.Commit) (string, git.Entries, error) {
|
||||
ret = append(ret, entry)
|
||||
}
|
||||
}
|
||||
return rpath, ret, nil
|
||||
return workflowDir, ret, nil
|
||||
}
|
||||
|
||||
func GetContentFromEntry(entry *git.TreeEntry) ([]byte, error) {
|
||||
|
||||
@@ -7,12 +7,83 @@ import (
|
||||
"testing"
|
||||
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
webhook_module "code.gitea.io/gitea/modules/webhook"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIsWorkflow(t *testing.T) {
|
||||
oldDirs := setting.Actions.WorkflowDirs
|
||||
defer func() {
|
||||
setting.Actions.WorkflowDirs = oldDirs
|
||||
}()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
dirs []string
|
||||
path string
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "default with yml extension",
|
||||
dirs: []string{".gitea/workflows", ".github/workflows"},
|
||||
path: ".gitea/workflows/test.yml",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "default with yaml extension",
|
||||
dirs: []string{".gitea/workflows", ".github/workflows"},
|
||||
path: ".github/workflows/test.yaml",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "only gitea configured, github path rejected",
|
||||
dirs: []string{".gitea/workflows"},
|
||||
path: ".github/workflows/test.yml",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "only github configured, gitea path rejected",
|
||||
dirs: []string{".github/workflows"},
|
||||
path: ".gitea/workflows/test.yml",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "custom workflow dir",
|
||||
dirs: []string{".custom/workflows"},
|
||||
path: ".custom/workflows/deploy.yml",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "non-workflow file",
|
||||
dirs: []string{".gitea/workflows", ".github/workflows"},
|
||||
path: ".gitea/workflows/readme.md",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "directory boundary",
|
||||
dirs: []string{".gitea/workflows"},
|
||||
path: ".gitea/workflows2/test.yml",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "unrelated path",
|
||||
dirs: []string{".gitea/workflows", ".github/workflows"},
|
||||
path: "src/main.go",
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
setting.Actions.WorkflowDirs = tt.dirs
|
||||
assert.Equal(t, tt.expected, IsWorkflow(tt.path))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDetectMatched(t *testing.T) {
|
||||
testCases := []struct {
|
||||
desc string
|
||||
|
||||
Reference in New Issue
Block a user