Skip to content
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

feat(uneditable): Adds linting for editable dashboards #155

Merged
merged 3 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/rules/template-uneditable-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# template-uneditable-rule
Checks that dashboard is not able to be edited in the ui. This is due to the fact dashboards are declared as code
and therefore that code should be the accurate source of truth for the dashboard
5 changes: 3 additions & 2 deletions lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,9 @@ type Dashboard struct {
Templating struct {
List []Template `json:"list"`
} `json:"templating"`
Rows []Row `json:"rows,omitempty"`
Panels []Panel `json:"panels,omitempty"`
Rows []Row `json:"rows,omitempty"`
Panels []Panel `json:"panels,omitempty"`
Editable bool `json:"editable,omitempty"`
}

// GetPanels returns the all panels whether they are nested in the (now deprecated) "rows" property or
Expand Down
19 changes: 19 additions & 0 deletions lint/rule_uneditable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package lint

func NewUneditableRule() *DashboardRuleFunc {
return &DashboardRuleFunc{
name: "uneditable-dashboard",
description: "Checks that the dashboard is not editable.",
fn: func(d Dashboard) DashboardRuleResults {
r := DashboardRuleResults{}
if d.Editable {
xorima marked this conversation as resolved.
Show resolved Hide resolved
r.AddFixableError(d, "is editable, it should be set to 'editable: false'", FixUneditableRule)
}
return r
},
}
}

func FixUneditableRule(d *Dashboard) {
d.Editable = false
}
64 changes: 64 additions & 0 deletions lint/rule_uneditable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package lint

import (
"encoding/json"
"testing"

"github.com/stretchr/testify/require"
)

func TestNewUneditableRule(t *testing.T) {
linter := NewUneditableRule()
xorima marked this conversation as resolved.
Show resolved Hide resolved

for _, tc := range []struct {
name string
result Result
dashboard Dashboard
fixed *Dashboard
}{
{
name: "OK",
result: ResultSuccess,
dashboard: Dashboard{
Title: "test",
Editable: false,
},
},
{
name: "error",
result: Result{
Severity: Error,
Message: `Dashboard 'test' is editable, it should be set to 'editable: false'`,
},
dashboard: Dashboard{
Title: "test",
Editable: true,
},
},
{
name: "autofix",
result: Result{
Severity: Fixed,
Message: `Dashboard 'test' is editable, it should be set to 'editable: false'`,
},
dashboard: Dashboard{
Title: "test",
Editable: true,
},
fixed: &Dashboard{
Title: "test",
Editable: false,
},
},
} {
t.Run(tc.name, func(t *testing.T) {
autofix := tc.fixed != nil
testRuleWithAutofix(t, linter, &tc.dashboard, []Result{tc.result}, autofix)
if autofix {
expected, _ := json.Marshal(tc.fixed)
actual, _ := json.Marshal(tc.dashboard)
require.Equal(t, string(expected), string(actual))
}
})
}
}
1 change: 1 addition & 0 deletions lint/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func NewRuleSet() RuleSet {
NewTargetJobRule(),
NewTargetInstanceRule(),
NewTargetCounterAggRule(),
NewUneditableRule(),
},
}
}
Expand Down
1 change: 1 addition & 0 deletions lint/testdata/dashboard.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"editable": true,
"__inputs": [
{
"name": "DS_PROMETHEUS",
Expand Down
Loading