Skip to content

Commit

Permalink
SCANNPM-2 Add support for deprecated properties
Browse files Browse the repository at this point in the history
  • Loading branch information
7PH committed May 6, 2024
1 parent cac43ab commit 5ca0cc0
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
];
27 changes: 25 additions & 2 deletions src/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
};
});
}
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
34 changes: 34 additions & 0 deletions test/unit/properties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 5ca0cc0

Please sign in to comment.