Skip to content

Commit 6ee05a8

Browse files
Merge pull request #242 from devtron-labs/fix/logs-search
fix: revert search working as regex search for logs
2 parents f4d7a03 + a7077f7 commit 6ee05a8

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/Constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export const PATTERNS = {
3434
KUBERNETES_KEY_NAME: /^(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$/,
3535
START_END_ALPHANUMERIC: /^([Az09].*[A-Za-z0-9])$|[A-Za-z0-9]$/,
3636
ALPHANUMERIC_WITH_SPECIAL_CHAR: /^[A-Za-z0-9._-]+$/, // allow alphanumeric,(.) ,(-),(_)
37+
ESCAPED_CHARACTERS: /[.*+?^${}()|[\]\\]/g,
3738
}
3839

3940
export const URLS = {

src/Shared/Components/CICDHistory/LogsRenderer.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { useEffect, useRef, useState } from 'react'
1919
import AnsiUp from 'ansi_up'
2020
import DOMPurify from 'dompurify'
2121
import { ANSI_UP_REGEX, ComponentSizeType } from '@Shared/constants'
22+
import { escapeRegExp } from '@Shared/Helpers'
2223
import {
2324
Progressing,
2425
Host,
@@ -197,19 +198,21 @@ export const LogsRenderer = ({
197198
// We will remove color through [0m and add background color of y6, till searchKey is present and then revert back to original color
198199
// While reverting if index is 0, would not add any escape code since it is the start of the log
199200
if (targetSearchKey && areStagesAvailable) {
201+
// Search is working on assumption that color codes are not nested for words.
200202
const logParts = log.split(ANSI_UP_REGEX)
201-
const availableEscapeCodes = log.match(ANSI_UP_REGEX)
203+
const availableEscapeCodes = log.match(ANSI_UP_REGEX) || []
204+
const searchRegex = new RegExp(`(${escapeRegExp(targetSearchKey)})`, 'g')
202205
const parts = logParts.reduce((acc, part, index) => {
203206
try {
207+
// Question: Can we directly set it as true inside the replace function?
208+
isSearchKeyPresent = isSearchKeyPresent || searchRegex.test(part)
204209
acc.push(
205210
part.replace(
206-
new RegExp(targetSearchKey, 'g'),
207-
`\x1B[0m\x1B[48;2;197;141;54m${targetSearchKey}\x1B[0m${index > 0 ? availableEscapeCodes[index - 1] : ''}`,
211+
searchRegex,
212+
(match) =>
213+
`\x1B[0m\x1B[48;2;197;141;54m${match}\x1B[0m${index > 0 ? availableEscapeCodes[index - 1] : ''}`,
208214
),
209215
)
210-
if (part.includes(targetSearchKey)) {
211-
isSearchKeyPresent = true
212-
}
213216
} catch (searchRegexError) {
214217
acc.push(part)
215218
}

src/Shared/Helpers.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@
1818
import { useEffect, useRef, useState, ReactElement } from 'react'
1919
import Tippy from '@tippyjs/react'
2020
import moment from 'moment'
21-
import { handleUTCTime, mapByKey, MaterialInfo, shallowEqual, SortingOrder, ZERO_TIME_STRING } from '../Common'
21+
import {
22+
handleUTCTime,
23+
mapByKey,
24+
MaterialInfo,
25+
PATTERNS,
26+
shallowEqual,
27+
SortingOrder,
28+
ZERO_TIME_STRING,
29+
} from '../Common'
2230
import {
2331
AggregationKeys,
2432
GitTriggers,
@@ -54,7 +62,8 @@ interface HighlightSearchTextProps {
5462
highlightClasses?: string
5563
}
5664

57-
// Disabling default export since this is a helper function and we would have to export a lot of functions in future.
65+
export const escapeRegExp = (text: string): string => text.replace(PATTERNS.ESCAPED_CHARACTERS, '\\$&')
66+
5867
export const highlightSearchText = ({ searchText, text, highlightClasses }: HighlightSearchTextProps): string => {
5968
if (!searchText) {
6069
return text

0 commit comments

Comments
 (0)