diff --git a/pkg/model/workflow.go b/pkg/model/workflow.go index e7d43e4b338..f2cb42320a9 100644 --- a/pkg/model/workflow.go +++ b/pkg/model/workflow.go @@ -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 \"//.github/workflows/\" and include an @ if it's a remote workflow.", j.Uses) + } } + return JobTypeDefault } diff --git a/pkg/model/workflow_test.go b/pkg/model/workflow_test.go index 8f8b7a9e4b3..e2b5f90effd 100644 --- a/pkg/model/workflow_test.go +++ b/pkg/model/workflow_test.go @@ -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) {