Commit 2a555301 authored by Alexander Turinske's avatar Alexander Turinske

Abstract out policy actions into a constant

- so that it can be reused all over the code base
parent 3288d070
...@@ -5,3 +5,9 @@ export const DEFAULT_MR_TITLE = s__('SecurityOrchestration|Update scan execution ...@@ -5,3 +5,9 @@ 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 SECURITY_POLICY_ACTIONS = {
APPEND: 'APPEND',
REMOVE: 'REMOVE',
REPLACE: 'REPLACE',
};
...@@ -2,7 +2,7 @@ import createPolicyProject from 'ee/threat_monitoring/graphql/mutations/create_p ...@@ -2,7 +2,7 @@ import createPolicyProject from 'ee/threat_monitoring/graphql/mutations/create_p
import createScanExecutionPolicy from 'ee/threat_monitoring/graphql/mutations/create_scan_execution_policy.mutation.graphql'; import createScanExecutionPolicy from 'ee/threat_monitoring/graphql/mutations/create_scan_execution_policy.mutation.graphql';
import { gqClient } from 'ee/threat_monitoring/utils'; import { gqClient } from 'ee/threat_monitoring/utils';
import createMergeRequestMutation from '~/graphql_shared/mutations/create_merge_request.mutation.graphql'; import createMergeRequestMutation from '~/graphql_shared/mutations/create_merge_request.mutation.graphql';
import { DEFAULT_MR_TITLE } from './constants'; import { DEFAULT_MR_TITLE, SECURITY_POLICY_ACTIONS } from './constants';
/** /**
* Checks if an error exists and throws it if it does * Checks if an error exists and throws it if it does
...@@ -67,7 +67,11 @@ const createMergeRequest = async ({ projectPath, sourceBranch, targetBranch }) = ...@@ -67,7 +67,11 @@ const createMergeRequest = async ({ projectPath, sourceBranch, targetBranch }) =
* @param {Object} payload contains the path to the project and the policy yaml value * @param {Object} payload contains the path to the project and the policy yaml value
* @returns {Object} contains the branch containing the updated policy file and any errors * @returns {Object} contains the branch containing the updated policy file and any errors
*/ */
const updatePolicy = async ({ action, projectPath, yamlEditorValue }) => { const updatePolicy = async ({
action = SECURITY_POLICY_ACTIONS.APPEND,
projectPath,
yamlEditorValue,
}) => {
const { const {
data: { data: {
scanExecutionPolicyCommit: { branch, errors }, scanExecutionPolicyCommit: { branch, errors },
...@@ -90,7 +94,7 @@ const updatePolicy = async ({ action, projectPath, yamlEditorValue }) => { ...@@ -90,7 +94,7 @@ const updatePolicy = async ({ action, projectPath, yamlEditorValue }) => {
* @returns {Object} contains the currently assigned security policy project and the created merge request * @returns {Object} contains the currently assigned security policy project and the created merge request
*/ */
export const modifyPolicy = async ({ export const modifyPolicy = async ({
action = 'APPEND', action,
assignedPolicyProject, assignedPolicyProject,
projectPath, projectPath,
yamlEditorValue, yamlEditorValue,
......
...@@ -10,13 +10,10 @@ import { ...@@ -10,13 +10,10 @@ import {
GRAPHQL_ERROR_MESSAGE, GRAPHQL_ERROR_MESSAGE,
modifyPolicy, modifyPolicy,
} from './lib'; } from './lib';
import { SECURITY_POLICY_ACTIONS } from './lib/constants';
export default { export default {
ACTION_FLAGS: { SECURITY_POLICY_ACTIONS,
APPEND: 'isCreatingMR',
REMOVE: 'isRemovingPolicy',
REPLACE: 'isCreatingMR',
},
DEFAULT_EDITOR_MODE: EDITOR_MODE_YAML, DEFAULT_EDITOR_MODE: EDITOR_MODE_YAML,
EDITOR_MODES: [EDITOR_MODES[1]], EDITOR_MODES: [EDITOR_MODES[1]],
i18n: { i18n: {
...@@ -64,10 +61,14 @@ export default { ...@@ -64,10 +61,14 @@ export default {
} }
}, },
async handleModifyPolicy(act) { async handleModifyPolicy(act) {
const action = act || (this.isEditing ? 'REPLACE' : 'APPEND'); const action =
act ||
(this.isEditing
? this.$options.SECURITY_POLICY_ACTIONS.REPLACE
: this.$options.SECURITY_POLICY_ACTIONS.APPEND);
this.$emit('error', ''); this.$emit('error', '');
this[this.$options.ACTION_FLAGS[action]] = true; this.setLoadingFlag(action, true);
try { try {
const { mergeRequest, policyProject } = await modifyPolicy({ const { mergeRequest, policyProject } = await modifyPolicy({
...@@ -80,7 +81,14 @@ export default { ...@@ -80,7 +81,14 @@ export default {
this.redirectToMergeRequest({ mergeRequest, policyProject }); this.redirectToMergeRequest({ mergeRequest, policyProject });
} catch (e) { } catch (e) {
this.handleError(e); this.handleError(e);
this[this.$options.ACTION_FLAGS[action]] = false; this.setLoadingFlag(action, false);
}
},
setLoadingFlag(action, val) {
if (action === SECURITY_POLICY_ACTIONS.REMOVE) {
this.isRemovingPolicy = val;
} else {
this.isCreatingMR = val;
} }
}, },
redirectToMergeRequest({ mergeRequest, policyProject }) { redirectToMergeRequest({ mergeRequest, policyProject }) {
...@@ -110,7 +118,7 @@ export default { ...@@ -110,7 +118,7 @@ export default {
:is-updating-policy="isCreatingMR" :is-updating-policy="isCreatingMR"
:policy-name="policy.name" :policy-name="policy.name"
:yaml-editor-value="yamlEditorValue" :yaml-editor-value="yamlEditorValue"
@remove-policy="handleModifyPolicy('REMOVE')" @remove-policy="handleModifyPolicy($options.SECURITY_POLICY_ACTIONS.REMOVE)"
@save-policy="handleModifyPolicy()" @save-policy="handleModifyPolicy()"
@update-yaml="updateYaml" @update-yaml="updateYaml"
> >
......
...@@ -4,6 +4,7 @@ import { ...@@ -4,6 +4,7 @@ import {
DEFAULT_SCAN_EXECUTION_POLICY, DEFAULT_SCAN_EXECUTION_POLICY,
modifyPolicy, modifyPolicy,
} from 'ee/threat_monitoring/components/policy_editor/scan_execution_policy/lib'; } from 'ee/threat_monitoring/components/policy_editor/scan_execution_policy/lib';
import { SECURITY_POLICY_ACTIONS } from 'ee/threat_monitoring/components/policy_editor/scan_execution_policy/lib/constants';
import ScanExecutionPolicyEditor from 'ee/threat_monitoring/components/policy_editor/scan_execution_policy/scan_execution_policy_editor.vue'; import ScanExecutionPolicyEditor from 'ee/threat_monitoring/components/policy_editor/scan_execution_policy/scan_execution_policy_editor.vue';
import { DEFAULT_ASSIGNED_POLICY_PROJECT } from 'ee/threat_monitoring/constants'; import { DEFAULT_ASSIGNED_POLICY_PROJECT } from 'ee/threat_monitoring/constants';
import { visitUrl } from '~/lib/utils/url_utility'; import { visitUrl } from '~/lib/utils/url_utility';
...@@ -62,10 +63,10 @@ describe('ScanExecutionPolicyEditor', () => { ...@@ -62,10 +63,10 @@ describe('ScanExecutionPolicyEditor', () => {
}); });
it.each` it.each`
status | action | event | factoryFn status | action | event | factoryFn
${'to save a new policy'} | ${'APPEND'} | ${'save-policy'} | ${factory} ${'to save a new policy'} | ${SECURITY_POLICY_ACTIONS.APPEND} | ${'save-policy'} | ${factory}
${'to update an existing policy'} | ${'REPLACE'} | ${'save-policy'} | ${() => factory({ propsData: { existingPolicy: { manifest: DEFAULT_SCAN_EXECUTION_POLICY } } })} ${'to update an existing policy'} | ${SECURITY_POLICY_ACTIONS.REPLACE} | ${'save-policy'} | ${() => factory({ propsData: { existingPolicy: { manifest: DEFAULT_SCAN_EXECUTION_POLICY } } })}
${'to delete an existing policy'} | ${'REMOVE'} | ${'remove-policy'} | ${() => factory({ propsData: { existingPolicy: { manifest: DEFAULT_SCAN_EXECUTION_POLICY } } })} ${'to delete an existing policy'} | ${SECURITY_POLICY_ACTIONS.REMOVE} | ${'remove-policy'} | ${() => factory({ propsData: { existingPolicy: { manifest: DEFAULT_SCAN_EXECUTION_POLICY } } })}
`( `(
'navigates to the new merge request when "modifyPolicy" is emitted $status', 'navigates to the new merge request when "modifyPolicy" is emitted $status',
async ({ action, event, factoryFn }) => { async ({ action, event, factoryFn }) => {
......
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