-
Notifications
You must be signed in to change notification settings - Fork 165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Lint actions #366
Open
msw-kialo
wants to merge
26
commits into
rhysd:main
Choose a base branch
from
kialo:lint-actions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,505
−150
Open
feat: Lint actions #366
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
61fecfd
Duplicate linting infrastructure to lint actions, too
msw-kialo dae9310
Draft action metadata parsing
lg-kialo 8992b49
Reenable a basic visitor and rules for action linting
msw-kialo 6b3655c
Add action input parsing
lg-kialo 5328f81
Start parsing actions
msw-kialo bf2f39c
Resolve staticcheck linting errors
msw-kialo cde5d87
Parse javascript and docker actions
msw-kialo f0c006c
Mimic dummy workflow/job while visits actions
msw-kialo f4693ce
Parse outputs
msw-kialo 88bbdc2
Add support for action linting to playground
lg-kialo ba8491f
Try to ignore broken test data for dogfooding tests
msw-kialo 8fe56c1
Fix more linting errors
msw-kialo 9f2987a
CI: workaround shellcheck/actionlint limitation
msw-kialo af49077
Fix playground TS linting errors
lg-kialo bb3a340
Merge branch 'rhysd:main' into lint-actions
lg-kialo 8c3038a
Add action specific methods to visit interface
msw-kialo 6cc4130
Parse action's branding info and validate it
msw-kialo bb1e69d
Link to right docs in comment
lg-kialo ef14959
Address TODO renaming `workflowKeyVal`
lg-kialo 1275407
Update comment
lg-kialo b95f955
Reverse ActionCommon / Action
msw-kialo 3bd8075
Unify interfaces
msw-kialo 4bf6973
Cleanups and improved naming
msw-kialo ae2dc96
More fixes
msw-kialo 847623f
Restructure / fix tests
msw-kialo 4b05130
Add more tests
msw-kialo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Reverse ActionCommon / Action
Use interface for the Runs block and a normal struct for the Action itself
- Loading branch information
commit b95f9551d38f848647c7dcd90772781553a0fedb
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -185,19 +185,15 @@ func (p *parser) parseActionInputs(pos *Pos, n *yaml.Node) map[string]*ActionInp | |||||||
|
||||||||
// https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#outputs-for-docker-container-and-javascript-actions | ||||||||
// https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#outputs-for-composite-actions | ||||||||
func (p *parser) parseActionOutput(id *String, n *yaml.Node, withValue bool) *ActionOutput { | ||||||||
func (p *parser) parseActionOutput(id *String, n *yaml.Node) *ActionOutput { | ||||||||
ret := &ActionOutput{Pos: posAt(n)} | ||||||||
|
||||||||
for _, kv := range p.parseMapping(fmt.Sprintf("%q outputs", id.Value), n, false, true) { | ||||||||
switch kv.id { | ||||||||
case "description": | ||||||||
ret.Description = p.parseString(kv.val, false) | ||||||||
case "value": | ||||||||
if withValue { | ||||||||
ret.Value = p.parseString(kv.val, false) | ||||||||
} else { | ||||||||
p.errorf(kv.val, "output %q cannot have \"value\" property for composite actions", id.Value) | ||||||||
} | ||||||||
ret.Value = p.parseString(kv.val, false) | ||||||||
default: | ||||||||
p.unexpectedKey(kv.key, "input", []string{ | ||||||||
"description", | ||||||||
|
@@ -208,18 +204,14 @@ func (p *parser) parseActionOutput(id *String, n *yaml.Node, withValue bool) *Ac | |||||||
} | ||||||||
} | ||||||||
|
||||||||
if withValue && ret.Value == nil { | ||||||||
p.errorf(n, "\"value\" property is missing in specification of output %q", id.Value) | ||||||||
} | ||||||||
|
||||||||
return ret | ||||||||
} | ||||||||
|
||||||||
func (p *parser) parseActionOutputs(pos *Pos, n *yaml.Node, withValue bool) map[string]*ActionOutput { | ||||||||
func (p *parser) parseActionOutputs(pos *Pos, n *yaml.Node) map[string]*ActionOutput { | ||||||||
outputs := p.parseSectionMapping("outputs", n, false, false) | ||||||||
ret := make(map[string]*ActionOutput, len(outputs)) | ||||||||
for _, kv := range outputs { | ||||||||
ret[kv.id] = p.parseActionOutput(kv.key, kv.val, withValue) | ||||||||
ret[kv.id] = p.parseActionOutput(kv.key, kv.val) | ||||||||
} | ||||||||
|
||||||||
return ret | ||||||||
|
@@ -1458,43 +1450,55 @@ func (p *parser) parse(n *yaml.Node) *Workflow { | |||||||
return w | ||||||||
} | ||||||||
|
||||||||
func (p *parser) extractActionKind(n *yaml.Node) Action { | ||||||||
c := &ActionCommon{} | ||||||||
for _, kv := range p.parseMapping("action", n, false, true) { | ||||||||
if kv.id != "runs" { | ||||||||
continue | ||||||||
} | ||||||||
for _, kv2 := range p.parseMapping("runs", kv.val, false, true) { | ||||||||
if kv2.id == "using" { | ||||||||
using := p.parseString(kv2.val, false).Value | ||||||||
switch { | ||||||||
case strings.HasPrefix(using, "node"): | ||||||||
return &JavaScriptAction{ActionCommon: *c} | ||||||||
case using == "docker": | ||||||||
return &DockerContainerAction{ActionCommon: *c} | ||||||||
case using == "composite": | ||||||||
return &CompositeAction{ActionCommon: *c} | ||||||||
default: | ||||||||
p.error(kv2.val, "unknown action type (only javascript, docker and composite are supported)") | ||||||||
// TODO look at unsupported key for better error message | ||||||||
return &JavaScriptAction{ActionCommon: *c} | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
// func (p *parser) extractActionKind(n *yaml.Node) ActionRuns { | ||||||||
// c := &Action{} | ||||||||
// for _, kv := range p.parseMapping("action", n, false, true) { | ||||||||
// if kv.id != "runs" { | ||||||||
// continue | ||||||||
// } | ||||||||
// for _, kv2 := range p.parseMapping("runs", kv.val, false, true) { | ||||||||
// if kv2.id == "using" { | ||||||||
// using := p.parseString(kv2.val, false).Value | ||||||||
// switch { | ||||||||
// case strings.HasPrefix(using, "node"): | ||||||||
// return &JavaScriptAction{Action: *c} | ||||||||
// case using == "docker": | ||||||||
// return &DockerContainerAction{Action: *c} | ||||||||
// case using == "composite": | ||||||||
// return &CompositeAction{Action: *c} | ||||||||
// default: | ||||||||
// p.error(kv2.val, "unknown action type (only javascript, docker and composite are supported)") | ||||||||
// // TODO look at unsupported key for better error message | ||||||||
// return &JavaScriptAction{Action: *c} | ||||||||
// } | ||||||||
// } | ||||||||
// } | ||||||||
|
||||||||
} | ||||||||
a := JavaScriptAction{ActionCommon: *c} | ||||||||
return &a | ||||||||
} | ||||||||
// } | ||||||||
// a := JavaScriptAction{Action: *c} | ||||||||
// return &a | ||||||||
// } | ||||||||
|
||||||||
func (p *parser) parseActionRuns(n *yaml.Node, a Action) { | ||||||||
func (p *parser) parseActionRuns(n *yaml.Node) ActionRuns { | ||||||||
var r ActionRuns | ||||||||
for _, kv := range p.parseMapping("runs", n, false, true) { | ||||||||
switch kv.id { | ||||||||
case "using": | ||||||||
// TODO ignore | ||||||||
using := p.parseString(kv.val, false).Value | ||||||||
switch { | ||||||||
case strings.HasPrefix(using, "node"): | ||||||||
r = &JavaScriptAction{} | ||||||||
case using == "docker": | ||||||||
r = &DockerContainerAction{} | ||||||||
case using == "composite": | ||||||||
r = &CompositeAction{} | ||||||||
default: | ||||||||
p.error(kv.val, "unknown action type (only javascript, docker and composite are supported)") | ||||||||
// TODO look at unsupported key for better error message | ||||||||
} | ||||||||
case "steps": | ||||||||
var def *CompositeAction | ||||||||
if ca, ok := a.(*CompositeAction); ok { | ||||||||
if ca, ok := r.(*CompositeAction); ok { | ||||||||
def = ca | ||||||||
} else { | ||||||||
p.errorAt(kv.key.Pos, "this action defines parameters for composite actions, but is something else") | ||||||||
|
@@ -1503,7 +1507,7 @@ func (p *parser) parseActionRuns(n *yaml.Node, a Action) { | |||||||
def.Steps = p.parseSteps(kv.val, true) | ||||||||
case "main", "pre", "pre-if", "post", "post-if": | ||||||||
var def *JavaScriptAction | ||||||||
if na, ok := a.(*JavaScriptAction); ok { | ||||||||
if na, ok := r.(*JavaScriptAction); ok { | ||||||||
def = na | ||||||||
} else { | ||||||||
p.errorAt(kv.key.Pos, "this action defines parameters for composite actions, but is something else") | ||||||||
|
@@ -1523,7 +1527,7 @@ func (p *parser) parseActionRuns(n *yaml.Node, a Action) { | |||||||
} | ||||||||
case "image", "entrypoint", "args", "env", "pre-entrypoint", "post-entrypoint": | ||||||||
var def *DockerContainerAction | ||||||||
if da, ok := a.(*DockerContainerAction); ok { | ||||||||
if da, ok := r.(*DockerContainerAction); ok { | ||||||||
def = da | ||||||||
} else { | ||||||||
p.errorAt(kv.key.Pos, "this action defines parameters for composite actions, but is something else") | ||||||||
|
@@ -1562,7 +1566,12 @@ func (p *parser) parseActionRuns(n *yaml.Node, a Action) { | |||||||
} | ||||||||
} | ||||||||
|
||||||||
switch a := a.(type) { | ||||||||
if r == nil { | ||||||||
p.error(n, "\"using\" is required to define what is going on") | ||||||||
return r | ||||||||
} | ||||||||
|
||||||||
switch a := r.(type) { | ||||||||
case *JavaScriptAction: | ||||||||
if a.Main == nil { | ||||||||
p.error(n, "\"main\" is required to run action in step") | ||||||||
|
@@ -1576,6 +1585,7 @@ func (p *parser) parseActionRuns(n *yaml.Node, a Action) { | |||||||
p.error(n, "\"steps\" is required for a composite action") | ||||||||
} | ||||||||
} | ||||||||
return r | ||||||||
} | ||||||||
|
||||||||
func (p *parser) parseBranding(n *yaml.Node) *Branding { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
@@ -1604,7 +1614,8 @@ func (p *parser) parseBranding(n *yaml.Node) *Branding { | |||||||
} | ||||||||
|
||||||||
// https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions | ||||||||
func (p *parser) parseAction(n *yaml.Node) Action { | ||||||||
func (p *parser) parseAction(n *yaml.Node) *Action { | ||||||||
a := &Action{} | ||||||||
if n.Line == 0 { | ||||||||
n.Line = 1 | ||||||||
} | ||||||||
|
@@ -1614,30 +1625,26 @@ func (p *parser) parseAction(n *yaml.Node) Action { | |||||||
|
||||||||
if len(n.Content) == 0 { | ||||||||
p.error(n, "action is empty") | ||||||||
return &JavaScriptAction{ActionCommon: ActionCommon{}} // TODO add Unknown type? | ||||||||
return a | ||||||||
} | ||||||||
|
||||||||
a := p.extractActionKind(n.Content[0]) | ||||||||
c := a.Common() | ||||||||
|
||||||||
for _, kv := range p.parseMapping("action", n.Content[0], false, true) { | ||||||||
k, v := kv.key, kv.val | ||||||||
switch kv.id { | ||||||||
case "name": | ||||||||
// TODO can it be empty? Also for the ones below | ||||||||
c.Name = p.parseString(v, true) | ||||||||
a.Name = p.parseString(v, true) | ||||||||
case "author": | ||||||||
c.Author = p.parseString(v, true) | ||||||||
a.Author = p.parseString(v, true) | ||||||||
case "description": | ||||||||
c.Description = p.parseString(v, true) | ||||||||
a.Description = p.parseString(v, true) | ||||||||
case "inputs": | ||||||||
c.Inputs = p.parseActionInputs(k.Pos, v) | ||||||||
a.Inputs = p.parseActionInputs(k.Pos, v) | ||||||||
case "outputs": | ||||||||
c.Outputs = p.parseActionOutputs(k.Pos, v, a.Kind() == ActionKindComposite) | ||||||||
a.Outputs = p.parseActionOutputs(k.Pos, v) | ||||||||
case "runs": | ||||||||
p.parseActionRuns(v, a) | ||||||||
a.Runs = p.parseActionRuns(v) | ||||||||
case "branding": | ||||||||
c.Branding = p.parseBranding(v) | ||||||||
a.Branding = p.parseBranding(v) | ||||||||
default: | ||||||||
p.unexpectedKey(k, "action", []string{ | ||||||||
"name", | ||||||||
|
@@ -1651,15 +1658,25 @@ func (p *parser) parseAction(n *yaml.Node) Action { | |||||||
} | ||||||||
} | ||||||||
|
||||||||
if c.Name == nil { | ||||||||
if a.Name == nil { | ||||||||
p.error(n, "\"name\" property is missing in action metadata") | ||||||||
} | ||||||||
if c.Description == nil { | ||||||||
if a.Description == nil { | ||||||||
p.error(n, "\"description\" property is missing in action metadata") | ||||||||
} | ||||||||
// if a.Runs == nil { | ||||||||
// p.error(n, "\"runs\" section is missing in action metadata") | ||||||||
// } | ||||||||
if a.Runs == nil { | ||||||||
p.error(n, "\"runs\" section is missing in action metadata") | ||||||||
} else { | ||||||||
requireValue := a.Runs.Kind() == ActionKindComposite | ||||||||
for _, o := range a.Outputs { | ||||||||
if o.Value == nil && requireValue { | ||||||||
p.errorAt(o.Pos, "output value is required for composite actions") | ||||||||
} | ||||||||
if o.Value != nil && !requireValue { | ||||||||
p.errorAt(o.Pos, "output value is only allowed for composite actions") | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
return a | ||||||||
} | ||||||||
|
@@ -1716,7 +1733,7 @@ func Parse(b []byte) (*Workflow, []*Error) { | |||||||
// ParseAction parses given source as byte sequence into workflow syntax tree. It returns all errors | ||||||||
// detected while parsing the input. It means that detecting one error does not stop parsing. Even | ||||||||
// if one or more errors are detected, parser will try to continue parsing and finding more errors. | ||||||||
func ParseAction(b []byte) (Action, []*Error) { | ||||||||
func ParseAction(b []byte) (*Action, []*Error) { | ||||||||
var n yaml.Node | ||||||||
|
||||||||
if err := yaml.Unmarshal(b, &n); err != nil { | ||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.