feat: parse workflows

This commit is contained in:
Jason Song
2022-10-08 18:20:17 +08:00
parent 91bea969d4
commit f355188e12
7 changed files with 173 additions and 55 deletions

View File

@ -5,57 +5,75 @@
package bots
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 DetectWorkflows(commit *git.Commit, event webhook.HookEventType) (git.Entries, []map[string]*model.Job, error) {
tree, err := commit.SubTree(".github/workflows")
func ListWorkflows(commit *git.Commit) (git.Entries, error) {
tree, err := commit.SubTree(".gitea/workflows")
if _, ok := err.(git.ErrNotExist); ok {
tree, err = commit.SubTree(".gitea/workflows")
tree, err = commit.SubTree(".github/workflows")
}
if _, ok := err.(git.ErrNotExist); ok {
return nil, nil, nil
return nil, nil
}
if err != nil {
return nil, nil, err
return nil, err
}
entries, err := tree.ListEntriesRecursiveFast()
if err != nil {
return nil, nil, err
return nil, err
}
matchedEntries := make(git.Entries, 0, len(entries))
jobs := make([]map[string]*model.Job, 0, len(entries))
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, nil, err
return nil, err
}
workflow, err := model.ReadWorkflow(f)
content, err := io.ReadAll(f)
_ = f.Close()
if err != nil {
f.Close()
return nil, nil, err
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() {
matchedEntries = append(matchedEntries, entry)
jobs = append(jobs, workflow.Jobs)
workflows[entry.Name()] = content
break
}
}
f.Close()
}
return matchedEntries, jobs, nil
return workflows, nil
}

View File

@ -26,7 +26,8 @@ import (
"code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
bots_service "code.gitea.io/gitea/services/bots"
"github.com/nektos/act/pkg/jobparser"
)
type botsNotifier struct {
@ -72,41 +73,37 @@ func notify(repo *repo_model.Repository, doer *user_model.User, payload, ref str
return
}
matchedEntries, jobs, err := bots_module.DetectWorkflows(commit, evt)
workflows, err := bots_module.DetectWorkflows(commit, evt)
if err != nil {
log.Error("detectWorkflows: %v", err)
log.Error("DetectWorkflows: %v", err)
return
}
log.Trace("detected %s has %d entries", commit.ID, len(matchedEntries))
if len(matchedEntries) == 0 {
if len(workflows) == 0 {
log.Trace("repo %s with commit %s couldn't find workflows", repo.RepoPath(), commit.ID)
return
}
workflowsStatuses := make(map[string]map[string]core.BuildStatus)
for i, entry := range matchedEntries {
taskStatuses := make(map[string]core.BuildStatus)
for k := range jobs[i] {
taskStatuses[k] = core.StatusPending
for id, content := range workflows {
run := bots_model.Run{
Name: commit.Message(),
RepoID: repo.ID,
WorkflowID: id,
TriggerUserID: doer.ID,
Ref: ref,
CommitSHA: commit.ID.String(),
Event: evt,
EventPayload: payload,
Status: core.StatusPending,
}
jobs, err := jobparser.Parse(content) // TODO: parse with options
if err != nil {
log.Error("jobparser.Parse: %v", err)
continue
}
if err := bots_model.InsertRun(&run, jobs); err != nil {
log.Error("InsertBotTask: %v", err)
}
workflowsStatuses[entry.Name()] = taskStatuses
}
build := bots_model.Build{
Name: commit.Message(),
RepoID: repo.ID,
TriggerUserID: doer.ID,
Event: evt,
EventPayload: payload,
Status: core.StatusPending,
Ref: ref,
CommitSHA: commit.ID.String(),
}
if err := bots_model.InsertBuild(&build, workflowsStatuses); err != nil {
log.Error("InsertBotTask: %v", err)
} else {
bots_service.PushToQueue(&build)
}
}