Skip to content

Commit f0038db

Browse files
committed
refactor(config): drive deprecation warnings from config.schema.json
`getDeprecatedConfigWarnings` hardcoded a check per deprecated field while `config.schema.json` already marked the same three fields `"deprecated": true`, so the two had to be kept in step by hand. The warnings are now generated from the schema. Adding `"deprecated": true` to a property is enough to produce a warning; `x-deprecated-replacement` names the field that supersedes it, and its absence produces the "deprecated and ignored" wording instead. Warning text is unchanged, and quicktype ignores the custom keyword, so `src/config/generated/config.ts` regenerates byte for byte. Closes #1680
1 parent 240d242 commit f0038db

3 files changed

Lines changed: 46 additions & 13 deletions

File tree

‎config.schema.json‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,14 @@
372372
"sslKeyPemPath": {
373373
"description": "Deprecated: Path to SSL private key file (use tls.key instead)",
374374
"type": "string",
375-
"deprecated": true
375+
"deprecated": true,
376+
"x-deprecated-replacement": "tls.key"
376377
},
377378
"sslCertPemPath": {
378379
"description": "Deprecated: Path to SSL certificate file (use tls.cert instead)",
379380
"type": "string",
380-
"deprecated": true
381+
"deprecated": true,
382+
"x-deprecated-replacement": "tls.cert"
381383
},
382384
"configurationSources": {
383385
"enabled": { "type": "boolean" },

‎src/config/deprecatedFields.ts‎

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,42 @@
1414
* limitations under the License.
1515
*/
1616

17+
import _ from 'lodash';
18+
import schema from '../../config.schema.json';
1719
import { GitProxyConfig } from './generated/config';
1820

21+
interface SchemaProperty {
22+
deprecated?: boolean;
23+
'x-deprecated-replacement'?: string;
24+
}
25+
26+
const schemaProperties = (schema as { properties: Record<string, SchemaProperty> }).properties;
27+
28+
function isSet(value: unknown): boolean {
29+
return typeof value === 'string' && value.trim() !== '';
30+
}
31+
1932
/**
2033
* Returns deprecation warnings for legacy top-level config keys in user overrides.
2134
* PR 3.0 (#1545) will replace warnings with startup failure for legacy-only configs.
2235
*/
2336
export function getDeprecatedConfigWarnings(userSettings: Partial<GitProxyConfig>): string[] {
37+
const settings = userSettings as Record<string, unknown>;
2438
const warnings: string[] = [];
2539

26-
if (userSettings.sslKeyPemPath?.trim() && !userSettings.tls?.key?.trim()) {
27-
warnings.push('"sslKeyPemPath" is deprecated; use "tls.key" instead (removal in GitProxy 3.0)');
28-
}
29-
30-
if (userSettings.sslCertPemPath?.trim() && !userSettings.tls?.cert?.trim()) {
31-
warnings.push(
32-
'"sslCertPemPath" is deprecated; use "tls.cert" instead (removal in GitProxy 3.0)',
33-
);
34-
}
40+
for (const [key, property] of Object.entries(schemaProperties)) {
41+
if (!property.deprecated || !isSet(settings[key])) {
42+
continue;
43+
}
3544

36-
if (typeof userSettings.proxyUrl === 'string' && userSettings.proxyUrl.trim() !== '') {
37-
warnings.push('"proxyUrl" is deprecated and ignored; remove it before GitProxy 3.0');
45+
const replacement = property['x-deprecated-replacement'];
46+
if (!replacement) {
47+
warnings.push(`"${key}" is deprecated and ignored; remove it before GitProxy 3.0`);
48+
} else if (!isSet(_.get(settings, replacement))) {
49+
warnings.push(
50+
`"${key}" is deprecated; use "${replacement}" instead (removal in GitProxy 3.0)`,
51+
);
52+
}
3853
}
3954

4055
return warnings;

‎test/deprecatedFields.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ describe('getDeprecatedConfigWarnings', () => {
3333
);
3434
});
3535

36+
it('warns for a deprecated field that has no replacement', () => {
37+
expect(getDeprecatedConfigWarnings({ proxyUrl: 'https://github.com' })).toEqual([
38+
'"proxyUrl" is deprecated and ignored; remove it before GitProxy 3.0',
39+
]);
40+
});
41+
42+
it('stays quiet when the replacement is already set', () => {
43+
expect(
44+
getDeprecatedConfigWarnings({
45+
sslKeyPemPath: 'key.pem',
46+
sslCertPemPath: 'cert.pem',
47+
tls: { enabled: true, key: 'k.pem', cert: 'c.pem' },
48+
}),
49+
).toEqual([]);
50+
});
51+
3652
it('returns no warnings for non-deprecated overrides', () => {
3753
expect(getDeprecatedConfigWarnings({ uiPort: 9000 })).toEqual([]);
3854
expect(

0 commit comments

Comments
 (0)