-
Notifications
You must be signed in to change notification settings - Fork 174
ROX-34014: Fix label search filter formatting for key=value pairs #19902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -428,6 +428,20 @@ const regexSearchOptions = [ | |
| .filter(({ inputType }) => inputType === 'text' || inputType === 'autocomplete') | ||
| .map(({ searchTerm }) => searchTerm); | ||
|
|
||
| /* | ||
| Search terms that use the key=value label format and need special handling | ||
| in both regex and exact-match search modes. | ||
| */ | ||
| const labelSearchOptions: Set<string> = new Set( | ||
| ['Cluster Label', 'Deployment Label', 'Image Label', 'Namespace Label', 'Node Label'].map( | ||
| (label) => label.toLowerCase() | ||
| ) | ||
| ); | ||
|
|
||
| export function isLabelSearchTerm(searchTerm: string): boolean { | ||
| return labelSearchOptions.has(searchTerm.toLowerCase()); | ||
| } | ||
|
|
||
| function isQuotedString(value: string): boolean { | ||
| return value.startsWith('"') && value.endsWith('"') && value.length >= 2; | ||
| } | ||
|
|
@@ -441,18 +455,46 @@ export function wrapInQuotes(value: string): string { | |
| return `"${escapedValue}"`; | ||
| } | ||
|
|
||
| /** | ||
| * Formats a label value by splitting on the first '=' and applying a formatter | ||
| * to each half. If no '=' is present, the value is used for both sides. | ||
| * | ||
| * Used for both exact-match (formatter = wrapInQuotes) and regex (formatter = r/ prefix) | ||
| * label formatting. | ||
| */ | ||
| export function formatLabelValue(value: string, formatPart: (part: string) => string): string { | ||
| const eqIndex = value.indexOf('='); | ||
| if (eqIndex !== -1) { | ||
| const key = value.slice(0, eqIndex); | ||
| const val = value.slice(eqIndex + 1); | ||
| return `${formatPart(key)}=${formatPart(val)}`; | ||
| } | ||
| return `${formatPart(value)}=${formatPart(value)}`; | ||
| } | ||
|
|
||
| /** | ||
| * Adds the regex search modifier to the search filter for any search options that support it. | ||
| * Skips regex wrapping for values that are already quoted (exact-match strings). | ||
| * | ||
| * Label search terms (e.g. "Deployment Label") receive special formatting because the | ||
| * search API treats labels as key=value pairs. If the "=" part is not present, the value is used for both sides. | ||
| * - Regex: app=reporting -> r/app=r/reporting, report -> r/report=r/report | ||
| * - Exact: app=reporting -> "app"="reporting" | ||
| */ | ||
| export function applyRegexSearchModifiers(searchFilter: SearchFilter): SearchFilter { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (complexity): Consider extracting the label-specific and generic regex formatting into small helper functions so You can flatten For example: function formatRegexValue(val: string): string {
return isQuotedString(val) ? val : `r/${val}`;
}
function formatLabelRegexValue(val: string): string {
const quoted = isQuotedString(val);
const rawValue = quoted ? val.slice(1, -1) : val;
const formatter = quoted ? wrapInQuotes : (v: string) => `r/${v}`;
return formatLabelValue(rawValue, formatter);
}Then export function applyRegexSearchModifiers(searchFilter: SearchFilter): SearchFilter {
const regexSearchFilter = cloneDeep(searchFilter);
Object.entries(regexSearchFilter).forEach(([key, value]) => {
if (regexSearchOptions.some((option) => option.toLowerCase() === key.toLowerCase())) {
const isLabel = isLabelSearchTerm(key);
regexSearchFilter[key] = searchValueAsArray(value).map((val) =>
isLabel ? formatLabelRegexValue(val) : formatRegexValue(val)
);
}
});
return regexSearchFilter;
}This keeps all existing behavior (including the label key=value semantics and quoted handling) but removes the nested |
||
| const regexSearchFilter = cloneDeep(searchFilter); | ||
|
|
||
| Object.entries(regexSearchFilter).forEach(([key, value]) => { | ||
| if (regexSearchOptions.some((option) => option.toLowerCase() === key.toLowerCase())) { | ||
| regexSearchFilter[key] = searchValueAsArray(value).map((val) => | ||
| isQuotedString(val) ? val : `r/${val}` | ||
| ); | ||
| const isLabel = isLabelSearchTerm(key); | ||
| regexSearchFilter[key] = searchValueAsArray(value).map((val) => { | ||
| if (isLabel) { | ||
| const rawValue = isQuotedString(val) ? val.slice(1, -1) : val; | ||
| const formatter = isQuotedString(val) ? wrapInQuotes : (v: string) => `r/${v}`; | ||
| return formatLabelValue(rawValue, formatter); | ||
| } | ||
| return isQuotedString(val) ? val : `r/${val}`; | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.