-
Notifications
You must be signed in to change notification settings - Fork 464
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
API environment endpoints #2529
Merged
Merged
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
850ea19
Endpoint implementation.
romain-growthbook cafd69e
Check id.
romain-growthbook 97ed030
Add/improve event interface.
romain-growthbook 8bfdbba
Merge remote-tracking branch 'origin/main' into env-api-endpoints
romain-growthbook 3325dd4
Use request org.
romain-growthbook 4572ee2
First stab at integration tests.
romain-growthbook 0ca16b0
Fix test name.
romain-growthbook ed4a87b
Finish tests.
romain-growthbook d8055ae
Fix doc.
romain-growthbook e0576a6
Respect order.
romain-growthbook a1a6324
Default description to ""
romain-growthbook 26965c8
Fix tests.
romain-growthbook 035b539
Factor stuff out.
romain-growthbook 7997e3c
Address PR review.
romain-growthbook 1a4ff32
Use map.
romain-growthbook File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/back-end/src/api/environments/deleteEnvironment.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { DeleteEnvironmentResponse } from "../../../types/openapi"; | ||
import { createApiRequestHandler } from "../../util/handler"; | ||
import { deleteEnvironmentValidator } from "../../validators/openapi"; | ||
import { updateOrganization } from "../../models/OrganizationModel"; | ||
import { OrganizationInterface } from "../../../types/organization"; | ||
import { auditDetailsDelete } from "../../services/audit"; | ||
|
||
export const deleteEnvironment = createApiRequestHandler( | ||
deleteEnvironmentValidator | ||
)( | ||
async (req): Promise<DeleteEnvironmentResponse> => { | ||
const id = req.params.id; | ||
const org = req.context.org; | ||
const environments = org.settings?.environments || []; | ||
|
||
const environment = environments.find((env) => env.id === id); | ||
if (!environment) { | ||
throw Error(`Environment ${id} does not exists!`); | ||
} | ||
|
||
if (!req.context.permissions.canDeleteEnvironment(environment)) | ||
throw Error("You do not have permission to delete this environment!"); | ||
|
||
const updates: Partial<OrganizationInterface> = { | ||
settings: { | ||
...org.settings, | ||
environments: [...environments.filter((env) => env.id !== id)], | ||
}, | ||
}; | ||
|
||
await updateOrganization(org.id, updates); | ||
|
||
await req.audit({ | ||
event: "environment.delete", | ||
entity: { | ||
object: "environment", | ||
id: environment.id, | ||
}, | ||
details: auditDetailsDelete(environment), | ||
}); | ||
|
||
return { | ||
deletedId: id, | ||
}; | ||
} | ||
); |
14 changes: 14 additions & 0 deletions
14
packages/back-end/src/api/environments/environments.router.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Router } from "express"; | ||
import { listEnvironments } from "./listEnvironments"; | ||
import { putEnvironment } from "./putEnvironment"; | ||
import { postEnvironment } from "./postEnvironment"; | ||
import { deleteEnvironment } from "./deleteEnvironment"; | ||
|
||
const router = Router(); | ||
|
||
router.get("/:environments", listEnvironments); | ||
router.post("/environments", postEnvironment); | ||
router.put("/environments/:environmentId", putEnvironment); | ||
router.delete("/environments/:environmentId", deleteEnvironment); | ||
|
||
export default router; |
41 changes: 41 additions & 0 deletions
41
packages/back-end/src/api/environments/listEnvironments.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { ListEnvironmentsResponse } from "../../../types/openapi"; | ||
import { createApiRequestHandler } from "../../util/handler"; | ||
import { findOrganizationById } from "../../models/OrganizationModel"; | ||
import { listEnvironmentsValidator } from "../../validators/openapi"; | ||
|
||
export const listEnvironments = createApiRequestHandler( | ||
listEnvironmentsValidator | ||
)( | ||
async (req): Promise<ListEnvironmentsResponse> => { | ||
const id = req.params.id; | ||
|
||
const org = await findOrganizationById(id); | ||
if (!org) { | ||
throw Error("Organization not found"); | ||
} | ||
romain-growthbook marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const environments = ( | ||
org.settings?.environments || [] | ||
).filter((environment) => | ||
req.context.permissions.canReadMultiProjectResource(environment.projects) | ||
); | ||
|
||
return { | ||
environments: environments.map( | ||
({ | ||
id, | ||
description = "", | ||
toggleOnList = false, | ||
defaultState = false, | ||
projects = [], | ||
}) => ({ | ||
id, | ||
projects, | ||
description, | ||
defaultState, | ||
toggleOnList, | ||
}) | ||
), | ||
}; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { PostEnvironmentResponse } from "../../../types/openapi"; | ||
import { createApiRequestHandler } from "../../util/handler"; | ||
import { postEnvironmentValidator } from "../../validators/openapi"; | ||
import { updateOrganization } from "../../models/OrganizationModel"; | ||
import { OrganizationInterface } from "../../../types/organization"; | ||
import { auditDetailsCreate } from "../../services/audit"; | ||
import { validatePayload } from "./validations"; | ||
|
||
export const postEnvironment = createApiRequestHandler( | ||
postEnvironmentValidator | ||
)( | ||
async (req): Promise<PostEnvironmentResponse> => { | ||
const environment = await validatePayload(req.context, req.body); | ||
|
||
const org = req.context.org; | ||
|
||
if (org.settings?.environments?.some((env) => env.id === environment.id)) { | ||
throw Error(`Environment ${environment.id} already exists!`); | ||
} | ||
|
||
if (!req.context.permissions.canCreateOrUpdateEnvironment(environment)) | ||
throw new Error("You don't have permission to update this environment!"); | ||
|
||
const updates: Partial<OrganizationInterface> = { | ||
settings: { | ||
...org.settings, | ||
environments: [...(org.settings?.environments || []), environment], | ||
}, | ||
}; | ||
|
||
await updateOrganization(org.id, updates); | ||
|
||
await req.audit({ | ||
event: "environment.create", | ||
entity: { | ||
object: "environment", | ||
id: environment.id, | ||
}, | ||
details: auditDetailsCreate(environment), | ||
}); | ||
|
||
return { | ||
environment, | ||
}; | ||
} | ||
); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have a specific permissions error that we can use that provides better categorization of errors.
E.G.