Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check environment doesnt exist before checking env quotas #3567

Merged
merged 1 commit into from
Oct 15, 2023
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
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