Skip to content

Commit

Permalink
Merge pull request #752 from snyk/fix/add-custom-accept-json-prefligh…
Browse files Browse the repository at this point in the history
…t-check

fix: add accept cfg preflight checks signaling codeagent deprec [HYB-437]
  • Loading branch information
aarlaud authored May 6, 2024
2 parents 6ddfa42 + 03deb89 commit ab013e7
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/client/checks/config/customAcceptFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Config } from '../../types/config';
import { CheckOptions, CheckResult } from '../types';

export const validateAcceptFlagsConfig = (
checkOptions: CheckOptions,
config: Config,
): CheckResult => {
const acceptFlagKeys = Object.keys(config).filter((x) =>
x.startsWith('ACCEPT_'),
);
if (
config.ACCEPT &&
config.ACCEPT != 'accept.json' &&
acceptFlagKeys.length > 0
) {
return {
id: checkOptions.id,
name: checkOptions.name,
status: 'error',
output: `ACCEPT_ flags are not compatible with custom accept.json files. Please refrain from using custom accept json (Code Agent is deprecated, see documentation).`,
} satisfies CheckResult;
}

return {
id: checkOptions.id,
name: checkOptions.name,
status: 'passing',
output: 'ACCEPT flags configuration OK.',
} satisfies CheckResult;
};
16 changes: 16 additions & 0 deletions lib/client/checks/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { Config } from '../../types/config';
import type { Check, CheckResult } from '../types';
import { validateBrokerClientUrl } from './brokerClientUrlCheck';
import { validateAcceptFlagsConfig } from './customAcceptFile';
import { validateUniversalConnectionsConfig } from './universalConnectionConfigCheck';

export function getConfigChecks(config: Config): Check[] {
return [
brokerClientUrlCheck(config),
universalBrokerConnectionsCheck(config),
acceptFlagsConfigurationCheck(config),
];
}

Expand Down Expand Up @@ -38,3 +40,17 @@ const universalBrokerConnectionsCheck = (config: Config): Check => {
},
} satisfies Check;
};

const acceptFlagsConfigurationCheck = (config: Config): Check => {
return {
id: 'accept-flags-config-validation',
name: 'Accept flags Configuration Check',
enabled: true,
check: function (): CheckResult {
return validateAcceptFlagsConfig(
{ id: this.id, name: this.name },
config,
);
},
} satisfies Check;
};
70 changes: 70 additions & 0 deletions test/unit/client/checks/config/acceptFlagsConfigCheck.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { validateAcceptFlagsConfig } from '../../../../../lib/client/checks/config/customAcceptFile';
import { aConfig } from '../../../../helpers/test-factories';

describe('client/checks/config', () => {
describe('validateAcceptFlagsConfig()', () => {
it('should return error check result if incompatible ACCEPT flags', async () => {
const id = `check_${Date.now()}`;
const config = aConfig({
ACCEPT_CODE: true,
ACCEPT: '/path/to/custom/accept/file',
});

const checkResult = validateAcceptFlagsConfig(
{ id: id, name: id },
config,
);
expect(checkResult.status).toEqual('error');
expect(checkResult.output).toContain(
'ACCEPT_ flags are not compatible with custom accept.json files. Please refrain from using custom accept json (Code Agent is deprecated, see documentation).',
);
});

it('should return error check result if incompatible ACCEPT numerous flags', async () => {
const id = `check_${Date.now()}`;
const config = aConfig({
ACCEPT_GIT: true,
ACCEPT_IAC: true,
ACCEPT_APPRISK: true,
ACCEPT: '/path/to/custom/accept/file',
});

const checkResult = validateAcceptFlagsConfig(
{ id: id, name: id },
config,
);
expect(checkResult.status).toEqual('error');
expect(checkResult.output).toContain(
'ACCEPT_ flags are not compatible with custom accept.json files. Please refrain from using custom accept json (Code Agent is deprecated, see documentation).',
);
});

it('should return passing check result if only ACCEPT custom file configured', async () => {
const id = `check_${Date.now()}`;
const config = aConfig({
ACCEPT: '/path/to/custom/accept/file',
});

const checkResult = validateAcceptFlagsConfig(
{ id: id, name: id },
config,
);
expect(checkResult.status).toEqual('passing');
expect(checkResult.output).toContain('ACCEPT flags configuration OK.');
});

it('should return passing check result if only ACCEPT_ flags configured', async () => {
const id = `check_${Date.now()}`;
const config = aConfig({
ACCEPT_CODE: true,
});

const checkResult = validateAcceptFlagsConfig(
{ id: id, name: id },
config,
);
expect(checkResult.status).toEqual('passing');
expect(checkResult.output).toContain('ACCEPT flags configuration OK.');
});
});
});

0 comments on commit ab013e7

Please sign in to comment.