mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	Fix/enable standard environments (#1134)
This commit is contained in:
		
							parent
							
								
									5829ec7b3d
								
							
						
					
					
						commit
						eb8265922d
					
				| @ -72,7 +72,11 @@ Object { | ||||
|     "db": true, | ||||
|     "ttlHours": 48, | ||||
|   }, | ||||
|   "ui": Object {}, | ||||
|   "ui": Object { | ||||
|     "flags": Object { | ||||
|       "E": true, | ||||
|     }, | ||||
|   }, | ||||
|   "versionCheck": Object { | ||||
|     "enable": true, | ||||
|     "url": "https://version.unleash.run", | ||||
|  | ||||
| @ -17,50 +17,3 @@ test('should create default config', async () => { | ||||
| 
 | ||||
|     expect(config).toMatchSnapshot(); | ||||
| }); | ||||
| 
 | ||||
| test('should enabled metricsV2 via options', async () => { | ||||
|     const config = createConfig({ | ||||
|         experimental: { | ||||
|             metricsV2: { enabled: true }, | ||||
|         }, | ||||
|     }); | ||||
| 
 | ||||
|     expect(config.experimental.metricsV2.enabled).toBe(true); | ||||
| }); | ||||
| 
 | ||||
| test('should enabled metricsV2 via env variable', async () => { | ||||
|     process.env.EXP_METRICS_V2 = 'true'; | ||||
|     const config = createConfig({}); | ||||
| 
 | ||||
|     expect(config.experimental.metricsV2.enabled).toBe(true); | ||||
|     delete process.env.EXP_METRICS_V2; | ||||
| }); | ||||
| 
 | ||||
| test('should enabled metricsV2 when environments is enabled via env variable', async () => { | ||||
|     process.env.EXP_ENVIRONMENTS = 'true'; | ||||
|     const config = createConfig({}); | ||||
| 
 | ||||
|     expect(config.experimental.environments.enabled).toBe(true); | ||||
|     expect(config.experimental.metricsV2.enabled).toBe(true); | ||||
|     delete process.env.EXP_ENVIRONMENTS; | ||||
| }); | ||||
| 
 | ||||
| test('should enabled metricsV2 when environments is enabled via options', async () => { | ||||
|     const config = createConfig({ | ||||
|         experimental: { | ||||
|             environments: { enabled: true }, | ||||
|         }, | ||||
|     }); | ||||
| 
 | ||||
|     expect(config.experimental.environments.enabled).toBe(true); | ||||
|     expect(config.experimental.metricsV2.enabled).toBe(true); | ||||
| }); | ||||
| 
 | ||||
| test('should set UI flag when environments is enabled', async () => { | ||||
|     process.env.EXP_ENVIRONMENTS = 'true'; | ||||
|     const config = createConfig({}); | ||||
| 
 | ||||
|     expect(config.experimental.environments.enabled).toBe(true); | ||||
|     expect(config.ui.flags?.E).toBe(true); | ||||
|     delete process.env.EXP_ENVIRONMENTS; | ||||
| }); | ||||
|  | ||||
| @ -56,30 +56,16 @@ function mergeAll<T>(objects: Partial<T>[]): T { | ||||
| function loadExperimental(options: IUnleashOptions): any { | ||||
|     const experimental = options.experimental || {}; | ||||
| 
 | ||||
|     if (safeBoolean(process.env.EXP_ENVIRONMENTS, false)) { | ||||
|         experimental.environments = { enabled: true }; | ||||
|     } | ||||
| 
 | ||||
|     if ( | ||||
|         safeBoolean(process.env.EXP_METRICS_V2, false) || | ||||
|         //@ts-ignore
 | ||||
|         experimental.environments?.enabled | ||||
|     ) { | ||||
|         experimental.metricsV2 = { enabled: true }; | ||||
|     } | ||||
| 
 | ||||
|     return experimental; | ||||
| } | ||||
| 
 | ||||
| function loadUI(options: IUnleashOptions, experimental: any = {}): IUIConfig { | ||||
| function loadUI(options: IUnleashOptions): IUIConfig { | ||||
|     const uiO = options.ui || {}; | ||||
|     const ui: IUIConfig = {}; | ||||
| 
 | ||||
|     if (experimental.environments?.enabled) { | ||||
|         ui.flags = { | ||||
|             E: true, | ||||
|         }; | ||||
|     } | ||||
|     ui.flags = { | ||||
|         E: true, | ||||
|     }; | ||||
|     return mergeAll([uiO, ui]); | ||||
| } | ||||
| 
 | ||||
| @ -254,7 +240,7 @@ export function createConfig(options: IUnleashOptions): IUnleashConfig { | ||||
| 
 | ||||
|     const experimental = loadExperimental(options); | ||||
| 
 | ||||
|     const ui = loadUI(options, experimental); | ||||
|     const ui = loadUI(options); | ||||
| 
 | ||||
|     const email: IEmailOption = mergeAll([defaultEmail, options.email]); | ||||
| 
 | ||||
|  | ||||
| @ -18,8 +18,6 @@ export default class ClientMetricsController extends Controller { | ||||
| 
 | ||||
|     metricsV2: ClientMetricsServiceV2; | ||||
| 
 | ||||
|     newServiceEnabled: boolean = false; | ||||
| 
 | ||||
|     constructor( | ||||
|         { | ||||
|             clientMetricsService, | ||||
| @ -31,11 +29,7 @@ export default class ClientMetricsController extends Controller { | ||||
|         config: IUnleashConfig, | ||||
|     ) { | ||||
|         super(config); | ||||
|         const { experimental, getLogger } = config; | ||||
|         if (experimental && experimental.metricsV2) { | ||||
|             //@ts-ignore
 | ||||
|             this.newServiceEnabled = experimental.metricsV2.enabled; | ||||
|         } | ||||
|         const { getLogger } = config; | ||||
| 
 | ||||
|         this.logger = getLogger('/api/client/metrics'); | ||||
|         this.metrics = clientMetricsService; | ||||
| @ -60,12 +54,8 @@ export default class ClientMetricsController extends Controller { | ||||
|         data.environment = this.resolveEnvironment(user, data); | ||||
|         await this.metrics.registerClientMetrics(data, clientIp); | ||||
| 
 | ||||
|         if (this.newServiceEnabled) { | ||||
|             await this.metricsV2.registerClientMetrics(data, clientIp); | ||||
|         } | ||||
|         await this.metricsV2.registerClientMetrics(data, clientIp); | ||||
| 
 | ||||
|         return res.status(202).end(); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| module.exports = ClientMetricsController; | ||||
|  | ||||
| @ -60,8 +60,6 @@ export default class ProjectService { | ||||
| 
 | ||||
|     private featureToggleService: FeatureToggleService; | ||||
| 
 | ||||
|     private environmentsEnabled: boolean = false; | ||||
| 
 | ||||
|     constructor( | ||||
|         { | ||||
|             projectStore, | ||||
| @ -92,8 +90,6 @@ export default class ProjectService { | ||||
|         this.featureTypeStore = featureTypeStore; | ||||
|         this.featureToggleService = featureToggleService; | ||||
|         this.logger = config.getLogger('services/project-service.js'); | ||||
|         this.environmentsEnabled = | ||||
|             config.experimental.environments?.enabled || false; | ||||
|     } | ||||
| 
 | ||||
|     async getProjects(query?: IProjectQuery): Promise<IProjectWithCount[]> { | ||||
| @ -127,26 +123,19 @@ export default class ProjectService { | ||||
| 
 | ||||
|         await this.store.create(data); | ||||
| 
 | ||||
|         if (this.environmentsEnabled) { | ||||
|             const enabledEnvironments = await this.environmentStore.getAll({ | ||||
|                 enabled: true, | ||||
|             }); | ||||
|         const enabledEnvironments = await this.environmentStore.getAll({ | ||||
|             enabled: true, | ||||
|         }); | ||||
| 
 | ||||
|             // TODO: Only if enabled!
 | ||||
|             await Promise.all( | ||||
|                 enabledEnvironments.map(async (e) => { | ||||
|                     await this.featureEnvironmentStore.connectProject( | ||||
|                         e.name, | ||||
|                         data.id, | ||||
|                     ); | ||||
|                 }), | ||||
|             ); | ||||
|         } else { | ||||
|             await this.featureEnvironmentStore.connectProject( | ||||
|                 'default', | ||||
|                 data.id, | ||||
|             ); | ||||
|         } | ||||
|         // TODO: Only if enabled!
 | ||||
|         await Promise.all( | ||||
|             enabledEnvironments.map(async (e) => { | ||||
|                 await this.featureEnvironmentStore.connectProject( | ||||
|                     e.name, | ||||
|                     data.id, | ||||
|                 ); | ||||
|             }), | ||||
|         ); | ||||
| 
 | ||||
|         await this.accessService.createDefaultProjectRoles(user, data.id); | ||||
| 
 | ||||
|  | ||||
							
								
								
									
										27
									
								
								src/migrations/20211126112551-disable-default-environment.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/migrations/20211126112551-disable-default-environment.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,27 @@ | ||||
| exports.up = function (db, cb) { | ||||
|     db.runSql( | ||||
|         ` | ||||
|         UPDATE environments  | ||||
|         SET enabled = false | ||||
|         WHERE name = 'default' | ||||
|         AND NOT EXISTS (select  * from features); | ||||
| 
 | ||||
|         DELETE FROM project_environments  | ||||
|         WHERE environment_name = 'default' | ||||
|         AND NOT EXISTS (select  * from features); | ||||
| 
 | ||||
|         INSERT INTO project_environments(project_id, environment_name)  | ||||
|         SELECT 'default', 'development' | ||||
|         WHERE NOT EXISTS (select  * from features); | ||||
|          | ||||
|         INSERT INTO project_environments(project_id, environment_name)  | ||||
|         SELECT 'default', 'production' | ||||
|         WHERE NOT EXISTS (select  * from features); | ||||
|       `,
 | ||||
|         cb, | ||||
|     ); | ||||
| }; | ||||
| 
 | ||||
| exports.down = function (db, cb) { | ||||
|     db.runSql('', cb); | ||||
| }; | ||||
| @ -548,40 +548,3 @@ test('A newly created project only gets connected to enabled environments', asyn | ||||
|     expect(connectedEnvs.some((e) => e === enabledEnv)).toBeTruthy(); | ||||
|     expect(connectedEnvs.some((e) => e === disabledEnv)).toBeFalsy(); | ||||
| }); | ||||
| 
 | ||||
| test('A newly created project only gets connected to default environment if experimental flag is disabled', async () => { | ||||
|     const config = createTestConfig({ | ||||
|         getLogger, | ||||
|         // @ts-ignore
 | ||||
|         experimental: { environments: { enabled: false } }, | ||||
|     }); | ||||
|     projectService = new ProjectService( | ||||
|         stores, | ||||
|         //@ts-ignore
 | ||||
|         config, | ||||
|         accessService, | ||||
|         featureToggleService, | ||||
|     ); | ||||
|     const project = { | ||||
|         id: 'environment-test-default', | ||||
|         name: 'New environment project', | ||||
|         description: 'Blah', | ||||
|     }; | ||||
|     const enabledEnv = 'connection_test'; | ||||
|     await db.stores.environmentStore.create({ | ||||
|         name: enabledEnv, | ||||
|         type: 'test', | ||||
|     }); | ||||
|     const disabledEnv = 'do_not_connect'; | ||||
|     await db.stores.environmentStore.create({ | ||||
|         name: disabledEnv, | ||||
|         type: 'test', | ||||
|         enabled: false, | ||||
|     }); | ||||
| 
 | ||||
|     await projectService.createProject(project, user); | ||||
|     const connectedEnvs = | ||||
|         await db.stores.projectStore.getEnvironmentsForProject(project.id); | ||||
|     expect(connectedEnvs).toHaveLength(1); // default, connection_test
 | ||||
|     expect(connectedEnvs[0]).toBe('default'); | ||||
| }); | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user