From f4c62a5c5dff0f883d203bb87a40fa2f56aa9cb2 Mon Sep 17 00:00:00 2001 From: Ashley Harrison Date: Fri, 17 Mar 2023 11:08:36 +0000 Subject: [PATCH] Navigation: handle case when there is no alerting node at all (#64941) * handle case when there is no alerting node at all * update backend tests --- pkg/services/navtree/navtreeimpl/applinks.go | 23 +++++++++------- .../navtree/navtreeimpl/applinks_test.go | 26 ++++++++++++++----- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/pkg/services/navtree/navtreeimpl/applinks.go b/pkg/services/navtree/navtreeimpl/applinks.go index 2ab91539a28..89192e03916 100644 --- a/pkg/services/navtree/navtreeimpl/applinks.go +++ b/pkg/services/navtree/navtreeimpl/applinks.go @@ -226,19 +226,22 @@ func (s *ServiceImpl) addPluginToSection(c *contextmodel.ReqContext, treeRoot *n Url: s.cfg.AppSubURL + "/monitoring", }) case navtree.NavIDAlertsAndIncidents: + alertsAndIncidentsChildren := []*navtree.NavLink{} if alertingNode != nil { - treeRoot.AddSection(&navtree.NavLink{ - Text: "Alerts & incidents", - Id: navtree.NavIDAlertsAndIncidents, - SubTitle: "Alerting and incident management apps", - Icon: "bell", - Section: navtree.NavSectionCore, - SortWeight: navtree.WeightAlertsAndIncidents, - Children: []*navtree.NavLink{alertingNode, appLink}, - Url: s.cfg.AppSubURL + "/alerts-and-incidents", - }) + alertsAndIncidentsChildren = append(alertsAndIncidentsChildren, alertingNode) treeRoot.RemoveSection(alertingNode) } + alertsAndIncidentsChildren = append(alertsAndIncidentsChildren, appLink) + treeRoot.AddSection(&navtree.NavLink{ + Text: "Alerts & incidents", + Id: navtree.NavIDAlertsAndIncidents, + SubTitle: "Alerting and incident management apps", + Icon: "bell", + Section: navtree.NavSectionCore, + SortWeight: navtree.WeightAlertsAndIncidents, + Children: alertsAndIncidentsChildren, + Url: s.cfg.AppSubURL + "/alerts-and-incidents", + }) default: s.log.Error("Plugin app nav id not found", "pluginId", plugin.ID, "navId", sectionID) } diff --git a/pkg/services/navtree/navtreeimpl/applinks_test.go b/pkg/services/navtree/navtreeimpl/applinks_test.go index 6646b3f52a1..0a19e39cc51 100644 --- a/pkg/services/navtree/navtreeimpl/applinks_test.go +++ b/pkg/services/navtree/navtreeimpl/applinks_test.go @@ -240,19 +240,34 @@ func TestAddAppLinks(t *testing.T) { alertsAndIncidentsNode := treeRoot.FindById(navtree.NavIDAlertsAndIncidents) require.Nil(t, alertsAndIncidentsNode) - // If there is no 'Alerting' node in the navigation (= alerting not enabled) then we don't auto-create the 'Alerts and Incidents' section + // It should appear and once an app tries to register to it and the `Alerting` nav node is present treeRoot = navtree.NavTreeRoot{} + treeRoot.AddSection(&navtree.NavLink{Id: navtree.NavIDAlerting, Text: "Alerting"}) service.navigationAppConfig = map[string]NavigationAppConfig{ "test-app1": {SectionID: navtree.NavIDAlertsAndIncidents}, } err = service.addAppLinks(&treeRoot, reqCtx) require.NoError(t, err) alertsAndIncidentsNode = treeRoot.FindById(navtree.NavIDAlertsAndIncidents) + require.NotNil(t, alertsAndIncidentsNode) + require.Len(t, alertsAndIncidentsNode.Children, 2) + require.Equal(t, "Alerting", alertsAndIncidentsNode.Children[0].Text) + require.Equal(t, "Test app1 name", alertsAndIncidentsNode.Children[1].Text) + }) + + t.Run("Should add a 'Alerts and Incidents' section if a plugin exists that wants to live there even without an alerting node", func(t *testing.T) { + service.features = featuremgmt.WithFeatures(featuremgmt.FlagTopnav) + service.navigationAppConfig = map[string]NavigationAppConfig{} + + // Check if the 'Alerts and Incidents' section is not there if no apps try to register to it + treeRoot := navtree.NavTreeRoot{} + err := service.addAppLinks(&treeRoot, reqCtx) + require.NoError(t, err) + alertsAndIncidentsNode := treeRoot.FindById(navtree.NavIDAlertsAndIncidents) require.Nil(t, alertsAndIncidentsNode) - // It should appear and once an app tries to register to it and the `Alerting` nav node is present + // If there is no 'Alerting' node in the navigation then we still auto-create the 'Alerts and Incidents' section when a plugin wants to live there treeRoot = navtree.NavTreeRoot{} - treeRoot.AddSection(&navtree.NavLink{Id: navtree.NavIDAlerting, Text: "Alerting"}) service.navigationAppConfig = map[string]NavigationAppConfig{ "test-app1": {SectionID: navtree.NavIDAlertsAndIncidents}, } @@ -260,9 +275,8 @@ func TestAddAppLinks(t *testing.T) { require.NoError(t, err) alertsAndIncidentsNode = treeRoot.FindById(navtree.NavIDAlertsAndIncidents) require.NotNil(t, alertsAndIncidentsNode) - require.Len(t, alertsAndIncidentsNode.Children, 2) - require.Equal(t, "Alerting", alertsAndIncidentsNode.Children[0].Text) - require.Equal(t, "Test app1 name", alertsAndIncidentsNode.Children[1].Text) + require.Len(t, alertsAndIncidentsNode.Children, 1) + require.Equal(t, "Test app1 name", alertsAndIncidentsNode.Children[0].Text) }) t.Run("Should be able to control app sort order with SortWeight (smaller SortWeight displayed first)", func(t *testing.T) {