Skip to content

Commit

Permalink
feat: Update GitServer create, edit forms (#86)
Browse files Browse the repository at this point in the history
Jira: EPMDPEDP-12859
Related: #86
Change-Id: I0e5d424c4e174f08cd40c09d9d0879fd29f0efb2
  • Loading branch information
callmevladik committed Nov 3, 2023
1 parent 7cb7b01 commit dcf5906
Show file tree
Hide file tree
Showing 51 changed files with 5,234 additions and 372 deletions.
16 changes: 13 additions & 3 deletions mocks/wrappers/default/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@
* @jest-environment jsdom
*/

import { createTheme } from '@material-ui/core';
import { ThemeProvider } from '@material-ui/styles';
import { SnackbarProvider } from 'notistack';
import React from 'react';
import { QueryClient, QueryClientProvider } from 'react-query';

export const TestWrapper: React.FC = ({ children }) => {
const queryClient = new QueryClient();
const theme = createTheme({
// @ts-ignore
palette: {
primaryColor: '#000',
},
});

return (
<QueryClientProvider client={queryClient}>
<SnackbarProvider>{children}</SnackbarProvider>
</QueryClientProvider>
<ThemeProvider theme={theme}>
<QueryClientProvider client={queryClient}>
<SnackbarProvider>{children}</SnackbarProvider>
</QueryClientProvider>
</ThemeProvider>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const createCodebaseBranchInstance = (
codebaseName: string
): EDPCodebaseBranchKubeObjectInterface => {
const { branchName, releaseBranchName, ...restProps } = formValues;
console.log(formValues);
const _branchName = formValues.release ? releaseBranchName : branchName;
const transformedBranchName = _branchName ? _branchName.replaceAll('/', '-') : '';

Expand Down
26 changes: 22 additions & 4 deletions src/k8s/EDPGitServer/hooks/useGitServerCRUD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface CreateGitServerProps {

interface EditGitServerProps {
gitServerData: EDPGitServerKubeObjectInterface;
gitServerSecretData: EDPKubeObjectInterface;
}

export const useGitServerCRUD = ({
Expand Down Expand Up @@ -45,6 +46,11 @@ export const useGitServerCRUD = ({
CRUD_TYPES.CREATE
>('gitServerSecretCreateMutation', K8s.secret.default, CRUD_TYPES.CREATE);

const gitServerSecretEditMutation = useResourceCRUDMutation<
EDPKubeObjectInterface,
CRUD_TYPES.EDIT
>('gitServerSecretEditMutation', K8s.secret.default, CRUD_TYPES.EDIT);

const createGitServer = React.useCallback(
async ({ gitServerData, gitServerSecretData }: CreateGitServerProps) => {
gitServerSecretCreateMutation.mutate(gitServerSecretData, {
Expand All @@ -60,8 +66,6 @@ export const useGitServerCRUD = ({
invokeOnSuccessCallback();
},
onError: () => {
gitServerSecretDeleteMutation.mutate(gitServerSecretData);

invokeOnErrorCallback();
},
});
Expand All @@ -76,7 +80,7 @@ export const useGitServerCRUD = ({
);

const editGitServer = React.useCallback(
async ({ gitServerData }: EditGitServerProps) => {
async ({ gitServerData, gitServerSecretData }: EditGitServerProps) => {
gitServerEditMutation.mutate(gitServerData, {
onSuccess: () => {
invokeOnSuccessCallback();
Expand All @@ -85,8 +89,22 @@ export const useGitServerCRUD = ({
invokeOnErrorCallback();
},
});

gitServerSecretEditMutation.mutate(gitServerSecretData, {
onSuccess: () => {
invokeOnSuccessCallback();
},
onError: () => {
invokeOnErrorCallback();
},
});
},
[gitServerEditMutation, invokeOnErrorCallback, invokeOnSuccessCallback]
[
gitServerEditMutation,
gitServerSecretEditMutation,
invokeOnErrorCallback,
invokeOnSuccessCallback,
]
);

const mutations = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ describe('testing createGitServerInstance', () => {
httpsPort: 443,
gitUser: 'git',
gitProvider: 'gerrit',
gitHost: 'github.com',
gitHost: 'gerrit.com',
});

expect(object).toEqual({
apiVersion: 'v2.edp.epam.com/v1',
kind: 'GitServer',
metadata: { name: 'github.com-8ygse' },
metadata: { name: 'gerrit.com-8ygse' },
spec: {
gitHost: 'github.com',
nameSshKeySecret: 'github.com-8ygse-config',
gitHost: 'gerrit.com',
nameSshKeySecret: 'gerrit-ciuser-sshkey',
sshPort: 22,
httpsPort: 443,
gitUser: 'git',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const createGitServerInstance = (
const gitServerName = `${gitHost}-${createRandomString()}`;
const nameSshKeySecret =
formValues.gitProvider === GIT_PROVIDERS.GERRIT
? `${gitServerName}-config`
? 'gerrit-ciuser-sshkey'
: `ci-${formValues.gitProvider}`;

const base: DeepPartial<EDPGitServerKubeObjectInterface> = {
Expand Down
9 changes: 6 additions & 3 deletions src/k8s/Secret/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ export class SecretKubeObject extends K8s.secret.default {
errorHandler,
}: StreamSecretsByTypeProps): () => void {
const url = `/api/${version}/namespaces/${namespace}/${pluralForm}`;
return streamResults(url, dataHandler, errorHandler, {
labelSelector: `${SECRET_LABEL_SECRET_TYPE}=${type}`,
});
if (type) {
return streamResults(url, dataHandler, errorHandler, {
labelSelector: `${SECRET_LABEL_SECRET_TYPE}=${type}`,
});
}
return streamResults(url, dataHandler, errorHandler);
}
}
61 changes: 50 additions & 11 deletions src/k8s/Secret/utils/createGitServerSecretInstance/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,63 @@
import { GIT_PROVIDERS } from '../../../../constants/gitProviders';
import { createGitServerSecretInstance } from './index';
import {
createGerritGitServerSecretInstance,
createGithubGitServerSecretInstance,
createGitlabGitServerSecretInstance,
} from './index';

describe('testing createGitServerSecretInstance', () => {
it('should create correct object', () => {
const object = createGitServerSecretInstance({
name: 'test-host-name.com-n84o8',
gitUser: 'git',
token: 'test access token',
it('should create correct gerrit git server secret', () => {
const object = createGerritGitServerSecretInstance({
sshPrivateKey: 'test private ssh key',
gitProvider: GIT_PROVIDERS.GERRIT,
sshPublicKey: 'test public ssh key',
username: 'test-username',
});

expect(object).toEqual({
apiVersion: 'v1',
kind: 'Secret',
metadata: { name: 'test-host-name.com-n84o8-config' },
metadata: { name: 'gerrit-ciuser-sshkey' },
data: {
username: 'Z2l0',
id_rsa: 'dGVzdCBwcml2YXRlIHNzaCBrZXkK',
token: 'dGVzdCBhY2Nlc3MgdG9rZW4=',
'id_rsa.pub': 'dGVzdCBwdWJsaWMgc3NoIGtleQ==',
username: 'dGVzdC11c2VybmFtZQ==',
},
});
});

it('should create correct github git server secret', () => {
const object = createGithubGitServerSecretInstance({
sshPrivateKey: 'test private ssh key',
token: 'test-token',
username: 'test-username',
});

expect(object).toEqual({
apiVersion: 'v1',
kind: 'Secret',
metadata: { name: 'ci-github' },
data: {
id_rsa: 'dGVzdCBwcml2YXRlIHNzaCBrZXkK',
token: 'dGVzdC10b2tlbg==',
username: 'dGVzdC11c2VybmFtZQ==',
},
});
});

it('should create correct gitlab git server secret', () => {
const object = createGitlabGitServerSecretInstance({
sshPrivateKey: 'test private ssh key',
secretString: 'test-secret-string',
token: 'test-token',
});

expect(object).toEqual({
apiVersion: 'v1',
kind: 'Secret',
metadata: { name: 'ci-github' },
data: {
id_rsa: 'dGVzdCBwcml2YXRlIHNzaCBrZXkK',
secretString: 'dGVzdC1zZWNyZXQtc3RyaW5n',
token: 'dGVzdC10b2tlbg==',
},
});
});
Expand Down
64 changes: 51 additions & 13 deletions src/k8s/Secret/utils/createGitServerSecretInstance/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,70 @@
import { GIT_PROVIDERS } from '../../../../constants/gitProviders';
import { DeepPartial } from '../../../../types/global';
import { EDPKubeObjectInterface } from '../../../../types/k8s';
import { safeEncode } from '../../../../utils/decodeEncode';

export const createGitServerSecretInstance = ({
name,
gitUser,
export const createGerritGitServerSecretInstance = ({
sshPrivateKey,
sshPublicKey,
username,
}: {
sshPrivateKey: string;
sshPublicKey: string;
username: string;
}): EDPKubeObjectInterface => {
return {
apiVersion: 'v1',
kind: 'Secret',
metadata: {
name: 'gerrit-ciuser-sshkey',
},
data: {
id_rsa: safeEncode(sshPrivateKey.trim() + '\n'),
'id_rsa.pub': safeEncode(sshPublicKey),
username: safeEncode(username),
},
};
};

export const createGithubGitServerSecretInstance = ({
sshPrivateKey,
token,
gitProvider,
username,
}: {
name: string;
gitUser: string;
sshPrivateKey: string;
token: string;
gitProvider: GIT_PROVIDERS;
}): DeepPartial<EDPKubeObjectInterface> => {
const _name = gitProvider === GIT_PROVIDERS.GERRIT ? `${name}-config` : `ci-${gitProvider}`;
username: string;
}): EDPKubeObjectInterface => {
return {
apiVersion: 'v1',
kind: 'Secret',
metadata: {
name: 'ci-github',
},
data: {
id_rsa: safeEncode(sshPrivateKey.trim() + '\n'),
token: safeEncode(token),
username: safeEncode(username),
},
};
};

export const createGitlabGitServerSecretInstance = ({
sshPrivateKey,
secretString,
token,
}: {
sshPrivateKey: string;
secretString: string;
token: string;
}): EDPKubeObjectInterface => {
return {
apiVersion: 'v1',
kind: 'Secret',
metadata: {
name: _name,
name: 'ci-github',
},
data: {
username: safeEncode(gitUser),
id_rsa: safeEncode(sshPrivateKey.trim() + '\n'),
secretString: safeEncode(secretString),
token: safeEncode(token),
},
};
Expand Down
26 changes: 26 additions & 0 deletions src/k8s/Secret/utils/editGitServerSecretInstance/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { set } from 'lodash';
import { FormNameObject } from '../../../../types/forms';
import { SecretKubeObjectInterface } from '../../types';

export const editGitServerSecretInstance = (
names: {
[key: string]: FormNameObject;
},
kubeObjectData: SecretKubeObjectInterface,
formValues: {
[key: string]: any;
}
): SecretKubeObjectInterface => {
const base = { ...kubeObjectData };

for (const [propKey, propValue] of Object.entries(formValues)) {
if (names[propKey].notUsedInFormData) {
continue;
}

const propPath = names[propKey].path;
set(base, propPath, propValue);
}

return base;
};
14 changes: 12 additions & 2 deletions src/pages/edp-configuration/components/ConfigurationBody/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const ConfigurationBody = ({
);

const isLoading = items === null;
const singleItem = items?.length === 1;

return (
<ConditionalWrapper
Expand Down Expand Up @@ -116,13 +117,22 @@ export const ConfigurationBody = ({
return (
<Grid item xs={12} key={key}>
<Accordion
expanded={expandedPanel === key}
expanded={
singleItem ? true : expandedPanel === key
}
onChange={handleChange(key)}
>
<AccordionSummary
expandIcon={
<Icon icon={ICONS.ARROW_DOWN} />
singleItem ? null : (
<Icon icon={ICONS.ARROW_DOWN} />
)
}
style={{
cursor: singleItem
? 'default'
: 'pointer',
}}
>
<Grid
container
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import React from 'react';
import { PageLogicWrapper } from '../../../../components/PageLogicWrapper';
import { DialogContextProvider } from '../../../../providers/Dialog';
import { DynamicDataContextProvider } from './providers/DynamicData/provider';
import { PageView } from './view';

export default function () {
return (
<PageLogicWrapper>
<DialogContextProvider>
<PageView />
<DynamicDataContextProvider>
<PageView />
</DynamicDataContextProvider>
</DialogContextProvider>
</PageLogicWrapper>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import { DynamicDataContextProviderValue } from './types';

export const DynamicDataContext = React.createContext<DynamicDataContextProviderValue>({
data: {
gitServer: null,
gitServerSecret: null,
},
isLoading: false,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import React from 'react';
import { DynamicDataContext } from './context';

export const useDynamicDataContext = () => React.useContext(DynamicDataContext);
Loading

0 comments on commit dcf5906

Please sign in to comment.