Commit bb319f74 authored by Fernando's avatar Fernando

Rework prepopulated modal rendering logic

* Instead of flag, use default name
* Rework feature flags to not use computed prop
parent 032e5a88
...@@ -25,30 +25,15 @@ export default { ...@@ -25,30 +25,15 @@ export default {
}, },
computed: { computed: {
...mapState('createModal', { ...mapState('createModal', {
rule(state) { rule: 'data',
// TODO: Remove feature flag in https://gitlab.com/gitlab-org/gitlab/-/issues/235114
if (this.isApprovalSuggestionsEnabled) {
/*
* rule-form component expects undefined if we pre-populate the form input,
* otherwise populate with existing rule
*/
return state.data?.initRuleField ? undefined : state.data;
}
return state.data;
},
originalData: 'data',
}), }),
// TODO: Remove feature flag in https://gitlab.com/gitlab-org/gitlab/-/issues/235114
isApprovalSuggestionsEnabled() {
return Boolean(this.glFeatures.approvalSuggestions);
},
initRuleFieldName() {
return this.originalData?.initRuleField && this.originalData?.name
? this.originalData.name
: '';
},
title() { title() {
return this.rule ? __('Update approval rule') : __('Add approval rule'); return this.rule && !this.rule.defaultRuleName
? __('Update approval rule')
: __('Add approval rule');
},
defaultRuleName() {
return this.rule && this.rule.defaultRuleName;
}, },
}, },
methods: { methods: {
...@@ -72,11 +57,11 @@ export default { ...@@ -72,11 +57,11 @@ export default {
> >
<!-- TODO: Remove feature flag in https://gitlab.com/gitlab-org/gitlab/-/issues/235114 --> <!-- TODO: Remove feature flag in https://gitlab.com/gitlab-org/gitlab/-/issues/235114 -->
<rule-form <rule-form
v-if="isApprovalSuggestionsEnabled" v-if="glFeatures.approvalSuggestions"
ref="form" ref="form"
:init-rule="rule" :init-rule="rule"
:is-mr-edit="isMrEdit" :is-mr-edit="isMrEdit"
:init-rule-field-name="initRuleFieldName" :default-rule-name="defaultRuleName"
/> />
<rule-form v-else ref="form" :init-rule="rule" :is-mr-edit="isMrEdit" /> <rule-form v-else ref="form" :init-rule="rule" :is-mr-edit="isMrEdit" />
</gl-modal-vuex> </gl-modal-vuex>
......
...@@ -2,7 +2,12 @@ ...@@ -2,7 +2,12 @@
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin'; import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
import { mapState, mapActions } from 'vuex'; import { mapState, mapActions } from 'vuex';
import { s__, n__, sprintf } from '~/locale'; import { s__, n__, sprintf } from '~/locale';
import { RULE_TYPE_ANY_APPROVER, RULE_TYPE_REGULAR , LICENSE_CHECK_NAME, VULNERABILITY_CHECK_NAME } from '../../constants'; import {
RULE_TYPE_ANY_APPROVER,
RULE_TYPE_REGULAR,
LICENSE_CHECK_NAME,
VULNERABILITY_CHECK_NAME,
} from '../../constants';
import UserAvatarList from '~/vue_shared/components/user_avatar/user_avatar_list.vue'; import UserAvatarList from '~/vue_shared/components/user_avatar/user_avatar_list.vue';
import Rules from '../rules.vue'; import Rules from '../rules.vue';
...@@ -88,10 +93,6 @@ export default { ...@@ -88,10 +93,6 @@ export default {
}, },
]; ];
}, },
// TODO: Remove feature flag in https://gitlab.com/gitlab-org/gitlab/-/issues/235114
isApprovalSuggestionsEnabled() {
return Boolean(this.glFeatures.approvalSuggestions);
},
}, },
watch: { watch: {
rules: { rules: {
...@@ -108,7 +109,7 @@ export default { ...@@ -108,7 +109,7 @@ export default {
}, },
created() { created() {
// TODO: Remove feature flag in https://gitlab.com/gitlab-org/gitlab/-/issues/235114 // TODO: Remove feature flag in https://gitlab.com/gitlab-org/gitlab/-/issues/235114
if (this.isApprovalSuggestionsEnabled) { if (this.glFeatures.approvalSuggestions) {
this.setSecurityConfigurationEndpoint(this.securityConfigurationPath); this.setSecurityConfigurationEndpoint(this.securityConfigurationPath);
this.fetchSecurityConfiguration(); this.fetchSecurityConfiguration();
} }
...@@ -205,7 +206,7 @@ export default { ...@@ -205,7 +206,7 @@ export default {
</template> </template>
<!-- TODO: Remove feature flag in https://gitlab.com/gitlab-org/gitlab/-/issues/235114 --> <!-- TODO: Remove feature flag in https://gitlab.com/gitlab-org/gitlab/-/issues/235114 -->
<template v-if="isApprovalSuggestionsEnabled"> <template v-if="glFeatures.approvalSuggestions">
<unconfigured-security-rule <unconfigured-security-rule
v-for="securityRule in securityRules" v-for="securityRule in securityRules"
:key="securityRule.name" :key="securityRule.name"
...@@ -213,7 +214,7 @@ export default { ...@@ -213,7 +214,7 @@ export default {
:rules="rules" :rules="rules"
:is-loading="isRulesLoading" :is-loading="isRulesLoading"
:match-rule="securityRule" :match-rule="securityRule"
@enable="openCreateModal({ name: securityRule.name, initRuleField: true })" @enable="openCreateModal({ defaultRuleName: securityRule.name })"
/> />
</template> </template>
</template> </template>
......
...@@ -32,7 +32,7 @@ export default { ...@@ -32,7 +32,7 @@ export default {
default: true, default: true,
required: false, required: false,
}, },
initRuleFieldName: { defaultRuleName: {
type: String, type: String,
required: false, required: false,
default: '', default: '',
...@@ -40,10 +40,9 @@ export default { ...@@ -40,10 +40,9 @@ export default {
}, },
data() { data() {
// TODO: Remove feature flag in https://gitlab.com/gitlab-org/gitlab/-/issues/235114 // TODO: Remove feature flag in https://gitlab.com/gitlab-org/gitlab/-/issues/235114
// Computed props not yet initilized can't use isApprovalSuggestionsEnabled
if (this.glFeatures.approvalSuggestions) { if (this.glFeatures.approvalSuggestions) {
return { return {
name: this.initRuleFieldName, name: this.defaultRuleName || '',
approvalsRequired: 1, approvalsRequired: 1,
minApprovalsRequired: 0, minApprovalsRequired: 0,
approvers: [], approvers: [],
...@@ -72,9 +71,9 @@ export default { ...@@ -72,9 +71,9 @@ export default {
}, },
computed: { computed: {
...mapState(['settings']), ...mapState(['settings']),
// TODO: Remove feature flag in https://gitlab.com/gitlab-org/gitlab/-/issues/235114 rule() {
isApprovalSuggestionsEnabled() { // If we are creating a new rule with a suggested approval name
return Boolean(this.glFeatures.approvalSuggestions); return this.defaultRuleName ? null : this.initRule;
}, },
approversByType() { approversByType() {
return groupBy(this.approvers, x => x.type); return groupBy(this.approvers, x => x.type);
...@@ -156,16 +155,16 @@ export default { ...@@ -156,16 +155,16 @@ export default {
); );
}, },
isPersisted() { isPersisted() {
return this.initRule && this.initRule.id; return this.rule && this.rule.id;
}, },
isNameVisible() { isNameVisible() {
return !this.settings.lockedApprovalsRuleName; return !this.settings.lockedApprovalsRuleName;
}, },
isNameDisabled() { isNameDisabled() {
// TODO: Remove feature flag in https://gitlab.com/gitlab-org/gitlab/-/issues/235114 // TODO: Remove feature flag in https://gitlab.com/gitlab-org/gitlab/-/issues/235114
if (this.isApprovalSuggestionsEnabled) { if (this.glFeatures.approvalSuggestions) {
return ( return (
Boolean(this.isPersisted || this.initRuleFieldName) && READONLY_NAMES.includes(this.name) Boolean(this.isPersisted || this.defaultRuleName) && READONLY_NAMES.includes(this.name)
); );
} }
return this.isPersisted && READONLY_NAMES.includes(this.name); return this.isPersisted && READONLY_NAMES.includes(this.name);
...@@ -175,7 +174,7 @@ export default { ...@@ -175,7 +174,7 @@ export default {
}, },
submissionData() { submissionData() {
return { return {
id: this.initRule && this.initRule.id, id: this.rule && this.rule.id,
name: this.settings.lockedApprovalsRuleName || this.name || DEFAULT_NAME, name: this.settings.lockedApprovalsRuleName || this.name || DEFAULT_NAME,
approvalsRequired: this.approvalsRequired, approvalsRequired: this.approvalsRequired,
users: this.userIds, users: this.userIds,
...@@ -258,7 +257,7 @@ export default { ...@@ -258,7 +257,7 @@ export default {
* Also delete the rule if necessary. * Also delete the rule if necessary.
*/ */
submitEmptySingleRule() { submitEmptySingleRule() {
const id = this.initRule && this.initRule.id; const id = this.rule && this.rule.id;
return Promise.all([this.submitFallback(), id ? this.deleteRule(id) : Promise.resolve()]); return Promise.all([this.submitFallback(), id ? this.deleteRule(id) : Promise.resolve()]);
}, },
...@@ -268,27 +267,27 @@ export default { ...@@ -268,27 +267,27 @@ export default {
return this.isValid; return this.isValid;
}, },
getInitialData() { getInitialData() {
if (!this.initRule) { if (!this.rule) {
return {}; return {};
} }
if (this.initRule.isFallback) { if (this.rule.isFallback) {
return { return {
approvalsRequired: this.initRule.approvalsRequired, approvalsRequired: this.rule.approvalsRequired,
isFallback: this.initRule.isFallback, isFallback: this.rule.isFallback,
}; };
} }
const { containsHiddenGroups = false, removeHiddenGroups = false } = this.initRule; const { containsHiddenGroups = false, removeHiddenGroups = false } = this.rule;
const users = this.initRule.users.map(x => ({ ...x, type: TYPE_USER })); const users = this.rule.users.map(x => ({ ...x, type: TYPE_USER }));
const groups = this.initRule.groups.map(x => ({ ...x, type: TYPE_GROUP })); const groups = this.rule.groups.map(x => ({ ...x, type: TYPE_GROUP }));
const branches = this.initRule.protectedBranches?.map(x => x.id) || []; const branches = this.rule.protectedBranches?.map(x => x.id) || [];
return { return {
name: this.initRule.name || '', name: this.rule.name || '',
approvalsRequired: this.initRule.approvalsRequired || 0, approvalsRequired: this.rule.approvalsRequired || 0,
minApprovalsRequired: this.initRule.minApprovalsRequired || 0, minApprovalsRequired: this.rule.minApprovalsRequired || 0,
containsHiddenGroups, containsHiddenGroups,
approvers: groups approvers: groups
.concat(users) .concat(users)
...@@ -345,7 +344,7 @@ export default { ...@@ -345,7 +344,7 @@ export default {
v-model="branchesToAdd" v-model="branchesToAdd"
:project-id="settings.projectId" :project-id="settings.projectId"
:is-invalid="!!validation.branches" :is-invalid="!!validation.branches"
:init-rule="initRule" :init-rule="rule"
/> />
<div class="invalid-feedback">{{ validation.branches }}</div> <div class="invalid-feedback">{{ validation.branches }}</div>
</div> </div>
......
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