From ec785719c49eb7a49921972a54034f94c24d0bd9 Mon Sep 17 00:00:00 2001
From: Artur Fedorov <afedorov@gitlab.com>
Date: Mon, 10 Mar 2025 17:53:48 +0000
Subject: [PATCH] Add policy type property to editors

Wrapper component already utilises
policy type property. It is passed
to child editor component

Changelog: changed
EE: true
---
 .../policy_editor/editor_wrapper.vue           |  7 +++++++
 .../pipeline_execution/editor_component.vue    | 14 ++++++++------
 .../scan_execution/editor_component.vue        | 18 +++++++++---------
 .../scan_result/editor_component.vue           | 14 ++++++++------
 .../editor_component.vue                       | 14 ++++++++------
 .../policy_editor/editor_wrapper_spec.js       | 13 +++++++------
 .../editor_component_spec.js                   |  1 +
 .../scan_execution/editor_component_spec.js    |  1 +
 .../scan_result/editor_component_spec.js       |  1 +
 .../editor_component_spec.js                   |  1 +
 10 files changed, 51 insertions(+), 33 deletions(-)

diff --git a/ee/app/assets/javascripts/security_orchestration/components/policy_editor/editor_wrapper.vue b/ee/app/assets/javascripts/security_orchestration/components/policy_editor/editor_wrapper.vue
index e9f878b10942f..f497a7baa0bef 100644
--- a/ee/app/assets/javascripts/security_orchestration/components/policy_editor/editor_wrapper.vue
+++ b/ee/app/assets/javascripts/security_orchestration/components/policy_editor/editor_wrapper.vue
@@ -83,6 +83,12 @@ export default {
     };
   },
   computed: {
+    policyUrlParameter() {
+      const selectedPolicy = Object.values(POLICY_TYPE_COMPONENT_OPTIONS).find(
+        ({ value }) => value === this.selectedPolicyType,
+      );
+      return selectedPolicy?.urlParameter || '';
+    },
     isEditing() {
       return Boolean(this.existingPolicy);
     },
@@ -221,6 +227,7 @@ export default {
       :is-creating="isCreating"
       :is-deleting="isDeleting"
       :is-editing="isEditing"
+      :selected-policy-type="policyUrlParameter"
       @save="handleSave"
       @error="setError($event)"
     />
diff --git a/ee/app/assets/javascripts/security_orchestration/components/policy_editor/pipeline_execution/editor_component.vue b/ee/app/assets/javascripts/security_orchestration/components/policy_editor/pipeline_execution/editor_component.vue
index 07d2486b321b2..627d9f872d288 100644
--- a/ee/app/assets/javascripts/security_orchestration/components/policy_editor/pipeline_execution/editor_component.vue
+++ b/ee/app/assets/javascripts/security_orchestration/components/policy_editor/pipeline_execution/editor_component.vue
@@ -6,7 +6,6 @@ import { setUrlFragment, queryToObject } from '~/lib/utils/url_utility';
 import { s__, __ } from '~/locale';
 import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
 import { DEFAULT_DEBOUNCE_AND_THROTTLE_MS } from '~/lib/utils/constants';
-import { POLICY_TYPE_COMPONENT_OPTIONS } from 'ee/security_orchestration/components/constants';
 import { extractPolicyContent } from 'ee/security_orchestration/components/utils';
 import {
   ACTION_SECTION_DISABLE_ERROR,
@@ -33,7 +32,6 @@ export default {
   ACTION: 'actions',
   EDITOR_MODE_RULE,
   EDITOR_MODE_YAML,
-  POLICY_TYPE: POLICY_TYPE_COMPONENT_OPTIONS.pipelineExecution.urlParameter,
   SECURITY_POLICY_ACTIONS,
   i18n: {
     ACTION_SECTION_DISABLE_ERROR,
@@ -81,12 +79,16 @@ export default {
       type: Boolean,
       required: true,
     },
+    selectedPolicyType: {
+      type: String,
+      required: true,
+    },
   },
   data() {
     let yamlEditorValue;
 
     if (this.existingPolicy) {
-      yamlEditorValue = policyToYaml(this.existingPolicy, this.$options.POLICY_TYPE);
+      yamlEditorValue = policyToYaml(this.existingPolicy, this.selectedPolicyType);
     } else {
       yamlEditorValue = getInitialPolicy(
         DEFAULT_PIPELINE_EXECUTION_POLICY,
@@ -146,7 +148,7 @@ export default {
   },
   methods: {
     areManifestsEqual(manifest) {
-      const policyManifest = policyToYaml(this.policy, this.$options.POLICY_TYPE);
+      const policyManifest = policyToYaml(this.policy, this.selectedPolicyType);
       return policyManifest === manifest && this.hasNewSplitView;
     },
     changeEditorMode(mode) {
@@ -164,7 +166,7 @@ export default {
        */
       const policy = extractPolicyContent({
         manifest: this.yamlEditorValue,
-        type: this.$options.POLICY_TYPE,
+        type: this.selectedPolicyType,
         withType: true,
       });
 
@@ -216,7 +218,7 @@ export default {
       this.policy = policy;
     },
     updateYamlEditorValue(policy) {
-      this.yamlEditorValue = policyToYaml(policy, this.$options.POLICY_TYPE);
+      this.yamlEditorValue = policyToYaml(policy, this.selectedPolicyType);
     },
   },
 };
diff --git a/ee/app/assets/javascripts/security_orchestration/components/policy_editor/scan_execution/editor_component.vue b/ee/app/assets/javascripts/security_orchestration/components/policy_editor/scan_execution/editor_component.vue
index 29a85101bf3c0..7ec5b547872eb 100644
--- a/ee/app/assets/javascripts/security_orchestration/components/policy_editor/scan_execution/editor_component.vue
+++ b/ee/app/assets/javascripts/security_orchestration/components/policy_editor/scan_execution/editor_component.vue
@@ -11,10 +11,7 @@ import {
   extractPolicyContent,
 } from 'ee/security_orchestration/components/utils';
 import OverloadWarningModal from 'ee/security_orchestration/components/overload_warning_modal.vue';
-import {
-  DEFAULT_SKIP_SI_CONFIGURATION,
-  POLICY_TYPE_COMPONENT_OPTIONS,
-} from 'ee/security_orchestration/components/constants';
+import { DEFAULT_SKIP_SI_CONFIGURATION } from 'ee/security_orchestration/components/constants';
 import {
   policyBodyToYaml,
   policyToYaml,
@@ -52,7 +49,6 @@ export default {
   RULE: 'rules',
   EDITOR_MODE_RULE,
   EDITOR_MODE_YAML,
-  POLICY_TYPE: POLICY_TYPE_COMPONENT_OPTIONS.scanExecution.urlParameter,
   SECURITY_POLICY_ACTIONS,
   i18n: {
     ACTIONS_LABEL,
@@ -139,10 +135,14 @@ export default {
       type: Boolean,
       required: true,
     },
+    selectedPolicyType: {
+      type: String,
+      required: true,
+    },
   },
   data() {
     const yamlEditorValue = this.existingPolicy
-      ? policyToYaml(this.existingPolicy, this.$options.POLICY_TYPE)
+      ? policyToYaml(this.existingPolicy, this.selectedPolicyType)
       : getPolicyYaml({ isGroup: isGroup(this.namespaceType) });
 
     const { policy, parsingError } = createPolicyObject(yamlEditorValue);
@@ -216,7 +216,7 @@ export default {
   },
   methods: {
     areManifestsEqual(manifest) {
-      const policyManifest = policyToYaml(this.policy, this.$options.POLICY_TYPE);
+      const policyManifest = policyToYaml(this.policy, this.selectedPolicyType);
       return policyManifest === manifest && this.hasNewSplitView;
     },
     addAction() {
@@ -292,7 +292,7 @@ export default {
        */
       const policy = extractPolicyContent({
         manifest: this.yamlEditorValue,
-        type: this.$options.POLICY_TYPE,
+        type: this.selectedPolicyType,
         withType: true,
       });
 
@@ -310,7 +310,7 @@ export default {
       this.specificActionSectionError = '';
     },
     updateYamlEditorValue(policy) {
-      this.yamlEditorValue = policyToYaml(policy, this.$options.POLICY_TYPE);
+      this.yamlEditorValue = policyToYaml(policy, this.selectedPolicyType);
     },
   },
 };
diff --git a/ee/app/assets/javascripts/security_orchestration/components/policy_editor/scan_result/editor_component.vue b/ee/app/assets/javascripts/security_orchestration/components/policy_editor/scan_result/editor_component.vue
index 18c46026133c5..f860c82d8bfe6 100644
--- a/ee/app/assets/javascripts/security_orchestration/components/policy_editor/scan_result/editor_component.vue
+++ b/ee/app/assets/javascripts/security_orchestration/components/policy_editor/scan_result/editor_component.vue
@@ -10,7 +10,6 @@ import {
   isProject,
 } from 'ee/security_orchestration/components/utils';
 import getSppLinkedProjectsGroups from 'ee/security_orchestration/graphql/queries/get_spp_linked_projects_groups.graphql';
-import { POLICY_TYPE_COMPONENT_OPTIONS } from 'ee/security_orchestration/components/constants';
 import {
   policyBodyToYaml,
   policyToYaml,
@@ -62,7 +61,6 @@ export default {
   SECURITY_POLICY_ACTIONS,
   EDITOR_MODE_YAML,
   EDITOR_MODE_RULE,
-  POLICY_TYPE: POLICY_TYPE_COMPONENT_OPTIONS.approval.urlParameter,
   i18n: {
     ACTION_SECTION_DISABLE_ERROR,
     ADD_ACTION_LABEL,
@@ -173,6 +171,10 @@ export default {
       type: Boolean,
       required: true,
     },
+    selectedPolicyType: {
+      type: String,
+      required: true,
+    },
   },
   data() {
     const newPolicyYaml = getPolicyYaml({
@@ -180,7 +182,7 @@ export default {
     });
 
     const yamlEditorValue = this.existingPolicy
-      ? policyToYaml(this.existingPolicy, this.$options.POLICY_TYPE)
+      ? policyToYaml(this.existingPolicy, this.selectedPolicyType)
       : newPolicyYaml;
 
     const { policy, parsingError } = createPolicyObject(yamlEditorValue);
@@ -335,7 +337,7 @@ export default {
   },
   methods: {
     areManifestsEqual(manifest) {
-      const policyManifest = policyToYaml(this.policy, this.$options.POLICY_TYPE);
+      const policyManifest = policyToYaml(this.policy, this.selectedPolicyType);
       return policyManifest === manifest && this.hasNewSplitView;
     },
     ruleHasBranchesProperty(rule) {
@@ -448,7 +450,7 @@ export default {
        */
       const policy = extractPolicyContent({
         manifest: this.yamlEditorValue,
-        type: this.$options.POLICY_TYPE,
+        type: this.selectedPolicyType,
         withType: true,
       });
 
@@ -478,7 +480,7 @@ export default {
       this.policy = policy;
     },
     updateYamlEditorValue(policy) {
-      this.yamlEditorValue = policyToYaml(policy, this.$options.POLICY_TYPE);
+      this.yamlEditorValue = policyToYaml(policy, this.selectedPolicyType);
     },
     async changeEditorMode(mode) {
       this.mode = mode;
diff --git a/ee/app/assets/javascripts/security_orchestration/components/policy_editor/vulnerability_management/editor_component.vue b/ee/app/assets/javascripts/security_orchestration/components/policy_editor/vulnerability_management/editor_component.vue
index 237765ec63335..6f1c7d2f2dd21 100644
--- a/ee/app/assets/javascripts/security_orchestration/components/policy_editor/vulnerability_management/editor_component.vue
+++ b/ee/app/assets/javascripts/security_orchestration/components/policy_editor/vulnerability_management/editor_component.vue
@@ -2,7 +2,6 @@
 import { GlButton, GlTooltipDirective, GlAlert, GlSprintf } from '@gitlab/ui';
 import { s__, sprintf, n__ } from '~/locale';
 import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
-import { POLICY_TYPE_COMPONENT_OPTIONS } from 'ee/security_orchestration/components/constants';
 import { extractPolicyContent } from 'ee/security_orchestration/components/utils';
 import {
   policyBodyToYaml,
@@ -30,7 +29,6 @@ export default {
   SECURITY_POLICY_ACTIONS,
   EDITOR_MODE_RULE,
   EDITOR_MODE_YAML,
-  POLICY_TYPE: POLICY_TYPE_COMPONENT_OPTIONS.vulnerabilityManagement.urlParameter,
   i18n: {
     ACTION_SECTION_DISABLE_ERROR,
     RULES_SECTION_DISABLE_ERROR,
@@ -78,12 +76,16 @@ export default {
       type: Boolean,
       required: true,
     },
+    selectedPolicyType: {
+      type: String,
+      required: true,
+    },
   },
   data() {
     let yamlEditorValue;
 
     if (this.existingPolicy) {
-      yamlEditorValue = policyToYaml(this.existingPolicy, this.$options.POLICY_TYPE);
+      yamlEditorValue = policyToYaml(this.existingPolicy, this.selectedPolicyType);
     } else {
       yamlEditorValue = DEFAULT_VULNERABILITY_MANAGEMENT_POLICY;
     }
@@ -114,7 +116,7 @@ export default {
   },
   methods: {
     areManifestsEqual(manifest) {
-      const policyManifest = policyToYaml(this.policy, this.$options.POLICY_TYPE);
+      const policyManifest = policyToYaml(this.policy, this.selectedPolicyType);
       return policyManifest === manifest && this.hasNewSplitView;
     },
     changeEditorMode(mode) {
@@ -128,7 +130,7 @@ export default {
        */
       const policy = extractPolicyContent({
         manifest: this.yamlEditorValue,
-        type: this.$options.POLICY_TYPE,
+        type: this.selectedPolicyType,
         withType: true,
       });
 
@@ -152,7 +154,7 @@ export default {
       this.policy = policy;
     },
     updateYamlEditorValue(policy) {
-      this.yamlEditorValue = policyToYaml(policy, this.$options.POLICY_TYPE);
+      this.yamlEditorValue = policyToYaml(policy, this.selectedPolicyType);
     },
     addRule() {
       this.policy.rules.push(buildDefaultNoLongerDetectedRule());
diff --git a/ee/spec/frontend/security_orchestration/components/policy_editor/editor_wrapper_spec.js b/ee/spec/frontend/security_orchestration/components/policy_editor/editor_wrapper_spec.js
index f7a2b9cf6cf13..a9a25aef0ecde 100644
--- a/ee/spec/frontend/security_orchestration/components/policy_editor/editor_wrapper_spec.js
+++ b/ee/spec/frontend/security_orchestration/components/policy_editor/editor_wrapper_spec.js
@@ -126,18 +126,19 @@ describe('EditorWrapper component', () => {
       });
 
       it.each`
-        policyTypeId                                                   | findComponent
-        ${POLICY_TYPE_COMPONENT_OPTIONS.pipelineExecution.value}       | ${findPipelineExecutionPolicyEditor}
-        ${POLICY_TYPE_COMPONENT_OPTIONS.scanExecution.value}           | ${findScanExecutionPolicyEditor}
-        ${POLICY_TYPE_COMPONENT_OPTIONS.approval.value}                | ${findScanResultPolicyEditor}
-        ${POLICY_TYPE_COMPONENT_OPTIONS.vulnerabilityManagement.value} | ${findVulnerabilityManagementPolicyEditor}
+        policyTypeId                                                   | findComponent                              | selectedPolicyType
+        ${POLICY_TYPE_COMPONENT_OPTIONS.pipelineExecution.value}       | ${findPipelineExecutionPolicyEditor}       | ${POLICY_TYPE_COMPONENT_OPTIONS.pipelineExecution.urlParameter}
+        ${POLICY_TYPE_COMPONENT_OPTIONS.scanExecution.value}           | ${findScanExecutionPolicyEditor}           | ${POLICY_TYPE_COMPONENT_OPTIONS.scanExecution.urlParameter}
+        ${POLICY_TYPE_COMPONENT_OPTIONS.approval.value}                | ${findScanResultPolicyEditor}              | ${POLICY_TYPE_COMPONENT_OPTIONS.approval.urlParameter}
+        ${POLICY_TYPE_COMPONENT_OPTIONS.vulnerabilityManagement.value} | ${findVulnerabilityManagementPolicyEditor} | ${POLICY_TYPE_COMPONENT_OPTIONS.vulnerabilityManagement.urlParameter}
       `(
         'renders the policy editor of type $policyType when selected',
-        ({ findComponent, policyTypeId }) => {
+        ({ findComponent, policyTypeId, selectedPolicyType }) => {
           factory({ propsData: { selectedPolicyType: policyTypeId } });
           const component = findComponent();
           expect(component.exists()).toBe(true);
           expect(component.props('isEditing')).toBe(false);
+          expect(component.props('selectedPolicyType')).toBe(selectedPolicyType);
         },
       );
     });
diff --git a/ee/spec/frontend/security_orchestration/components/policy_editor/pipeline_execution/editor_component_spec.js b/ee/spec/frontend/security_orchestration/components/policy_editor/pipeline_execution/editor_component_spec.js
index e7d75915feedb..be000ce7e80e5 100644
--- a/ee/spec/frontend/security_orchestration/components/policy_editor/pipeline_execution/editor_component_spec.js
+++ b/ee/spec/frontend/security_orchestration/components/policy_editor/pipeline_execution/editor_component_spec.js
@@ -59,6 +59,7 @@ describe('EditorComponent', () => {
     wrapper = shallowMountExtended(EditorComponent, {
       propsData: {
         assignedPolicyProject: DEFAULT_ASSIGNED_POLICY_PROJECT,
+        selectedPolicyType: POLICY_TYPE_COMPONENT_OPTIONS.pipelineExecution.urlParameter,
         isCreating: false,
         isDeleting: false,
         isEditing: false,
diff --git a/ee/spec/frontend/security_orchestration/components/policy_editor/scan_execution/editor_component_spec.js b/ee/spec/frontend/security_orchestration/components/policy_editor/scan_execution/editor_component_spec.js
index ea2a666719142..cbc3d2aab370c 100644
--- a/ee/spec/frontend/security_orchestration/components/policy_editor/scan_execution/editor_component_spec.js
+++ b/ee/spec/frontend/security_orchestration/components/policy_editor/scan_execution/editor_component_spec.js
@@ -86,6 +86,7 @@ describe('EditorComponent', () => {
       apolloProvider: createMockApolloProvider(handler),
       propsData: {
         assignedPolicyProject: DEFAULT_ASSIGNED_POLICY_PROJECT,
+        selectedPolicyType: POLICY_TYPE_COMPONENT_OPTIONS.scanExecution.urlParameter,
         errorSources: [],
         isCreating: false,
         isDeleting: false,
diff --git a/ee/spec/frontend/security_orchestration/components/policy_editor/scan_result/editor_component_spec.js b/ee/spec/frontend/security_orchestration/components/policy_editor/scan_result/editor_component_spec.js
index a1d3efb729f12..9fdabe4153f1a 100644
--- a/ee/spec/frontend/security_orchestration/components/policy_editor/scan_result/editor_component_spec.js
+++ b/ee/spec/frontend/security_orchestration/components/policy_editor/scan_result/editor_component_spec.js
@@ -108,6 +108,7 @@ describe('EditorComponent', () => {
       propsData: {
         assignedPolicyProject: DEFAULT_ASSIGNED_POLICY_PROJECT,
         errorSources: [],
+        selectedPolicyType: POLICY_TYPE_COMPONENT_OPTIONS.approval.urlParameter,
         isCreating: false,
         isDeleting: false,
         isEditing: false,
diff --git a/ee/spec/frontend/security_orchestration/components/policy_editor/vulnerability_management/editor_component_spec.js b/ee/spec/frontend/security_orchestration/components/policy_editor/vulnerability_management/editor_component_spec.js
index dc711a8b5e516..31cb2df07078f 100644
--- a/ee/spec/frontend/security_orchestration/components/policy_editor/vulnerability_management/editor_component_spec.js
+++ b/ee/spec/frontend/security_orchestration/components/policy_editor/vulnerability_management/editor_component_spec.js
@@ -39,6 +39,7 @@ describe('EditorComponent', () => {
       },
       propsData: {
         assignedPolicyProject: DEFAULT_ASSIGNED_POLICY_PROJECT,
+        selectedPolicyType: POLICY_TYPE_COMPONENT_OPTIONS.vulnerabilityManagement.urlParameter,
         isCreating: false,
         isDeleting: false,
         isEditing: false,
-- 
GitLab