Skip to content

Commit 8c2b706

Browse files
committed
fix: use regex for parsing the deprecated image
1 parent 1f50574 commit 8c2b706

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

lib/metric/deprecated-images/deprecated-images.collector.ts

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// @ts-expect-error it's a js library
22
import cfLogs from 'cf-logs';
33

4+
// eslint-disable-next-line import/no-unresolved
45
import { DeprecatedImageDto } from './deprecated-image.dto';
56

67
const logger = cfLogs.Logger('codefresh:containerLogger');
@@ -22,33 +23,22 @@ class DeprecatedImagesCollector {
2223
}
2324

2425
catchDeprecatedImage(logText: string) {
25-
if (logText.includes('[DEPRECATION NOTICE]')) {
26-
const imageName = this._parseImageName(logText);
27-
28-
if (imageName === null) {
29-
logger.error(`detected pulling of the deprecated image but failed to parse the image name. The original log text: '${logText}'`);
30-
} else {
31-
logger.warn(`detected pulling of the deprecated image '${imageName}'. The original log text: '${logText}'`);
32-
33-
this.push({
34-
image: imageName,
35-
});
36-
}
37-
}
38-
}
39-
40-
private _parseImageName(logText: string) {
41-
const startMarker = 'Suggest the author of ';
42-
const endMarker = ' to upgrade the image';
26+
const imageName = this._parseImageName(logText);
4327

44-
const startIndex = logText.indexOf(startMarker);
45-
const endIndex = logText.indexOf(endMarker);
28+
if (imageName !== null) {
29+
logger.warn(`detected pulling of the deprecated image '${imageName}'. The original log text: '${logText}'`);
4630

47-
if (startIndex > -1 && endIndex > -1 && endIndex > startIndex) {
48-
return logText.substring(startIndex + startMarker.length, endIndex);
31+
this.push({
32+
image: imageName,
33+
});
4934
}
35+
}
5036

51-
return null;
37+
private _parseImageName(logText: string) {
38+
// eslint-disable-next-line no-control-regex
39+
const regex = /^\u001b\[31m\u001b\[1m\[DEPRECATION NOTICE].+?Suggest the author of (?<image>.+?) to/;
40+
const match = logText.match(regex);
41+
return match?.groups?.image ?? null;
5242
}
5343
}
5444

0 commit comments

Comments
 (0)