Skip to content

Commit

Permalink
Merge pull request #3567 from uselagoon/environment-quota-fix
Browse files Browse the repository at this point in the history
fix: check environment doesnt exist before checking env quotas
  • Loading branch information
tobybellwood authored Oct 15, 2023
2 parents 3017c18 + 9c6bb5f commit 083aa40
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mutation PopulateApi {
friendlyName: "Test Organization"
description: "An organization for testing"
quotaProject: 5
quotaEnvironment: 15
quotaEnvironment: 4
quotaGroup: 10
quotaNotification: 10
}) {
Expand Down
15 changes: 9 additions & 6 deletions node-packages/commons/src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
);
}
}
}

Expand Down
15 changes: 9 additions & 6 deletions services/api/src/resources/environment/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
);
}
}
}

Expand Down

0 comments on commit 083aa40

Please sign in to comment.