Commit 038c2297 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch 'new-feature-flags-store' into 'master'

Align Vuex Store to New Version Feature Flags

See merge request gitlab-org/gitlab!28048
parents b4bbae54 d5f9f7a6
...@@ -14,3 +14,6 @@ export const INTERNAL_ID_PREFIX = 'internal_'; ...@@ -14,3 +14,6 @@ export const INTERNAL_ID_PREFIX = 'internal_';
export const fetchPercentageParams = property(['parameters', 'percentage']); export const fetchPercentageParams = property(['parameters', 'percentage']);
export const fetchUserIdParams = property(['parameters', 'userIds']); export const fetchUserIdParams = property(['parameters', 'userIds']);
export const NEW_VERSION_FLAG = 'new_version_flag';
export const LEGACY_FLAG = 'legacy_flag';
...@@ -8,6 +8,7 @@ import { ...@@ -8,6 +8,7 @@ import {
PERCENT_ROLLOUT_GROUP_ID, PERCENT_ROLLOUT_GROUP_ID,
fetchPercentageParams, fetchPercentageParams,
fetchUserIdParams, fetchUserIdParams,
LEGACY_FLAG,
} from '../../constants'; } from '../../constants';
/** /**
...@@ -104,6 +105,7 @@ export const mapFromScopesViewModel = params => { ...@@ -104,6 +105,7 @@ export const mapFromScopesViewModel = params => {
description: params.description, description: params.description,
active: params.active, active: params.active,
scopes_attributes: scopes, scopes_attributes: scopes,
version: LEGACY_FLAG,
}, },
}; };
...@@ -141,3 +143,45 @@ export const createNewEnvironmentScope = (overrides = {}, featureFlagPermissions ...@@ -141,3 +143,45 @@ export const createNewEnvironmentScope = (overrides = {}, featureFlagPermissions
return newScope; return newScope;
}; };
const mapStrategyScopesToRails = scopes =>
scopes.length === 0
? [{ environment_scope: '*' }]
: scopes.map(s => ({
id: s.id,
_destroy: s.shouldBeDestroyed,
environment_scope: s.environmentScope,
}));
const mapStrategyScopesToView = scopes =>
scopes.map(s => ({
id: s.id,
// eslint-disable-next-line no-underscore-dangle
shouldBeDestroyed: Boolean(s._destroy),
environmentScope: s.environment_scope,
}));
export const mapStrategiesToViewModel = strategiesFromRails =>
(strategiesFromRails || []).map(s => ({
id: s.id,
name: s.name,
parameters: s.parameters,
// eslint-disable-next-line no-underscore-dangle
shouldBeDestroyed: Boolean(s._destroy),
scopes: mapStrategyScopesToView(s.scopes),
}));
export const mapStrategiesToRails = params => ({
operations_feature_flag: {
name: params.name,
description: params.description,
version: params.version,
strategies_attributes: (params.strategies || []).map(s => ({
id: s.id,
name: s.name,
parameters: s.parameters,
_destroy: s.shouldBeDestroyed,
scopes: mapStrategyScopesToRails(s.scopes || []),
})),
},
});
import * as types from './mutation_types'; import * as types from './mutation_types';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
import { visitUrl } from '~/lib/utils/url_utility'; import { visitUrl } from '~/lib/utils/url_utility';
import { mapFromScopesViewModel } from '../helpers'; import { NEW_VERSION_FLAG } from '../../../constants';
import { mapFromScopesViewModel, mapStrategiesToRails } from '../helpers';
/** /**
* Commits mutation to set the main endpoint * Commits mutation to set the main endpoint
...@@ -29,8 +30,13 @@ export const setPath = ({ commit }, path) => commit(types.SET_PATH, path); ...@@ -29,8 +30,13 @@ export const setPath = ({ commit }, path) => commit(types.SET_PATH, path);
export const createFeatureFlag = ({ state, dispatch }, params) => { export const createFeatureFlag = ({ state, dispatch }, params) => {
dispatch('requestCreateFeatureFlag'); dispatch('requestCreateFeatureFlag');
axios return axios
.post(state.endpoint, mapFromScopesViewModel(params)) .post(
state.endpoint,
params.version === NEW_VERSION_FLAG
? mapStrategiesToRails(params)
: mapFromScopesViewModel(params),
)
.then(() => { .then(() => {
dispatch('receiveCreateFeatureFlagSuccess'); dispatch('receiveCreateFeatureFlagSuccess');
visitUrl(state.path); visitUrl(state.path);
......
...@@ -3,6 +3,8 @@ import { ...@@ -3,6 +3,8 @@ import {
mapToScopesViewModel, mapToScopesViewModel,
mapFromScopesViewModel, mapFromScopesViewModel,
createNewEnvironmentScope, createNewEnvironmentScope,
mapStrategiesToViewModel,
mapStrategiesToRails,
} from 'ee/feature_flags/store/modules/helpers'; } from 'ee/feature_flags/store/modules/helpers';
import { import {
ROLLOUT_STRATEGY_ALL_USERS, ROLLOUT_STRATEGY_ALL_USERS,
...@@ -11,6 +13,8 @@ import { ...@@ -11,6 +13,8 @@ import {
PERCENT_ROLLOUT_GROUP_ID, PERCENT_ROLLOUT_GROUP_ID,
INTERNAL_ID_PREFIX, INTERNAL_ID_PREFIX,
DEFAULT_PERCENT_ROLLOUT, DEFAULT_PERCENT_ROLLOUT,
LEGACY_FLAG,
NEW_VERSION_FLAG,
} from 'ee/feature_flags/constants'; } from 'ee/feature_flags/constants';
describe('feature flags helpers spec', () => { describe('feature flags helpers spec', () => {
...@@ -174,6 +178,7 @@ describe('feature flags helpers spec', () => { ...@@ -174,6 +178,7 @@ describe('feature flags helpers spec', () => {
name: 'name', name: 'name',
description: 'description', description: 'description',
active: true, active: true,
version: LEGACY_FLAG,
scopes_attributes: [ scopes_attributes: [
{ {
id: 4, id: 4,
...@@ -233,21 +238,11 @@ describe('feature flags helpers spec', () => { ...@@ -233,21 +238,11 @@ describe('feature flags helpers spec', () => {
expect(actualScopes).toEqual([]); expect(actualScopes).toEqual([]);
}); });
describe('with user IDs per environment', () => { describe('with user IDs per environment', () => {
let oldGon;
beforeEach(() => {
oldGon = window.gon;
window.gon = { features: { featureFlagsUsersPerEnvironment: true } };
});
afterEach(() => {
window.gon = oldGon;
});
it('sets the user IDs as a comma separated string', () => { it('sets the user IDs as a comma separated string', () => {
const input = { const input = {
name: 'name', name: 'name',
description: 'description', description: 'description',
active: true,
scopes: [ scopes: [
{ {
id: 4, id: 4,
...@@ -268,6 +263,8 @@ describe('feature flags helpers spec', () => { ...@@ -268,6 +263,8 @@ describe('feature flags helpers spec', () => {
operations_feature_flag: { operations_feature_flag: {
name: 'name', name: 'name',
description: 'description', description: 'description',
version: LEGACY_FLAG,
active: true,
scopes_attributes: [ scopes_attributes: [
{ {
id: 4, id: 4,
...@@ -348,4 +345,120 @@ describe('feature flags helpers spec', () => { ...@@ -348,4 +345,120 @@ describe('feature flags helpers spec', () => {
); );
}); });
}); });
describe('mapStrategiesToViewModel', () => {
it('should map rails casing to view model casing', () => {
expect(
mapStrategiesToViewModel([
{
id: '1',
name: 'default',
parameters: {},
scopes: [
{
environment_scope: '*',
id: '1',
},
],
},
]),
).toEqual([
{
id: '1',
name: 'default',
parameters: {},
shouldBeDestroyed: false,
scopes: [
{
shouldBeDestroyed: false,
environmentScope: '*',
id: '1',
},
],
},
]);
});
});
describe('mapStrategiesToRails', () => {
it('should map rails casing to view model casing', () => {
expect(
mapStrategiesToRails({
name: 'test',
description: 'test description',
version: NEW_VERSION_FLAG,
strategies: [
{
id: '1',
name: 'default',
parameters: {},
shouldBeDestroyed: true,
scopes: [
{
environmentScope: '*',
id: '1',
shouldBeDestroyed: true,
},
],
},
],
}),
).toEqual({
operations_feature_flag: {
name: 'test',
description: 'test description',
version: NEW_VERSION_FLAG,
strategies_attributes: [
{
id: '1',
name: 'default',
parameters: {},
_destroy: true,
scopes: [
{
environment_scope: '*',
id: '1',
_destroy: true,
},
],
},
],
},
});
});
it('should insert a default * scope if there are none', () => {
expect(
mapStrategiesToRails({
name: 'test',
description: 'test description',
version: NEW_VERSION_FLAG,
strategies: [
{
id: '1',
name: 'default',
parameters: {},
scopes: [],
},
],
}),
).toEqual({
operations_feature_flag: {
name: 'test',
description: 'test description',
version: NEW_VERSION_FLAG,
strategies_attributes: [
{
id: '1',
name: 'default',
parameters: {},
scopes: [
{
environment_scope: '*',
},
],
},
],
},
});
});
});
}); });
...@@ -14,8 +14,13 @@ import { TEST_HOST } from 'spec/test_constants'; ...@@ -14,8 +14,13 @@ import { TEST_HOST } from 'spec/test_constants';
import { import {
ROLLOUT_STRATEGY_ALL_USERS, ROLLOUT_STRATEGY_ALL_USERS,
ROLLOUT_STRATEGY_PERCENT_ROLLOUT, ROLLOUT_STRATEGY_PERCENT_ROLLOUT,
LEGACY_FLAG,
NEW_VERSION_FLAG,
} from 'ee/feature_flags/constants'; } from 'ee/feature_flags/constants';
import { mapFromScopesViewModel } from 'ee/feature_flags/store/modules/helpers'; import {
mapFromScopesViewModel,
mapStrategiesToRails,
} from 'ee/feature_flags/store/modules/helpers';
import axios from '~/lib/utils/axios_utils'; import axios from '~/lib/utils/axios_utils';
jest.mock('~/lib/utils/url_utility'); jest.mock('~/lib/utils/url_utility');
...@@ -60,6 +65,7 @@ describe('Feature flags New Module Actions', () => { ...@@ -60,6 +65,7 @@ describe('Feature flags New Module Actions', () => {
name: 'name', name: 'name',
description: 'description', description: 'description',
active: true, active: true,
version: LEGACY_FLAG,
scopes: [ scopes: [
{ {
id: 1, id: 1,
...@@ -105,6 +111,43 @@ describe('Feature flags New Module Actions', () => { ...@@ -105,6 +111,43 @@ describe('Feature flags New Module Actions', () => {
done, done,
); );
}); });
it('sends strategies for new style feature flags', done => {
const newVersionFlagParams = {
name: 'name',
description: 'description',
active: true,
version: NEW_VERSION_FLAG,
strategies: [
{
name: ROLLOUT_STRATEGY_ALL_USERS,
parameters: {},
id: 1,
scopes: [{ id: 1, environmentScope: 'environmentScope', shouldBeDestroyed: false }],
shouldBeDestroyed: false,
},
],
};
mock
.onPost(`${TEST_HOST}/endpoint.json`, mapStrategiesToRails(newVersionFlagParams))
.replyOnce(200);
testAction(
createFeatureFlag,
newVersionFlagParams,
mockedState,
[],
[
{
type: 'requestCreateFeatureFlag',
},
{
type: 'receiveCreateFeatureFlagSuccess',
},
],
done,
);
});
}); });
describe('error', () => { describe('error', () => {
......
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