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

API environment endpoints #2529

Merged
merged 15 commits into from
May 30, 2024
Merged

API environment endpoints #2529

merged 15 commits into from
May 30, 2024

Conversation

romain-growthbook
Copy link
Contributor

No description provided.

Copy link

github-actions bot commented May 15, 2024

Your preview environment pr-2529-bttf has been deployed.

Copy link

github-actions bot commented May 28, 2024

Deploy preview for docs ready!

✅ Preview
https://docs-n41pweuz2-growthbook.vercel.app

Built with commit 1a4ff32.
This pull request is being automatically deployed with vercel-action

packages/back-end/test/api/environments.test.ts Outdated Show resolved Hide resolved
},
],
},
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lots of copy/pasting in this test files makes it a little hard to follow and maintain. For example, if we add a new method to req.context that is needed under-the-hood, we will have to update it 13 times in this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually the opposite, the context is only setting what's needed for the API call to consume. If a new method is added, the tests don't need to worry about it. The only case they'd need to be edited is if one of the API the implementation rely on changes, for instance if a permission method is modified, which is what we want to catch.

Generally speaking, I lean more and more toward making my tests more verbose and duplicated, contrary to actual code. This adds some maintenance burden but seems to make the tests more robust. The problem with factoring out too much is that abstract code is more prone to silent issues that would make all tests seemingly pass because, for instance, one higher order loop, is not operating the way it needs to. Having all tests be rigid and and less smart makes it more obvious when something breaks.

That being said, the code needs some factorization of the setup. I have added a api.setup.ts utility function that handles the app and request context setup and teardown. Now the tests only have to specify what request context they want. Should make it more readable!

Copy link
Collaborator

@mknowlton89 mknowlton89 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few small comments.

I'm less familiar/confident with the BaseModel changes, but overall looks good!

packages/back-end/src/api/openapi/openapi.yaml Outdated Show resolved Hide resolved
properties:
description:
type: string
description: The description of the new environment
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Should probably remove the new reference here.

}

if (!req.context.permissions.canDeleteEnvironment(environment))
throw Error("You do not have permission to delete this environment!");
Copy link
Collaborator

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.

if (!context.permissions.canDeleteEnvironment(environment)) {
context.permissions.throwPermissionError();
}

packages/back-end/src/api/environments/listEnvironments.ts Outdated Show resolved Hide resolved
packages/back-end/src/api/environments/postEnvironment.ts Outdated Show resolved Hide resolved
packages/back-end/src/api/environments/putEnvironment.ts Outdated Show resolved Hide resolved
@romain-growthbook
Copy link
Contributor Author

All PR comments have been addressed now!

environments: environments.reduce(
(ret, env) => [...ret, env.id === id ? updatedEnvironment : env],
[]
),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor code style, but map makes a lot more sense here than reduce. I tend to avoid reduce whenever possible since it's usually much harder to read and follow the logic compared to alternatives.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100%

@romain-growthbook romain-growthbook merged commit 558be1f into main May 30, 2024
5 checks passed
@romain-growthbook romain-growthbook deleted the env-api-endpoints branch May 30, 2024 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants