Commit 40a5c7c6 authored by Alexander Turinske's avatar Alexander Turinske

Fix bug where a rule does not have branches

- it is currently allowed on the backend to create a policy
  with a rule, but now branches, which will do nothing
- update the sidebar to display an empty text for this use
  case
- update tests
parent 219f5799
...@@ -5,6 +5,7 @@ export const DEFAULT_MR_TITLE = s__('SecurityOrchestration|Update scan execution ...@@ -5,6 +5,7 @@ export const DEFAULT_MR_TITLE = s__('SecurityOrchestration|Update scan execution
export const GRAPHQL_ERROR_MESSAGE = s__( export const GRAPHQL_ERROR_MESSAGE = s__(
'SecurityOrchestration|There was a problem creating the new security policy', 'SecurityOrchestration|There was a problem creating the new security policy',
); );
export const NO_RULE_MESSAGE = s__('SecurityOrhestration|No rules defined - policy will not run.');
export const SECURITY_POLICY_ACTIONS = { export const SECURITY_POLICY_ACTIONS = {
APPEND: 'APPEND', APPEND: 'APPEND',
......
import { convertToTitleCase, humanize } from '~/lib/utils/text_utility'; import { convertToTitleCase, humanize } from '~/lib/utils/text_utility';
import { sprintf, s__, n__ } from '~/locale'; import { sprintf, s__, n__ } from '~/locale';
import { NO_RULE_MESSAGE } from './constants';
const getActionText = (scanType) => const getActionText = (scanType) =>
sprintf(s__('SecurityOrchestration|Executes a %{scanType} scan'), { sprintf(s__('SecurityOrchestration|Executes a %{scanType} scan'), {
...@@ -16,7 +17,7 @@ const humanizeBranches = (originalBranches) => { ...@@ -16,7 +17,7 @@ const humanizeBranches = (originalBranches) => {
const plural = n__('branch', 'branches', branches.length); const plural = n__('branch', 'branches', branches.length);
if (branches.length <= 1) { if (branches.length === 1) {
return sprintf(s__('SecurityOrchestration|%{branches} %{plural}'), { return sprintf(s__('SecurityOrchestration|%{branches} %{plural}'), {
branches: branches.join(','), branches: branches.join(','),
plural, plural,
...@@ -69,5 +70,9 @@ export const humanizeActions = (actions) => { ...@@ -69,5 +70,9 @@ export const humanizeActions = (actions) => {
* @returns {Array} * @returns {Array}
*/ */
export const humanizeRules = (rules) => { export const humanizeRules = (rules) => {
return rules.map((r) => HUMANIZE_RULES_METHODS[r.type](r) || ''); const humanizedRules = rules.reduce((acc, curr) => {
return curr.branches ? [...acc, HUMANIZE_RULES_METHODS[curr.type](curr)] : acc;
}, []);
return humanizedRules.length ? humanizedRules : [NO_RULE_MESSAGE];
}; };
...@@ -33,13 +33,13 @@ describe('ScanExecutionPolicy component', () => { ...@@ -33,13 +33,13 @@ describe('ScanExecutionPolicy component', () => {
}); });
it.each` it.each`
component | findComponent | text component | finder | text
${'actions'} | ${findActions} | ${''} ${'actions'} | ${findActions} | ${''}
${'rules'} | ${findRules} | ${''} ${'rules'} | ${findRules} | ${''}
${'description'} | ${findDescription} | ${'This policy enforces pipeline configuration to have a job with DAST scan'} ${'description'} | ${findDescription} | ${'This policy enforces pipeline configuration to have a job with DAST scan'}
${'latest scan'} | ${findLatestScan} | ${''} ${'latest scan'} | ${findLatestScan} | ${''}
`('does render the policy $component', ({ findComponent, text }) => { `('does render the policy $component', ({ finder, text }) => {
const component = findComponent(); const component = finder();
expect(component.exists()).toBe(true); expect(component.exists()).toBe(true);
if (text) { if (text) {
expect(component.text()).toBe(text); expect(component.text()).toBe(text);
...@@ -61,11 +61,11 @@ describe('ScanExecutionPolicy component', () => { ...@@ -61,11 +61,11 @@ describe('ScanExecutionPolicy component', () => {
}); });
it.each` it.each`
component | findComponent component | finder
${'description'} | ${findDescription} ${'description'} | ${findDescription}
${'latest scan'} | ${findLatestScan} ${'latest scan'} | ${findLatestScan}
`('does render the policy $component', ({ findComponent }) => { `('does render the policy $component', ({ finder }) => {
expect(findComponent().exists()).toBe(false); expect(finder().exists()).toBe(false);
}); });
}); });
}); });
import { import {
humanizeActions, humanizeActions,
humanizeRules, humanizeRules,
NO_RULE_MESSAGE,
} from 'ee/threat_monitoring/components/policy_editor/scan_execution_policy/lib'; } from 'ee/threat_monitoring/components/policy_editor/scan_execution_policy/lib';
const mockActions = [ const mockActions = [
...@@ -13,6 +14,7 @@ const mockRules = [ ...@@ -13,6 +14,7 @@ const mockRules = [
{ type: 'schedule', cadence: '*/10 * * * *', branches: ['main'] }, { type: 'schedule', cadence: '*/10 * * * *', branches: ['main'] },
{ type: 'pipeline', branches: ['release/*', 'staging'] }, { type: 'pipeline', branches: ['release/*', 'staging'] },
{ type: 'pipeline', branches: ['release/1.*', 'canary', 'staging'] }, { type: 'pipeline', branches: ['release/1.*', 'canary', 'staging'] },
{ type: 'pipeline' },
]; ];
describe('humanizeActions', () => { describe('humanizeActions', () => {
...@@ -32,8 +34,12 @@ describe('humanizeActions', () => { ...@@ -32,8 +34,12 @@ describe('humanizeActions', () => {
}); });
describe('humanizeRules', () => { describe('humanizeRules', () => {
it('returns an empty Array of rules as an empty Array', () => { it('returns the empty rules message in an Array if no rules are specified', () => {
expect(humanizeRules([])).toStrictEqual([]); expect(humanizeRules([])).toStrictEqual([NO_RULE_MESSAGE]);
});
it('returns the empty rules message in an Array if a single rule is passed in without a branch', () => {
expect(humanizeRules([])).toStrictEqual([NO_RULE_MESSAGE]);
}); });
it('returns a single rule as a human-readable string', () => { it('returns a single rule as a human-readable string', () => {
......
...@@ -29760,6 +29760,9 @@ msgstr "" ...@@ -29760,6 +29760,9 @@ msgstr ""
msgid "SecurityOrchestration|view results" msgid "SecurityOrchestration|view results"
msgstr "" msgstr ""
msgid "SecurityOrhestration|No rules defined - policy will not run."
msgstr ""
msgid "SecurityPolicies|+%{count} more" msgid "SecurityPolicies|+%{count} more"
msgstr "" msgstr ""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment