Alerting: Use Runbook URL label everywhere and add validation in the alert rule… (#90523)

* 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
This commit is contained in:
Sonia Aguilar 2024-07-17 16:40:17 +02:00 committed by GitHub
parent 07c2d886fa
commit 42c29cac0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 8 deletions

View File

@ -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';

View File

@ -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) ? (
<TextLink variant="bodySmall" href={runbookUrl} external>
{truncatedUrl}
</TextLink>
) : (
<Text variant="bodySmall">{truncatedUrl}</Text>
);
metadata.push({
label: 'Runbook',
value: (
<TextLink variant="bodySmall" href={runbookUrl} external>
{/* 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 })}
</TextLink>
),
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;