-
Notifications
You must be signed in to change notification settings - Fork 6
feat: validate grafanaDep with semver #515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8584ff7
feat: validate grafanaDep with semver
s4kh 3d58842
test: add parent key
s4kh ebfb600
test: error msg change
s4kh efc922e
ref: log the error
s4kh 309ccac
ref: extract to separate analyzer, use semver
s4kh cfb934f
test: empty or missing grafana dep
s4kh 0ce9e9a
chore: retrigger ci
s4kh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
pkg/analysis/passes/grafanadependency/grafanadependency.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package grafanadependency | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "github.com/Masterminds/semver/v3" | ||
| "github.com/grafana/plugin-validator/pkg/analysis" | ||
| "github.com/grafana/plugin-validator/pkg/analysis/passes/metadata" | ||
| ) | ||
|
|
||
| var ( | ||
| invalidGrafanaDependency = &analysis.Rule{Name: "invalid-grafana-dependency", Severity: analysis.Error} | ||
| validGrafanaDependency = &analysis.Rule{Name: "valid-grafana-dependency", Severity: analysis.OK} | ||
| ) | ||
|
|
||
| var Analyzer = &analysis.Analyzer{ | ||
| Name: "grafanadependency", | ||
| Requires: []*analysis.Analyzer{metadata.Analyzer}, | ||
| Run: run, | ||
| Rules: []*analysis.Rule{invalidGrafanaDependency, validGrafanaDependency}, | ||
| ReadmeInfo: analysis.ReadmeInfo{ | ||
| Name: "Metadata Grafana Dependency", | ||
| Description: "Checks that dependencies.grafanaDependency in `plugin.json` is valid.", | ||
| }, | ||
| } | ||
|
|
||
| func run(pass *analysis.Pass) (interface{}, error) { | ||
| metadataBytes, ok := pass.ResultOf[metadata.Analyzer].([]byte) | ||
| if !ok { | ||
| return nil, nil | ||
| } | ||
|
|
||
| var data metadata.Metadata | ||
| if err := json.Unmarshal(metadataBytes, &data); err != nil { | ||
| // if we fail to unmarshall it means the schema is incorrect | ||
| // we will let the metadatavalid validator handle it | ||
| return nil, nil | ||
| } | ||
|
|
||
| _, err := semver.NewConstraint(data.Dependencies.GrafanaDependency) | ||
| if err != nil { | ||
| pass.ReportResult( | ||
| pass.AnalyzerName, | ||
| invalidGrafanaDependency, | ||
| fmt.Sprintf("plugin.json: dependencies.grafanaDependency field has invalid or empty version constraint: %q", data.Dependencies.GrafanaDependency), | ||
| "The plugin.json file has an invalid or empty grafanaDependency field. Please refer to the documentation for more information. https://grafana.com/docs/grafana/latest/developers/plugins/metadata/#grafanadependency", | ||
| ) | ||
| return nil, nil | ||
| } | ||
|
|
||
| if validGrafanaDependency.ReportAll { | ||
| pass.ReportResult(pass.AnalyzerName, validGrafanaDependency, "plugin.json: dependencies.grafanaDependency field is valid", "") | ||
| } | ||
|
|
||
| return nil, nil | ||
| } |
84 changes: 84 additions & 0 deletions
84
pkg/analysis/passes/grafanadependency/grafanadependency_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package grafanadependency | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/grafana/plugin-validator/pkg/analysis" | ||
| "github.com/grafana/plugin-validator/pkg/analysis/passes/metadata" | ||
| "github.com/grafana/plugin-validator/pkg/testpassinterceptor" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestGrafanaDependency(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| name string | ||
| pluginJSON string | ||
| titleMsg string | ||
| }{ | ||
| { | ||
| name: "valid grafanaDependency constraint", | ||
| pluginJSON: `{ | ||
| "id": "test-org-app", | ||
| "dependencies": { "grafanaDependency": ">=11.6.0" } | ||
| }`, | ||
| titleMsg: "", | ||
| }, | ||
| { | ||
| name: "complex but valid grafanaDependency constraint", | ||
| pluginJSON: `{ | ||
| "id": "test-org-app", | ||
| "dependencies": { "grafanaDependency": ">=11.6.11 <12 || >=12.0.10 <12.1 || >=12.1.7 <12.2 || >=12.2.5" } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using |
||
| }`, | ||
| titleMsg: "", | ||
| }, | ||
| { | ||
| name: "invalid grafanaDependency constraint", | ||
| pluginJSON: `{ | ||
| "id": "test-org-app", | ||
| "dependencies": { "grafanaDependency": ">=invalid2" } | ||
| }`, | ||
| titleMsg: "plugin.json: dependencies.grafanaDependency field has invalid or empty version constraint: \">=invalid2\"", | ||
| }, | ||
| { | ||
| name: "empty grafanaDependency", | ||
| pluginJSON: `{ | ||
| "id": "test-org-app", | ||
| "dependencies": { "grafanaDependency": "" } | ||
| }`, | ||
| titleMsg: "plugin.json: dependencies.grafanaDependency field has invalid or empty version constraint: \"\"", | ||
| }, | ||
| { | ||
| name: "missing grafanaDependency", | ||
| pluginJSON: `{ | ||
| "id": "test-org-app", | ||
| "dependencies": {"plugins": [ ]} | ||
| }`, | ||
| titleMsg: "plugin.json: dependencies.grafanaDependency field has invalid or empty version constraint: \"\"", | ||
| }, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| var interceptor testpassinterceptor.TestPassInterceptor | ||
| pass := &analysis.Pass{ | ||
| RootDir: filepath.Join("./"), | ||
| ResultOf: map[*analysis.Analyzer]interface{}{ | ||
| metadata.Analyzer: []byte(tc.pluginJSON), | ||
| }, | ||
| Report: interceptor.ReportInterceptor(), | ||
| } | ||
|
|
||
| _, err := Analyzer.Run(pass) | ||
| require.NoError(t, err) | ||
| if len(tc.titleMsg) > 0 { | ||
| require.Len(t, interceptor.Diagnostics, 1) | ||
| require.Equal( | ||
| t, | ||
| tc.titleMsg, | ||
| interceptor.Diagnostics[0].Title, | ||
| ) | ||
| } else { | ||
| require.Len(t, interceptor.Diagnostics, 0) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add test cases for:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. Added the tests.
Unmarshalsucceeds andNewConstraintreturns error -improper constraintthus same title message is returned