Skip to content

Commit

Permalink
feat(uneditable): Adds linting for editable dashboards
Browse files Browse the repository at this point in the history
Today this linter tests a number of settings, but it does not validate
that the dashboard cannot be edited after it is deployed in the UI.
If this linter is to ensure the source of truth for linted dashboards
are a json code object, we should ensure they are not editable by default
after deployment.

Signed-off-by: Jason Field <[email protected]>
  • Loading branch information
xorima committed Oct 17, 2023
1 parent 6c322b3 commit df1b122
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
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
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 {

Check failure on line 9 in lint/rule_uneditable.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, ubuntu-latest)

d.Editable undefined (type Dashboard has no field or method Editable)

Check failure on line 9 in lint/rule_uneditable.go

View workflow job for this annotation

GitHub Actions / build (1.18.x, ubuntu-latest)

d.Editable undefined (type Dashboard has no field or method Editable)
r.AddFixableError(d, "is editable, it should be set to 'editable: false'", FixUneditableRule)
}
return r
},
}
}

func FixUneditableRule(d *Dashboard) {
d.Editable = false

Check failure on line 18 in lint/rule_uneditable.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, ubuntu-latest)

d.Editable undefined (type *Dashboard has no field or method Editable)

Check failure on line 18 in lint/rule_uneditable.go

View workflow job for this annotation

GitHub Actions / build (1.18.x, ubuntu-latest)

d.Editable undefined (type *Dashboard has no field or method Editable)
}
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()

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/testdata/dashboard.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"editable": true,
"rows": [
{
"panels": [
Expand Down

0 comments on commit df1b122

Please sign in to comment.