diff --git a/modules/actions/workflows.go b/modules/actions/workflows.go index 4ac06def4d5a1..83bbfa3a1b87d 100644 --- a/modules/actions/workflows.go +++ b/modules/actions/workflows.go @@ -107,6 +107,13 @@ func GetEventsFromContent(content []byte) ([]*jobparser.Event, error) { return events, nil } +// ValidateWorkflowContent catches structural errors (e.g. blank lines in run: | blocks) +// that model.ReadWorkflow alone does not detect. +func ValidateWorkflowContent(content []byte) error { + _, err := jobparser.Parse(content) + return err +} + func DetectWorkflows( gitRepo *git.Repository, commit *git.Commit, @@ -129,6 +136,9 @@ func DetectWorkflows( // one workflow may have multiple events events, err := GetEventsFromContent(content) + if err == nil { + err = ValidateWorkflowContent(content) + } if err != nil { log.Warn("ignore invalid workflow %q: %v", entry.Name(), err) continue @@ -173,6 +183,9 @@ func DetectScheduledWorkflows(gitRepo *git.Repository, commit *git.Commit) ([]*D // one workflow may have multiple events events, err := GetEventsFromContent(content) + if err == nil { + err = ValidateWorkflowContent(content) + } if err != nil { log.Warn("ignore invalid workflow %q: %v", entry.Name(), err) continue diff --git a/routers/web/repo/actions/actions.go b/routers/web/repo/actions/actions.go index 988d2d0a993b9..7d373c79ed6b7 100644 --- a/routers/web/repo/actions/actions.go +++ b/routers/web/repo/actions/actions.go @@ -151,6 +151,11 @@ func prepareWorkflowTemplate(ctx *context.Context, commit *git.Commit) (workflow workflows = append(workflows, workflow) continue } + if err := actions.ValidateWorkflowContent(content); err != nil { + workflow.ErrMsg = ctx.Locale.TrString("actions.runs.invalid_workflow_helper", err.Error()) + workflows = append(workflows, workflow) + continue + } workflow.Workflow = wf // The workflow must contain at least one job without "needs". Otherwise, a deadlock will occur and no jobs will be able to run. hasJobWithoutNeeds := false diff --git a/routers/web/repo/view_file.go b/routers/web/repo/view_file.go index 3ae0dab25b8a3..65fcb8adba233 100644 --- a/routers/web/repo/view_file.go +++ b/routers/web/repo/view_file.go @@ -25,8 +25,6 @@ import ( "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/services/context" issue_service "code.gitea.io/gitea/services/issue" - - "github.com/nektos/act/pkg/model" ) func prepareLatestCommitInfo(ctx *context.Context) bool { @@ -184,8 +182,7 @@ func prepareFileView(ctx *context.Context, entry *git.TreeEntry) { if err != nil { log.Error("actions.GetContentFromEntry: %v", err) } - _, workFlowErr := model.ReadWorkflow(bytes.NewReader(content)) - if workFlowErr != nil { + if workFlowErr := actions.ValidateWorkflowContent(content); workFlowErr != nil { ctx.Data["FileError"] = ctx.Locale.Tr("actions.runs.invalid_workflow_helper", workFlowErr.Error()) } } else if issue_service.IsCodeOwnerFile(ctx.Repo.TreePath) {