mirror of
https://github.com/go-gitea/gitea.git
synced 2025-08-20 15:44:18 +09:00
chore: rename to workflows.go
This commit is contained in:
78
modules/actions/workflows.go
Normal file
78
modules/actions/workflows.go
Normal file
@ -0,0 +1,78 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package actions
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/models/webhook"
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
|
||||
"github.com/nektos/act/pkg/model"
|
||||
)
|
||||
|
||||
func ListWorkflows(commit *git.Commit) (git.Entries, error) {
|
||||
tree, err := commit.SubTree(".gitea/workflows")
|
||||
if _, ok := err.(git.ErrNotExist); ok {
|
||||
tree, err = commit.SubTree(".github/workflows")
|
||||
}
|
||||
if _, ok := err.(git.ErrNotExist); ok {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
entries, err := tree.ListEntriesRecursiveFast()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
idx := 0
|
||||
for _, entry := range entries {
|
||||
if !strings.HasSuffix(entry.Name(), ".yml") && !strings.HasSuffix(entry.Name(), ".yaml") {
|
||||
continue
|
||||
}
|
||||
entries[idx] = entry
|
||||
idx++
|
||||
}
|
||||
|
||||
return entries[:idx], nil
|
||||
}
|
||||
|
||||
func DetectWorkflows(commit *git.Commit, event webhook.HookEventType) (map[string][]byte, error) {
|
||||
entries, err := ListWorkflows(commit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
workflows := make(map[string][]byte, len(entries))
|
||||
for _, entry := range entries {
|
||||
f, err := entry.Blob().DataAsync()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
content, err := io.ReadAll(f)
|
||||
_ = f.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
workflow, err := model.ReadWorkflow(bytes.NewReader(content))
|
||||
if err != nil {
|
||||
log.Warn("ignore invalid workflow %q: %v", entry.Name(), err)
|
||||
continue
|
||||
}
|
||||
for _, e := range workflow.On() {
|
||||
if e == event.Event() {
|
||||
workflows[entry.Name()] = content
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return workflows, nil
|
||||
}
|
Reference in New Issue
Block a user