Skip to content

Commit

Permalink
Merge pull request #279 from grosser/grosser/rev
Browse files Browse the repository at this point in the history
dashboard tags
  • Loading branch information
grosser committed May 19, 2023
2 parents 2f300be + ab6553e commit e5792de
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 36 deletions.
18 changes: 7 additions & 11 deletions lib/kennel/models/dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ class Dashboard < Record
include TemplateVariables

READONLY_ATTRIBUTES = superclass::READONLY_ATTRIBUTES + [
:author_handle, :author_name, :modified_at, :deleted_at, :url, :is_read_only, :notify_list, :restricted_roles,
:tags
:author_handle, :author_name, :modified_at, :deleted_at, :url, :is_read_only, :notify_list, :restricted_roles
]
TRACKING_FIELD = :description
REQUEST_DEFAULTS = {
Expand Down Expand Up @@ -72,6 +71,8 @@ class Dashboard < Record
template_variable_presets: nil
}.freeze

TAG_PREFIX = /^team:/

settings(
:title, :description, :definitions, :widgets, :layout_type, :template_variable_presets, :reflow_type,
:tags
Expand All @@ -83,10 +84,7 @@ class Dashboard < Record
widgets: -> { [] },
template_variable_presets: -> { DEFAULTS.fetch(:template_variable_presets) },
reflow_type: -> { layout_type == "ordered" ? "auto" : nil },
tags: -> do # not inherited by default to make onboarding to using dashboard tags simple
team = project.team
team.tag_dashboards ? team.tags : []
end
tags: -> { project.tags }
)

class << self
Expand Down Expand Up @@ -153,16 +151,14 @@ def widgets_pairs(*pair)
def build_json
all_widgets = render_definitions(definitions) + widgets
expand_q all_widgets
tags = tags()
tags_as_string = (tags.empty? ? "" : " (#{tags.join(" ")})")

json = super.merge(
layout_type: layout_type,
title: "#{title}#{tags_as_string}#{LOCK}",
title: "#{title}#{LOCK}",
description: description,
template_variables: render_template_variables,
template_variable_presets: template_variable_presets,
widgets: all_widgets
widgets: all_widgets,
tags: tags.grep(TAG_PREFIX)
)

json[:reflow_type] = reflow_type if reflow_type # setting nil breaks create with "ordered"
Expand Down
2 changes: 1 addition & 1 deletion lib/kennel/models/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def build
rescue StandardError
if unfiltered_validation_errors.empty?
@unfiltered_validation_errors = nil
raise PrepareError, safe_tracking_id
raise PrepareError, safe_tracking_id # FIXME: this makes errors hard to debug when running tests
end
end

Expand Down
5 changes: 2 additions & 3 deletions lib/kennel/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
module Kennel
module Models
class Team < Base
settings :mention, :tags, :renotify_interval, :kennel_id, :tag_dashboards
settings :mention, :tags, :renotify_interval, :kennel_id
defaults(
tags: -> { ["team:#{kennel_id.sub(/^teams_/, "")}"] },
renotify_interval: -> { 0 },
tag_dashboards: -> { false }
renotify_interval: -> { 0 }
)
end
end
Expand Down
34 changes: 19 additions & 15 deletions test/kennel/models/dashboard_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def dashboard(extra = {})
template_variables: [],
template_variable_presets: nil,
widgets: [],
reflow_type: "auto"
reflow_type: "auto",
tags: ["team:test_team"]
}
end
let(:dashboard_with_requests) do
Expand Down Expand Up @@ -59,15 +60,6 @@ def dashboard(extra = {})
dashboard_with_requests.as_json.must_equal expected_json_with_requests
end

it "complains when datadog would created a diff by sorting template_variable_presets" do
validation_error_from(dashboard(template_variable_presets: -> { [{ name: "B" }, { name: "A" }] }))
.must_equal "template_variable_presets must be sorted by name"
end

it "doesn't complain on sorted template_variable_presets" do
dashboard(template_variable_presets: -> { [{ name: "A" }, { name: "B" }] }).as_json
end

it "adds ID when given" do
dashboard(id: -> { "abc" }).as_json.must_equal expected_json.merge(id: "abc")
end
Expand All @@ -85,11 +77,6 @@ def dashboard(extra = {})
dashboard(layout_type: -> { "free" }).as_json.must_equal(expected_json)
end

it "adds team tags when requested" do
project.team.class.any_instance.expects(:tag_dashboards).returns(true)
dashboard.as_json[:title].must_equal "Hello (team:test_team)🔒"
end

describe "definitions" do
def prepare_error_of(expected)
matcher = Module.new
Expand Down Expand Up @@ -139,6 +126,23 @@ def prepare_error_of(expected)
end
end
end

describe "template_variable_presets" do
it "doesn't complain on sorted template_variable_presets" do
dashboard(template_variable_presets: -> { [{ name: "A" }, { name: "B" }] }).as_json
end

it "complains when datadog would created a diff by sorting template_variable_presets" do
validation_error_from(dashboard(template_variable_presets: -> { [{ name: "B" }, { name: "A" }] }))
.must_equal "template_variable_presets must be sorted by name"
end
end

describe "tags" do
it "does not add non-team tags, mirroring datadogs validation" do
dashboard(tags: -> { ["foo"] }).as_json[:tags].must_equal []
end
end
end

describe "#resolve_linked_tracking_ids" do
Expand Down
6 changes: 0 additions & 6 deletions test/kennel/models/team_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,4 @@
Teams::MyTeam.new.renotify_interval.must_equal 0
end
end

describe "#tag_dashboards" do
it "is false" do
Teams::MyTeam.new.tag_dashboards.must_equal false
end
end
end

0 comments on commit e5792de

Please sign in to comment.