From 42c29cac0b7173abdbb384a2ecdf014359606fa8 Mon Sep 17 00:00:00 2001
From: Sonia Aguilar <33540275+soniaAguilarPeiron@users.noreply.github.com>
Date: Wed, 17 Jul 2024 16:40:17 +0200
Subject: [PATCH] =?UTF-8?q?Alerting:=20Use=20Runbook=20URL=20label=20every?=
=?UTF-8?q?where=20and=20add=20validation=20in=20the=20alert=20rule?=
=?UTF-8?q?=E2=80=A6=20(#90523)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* Use Runbook URL label everywhere and add validation in the alert rule form for it
* remove validation in alert rule form and render link on detail view only when its a valid url
---
.../rule-editor/AnnotationsStep.tsx | 2 +-
.../components/rule-viewer/RuleViewer.tsx | 32 +++++++++++++++----
2 files changed, 26 insertions(+), 8 deletions(-)
diff --git a/public/app/features/alerting/unified/components/rule-editor/AnnotationsStep.tsx b/public/app/features/alerting/unified/components/rule-editor/AnnotationsStep.tsx
index df6d82009c1..12c66c2ca01 100644
--- a/public/app/features/alerting/unified/components/rule-editor/AnnotationsStep.tsx
+++ b/public/app/features/alerting/unified/components/rule-editor/AnnotationsStep.tsx
@@ -5,7 +5,7 @@ import { useFieldArray, useFormContext } from 'react-hook-form';
import { useToggle } from 'react-use';
import { GrafanaTheme2 } from '@grafana/data';
-import { Button, Field, Input, Text, TextArea, useStyles2, Stack } from '@grafana/ui';
+import { Button, Field, Input, Stack, Text, TextArea, useStyles2 } from '@grafana/ui';
import { DashboardModel } from '../../../../dashboard/state';
import { RuleFormValues } from '../../types/rule-form';
diff --git a/public/app/features/alerting/unified/components/rule-viewer/RuleViewer.tsx b/public/app/features/alerting/unified/components/rule-viewer/RuleViewer.tsx
index 0ff7c324605..13922c65f58 100644
--- a/public/app/features/alerting/unified/components/rule-viewer/RuleViewer.tsx
+++ b/public/app/features/alerting/unified/components/rule-viewer/RuleViewer.tsx
@@ -148,14 +148,18 @@ const createMetadata = (rule: CombinedRule): PageInfoItem[] => {
const interval = group.interval;
if (runbookUrl) {
+ /* TODO instead of truncating the string, we should use flex and text overflow properly to allow it to take up all of the horizontal space available */
+ const truncatedUrl = truncate(runbookUrl, { length: 42 });
+ const valueToAdd = isValidRunbookURL(runbookUrl) ? (
+
+ {truncatedUrl}
+
+ ) : (
+ {truncatedUrl}
+ );
metadata.push({
- label: 'Runbook',
- value: (
-
- {/* TODO instead of truncating the string, we should use flex and text overflow properly to allow it to take up all of the horizontal space available */}
- {truncate(runbookUrl, { length: 42 })}
-
- ),
+ label: 'Runbook URL',
+ value: valueToAdd,
});
}
@@ -360,4 +364,18 @@ const getStyles = () => ({
}),
});
+function isValidRunbookURL(url: string) {
+ const isRelative = url.startsWith('/');
+ let isAbsolute = false;
+
+ try {
+ new URL(url);
+ isAbsolute = true;
+ } catch (_) {
+ return false;
+ }
+
+ return isRelative || isAbsolute;
+}
+
export default RuleViewer;