Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 28 additions & 73 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
"graphql": "^16.9.0",
"graphql-http": "^1.22.1",
"graphql-tag": "^2.12.6",
"helmet": "^7.1.0",
"helmet": "^8.0.0",
"jsonwebtoken": "^9.0.2",
"moment": "^2.30.1",
"mongodb": "^6.9.0",
"mongoose": "^8.6.4",
"mongoose": "^8.7.0",
"pino": "^9.4.0",
"pino-pretty": "^11.2.2",
"swagger-ui-express": "^5.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/routers/gitops.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as Service from '../services/gitops/index.js';

const router = new express.Router();

router.post('/gitops/v1/push', featureFlag, gitopsAuth, [
router.post('/gitops/v1/push', gitopsAuth, featureFlag, [
body('environment').isString(),
body('changes').isArray(),
body('changes.*.path').isArray({ min: 0, max: 3 }),
Expand Down
4 changes: 2 additions & 2 deletions src/services/config-strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export async function populateAdmin(configStrategies) {
}
}

export async function createStrategy(args, admin) {
export async function createStrategy(args, admin, status = true) {
const config = await getConfigById(args.config);

if (!args.env) {
Expand All @@ -61,7 +61,7 @@ export async function createStrategy(args, admin) {

let configStrategy = new ConfigStrategy({
...args,
activated: new Map().set(environment.name, true),
activated: new Map().set(environment.name, status),
domain: config.domain,
owner: admin._id
});
Expand Down
4 changes: 4 additions & 0 deletions src/services/gitops/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Logger from '../../helpers/logger.js';
import { getDomainById, updateDomainVersion } from '../domain.js';
import { processChanged } from './push-changed.js';
import { processDeleted } from './push-deleted.js';
Expand All @@ -6,6 +7,9 @@ import { processNew } from './push-new.js';
export const ADMIN_EMAIL = 'gitops@admin.noreply.switcherapi.com';

export async function pushChanges(domainId, environment, changes) {
Logger.debug(`Pushing changes to domain ${domainId} for environment ${environment}`);
Logger.debug(`Changes: ${JSON.stringify(changes)}`);

let domain = await getDomainById(domainId);
for (const change of changes) {
switch (change.action) {
Expand Down
7 changes: 4 additions & 3 deletions src/services/gitops/push-changed.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ export async function processChanged(domain, change, environment) {

async function processChangedGroup(domain, change, environment) {
const admin = { _id: domain.owner, email: ADMIN_EMAIL };
const content = change.content;
const group = await getGroupConfig({ domain: domain._id, name: change.path[0] });

await updateGroup(group._id, {
description: getChangedValue(change.content.description, group.description),
activated: new Map().set(environment, getChangedValue(change.content.activated, group.activated.get(environment)))
description: getChangedValue(content.description, group.description),
activated: new Map().set(environment, getChangedValue(content.activated, group.activated.get(environment)))
}, admin);
}

Expand All @@ -48,7 +49,7 @@ async function processChangedStrategy(domain, change, environment) {
const config = await getConfig({ domain: domain._id, group: group._id, key: path[1] });

const strategies = await getStrategies({ config: config._id });
const strategy = strategies.find(strategy => strategy.strategy === path[2] && strategy.activated.get(environment));
const strategy = strategies.find(strategy => strategy.strategy === path[2] && strategy.activated.has(environment));

await updateStrategy(strategy._id, {
description: getChangedValue(content.description, group.description),
Expand Down
31 changes: 18 additions & 13 deletions src/services/gitops/push-new.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ export async function processNew(domain, change, environment) {

async function processNewGroup(domain, change, environment) {
const admin = { _id: domain.owner };
const content = change.content;
const group = await createGroup({
domain: domain._id,
name: change.content.name,
description: change.content.description,
activated: new Map().set(environment, change.content.activated),
name: content.name,
description: getNewValue(content.description, ''),
activated: new Map().set(environment, getNewValue(content.activated, true)),
owner: domain.owner
}, admin);

if (change.content.configs?.length) {
for (const config of change.content.configs) {
if (content.config?.length) {
for (const config of content.config) {
await processNewConfig(domain, {
path: [group.name],
content: config
Expand All @@ -51,23 +52,23 @@ async function processNewConfig(domain, change, environment) {
const group = await getGroupConfig({ domain: domain._id, name: path[0] });

let componentIds;
if (change.content.components?.length) {
const components = await getComponents({ domain: domain._id, name: { $in: change.content.components } });
if (content.components?.length) {
const components = await getComponents({ domain: domain._id, name: { $in: content.components } });
componentIds = components.map(component => component._id);
}

const config = await createConfig({
domain: domain._id,
group: group._id,
key: content.key,
description: content.description,
activated: new Map().set(environment, content.activated),
description: getNewValue(content.description, ''),
activated: new Map().set(environment, getNewValue(content.activated, true)),
owner: domain.owner,
components: componentIds
}, admin);

if (change.content.strategies?.length) {
for (const strategy of change.content.strategies) {
if (content.strategies?.length) {
for (const strategy of content.strategies) {
await processNewStrategy(domain, {
path: [group.name, config.key],
content: strategy
Expand All @@ -84,14 +85,14 @@ async function processNewStrategy(domain, change, environment) {

await createStrategy({
env: environment,
description: content.description,
description: getNewValue(content.description, ''),
strategy: content.strategy,
values: content.values,
operation: content.operation,
config: config._id,
domain: domain._id,
owner: domain.owner
}, admin);
}, admin, getNewValue(content.activated, true));
}

async function processNewStrategyValue(domain, change) {
Expand Down Expand Up @@ -122,4 +123,8 @@ async function processNewComponent(domain, change) {
await addComponent(config._id, { component: id }, admin);
}
}
}

function getNewValue(newValue, defaultValue) {
return newValue !== undefined ? newValue : defaultValue;
}
2 changes: 1 addition & 1 deletion tests/gitops.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe('GitOps - Push New', () => {
name: 'New Group and Switcher',
description: 'New Group Description',
activated: true,
configs: [{
config: [{
key: 'NEW_SWITCHER_FROM_GROUP',
description: 'New Switcher',
activated: false,
Expand Down