From 5ca0cc0e4b329e0233c0dc3484098c980e7c5f28 Mon Sep 17 00:00:00 2001 From: 7PH Date: Mon, 6 May 2024 13:17:08 +0200 Subject: [PATCH] SCANNPM-2 Add support for deprecated properties --- src/constants.ts | 8 ++++++++ src/properties.ts | 27 +++++++++++++++++++++++++-- src/types.ts | 6 ++++++ test/unit/properties.test.ts | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 0dff8ca..1e64f90 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -64,3 +64,11 @@ export const SCANNER_CLI_MIRROR = export const SCANNER_CLI_INSTALL_PATH = 'native-sonar-scanner'; export const WINDOWS_WHERE_EXE_PATH = 'C:\\Windows\\System32\\where.exe'; + +export const SCANNER_DEPRECATED_PROPERTIES: ScannerProperty[][] = [ + [ScannerProperty.SonarWsTimeout, ScannerProperty.SonarScannerResponseTimeout], + [ScannerProperty.HttpProxyHost, ScannerProperty.SonarScannerProxyHost], + [ScannerProperty.HttpProxyPort, ScannerProperty.SonarScannerProxyPort], + [ScannerProperty.HttpProxyUser, ScannerProperty.SonarScannerProxyUser], + [ScannerProperty.HttpProxyPassword, ScannerProperty.SonarScannerProxyPassword], +]; diff --git a/src/properties.ts b/src/properties.ts index bbfe697..49693dc 100644 --- a/src/properties.ts +++ b/src/properties.ts @@ -28,6 +28,7 @@ import { ENV_VAR_PREFIX, NPM_CONFIG_ENV_VAR_PREFIX, SCANNER_BOOTSTRAPPER_NAME, + SCANNER_DEPRECATED_PROPERTIES, SONARCLOUD_API_BASE_URL, SONARCLOUD_URL, SONARCLOUD_URL_REGEX, @@ -363,6 +364,28 @@ function getHttpProxyEnvProperties(serverUrl: string): ScannerProperties { return properties; } +function hotfixDeprecatedProperties(properties: ScannerProperties): ScannerProperties { + for (const [oldProp, newProp] of SCANNER_DEPRECATED_PROPERTIES) { + if (typeof properties[oldProp] !== 'undefined') { + if (typeof properties[newProp] === 'undefined') { + log( + LogLevel.WARN, + `Property "${oldProp}" is deprecated and will be removed in a future version. Please use "${newProp}" instead.`, + ); + properties[newProp] = properties[oldProp]; + } else { + log( + LogLevel.WARN, + `Both properties "${oldProp}" and "${newProp}" are set. "${oldProp}" is deprecated and will be removed in a future version. Value of deprecated property "${oldProp}" will be ignored.`, + ); + properties[oldProp] = properties[newProp]; + } + } + } + + return properties; +} + export function getProperties( scanOptions: ScanOptions, startTimestampMs: number, @@ -416,11 +439,11 @@ export function getProperties( // Hotfix host properties with custom SonarCloud URL const hostProperties = getHostProperties(properties); - return { + return hotfixDeprecatedProperties({ ...properties, // Can't be overridden: ...hostProperties, ...getBootstrapperProperties(startTimestampMs), 'sonar.projectBaseDir': projectBaseDir, - }; + }); } diff --git a/src/types.ts b/src/types.ts index 1aa1ca8..4912dcc 100644 --- a/src/types.ts +++ b/src/types.ts @@ -58,6 +58,12 @@ export enum ScannerProperty { SonarScannerInternalSqVersion = 'sonar.scanner.internal.sqVersion', SonarScannerCliVersion = 'sonar.scanner.version', SonarScannerCliMirror = 'sonar.scanner.mirror', + // Deprecated properties: + SonarWsTimeout = 'sonar.ws.timeout', + HttpProxyHost = 'http.proxyHost', + HttpProxyPort = 'http.proxyPort', + HttpProxyUser = 'http.proxyUser', + HttpProxyPassword = 'http.proxyPassword', } export type ScannerProperties = { diff --git a/test/unit/properties.test.ts b/test/unit/properties.test.ts index 898e236..6123f6c 100644 --- a/test/unit/properties.test.ts +++ b/test/unit/properties.test.ts @@ -481,6 +481,40 @@ describe('getProperties', () => { ); }); + it('should warn and replace deprecated properties', () => { + projectHandler.reset('fake_project_with_sonar_properties_file'); + projectHandler.setEnvironmentVariables({ + SONAR_SCANNER_JSON_PARAMS: JSON.stringify({ + 'sonar.ws.timeout': '000', + }), + }); + + const properties = getProperties( + { + options: { + 'sonar.scanner.responseTimeout': '111', + 'http.proxyHost': 'my-proxy.io', + }, + }, + projectHandler.getStartTime(), + ); + + expect(properties).toMatchObject({ + 'sonar.scanner.responseTimeout': '111', // Should not replace the deprecated property because its new version is also present + 'sonar.ws.timeout': '111', + 'sonar.scanner.proxyHost': 'my-proxy.io', // Should replace the deprecated property with the new one + 'http.proxyHost': 'my-proxy.io', + }); + expect(log).toHaveBeenCalledWith( + LogLevel.WARN, + 'Both properties "sonar.ws.timeout" and "sonar.scanner.responseTimeout" are set. "sonar.ws.timeout" is deprecated and will be removed in a future version. Value of deprecated property "sonar.ws.timeout" will be ignored.', + ); + expect(log).toHaveBeenCalledWith( + LogLevel.WARN, + 'Property "http.proxyHost" is deprecated and will be removed in a future version. Please use "sonar.scanner.proxyHost" instead.', + ); + }); + it('should set the [ScannerProperty.SonarScannerCliVersion] for all existing formats', () => { projectHandler.reset('fake_project_with_sonar_properties_file'); projectHandler.setEnvironmentVariables({