Skip to content

Commit c8542d9

Browse files
committed
refactor(actions): verify workflow file with actionlint
1 parent e68d455 commit c8542d9

File tree

2 files changed

+12
-40
lines changed

2 files changed

+12
-40
lines changed

modules/actions/jobparser/model.go

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -206,43 +206,6 @@ func (evt *Event) Inputs() []WorkflowDispatchInput {
206206
return evt.inputs
207207
}
208208

209-
func parseWorkflowDispatchInputs(inputs map[string]interface{}) ([]WorkflowDispatchInput, error) {
210-
var results []WorkflowDispatchInput
211-
for name, input := range inputs {
212-
inputMap, ok := input.(map[string]interface{})
213-
if !ok {
214-
return nil, fmt.Errorf("invalid input: %v", input)
215-
}
216-
input := WorkflowDispatchInput{
217-
Name: name,
218-
}
219-
if desc, ok := inputMap["description"].(string); ok {
220-
input.Description = desc
221-
}
222-
if required, ok := inputMap["required"].(bool); ok {
223-
input.Required = required
224-
}
225-
if defaultVal, ok := inputMap["default"].(string); ok {
226-
input.Default = defaultVal
227-
}
228-
if inputType, ok := inputMap["type"].(string); ok {
229-
input.Type = inputType
230-
}
231-
if options, ok := inputMap["options"].([]string); ok {
232-
input.Options = options
233-
} else if options, ok := inputMap["options"].([]interface{}); ok {
234-
for _, option := range options {
235-
if opt, ok := option.(string); ok {
236-
input.Options = append(input.Options, opt)
237-
}
238-
}
239-
}
240-
241-
results = append(results, input)
242-
}
243-
return results, nil
244-
}
245-
246209
// Helper to convert actionlint errors
247210
func acErrToError(acErrs []*actionlint.Error) []error {
248211
errs := make([]error, len(acErrs))
@@ -339,6 +302,16 @@ func GetEventsFromContent(content []byte) ([]*Event, error) {
339302
return events, nil
340303
}
341304

305+
func ValidateWorkflow(content []byte) error {
306+
_, errs := actionlint.Parse(content)
307+
err := make([]error, len(errs))
308+
for _, e := range errs {
309+
err = append(err, fmt.Errorf("%d:%d: %s", e.Line, e.Column, e.Message))
310+
}
311+
return errors.Join(err...)
312+
313+
}
314+
342315
// parseMappingNode parse a mapping node and preserve order.
343316
func parseMappingNode[T any](node *yaml.Node) ([]string, []T, error) {
344317
if node.Kind != yaml.MappingNode {

routers/web/repo/view_file.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"code.gitea.io/gitea/models/renderhelper"
1818
user_model "code.gitea.io/gitea/models/user"
1919
"code.gitea.io/gitea/modules/actions"
20+
"code.gitea.io/gitea/modules/actions/jobparser"
2021
"code.gitea.io/gitea/modules/charset"
2122
"code.gitea.io/gitea/modules/git"
2223
"code.gitea.io/gitea/modules/highlight"
@@ -27,8 +28,6 @@ import (
2728
"code.gitea.io/gitea/services/context"
2829
issue_service "code.gitea.io/gitea/services/issue"
2930
files_service "code.gitea.io/gitea/services/repository/files"
30-
31-
"github.com/nektos/act/pkg/model"
3231
)
3332

3433
func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
@@ -75,7 +74,7 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) {
7574
if err != nil {
7675
log.Error("actions.GetContentFromEntry: %v", err)
7776
}
78-
_, workFlowErr := model.ReadWorkflow(bytes.NewReader(content))
77+
workFlowErr := jobparser.ValidateWorkflow(content)
7978
if workFlowErr != nil {
8079
ctx.Data["FileError"] = ctx.Locale.Tr("actions.runs.invalid_workflow_helper", workFlowErr.Error())
8180
}

0 commit comments

Comments
 (0)