Skip to content
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

Support reusable workflows in the same repository #107

Merged
merged 1 commit into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions rule_workflow_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,36 @@ func (rule *RuleWorkflowCall) VisitJobPre(n *Job) error {
rule.errorf(u.Pos, "reusable workflow cannot be nested. but this workflow hooks \"workflow_call\" event at %s", rule.workflowCallEventPos)
}

if !strings.Contains(u.Value, "${{") && !checkWorkflowCallUsesFormat(u.Value) {
if !strings.Contains(u.Value, "${{") && !(checkWorkflowCallUsesLocalFormat(u.Value) || checkWorkflowCallUsesRepoFormat(u.Value)) {
rule.errorf(u.Pos, "reusable workflow call %q at \"uses\" is not following the format \"owner/repo/path/to/workflow.yml@ref\". see https://docs.github.com/en/actions/learn-github-actions/reusing-workflows for more details", u.Value)
}

return nil
}

// Parse ./{path/{filename}
// https://docs.github.com/en/actions/learn-github-actions/reusing-workflows#calling-a-reusable-workflow
func checkWorkflowCallUsesLocalFormat(u string) bool {
if !strings.HasPrefix(u, "./") {
return false
}
u = strings.TrimPrefix(u, "./")

// Cannot container a ref
idx := strings.IndexRune(u, '@')
if idx > 0 {
return false
}

return len(u) > 0
}

// Parse {owner}/{repo}/{path to workflow.yml}@{ref}
// https://docs.github.com/en/actions/learn-github-actions/reusing-workflows#calling-a-reusable-workflow
func checkWorkflowCallUsesFormat(u string) bool {
func checkWorkflowCallUsesRepoFormat(u string) bool {
// Repo reference must start with owner
if strings.HasPrefix(u, ".") {
return false // Local path is not supported.
return false
}

idx := strings.IndexRune(u, '/')
Expand Down
2 changes: 2 additions & 0 deletions rule_workflow_call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ func TestRuleWorkflowCallCheckWorkflowCallUsesFormat(t *testing.T) {
{"owner/repo/x.yml@ref", true},
{"owner/repo/x.yml@@", true},
{"owner/repo/x.yml@release/v1", true},
{"./path/to/x.yml", true},
{"${{ env.FOO }}", true},
{"./path/to/x.yml@ref", false},
{"/path/to/x.yml@ref", false},
{"./", false},
{"owner/x.yml@ref", false},
{"owner/repo@ref", false},
{"owner/repo/x.yml", false},
Expand Down