Skip to content

Commit 18b0221

Browse files
authored
Fixes GitOps route API environment status update (#563)
1 parent 0893762 commit 18b0221

File tree

6 files changed

+117
-35
lines changed

6 files changed

+117
-35
lines changed

npm-shrinkwrap.json

Lines changed: 40 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "switcher-api",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"description": "Feature Flag/Toggle API",
55
"main": "src/start.js",
66
"type": "module",
@@ -41,7 +41,7 @@
4141
"cors": "^2.8.5",
4242
"express": "^5.1.0",
4343
"express-basic-auth": "^1.2.1",
44-
"express-rate-limit": "^7.5.1",
44+
"express-rate-limit": "^8.0.1",
4545
"express-validator": "^7.2.1",
4646
"graphql": "^16.11.0",
4747
"graphql-http": "^1.22.4",
@@ -50,7 +50,7 @@
5050
"jsonwebtoken": "^9.0.2",
5151
"moment": "^2.30.1",
5252
"mongodb": "^6.17.0",
53-
"mongoose": "^8.16.3",
53+
"mongoose": "^8.16.4",
5454
"pino": "^9.7.0",
5555
"pino-pretty": "^13.0.0",
5656
"swagger-ui-express": "^5.0.1",

sonar-project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
sonar.projectKey=switcherapi_switcher-api
22
sonar.projectName=switcher-api
33
sonar.organization=switcherapi
4-
sonar.projectVersion=1.4.0
4+
sonar.projectVersion=1.4.1
55
sonar.links.homepage=https://github.com/switcherapi/switcher-api
66

77
sonar.testExecutionReportPaths=test-report.xml

src/api-docs/swagger-info.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export default {
22
title: 'Switcher API',
3-
version: 'v1.4.0',
3+
version: 'v1.4.1',
44
description: 'Switcher API is a Feature Flag API focused on toggling features over different environments and applications.',
55
contact: {
66
name: 'Roger Floriano (petruki)',

src/services/gitops/push-changed.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ async function processChangedGroup(domain, change, environment) {
1717
const admin = { _id: domain.owner, email: ADMIN_EMAIL };
1818
const content = change.content;
1919
const group = await getGroupConfig({ domain: domain._id, name: change.path[0] });
20+
21+
const updatedActivated = new Map(group.activated);
22+
updatedActivated.set(environment, getChangedValue(content.activated, group.activated.get(environment)));
2023

2124
await updateGroup(group._id, {
2225
description: getChangedValue(content.description, group.description),
23-
activated: new Map().set(environment, getChangedValue(content.activated, group.activated.get(environment)))
26+
activated: updatedActivated
2427
}, admin);
2528
}
2629

@@ -29,9 +32,12 @@ async function processChangedConfig(domain, change, environment) {
2932
const admin = { _id: domain.owner, email: ADMIN_EMAIL };
3033
const config = await getConfig({ domain: domain._id, key: change.path[1] });
3134

35+
const updatedActivated = new Map(config.activated);
36+
updatedActivated.set(environment, getChangedValue(content.activated, config.activated.get(environment)));
37+
3238
await updateConfig(config._id, {
3339
description: getChangedValue(content.description, config.description),
34-
activated: new Map().set(environment, getChangedValue(content.activated, config.activated.get(environment)))
40+
activated: updatedActivated
3541
}, admin);
3642

3743
if (content.relay) {

tests/gitops.test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,38 @@ describe('GitOps - Push Changed', () => {
429429
expect(group.description).toBe('Changed Group Description');
430430
});
431431

432+
test('GITOPS_SUITE - Should push changes - Changed Group while keeping default', async () => {
433+
const token = generateToken('30s');
434+
435+
const lastUpdate = Date.now();
436+
const req = await request(app)
437+
.post('/gitops/v1/push')
438+
.set('Authorization', `Bearer ${token}`)
439+
.send({
440+
environment: 'staging',
441+
changes: [{
442+
action: 'CHANGED',
443+
diff: 'GROUP',
444+
path: ['Group Test'],
445+
content: {
446+
activated: true,
447+
description: 'Changed Group Description'
448+
}
449+
}]
450+
})
451+
.expect(200);
452+
453+
expect(req.body.message).toBe('Changes applied successfully');
454+
expect(req.body.version).toBeGreaterThan(lastUpdate);
455+
456+
// Check if the changes were applied
457+
const group = await GroupConfig.findOne({ name: 'Group Test', domain: domainId }).lean().exec();
458+
expect(group).not.toBeNull();
459+
expect(group.activated['staging']).toBe(true);
460+
expect(group.activated[EnvType.DEFAULT]).toBeDefined();
461+
expect(group.description).toBe('Changed Group Description');
462+
});
463+
432464
test('GITOPS_SUITE - Should push changes - Changed Switcher', async () => {
433465
const token = generateToken('30s');
434466

@@ -460,6 +492,38 @@ describe('GitOps - Push Changed', () => {
460492
expect(config.description).toBe('Changed Switcher Description');
461493
});
462494

495+
test('GITOPS_SUITE - Should push changes - Changed Switcher while keeping default', async () => {
496+
const token = generateToken('30s');
497+
498+
const lastUpdate = Date.now();
499+
const req = await request(app)
500+
.post('/gitops/v1/push')
501+
.set('Authorization', `Bearer ${token}`)
502+
.send({
503+
environment: 'staging',
504+
changes: [{
505+
action: 'CHANGED',
506+
diff: 'CONFIG',
507+
path: ['Group Test', 'TEST_CONFIG_KEY'],
508+
content: {
509+
activated: true,
510+
description: 'Changed Switcher Description'
511+
}
512+
}]
513+
})
514+
.expect(200);
515+
516+
expect(req.body.message).toBe('Changes applied successfully');
517+
expect(req.body.version).toBeGreaterThan(lastUpdate);
518+
519+
// Check if the changes were applied
520+
const config = await Config.findOne({ key: 'TEST_CONFIG_KEY', domain: domainId }).lean().exec();
521+
expect(config).not.toBeNull();
522+
expect(config.activated['staging']).toBe(true);
523+
expect(config.activated[EnvType.DEFAULT]).toBeDefined();
524+
expect(config.description).toBe('Changed Switcher Description');
525+
});
526+
463527
test('GITOPS_SUITE - Should push changes - Changed Switcher Relay (added)', async () => {
464528
const token = generateToken('30s');
465529

0 commit comments

Comments
 (0)