From 14d98f02dac0c92190d490d7ff6faf2d801164be Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Mon, 3 Oct 2022 16:04:16 +0200 Subject: [PATCH] Alerting: Allow created by to be manually set when there's no creator for silences (#55952) (#56161) * Alerting: Allow created by to be manually set when there's no creator Grafana has a mode that allows unauthenticated interaction, typically the created by field of a silence is inferred from the current logged user. When this is not present, the field is left black and thus the silence creation fails. This allows us to set the created by when we is not possible to infer it from the current user. * Show created by input field only if user is not logged * Add test for new logic with createdBy input field Co-authored-by: Sonia Aguilar (cherry picked from commit 501e921b2b13d19964a2a0e900682e6707199158) Co-authored-by: gotjosh --- .../alerting/unified/Silences.test.tsx | 18 +++++++++++++++++- .../components/silences/SilencesEditor.tsx | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/public/app/features/alerting/unified/Silences.test.tsx b/public/app/features/alerting/unified/Silences.test.tsx index d3f1fe9b1a5..efe5d0b6b37 100644 --- a/public/app/features/alerting/unified/Silences.test.tsx +++ b/public/app/features/alerting/unified/Silences.test.tsx @@ -6,7 +6,7 @@ import { Router } from 'react-router-dom'; import { byLabelText, byPlaceholderText, byRole, byTestId, byText } from 'testing-library-selector'; import { dateTime } from '@grafana/data'; -import { locationService, setDataSourceSrv } from '@grafana/runtime'; +import { locationService, setDataSourceSrv, config } from '@grafana/runtime'; import { contextSrv } from 'app/core/services/context_srv'; import { AlertState, MatcherOperator } from 'app/plugins/datasource/alertmanager/types'; import { configureStore } from 'app/store/configureStore'; @@ -70,6 +70,7 @@ const ui = { matcherOperator: (operator: MatcherOperator) => byText(operator, { exact: true }), addMatcherButton: byRole('button', { name: 'Add matcher' }), submit: byText('Submit'), + createdBy: byText(/created by \*/i), }, }; @@ -112,6 +113,11 @@ const resetMocks = () => { mocks.contextSrv.hasAccess.mockImplementation(() => true); }; +const setUserLogged = (isLogged: boolean) => { + config.bootData.user.isSignedIn = isLogged; + config.bootData.user.name = isLogged ? 'admin' : ''; +}; + describe('Silences', () => { beforeAll(resetMocks); afterEach(resetMocks); @@ -210,9 +216,19 @@ describe('Silence edit', () => { afterEach(resetMocks); beforeEach(() => { + setUserLogged(true); setDataSourceSrv(new MockDataSourceSrv(dataSources)); }); + it('Should not render createdBy if user is logged in and has a name', async () => { + renderSilences(baseUrlPath); + await waitFor(() => expect(ui.editor.createdBy.query()).not.toBeInTheDocument()); + }); + it('Should render createdBy if user is not logged or has no name', async () => { + setUserLogged(false); + renderSilences(baseUrlPath); + await waitFor(() => expect(ui.editor.createdBy.get()).toBeInTheDocument()); + }); it( 'prefills the matchers field with matchers params', async () => { diff --git a/public/app/features/alerting/unified/components/silences/SilencesEditor.tsx b/public/app/features/alerting/unified/components/silences/SilencesEditor.tsx index 52fa9d54bdc..2c37eddfd47 100644 --- a/public/app/features/alerting/unified/components/silences/SilencesEditor.tsx +++ b/public/app/features/alerting/unified/components/silences/SilencesEditor.tsx @@ -164,6 +164,7 @@ export const SilencesEditor: FC = ({ silence, alertManagerSourceName }) = 700, [clearErrors, duration, endsAt, prevDuration, setValue, startsAt] ); + const userLogged = Boolean(config.bootData.user.isSignedIn && config.bootData.user.name); return ( @@ -206,6 +207,20 @@ export const SilencesEditor: FC = ({ silence, alertManagerSourceName }) = placeholder="Details about the silence" /> + {!userLogged && ( + + + + )}