fix(ui): un-escape regex values when editing silences

This commit is contained in:
Łukasz Mierzwa
2022-02-03 15:54:53 +00:00
committed by Łukasz Mierzwa
parent 9d6f44dcea
commit e3753bef6f
3 changed files with 76 additions and 5 deletions

View File

@@ -1,5 +1,11 @@
# Changelog
## v0.97
### Fixed
- Fix escaping regex values in when editing silences #3936.
## v0.96
### Fixed

View File

@@ -681,6 +681,60 @@ describe("SilenceFormStore.data", () => {
},
result: { name: "foo", values: ["server(0|1)"] },
},
{
matcher: {
name: "foo",
value: "10\\.0\\.0\\.1|10\\.0\\.0\\.2",
isRegex: true,
isEqual: true,
},
result: { name: "foo", values: ["10.0.0.1", "10.0.0.2"] },
},
{
matcher: {
name: "foo",
value: "10\\.0\\.0\\.1|10\\.0\\.0\\.2",
isRegex: false,
isEqual: true,
},
result: { name: "foo", values: ["10\\.0\\.0\\.1|10\\.0\\.0\\.2"] },
},
{
matcher: {
name: "foo",
value: "(bar\\-1|bar\\-2|1\\.2)",
isRegex: true,
isEqual: true,
},
result: { name: "foo", values: ["bar-1", "bar-2", "1.2"] },
},
{
matcher: {
name: "foo",
value: "(bar\\-1|bar\\-2|1\\.2)",
isRegex: false,
isEqual: true,
},
result: { name: "foo", values: ["(bar\\-1|bar\\-2|1\\.2)"] },
},
{
matcher: {
name: "foo",
value: "foo bar|1-2",
isRegex: true,
isEqual: true,
},
result: { name: "foo", values: ["foo bar", "1-2"] },
},
{
matcher: {
name: "foo",
value: "foo bar|1-2",
isRegex: false,
isEqual: true,
},
result: { name: "foo", values: ["foo bar|1-2"] },
},
];
for (const t of tests) {
it(`fillFormFromSilence() unpacks ${t.matcher.name}=${t.matcher.value} isRegex=${t.matcher.isRegex} into ${t.result.name}=${t.result.values}`, () => {

View File

@@ -81,6 +81,10 @@ export const EscapeRegex = (v: string): string => {
return v.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
};
export const UnescapeRegex = (v: string): string => {
return v.replaceAll("\\", "");
};
const MatchersFromGroup = (
group: APIAlertGroupT,
stripLabels: string[],
@@ -247,15 +251,22 @@ const GenerateAlertmanagerSilenceData = (
};
const UnpackRegexMatcherValues = (isRegex: boolean, value: string) => {
if (isRegex && value.match(/^\((\w+\|)+\w+\)$/)) {
return value
let val: string = value;
if (isRegex) {
val = UnescapeRegex(val);
}
if (isRegex && val.match(/^\(([a-zA-Z0-9_\-. ]+\|)+[a-zA-Z0-9_\-. ]+\)$/)) {
return val
.slice(1, -1)
.split("|")
.map((v) => StringToOption(v));
} else if (isRegex && value.match(/^(\w+\|)+\w+$/)) {
return value.split("|").map((v) => StringToOption(v));
} else if (
isRegex &&
val.match(/^([a-zA-Z0-9_\-. ]+\|)+[a-zA-Z0-9_\-. ]+$/)
) {
return val.split("|").map((v) => StringToOption(v));
} else {
return [StringToOption(value)];
return [StringToOption(val)];
}
};