Skip to content

Commit

Permalink
feat: support runs-on labels and group
Browse files Browse the repository at this point in the history
Signed-off-by: Raffael Sahli <[email protected]>
  • Loading branch information
raffis committed Oct 24, 2023
1 parent db71c41 commit 27bcef3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
28 changes: 26 additions & 2 deletions pkg/model/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,39 @@ func (j *Job) Needs() []string {
// RunsOn list for Job
func (j *Job) RunsOn() []string {
switch j.RawRunsOn.Kind {
case yaml.MappingNode:
var val struct {
Group string
Labels yaml.Node
}

if !decodeNode(j.RawRunsOn, &val) {
return nil
}

Check warning on line 286 in pkg/model/workflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/model/workflow.go#L285-L286

Added lines #L285 - L286 were not covered by tests

labels := nodeAsStringSlice(val.Labels)

if val.Group != "" {
labels = append(labels, val.Group)
}

return labels
default:
return nodeAsStringSlice(j.RawRunsOn)

Check warning on line 296 in pkg/model/workflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/model/workflow.go#L295-L296

Added lines #L295 - L296 were not covered by tests
}
}

func nodeAsStringSlice(node yaml.Node) []string {
switch node.Kind {
case yaml.ScalarNode:
var val string
if !decodeNode(j.RawRunsOn, &val) {
if !decodeNode(node, &val) {
return nil

Check warning on line 305 in pkg/model/workflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/model/workflow.go#L305

Added line #L305 was not covered by tests
}
return []string{val}
case yaml.SequenceNode:
var val []string
if !decodeNode(j.RawRunsOn, &val) {
if !decodeNode(node, &val) {
return nil

Check warning on line 311 in pkg/model/workflow.go

View check run for this annotation

Codecov / codecov/patch

pkg/model/workflow.go#L311

Added line #L311 was not covered by tests
}
return val
Expand Down
35 changes: 35 additions & 0 deletions pkg/model/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,41 @@ jobs:
assert.Contains(t, workflow.On(), "pull_request")
}

func TestReadWorkflow_RunsOnLabels(t *testing.T) {
yaml := `
name: local-action-docker-url
jobs:
test:
container: nginx:latest
runs-on:
labels: ubuntu-latest
steps:
- uses: ./actions/docker-url`

workflow, err := ReadWorkflow(strings.NewReader(yaml))
assert.NoError(t, err, "read workflow should succeed")
assert.Equal(t, workflow.Jobs["test"].RunsOn(), []string{"ubuntu-latest"})
}

func TestReadWorkflow_RunsOnLabelsWithGroup(t *testing.T) {
yaml := `
name: local-action-docker-url
jobs:
test:
container: nginx:latest
runs-on:
labels: [ubuntu-latest]
group: linux
steps:
- uses: ./actions/docker-url`

workflow, err := ReadWorkflow(strings.NewReader(yaml))
assert.NoError(t, err, "read workflow should succeed")
assert.Equal(t, workflow.Jobs["test"].RunsOn(), []string{"ubuntu-latest", "linux"})
}

func TestReadWorkflow_StringContainer(t *testing.T) {
yaml := `
name: local-action-docker-url
Expand Down

0 comments on commit 27bcef3

Please sign in to comment.