Skip to content

Commit

Permalink
throw if uses is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshMcCullough committed May 13, 2023
1 parent b5fa245 commit 237ea80
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
19 changes: 15 additions & 4 deletions pkg/model/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,22 @@ func (j JobType) String() string {

// Type returns the type of the job
func (j *Job) Type() JobType {
if strings.HasPrefix(j.Uses, "./.github/workflows") && (strings.HasSuffix(j.Uses, ".yml") || strings.HasSuffix(j.Uses, ".yaml")) {
return JobTypeReusableWorkflowLocal
} else if !strings.HasPrefix(j.Uses, "./") && strings.Contains(j.Uses, ".github/workflows") && (strings.Contains(j.Uses, ".yml@") || strings.Contains(j.Uses, ".yaml@")) {
return JobTypeReusableWorkflowRemote
isYaml, _ := regexp.MatchString(`\.(ya?ml)(?:$|@)`, j.Uses)

if isYaml {
isLocalPath := strings.HasPrefix(j.Uses, "./.github/workflows/")
isRemotePath := strings.Contains(j.Uses, "/.github/workflows/")
hasVersion, _ := regexp.MatchString(`\.ya?ml@`, j.Uses)

if isLocalPath {
return JobTypeReusableWorkflowLocal
} else if isRemotePath && hasVersion {
return JobTypeReusableWorkflowRemote
} else {
log.Fatalf("`uses` key references invalid workflow path \"%s\" must start with \"./.github/workflows/\" if it's a local workflow, or must start with \"<org>/<repo>/.github/workflows/\" and include an @ if it's a remote workflow.", j.Uses)
}
}

return JobTypeDefault
}

Expand Down
18 changes: 11 additions & 7 deletions pkg/model/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,24 @@ jobs:
runs-on: ubuntu-latest
steps:
- run: echo
remote-reusable-workflow:
runs-on: ubuntu-latest
remote-reusable-workflow-yml:
uses: remote/repo/.github/workflows/workflow.yml@main
local-reusable-workflow:
runs-on: ubuntu-latest
remote-reusable-workflow-yaml:
uses: remote/repo/.github/workflows/workflow.yaml@main
local-reusable-workflow-yml:
uses: ./.github/workflows/workflow.yml
local-reusable-workflow-yaml:
uses: ./.github/workflows/workflow.yaml
`

workflow, err := ReadWorkflow(strings.NewReader(yaml))
assert.NoError(t, err, "read workflow should succeed")
assert.Len(t, workflow.Jobs, 3)
assert.Len(t, workflow.Jobs, 5)
assert.Equal(t, workflow.Jobs["default-job"].Type(), JobTypeDefault)
assert.Equal(t, workflow.Jobs["remote-reusable-workflow"].Type(), JobTypeReusableWorkflowRemote)
assert.Equal(t, workflow.Jobs["local-reusable-workflow"].Type(), JobTypeReusableWorkflowLocal)
assert.Equal(t, workflow.Jobs["remote-reusable-workflow-yml"].Type(), JobTypeReusableWorkflowRemote)
assert.Equal(t, workflow.Jobs["remote-reusable-workflow-yaml"].Type(), JobTypeReusableWorkflowRemote)
assert.Equal(t, workflow.Jobs["local-reusable-workflow-yml"].Type(), JobTypeReusableWorkflowLocal)
assert.Equal(t, workflow.Jobs["local-reusable-workflow-yaml"].Type(), JobTypeReusableWorkflowLocal)
}

func TestReadWorkflow_StepsTypes(t *testing.T) {
Expand Down

0 comments on commit 237ea80

Please sign in to comment.