1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

feat: skip change request in UI (#3495)

This commit is contained in:
Mateusz Kwasniewski 2023-04-11 14:47:54 +02:00 committed by GitHub
parent 536230a364
commit 3b1157b869
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 121 additions and 9 deletions

View File

@ -192,6 +192,10 @@ const otherRequests = (feature: string) => {
version: 1,
tags: [],
});
testServerRoute(server, `api/admin/tag-types`, {
tagTypes: [],
version: 1,
});
testServerRoute(server, 'api/admin/user', {
user: {
isAPI: false,

View File

@ -73,6 +73,10 @@ const setupOtherRoutes = (feature: string) => {
version: 1,
tags: [],
});
testServerRoute(server, `api/admin/tag-types`, {
tagTypes: [],
version: 1,
});
testServerRoute(server, `api/admin/tags/simple`, {
version: 1,
tags: [],

View File

@ -1,6 +1,6 @@
import { ReactElement, ReactNode, useMemo } from 'react';
import AccessContext, { IAccessContext } from 'contexts/AccessContext';
import { ADMIN } from './permissions';
import { ADMIN, SKIP_CHANGE_REQUEST } from './permissions';
import { IPermission } from 'interfaces/user';
import { useAuthPermissions } from 'hooks/api/getters/useAuth/useAuthPermissions';
@ -63,7 +63,7 @@ const checkPermission = (
return false;
}
if (p.permission === ADMIN) {
if (p.permission === ADMIN && permission !== SKIP_CHANGE_REQUEST) {
return true;
}

View File

@ -0,0 +1,76 @@
import { render, screen } from '@testing-library/react';
import { AccessProviderMock } from '../component/providers/AccessProvider/AccessProviderMock';
import { useChangeRequestsEnabled } from './useChangeRequestsEnabled';
import { FC } from 'react';
import { testServerRoute, testServerSetup } from '../utils/testServer';
import {
SKIP_CHANGE_REQUEST,
ADMIN,
} from '../component/providers/AccessProvider/permissions';
const project = 'project';
const environment = 'production';
const TestComponent: FC = () => {
const { isChangeRequestConfigured } = useChangeRequestsEnabled(project);
const string = isChangeRequestConfigured('production')
? 'change request'
: 'regular mode';
return <div>{string}</div>;
};
const server = testServerSetup();
testServerRoute(
server,
`/api/admin/projects/${project}/change-requests/config`,
[
{
environment,
changeRequestEnabled: true,
},
]
);
testServerRoute(server, '/api/admin/ui-config', {
versionInfo: {
current: { enterprise: 'present' },
},
});
test('SKIP_CHANGE_REQUEST disables change request mode', async () => {
render(
<AccessProviderMock
permissions={[
{
permission: ADMIN,
project,
environment,
},
]}
>
<TestComponent />
</AccessProviderMock>
);
const result = await screen.findByText('change request');
expect(result).toBeInTheDocument();
render(
<AccessProviderMock
permissions={[
{
permission: SKIP_CHANGE_REQUEST,
project,
environment,
},
]}
>
<TestComponent />
</AccessProviderMock>
);
const regularModeElement = await screen.findByText('regular mode');
expect(regularModeElement).toBeInTheDocument();
});

View File

@ -1,17 +1,26 @@
import { SKIP_CHANGE_REQUEST } from 'component/providers/AccessProvider/permissions';
import React from 'react';
import { useChangeRequestConfig } from './api/getters/useChangeRequestConfig/useChangeRequestConfig';
import { useCheckProjectPermissions } from './useHasAccess';
export const useChangeRequestsEnabled = (projectId: string) => {
const { data } = useChangeRequestConfig(projectId);
const checkAccess = useCheckProjectPermissions(projectId);
const isChangeRequestConfigured = React.useCallback(
(environment: string): boolean => {
return data.some(draft => {
return (
draft.environment === environment &&
draft.changeRequestEnabled
);
});
const canSkipChangeRequest = checkAccess(
SKIP_CHANGE_REQUEST,
environment
);
return canSkipChangeRequest
? false
: data.some(draft => {
return (
draft.environment === environment &&
draft.changeRequestEnabled
);
});
},
[JSON.stringify(data)]
);

View File

@ -15,7 +15,7 @@ import useProject from './api/getters/useProject/useProject';
* This is for features not integrated with change request.
* If the feature is integrated with change request, use useCheckProjectAccess instead.
*/
const useCheckProjectPermissions = (projectId?: string) => {
export const useCheckProjectPermissions = (projectId?: string) => {
const { hasAccess } = useContext(AccessContext);
const checkPermission = (

View File

@ -0,0 +1,19 @@
'use strict';
exports.up = function (db, callback) {
db.runSql(
`
UPDATE permissions SET display_name = 'Skip change request process' WHERE permission = 'SKIP_CHANGE_REQUEST';
`,
callback,
);
};
exports.down = function (db, callback) {
db.runSql(
`
UPDATE permissions SET display_name = 'Skip change request process (API-only)' WHERE permission = 'SKIP_CHANGE_REQUEST';
`,
callback,
);
};