From 9c6bb5fbffb244f76bfb730555eb7cf29d9a9a2e Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Fri, 13 Oct 2023 11:00:03 +1100 Subject: [PATCH] fix: check environment doesnt exist before checking env quotas --- .../04-populate-api-data-organizations.gql | 2 +- node-packages/commons/src/tasks.ts | 15 +++++++++------ .../api/src/resources/environment/resolvers.ts | 15 +++++++++------ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/local-dev/api-data-watcher-pusher/api-data/04-populate-api-data-organizations.gql b/local-dev/api-data-watcher-pusher/api-data/04-populate-api-data-organizations.gql index 69c5126291..2c2b44d44a 100644 --- a/local-dev/api-data-watcher-pusher/api-data/04-populate-api-data-organizations.gql +++ b/local-dev/api-data-watcher-pusher/api-data/04-populate-api-data-organizations.gql @@ -7,7 +7,7 @@ mutation PopulateApi { friendlyName: "Test Organization" description: "An organization for testing" quotaProject: 5 - quotaEnvironment: 15 + quotaEnvironment: 4 quotaGroup: 10 quotaNotification: 10 }) { diff --git a/node-packages/commons/src/tasks.ts b/node-packages/commons/src/tasks.ts index 9f7dfbc988..3ea01eb0b4 100644 --- a/node-packages/commons/src/tasks.ts +++ b/node-packages/commons/src/tasks.ts @@ -768,12 +768,15 @@ export const createDeployTask = async function(deployData: any) { const environments = await getEnvironmentsForProject(projectName); if (project.organization) { - // check the environment quota, this prevents environments being deployed by the api or webhooks - const curOrg = await getOrganizationById(project.organization); - if (curOrg.environments.length >= curOrg.quotaEnvironment) { - throw new OrganizationEnvironmentLimit( - `'${branchName}' would exceed the organization environment quota of ${curOrg.quotaEnvironment}` - ); + // if this would be a new environment, check it against the environment quota + if (!environments.project.environments.map(e => e.name).find(i => i === branchName)) { + // check the environment quota, this prevents environments being deployed by the api or webhooks + const curOrg = await getOrganizationById(project.organization); + if (curOrg.environments.length >= curOrg.quotaEnvironment) { + throw new OrganizationEnvironmentLimit( + `'${branchName}' would exceed the organization environment quota of ${curOrg.quotaEnvironment}` + ); + } } } diff --git a/services/api/src/resources/environment/resolvers.ts b/services/api/src/resources/environment/resolvers.ts index 8009cf7315..a90946a938 100644 --- a/services/api/src/resources/environment/resolvers.ts +++ b/services/api/src/resources/environment/resolvers.ts @@ -367,13 +367,16 @@ export const addOrUpdateEnvironment: ResolverFn = async ( } if (projectOpenshift.organization) { - // check the environment quota, this prevents environments being added directly via the api + // if this would be a new environment, check it against the quota const curEnvs = await organizationHelpers(sqlClientPool).getEnvironmentsByOrganizationId(projectOpenshift.organization) - const curOrg = await organizationHelpers(sqlClientPool).getOrganizationById(projectOpenshift.organization) - if (curEnvs.length >= curOrg.quotaEnvironment && curOrg.quotaEnvironment != -1) { - throw new Error( - `Environment would exceed organization environment quota: ${curEnvs.length}/${curOrg.quotaEnvironment}` - ); + if (!curEnvs.map(e => e.name).find(i => i === input.name)) { + // check the environment quota, this prevents environments being added directly via the api + const curOrg = await organizationHelpers(sqlClientPool).getOrganizationById(projectOpenshift.organization) + if (curEnvs.length >= curOrg.quotaEnvironment && curOrg.quotaEnvironment != -1) { + throw new Error( + `Environment would exceed organization environment quota: ${curEnvs.length}/${curOrg.quotaEnvironment}` + ); + } } }