Skip to content

Commit

Permalink
fix: detect orb used in job parameters (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulesFaucherre authored Nov 6, 2023
1 parent b6136db commit 24efc32
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/parser/validate/orb.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ func (val Validate) checkIfOrbIsUsed(orb ast.Orb) bool {
if val.checkIfStepsContainOrb(steps, orb.Name) {
return true
}

if val.checkIfJobParamContainOrb(jobRef.Parameters, orb.Name) {
return true
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/parser/validate/orb_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package validate

import (
"os"
"testing"

"github.com/CircleCI-Public/circleci-yaml-language-server/pkg/utils"
"github.com/stretchr/testify/assert"
"go.lsp.dev/protocol"
)

Expand Down Expand Up @@ -257,3 +259,15 @@ workflows:

CheckYamlErrors(t, testCases)
}

func TestOrbStepsUsedInParameters(t *testing.T) {
content, err := os.ReadFile("testdata/orb_steps_used_in_params.yml")
assert.NoError(t, err)
val := CreateValidateFromYAML(string(content))
val.Validate(false)
for _, diag := range *val.Diagnostics {
if diag.Message == "Orb is unused" {
t.Errorf("Got orb is unused diagnostic")
}
}
}
32 changes: 32 additions & 0 deletions pkg/parser/validate/steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,38 @@ func (val Validate) checkIfStepsContainOrb(steps []ast.Step, orbName string) boo
return false
}

func (val Validate) checkIfJobParamContainOrb(params map[string]ast.ParameterValue, orbName string) bool {
for _, p := range params {
array, ok := p.Value.([]ast.ParameterValue)
if !ok {
continue
}

for _, value := range array {
if value.Type != "steps" {
break
}

steps, ok := value.Value.([]ast.Step)
if !ok {
continue
}

for _, step := range steps {
name := step.GetName()
split := strings.Split(name, "/")
if len(split) != 2 {
continue
}
if split[0] == orbName {
return true
}
}
}
}
return false
}

func (val Validate) checkIfJobUseOrb(job ast.Job, orbName string) bool {
if val.checkIfStepsContainOrb(job.Steps, orbName) {
return true
Expand Down
27 changes: 27 additions & 0 deletions pkg/parser/validate/testdata/orb_steps_used_in_params.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: 2.1

orbs:
slack: circleci/[email protected]

jobs:
run-steps:
parameters:
custom-steps:
type: steps
default: []
docker:
- image: cimg/node:18.18.1
steps:
- steps: << parameters.custom-steps >>

workflows:
workflow:
jobs:
- run-steps:
custom-steps:
- slack/notify:
custom: ""
event: always
- slack/notify:
custom: "toto"
event: always

0 comments on commit 24efc32

Please sign in to comment.