-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added
workflow_run.workflows
references check
- Loading branch information
Showing
5 changed files
with
245 additions
and
3 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package actionlint | ||
|
||
import ( | ||
"path/filepath" | ||
"strings" | ||
"sync" | ||
) | ||
|
||
type RuleWorkflowRunSharedState struct { | ||
refs map[string]*workflowRef | ||
mutex sync.Mutex | ||
} | ||
|
||
type RuleWorkflowRun struct { | ||
RuleBase | ||
*RuleWorkflowRunSharedState | ||
} | ||
|
||
type workflowRef struct { | ||
seen bool | ||
refAt []*workflowRefBy | ||
} | ||
|
||
type workflowRefBy struct { | ||
path string | ||
*Pos | ||
} | ||
|
||
func NewRuleWorkflowRunSharedState() *RuleWorkflowRunSharedState { | ||
return &RuleWorkflowRunSharedState{ | ||
refs: make(map[string]*workflowRef), | ||
mutex: sync.Mutex{}, | ||
} | ||
} | ||
|
||
func NewRuleWorkflowRun(state *RuleWorkflowRunSharedState) *RuleWorkflowRun { | ||
return &RuleWorkflowRun{ | ||
RuleBase: RuleBase{ | ||
name: "workflow_run", | ||
desc: "Checks referenced workflows in `workflow_run` exists", | ||
}, | ||
RuleWorkflowRunSharedState: state, | ||
} | ||
} | ||
|
||
func (state *RuleWorkflowRunSharedState) resolve(workflow string) *workflowRef { | ||
ref := state.refs[workflow] | ||
if ref == nil { | ||
ref = &workflowRef{refAt: make([]*workflowRefBy, 0)} | ||
state.refs[workflow] = ref | ||
} | ||
return ref | ||
} | ||
|
||
func (w *Workflow) computeName() string { | ||
if w.Name != nil { | ||
return w.Name.Value | ||
} | ||
return strings.TrimSuffix(filepath.Base(w.Path), filepath.Ext(w.Path)) | ||
} | ||
|
||
func (rule *RuleWorkflowRun) VisitWorkflowPre(node *Workflow) error { | ||
rule.workflowSeen(node) | ||
|
||
for _, event := range node.On { | ||
switch e := event.(type) { | ||
case *WebhookEvent: | ||
for _, ref := range e.Workflows { | ||
rule.workflowReferenced(node.Path, ref) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (rule *RuleWorkflowRun) workflowSeen(node *Workflow) { | ||
name := node.computeName() | ||
rule.Debug("workflowSeen: %q\n", name) | ||
|
||
rule.mutex.Lock() | ||
defer rule.mutex.Unlock() | ||
|
||
rule.resolve(name).seen = true | ||
} | ||
|
||
func (rule *RuleWorkflowRun) workflowReferenced(path string, refAt *String) { | ||
rule.Debug("workflowReferenced: %q\n", refAt.Value) | ||
|
||
rule.mutex.Lock() | ||
defer rule.mutex.Unlock() | ||
|
||
ref := rule.resolve(refAt.Value) | ||
ref.refAt = append(ref.refAt, &workflowRefBy{path, refAt.Pos}) | ||
} | ||
|
||
func (state *RuleWorkflowRunSharedState) ComputeMissingReferences() map[string][]*Error { | ||
state.mutex.Lock() | ||
defer state.mutex.Unlock() | ||
|
||
errs := make(map[string][]*Error) | ||
for workflow, refs := range state.refs { | ||
if !refs.seen { | ||
for _, refAt := range refs.refAt { | ||
errs[refAt.path] = append( | ||
errs[refAt.path], | ||
errorfAt(refAt.Pos, "workflow_run", "Workflow %q is not defined", workflow), | ||
) | ||
} | ||
} | ||
} | ||
return errs | ||
} |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package actionlint | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestRuleWorkflowRunCheck(t *testing.T) { | ||
type expectedErr struct { | ||
path string | ||
workflows []*Workflow | ||
} | ||
|
||
pos := &Pos{1, 1} | ||
|
||
expectErr := func(expected ...expectedErr) map[string][]*Error { | ||
errs := make(map[string][]*Error) | ||
for _, e := range expected { | ||
for _, w := range e.workflows { | ||
name := w.computeName() | ||
errs[e.path] = append(errs[e.path], errorfAt(pos, "workflow_run", "Workflow %q is not defined", name)) | ||
} | ||
} | ||
return errs | ||
} | ||
|
||
w1 := &Workflow{Path: "repo/w1.yml"} | ||
w2 := &Workflow{Path: "repo/w2.yml", Name: &String{Value: "A Workflow", Pos: pos}} | ||
w3 := &Workflow{ | ||
Path: "repo/w3.yml", | ||
Name: &String{Value: "anotherWorkflow", Pos: &Pos{1, 1}}, | ||
On: []Event{&WebhookEvent{ | ||
Hook: &String{Value: "workflow_run"}, | ||
Workflows: []*String{ | ||
{Value: "w1", Pos: pos}, | ||
{Value: "A Workflow", Pos: pos}, | ||
}}}, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
workflows []*Workflow | ||
expectErrs map[string][]*Error | ||
}{ | ||
{"only w1", []*Workflow{w1}, expectErr()}, | ||
{"only w2", []*Workflow{w2}, expectErr()}, | ||
{"w1 and w2", []*Workflow{w1, w2}, expectErr()}, | ||
{"only w3", []*Workflow{w3}, expectErr(expectedErr{"repo/w3.yml", []*Workflow{w1, w2}})}, | ||
{"w1 and w3", []*Workflow{w1, w3}, expectErr(expectedErr{"repo/w3.yml", []*Workflow{w2}})}, | ||
{"w2 and w3", []*Workflow{w2, w3}, expectErr(expectedErr{"repo/w3.yml", []*Workflow{w1}})}, | ||
{"all", []*Workflow{w1, w2, w3}, expectErr()}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
r := NewRuleWorkflowRun(NewRuleWorkflowRunSharedState()) | ||
for _, w := range tc.workflows { | ||
if err := r.VisitWorkflowPre(w); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
errs := r.ComputeMissingReferences() | ||
|
||
if len(tc.expectErrs) != len(errs) { | ||
t.Fatalf("Wanted %d errors but have %d", len(tc.expectErrs), len(errs)) | ||
} | ||
for key, expected := range tc.expectErrs { | ||
actual := errs[key] | ||
if !reflect.DeepEqual(expected, actual) { | ||
t.Fatalf("Errors does not match:\nWanted: %v\nBut have: %v", expected, actual) | ||
} | ||
} | ||
}) | ||
} | ||
} |