debian-mirror-gitlab/app/assets/javascripts/feature_flags/store/helpers.js

69 lines
2 KiB
JavaScript
Raw Normal View History

2021-09-30 23:02:18 +05:30
import { ROLLOUT_STRATEGY_GITLAB_USER_LIST, NEW_VERSION_FLAG } from '../constants';
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
const mapStrategyScopesToRails = (scopes) =>
2021-01-03 14:25:43 +05:30
scopes.length === 0
? [{ environment_scope: '*' }]
2021-03-08 18:12:59 +05:30
: scopes.map((s) => ({
2021-01-03 14:25:43 +05:30
id: s.id,
_destroy: s.shouldBeDestroyed,
environment_scope: s.environmentScope,
}));
2021-03-08 18:12:59 +05:30
const mapStrategyScopesToView = (scopes) =>
scopes.map((s) => ({
2021-01-03 14:25:43 +05:30
id: s.id,
// eslint-disable-next-line no-underscore-dangle
shouldBeDestroyed: Boolean(s._destroy),
environmentScope: s.environment_scope,
}));
2021-03-08 18:12:59 +05:30
const mapStrategiesParametersToViewModel = (params) => {
2021-01-03 14:25:43 +05:30
if (params.userIds) {
return { ...params, userIds: params.userIds.split(',').join(', ') };
}
return params;
};
2021-03-08 18:12:59 +05:30
export const mapStrategiesToViewModel = (strategiesFromRails) =>
(strategiesFromRails || []).map((s) => ({
2021-01-03 14:25:43 +05:30
id: s.id,
name: s.name,
parameters: mapStrategiesParametersToViewModel(s.parameters),
2021-01-29 00:20:46 +05:30
userList: s.user_list,
2021-01-03 14:25:43 +05:30
// eslint-disable-next-line no-underscore-dangle
shouldBeDestroyed: Boolean(s._destroy),
scopes: mapStrategyScopesToView(s.scopes),
}));
2021-03-08 18:12:59 +05:30
const mapStrategiesParametersToRails = (params) => {
2021-01-03 14:25:43 +05:30
if (params.userIds) {
return { ...params, userIds: params.userIds.replace(/\s*,\s*/g, ',') };
}
return params;
};
2021-03-08 18:12:59 +05:30
const mapStrategyToRails = (strategy) => {
2021-01-03 14:25:43 +05:30
const mappedStrategy = {
id: strategy.id,
name: strategy.name,
_destroy: strategy.shouldBeDestroyed,
scopes_attributes: mapStrategyScopesToRails(strategy.scopes || []),
parameters: mapStrategiesParametersToRails(strategy.parameters),
};
if (strategy.name === ROLLOUT_STRATEGY_GITLAB_USER_LIST) {
2021-01-29 00:20:46 +05:30
mappedStrategy.user_list_id = strategy.userList.id;
2021-01-03 14:25:43 +05:30
}
return mappedStrategy;
};
2021-03-08 18:12:59 +05:30
export const mapStrategiesToRails = (params) => ({
2021-01-03 14:25:43 +05:30
operations_feature_flag: {
name: params.name,
description: params.description,
active: params.active,
strategies_attributes: (params.strategies || []).map(mapStrategyToRails),
2021-09-30 23:02:18 +05:30
version: NEW_VERSION_FLAG,
2021-01-03 14:25:43 +05:30
},
});