Repository navigation
fix(Projects): Remove N+1 calls to get project environments #8957
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
Merged
+297
−115
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fd2e6b7
feat(EmptyState): Add an error status with a retry action
talissoncosta de11c87
fix(Projects): Remove N+1 calls to get project environments
talissoncosta 53332f9
test(Permissions): Cover the environment list in both user permission…
talissoncosta e2663b3
fix(Projects): Wait for ProjectStore before rendering a project page
talissoncosta 27fda93
test(Permissions): Type the API setup responses instead of asserting …
talissoncosta 78a944e
test(Projects): Trim tests and comments that repeat the code
talissoncosta fd004cf
test(Permissions): Use the seeded projects instead of creating them
talissoncosta 4f85618
refactor(Projects): Load the redirect's environments with the shared …
talissoncosta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,15 @@ | ||
| import { skipToken } from '@reduxjs/toolkit/query' | ||
| import { useGetAllEnvironmentsQuery } from 'common/services/useEnvironment' | ||
| import { sortEnvironmentsByName } from 'common/utils/sortEnvironmentsByName' | ||
|
|
||
| export const useSortedProjectEnvironments = (projectId: number | undefined) => { | ||
| // currentData: data would keep the previous project's list while the next loads | ||
| const { currentData, isError, isFetching, refetch } = | ||
| useGetAllEnvironmentsQuery(projectId ? { projectId } : skipToken) | ||
| return { | ||
| environments: currentData && sortEnvironmentsByName(currentData), | ||
| isError, | ||
| isLoading: isFetching && !currentData, | ||
| refetch, | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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,19 @@ | ||
| import type { Environment } from 'common/types/responses' | ||
|
|
||
| export const makeEnvironment = (id: number, name: string): Environment => ({ | ||
| allow_client_traits: true, | ||
| api_key: `key-${id}`, | ||
| hide_disabled_flags: null, | ||
| hide_sensitive_data: false, | ||
| id, | ||
| is_creating: false, | ||
| metadata: [], | ||
| minimum_change_request_approvals: null, | ||
| name, | ||
| project: 1, | ||
| show_disabled_flags: false, | ||
| use_identity_composite_key_for_hashing: false, | ||
| use_identity_overrides_in_local_eval: false, | ||
| use_mv_v2_evaluation: false, | ||
| use_v2_feature_versioning: false, | ||
| }) |
19 changes: 19 additions & 0 deletions
19
frontend/common/utils/__tests__/projectLandingPath.test.ts
This file contains hidden or 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,19 @@ | ||
| import { getProjectLandingPath } from 'common/utils/projectLandingPath' | ||
| import { makeEnvironment } from './environmentFixture' | ||
|
|
||
| describe('getProjectLandingPath', () => { | ||
| it('opens the features of the first environment', () => { | ||
| const environments = [ | ||
| makeEnvironment(2, 'Development'), | ||
| makeEnvironment(1, 'Production'), | ||
| ] | ||
|
|
||
| expect(getProjectLandingPath(7, environments)).toBe( | ||
| '/project/7/environment/key-2/features', | ||
| ) | ||
| }) | ||
|
|
||
| it('opens create environment when the project has none', () => { | ||
| expect(getProjectLandingPath(7, [])).toBe('/project/7/environment/create') | ||
| }) | ||
| }) |
37 changes: 37 additions & 0 deletions
37
frontend/common/utils/__tests__/sortEnvironmentsByName.test.ts
This file contains hidden or 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,37 @@ | ||
| import { sortEnvironmentsByName } from 'common/utils/sortEnvironmentsByName' | ||
| import { makeEnvironment } from './environmentFixture' | ||
|
|
||
| const names = (environments: { name: string }[]) => | ||
| environments.map((env) => env.name) | ||
|
|
||
| describe('sortEnvironmentsByName', () => { | ||
| it('orders environments by name rather than by id', () => { | ||
| const environments = [ | ||
| makeEnvironment(1, 'Production'), | ||
| makeEnvironment(2, 'Development'), | ||
| makeEnvironment(3, 'Staging'), | ||
| ] | ||
|
|
||
| expect(names(sortEnvironmentsByName(environments))).toEqual([ | ||
| 'Development', | ||
| 'Production', | ||
| 'Staging', | ||
| ]) | ||
| }) | ||
|
|
||
| it('puts capitalised names before lowercase ones, as the UI always has', () => { | ||
| const environments = [ | ||
| makeEnvironment(1, 'test'), | ||
| makeEnvironment(2, 'Production'), | ||
| makeEnvironment(3, 'new env'), | ||
| makeEnvironment(4, 'Development'), | ||
| ] | ||
|
|
||
| expect(names(sortEnvironmentsByName(environments))).toEqual([ | ||
| 'Development', | ||
| 'Production', | ||
| 'new env', | ||
| 'test', | ||
| ]) | ||
| }) | ||
| }) |
This file contains hidden or 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,11 @@ | ||
| import type { Environment } from 'common/types/responses' | ||
|
|
||
| export function getProjectLandingPath( | ||
| projectId: number, | ||
| environmentsByName: Environment[], | ||
| ): string { | ||
| const environment = environmentsByName[0] | ||
| return environment | ||
| ? `/project/${projectId}/environment/${environment.api_key}/features` | ||
| : `/project/${projectId}/environment/create` | ||
| } |
This file contains hidden or 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,9 @@ | ||
| import sortBy from 'lodash/sortBy' | ||
| import type { Environment } from 'common/types/responses' | ||
|
|
||
| // The API orders environments by id | ||
| export function sortEnvironmentsByName( | ||
| environments: Environment[], | ||
| ): Environment[] { | ||
| return sortBy(environments, 'name') | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,59 @@ | ||
| import { test, expect } from '../test-setup' | ||
| import { createHelpers, log } from '../helpers' | ||
| import { | ||
| E2E_NON_ADMIN_USER_WITH_ENV_PERMISSIONS, | ||
| E2E_USER, | ||
| PASSWORD, | ||
| } from '../config' | ||
|
|
||
| // Seeded projects: the first has Development and Production, the second only Development | ||
| const PROJECT_WITH_TWO_ENVIRONMENTS = 'My Test Project' | ||
| const PROJECT_WITH_ONE_ENVIRONMENT = 'My Test Project 2' | ||
|
|
||
| const MEMBER_ACTIONS = ['Manage user', 'Inspect permissions'] | ||
|
|
||
| test.describe('User permissions dialogs', () => { | ||
| for (const action of MEMBER_ACTIONS) { | ||
| test(`${action} lists only the picked project's environments @enterprise`, async ({ | ||
| page, | ||
| }) => { | ||
| const { login } = createHelpers(page) | ||
|
|
||
| log(`Open ${action} for a member`) | ||
| await login(E2E_USER, PASSWORD) | ||
| await page.getByRole('link', { name: 'Users and Permissions' }).click() | ||
| await page | ||
| .getByRole('button', { | ||
| name: `More actions for ${E2E_NON_ADMIN_USER_WITH_ENV_PERMISSIONS}`, | ||
| }) | ||
| .click() | ||
| // The menu items aren't buttons yet | ||
| await page.getByText(action, { exact: true }).click() | ||
| const dialog = page.getByRole('dialog') | ||
| await dialog | ||
| .getByRole('button', { exact: true, name: 'Environment' }) | ||
| .click() | ||
|
|
||
| // E2E builds render Select's options as plain, always visible links | ||
| const pickProject = async (name: string) => { | ||
| // Inactive tabs stay in the page, so the Project tab also lists the name | ||
| await dialog | ||
| .getByText(name, { exact: true }) | ||
| .filter({ visible: true }) | ||
| .click() | ||
| } | ||
|
|
||
| log('A project lists its environments') | ||
| await pickProject(PROJECT_WITH_ONE_ENVIRONMENT) | ||
| await expect( | ||
| dialog.getByText('Development', { exact: true }), | ||
| ).toBeVisible() | ||
|
|
||
| log("Switching project loads the new project's list") | ||
| await pickProject(PROJECT_WITH_TWO_ENVIRONMENTS) | ||
| await expect( | ||
| dialog.getByText('Production', { exact: true }), | ||
| ).toBeVisible() | ||
| }) | ||
| } | ||
| }) | ||
This file contains hidden or 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
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.