Skip to content

Commit

Permalink
feat!: remove all linting capabilities
Browse files Browse the repository at this point in the history
Linting using this tool has been deprecated for a long time, instead
sweater-comb should be used.

This patch will call out to sweater-comb if --lint is specified however
that usage is discouraged and will be removed in a future version.
  • Loading branch information
jgresty committed Feb 12, 2024
1 parent 1ec49fa commit 4dfc414
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 1,919 deletions.
43 changes: 6 additions & 37 deletions config/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ type API struct {
// in each version is a complete OpenAPI document describing the resource
// at that version.
type ResourceSet struct {
Description string `json:"description"`
Linter string `json:"linter"`
LinterOverrides map[string]Linters `json:"linter-overrides"`
Path string `json:"path"`
Excludes []string `json:"excludes"`
Description string `json:"description"`
Path string `json:"path"`
Excludes []string `json:"excludes"`
}

func (r *ResourceSet) validate() error {
Expand All @@ -71,9 +69,8 @@ type Overlay struct {
// Output defines where the aggregate versioned OpenAPI specs should be created
// during compilation.
type Output struct {
Path string `json:"path,omitempty"`
Paths []string `json:"paths,omitempty"`
Linter string `json:"linter"`
Path string `json:"path,omitempty"`
Paths []string `json:"paths,omitempty"`
}

// EffectivePaths returns a slice of effective configured output paths, whether
Expand All @@ -89,50 +86,22 @@ func (a APIs) init(p *Project) error {
if len(a) == 0 {
return fmt.Errorf("no apis defined")
}
// Referenced linters and generators all exist
// Referenced generators all exist
for name, api := range a {
api.Name = name
if len(api.Resources) == 0 {
return fmt.Errorf("no resources defined (apis.%s.resources)", api.Name)
}
for rcIndex, resource := range api.Resources {
if resource.Linter != "" {
if _, ok := p.Linters[resource.Linter]; !ok {
return fmt.Errorf("linter %q not found (apis.%s.resources[%d].linter)",
resource.Linter, api.Name, rcIndex)
}
}
if err := resource.validate(); err != nil {
return fmt.Errorf("%w (apis.%s.resources[%d])", err, api.Name, rcIndex)
}
for rcName, versionMap := range resource.LinterOverrides {
for version, linter := range versionMap {
err := linter.validate()
if err != nil {
return fmt.Errorf("%w (apis.%s.resources[%d].linter-overrides.%s.%s)",
err, api.Name, rcIndex, rcName, version)
}
if linter.OpticCI != nil {
return fmt.Errorf("optic linter does not support overrides (apis.%s.resources[%d].linter-overrides.%s.%s)",
api.Name, rcIndex, rcName, version)
}
}
}
}
if api.Output != nil {
if len(api.Output.Paths) > 0 && api.Output.Path != "" {
return fmt.Errorf("output should specify one of 'path' or 'paths', not both (apis.%s.output)",
api.Name)
}
if api.Output.Linter != "" {
if linter, ok := p.Linters[api.Output.Linter]; !ok {
return fmt.Errorf("linter %q not found (apis.%s.output.linter)",
api.Output.Linter, api.Name)
} else if linter.OpticCI != nil {
return fmt.Errorf("optic linter does not yet support compiled specs (apis.%s.output.linter)",
api.Name)
}
}
}
}
return nil
Expand Down
156 changes: 2 additions & 154 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,13 @@ func TestLoad(t *testing.T) {
c := qt.New(t)
conf := bytes.NewBufferString(`
version: "1"
linters:
apitest-resource:
description: Test resource rules
spectral:
rules:
- resource-rules.yaml
script: node_modules/.bin/spectral
apitest-compiled:
description: Test compiled rules
spectral:
rules:
- compiled-rules.yaml
extraArgs: ['--format', 'json', '-v']
ci-rules:
optic-ci:
original: target-branch
ciContext: ci-context.json
uploadResults: true
apis:
test:
resources:
- linter: apitest-resource
path: testdata/resources
- path: testdata/resources
excludes:
- testdata/resources/schemas/**
- linter: ci-rules
path: testdata/resources
- path: testdata/resources
excludes:
- testdata/resources/schemas/**
overlays:
Expand All @@ -49,98 +29,19 @@ apis:
description: Test API
output:
path: testdata/output
linter: apitest-compiled
`)
proj, err := config.Load(conf)
c.Assert(err, qt.IsNil)
c.Assert(proj, qt.DeepEquals, &config.Project{
Version: "1",
Generators: config.Generators{},
Linters: config.Linters{
"apitest-resource": {
Name: "apitest-resource",
Description: "Test resource rules",
Spectral: &config.SpectralLinter{
Rules: []string{
"resource-rules.yaml",
},
Script: "node_modules/.bin/spectral",
ExtraArgs: []string{"--format", "text"},
},
},
"apitest-compiled": {
Name: "apitest-compiled",
Description: "Test compiled rules",
Spectral: &config.SpectralLinter{
Rules: []string{
"compiled-rules.yaml",
},
ExtraArgs: []string{"--format", "json", "-v"},
},
},
"ci-rules": {
Name: "ci-rules",
OpticCI: &config.OpticCILinter{
Image: "snyk/sweater-comb:latest",
Original: "target-branch",
},
},
},
APIs: config.APIs{
"test": {
Name: "test",
Resources: []*config.ResourceSet{{
Linter: "apitest-resource",
Path: "testdata/resources",
Excludes: []string{"testdata/resources/schemas/**"},
}, {
Linter: "ci-rules",
Path: "testdata/resources",
Excludes: []string{"testdata/resources/schemas/**"},
}},
Overlays: []*config.Overlay{{
Inline: `
servers:
- url: ${API_BASE_URL}
description: Test API`[1:],
}},
Output: &config.Output{
Path: "testdata/output",
Linter: "apitest-compiled",
},
},
},
})
}

func TestLoadNoLinters(t *testing.T) {
c := qt.New(t)
conf := bytes.NewBufferString(`
version: "1"
apis:
test:
resources:
- path: testdata/resources
excludes:
- testdata/resources/schemas/**
overlays:
- inline: |-
servers:
- url: ${API_BASE_URL}
description: Test API
output:
path: testdata/output
`)
proj, err := config.Load(conf)
c.Assert(err, qt.IsNil)
c.Assert(proj, qt.DeepEquals, &config.Project{
Version: "1",
Generators: config.Generators{},
Linters: config.Linters{},
APIs: config.APIs{
"test": {
Name: "test",
Resources: []*config.ResourceSet{{
Path: "testdata/resources",
Excludes: []string{"testdata/resources/schemas/**"},
}},
Expand Down Expand Up @@ -183,66 +84,13 @@ apis:
testapi:
resources:
- path: resources
linter: foo`[1:],
err: `linter "foo" not found \(apis\.testapi\.resources\[0\]\.linter\)`,
}, {
conf: `
version: "1"
linters:
ci:
optic-ci: {}
apis:
testapi:
resources:
- path: resources
linter: ci
linter-overrides:
foo:
2021-09-01:
optic-ci: {}
`[1:],
err: `optic linter does not support overrides \(apis\.testapi\.resources\[0\]\.linter-overrides\.foo\.2021-09-01\)`,
}, {
conf: `
version: "1"
linters:
ci:
optic-ci: {}
apis:
testapi:
resources:
- path: resources
linter: ci
output:
path: /somewhere/else
linter: ci
`[1:],
err: `optic linter does not yet support compiled specs \(apis\.testapi\.output\.linter\)`,
}, {
conf: `
version: "1"
linters:
ci:
optic-ci: {}
apis:
testapi:
resources:
- path: resources
linter: ci
output:
path: /somewhere/else
paths:
- /another/place
- /and/another
linter: ci
`[1:],
err: `output should specify one of 'path' or 'paths', not both \(apis\.testapi\.output\)`,
}, {
conf: `
linters:
ci:
`[1:],
err: `missing linter definition \(linters\.ci\)`,
}, {
err: `no apis defined`,
}}
Expand Down
Loading

0 comments on commit 4dfc414

Please sign in to comment.