From c352b05d6c63f6781a4e8531937ab2f15a1b6986 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Fri, 9 Aug 2024 00:31:06 -0400 Subject: [PATCH] feat: add job name and job name default support --- WORKFLOW_SUPPORT.md | 2 +- internal/workflow/create.go | 10 ++++++++++ internal/workflow/create_test.go | 16 ++++++++++++++++ internal/workflow/models.go | 2 ++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/WORKFLOW_SUPPORT.md b/WORKFLOW_SUPPORT.md index 30a8555..a8eb50f 100644 --- a/WORKFLOW_SUPPORT.md +++ b/WORKFLOW_SUPPORT.md @@ -12,7 +12,7 @@ syntax](https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for - [ ] env - [ ] defaults - [x] jobs - - [ ] jobs..name + - [x] jobs..name - [ ] jobs..permissions - [x] jobs..needs (ordering, not success) - [ ] jobs..if diff --git a/internal/workflow/create.go b/internal/workflow/create.go index 04de2ee..4075411 100644 --- a/internal/workflow/create.go +++ b/internal/workflow/create.go @@ -16,6 +16,16 @@ func FromYamlBytes(raw []byte) (*Workflow, error) { return nil, yamlError } + // If not "name" prop is provided, it defaults to the + // key that is used in the jobs object. + for jobLabel, job := range workflow.Jobs { + if job.Name == "" { + job.Name = jobLabel + } + + workflow.Jobs[jobLabel] = job + } + return &workflow, nil } diff --git a/internal/workflow/create_test.go b/internal/workflow/create_test.go index 147a8ed..9e7f4de 100644 --- a/internal/workflow/create_test.go +++ b/internal/workflow/create_test.go @@ -26,6 +26,22 @@ jobs: } } +func TestFromYamlBytesDefaultsJobNameToObjectKey(t *testing.T) { + sample := ` +jobs: + jobA: + runs-on: default + steps: + - run: echo "test" + ` + + workflow, _ := FromYamlBytes([]byte(sample)) + + if workflow.Jobs["jobA"].Name != "jobA" { + t.Errorf("Expected job name to be \"jobA\", got %s instead.", workflow.Jobs["jobA"].Name) + } +} + func TestFromYamlBytesReturnsErrorIfErrorParsing(t *testing.T) { sample := `not-yaml!` diff --git a/internal/workflow/models.go b/internal/workflow/models.go index 481dd13..04d0db4 100644 --- a/internal/workflow/models.go +++ b/internal/workflow/models.go @@ -8,6 +8,8 @@ type Job struct { RunsOn string `yaml:"runs-on"` Steps []Step `yaml:"steps"` Needs []string `yaml:"needs"` + // Job name; this isn't guaranteed to be unique. + Name string `yaml:"name"` } func (j Job) Validate() []error {