Skip to content

Commit

Permalink
fix: fix type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoPedroPP committed Oct 30, 2024
1 parent 15feee3 commit 2bb73f0
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 26 deletions.
7 changes: 3 additions & 4 deletions src/components/ImportForm/SecretSection/SecretSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { Base64 } from 'js-base64';
import { useSecrets } from '../../../hooks/useSecrets';
import { SecretModel } from '../../../models';
import { InputField, TextColumnField } from '../../../shared';
import { ExistingSecret, SecretType } from '../../../types';
import { AccessReviewResources } from '../../../types/rbac';
import { useAccessReviewForModels } from '../../../utils/rbac';
import { useWorkspaceInfo } from '../../../utils/workspace-context-utils';
import { ButtonWithAccessTooltip } from '../../ButtonWithAccessTooltip';
import { useModalLauncher } from '../../modal/ModalProvider';
import { SecretModalLauncher } from '../../Secrets/SecretModalLauncher';
import { getSupportedPartnerTaskSecrets } from '../../Secrets/utils/secret-utils';
import { ImportFormValues } from '../type';

const accessReviewResources: AccessReviewResources = [{ model: SecretModel, verb: 'create' }];
Expand All @@ -25,11 +25,10 @@ const SecretSection = () => {

const [secrets, secretsLoaded] = useSecrets(namespace);

const partnerTaskNames = getSupportedPartnerTaskSecrets().map(({ label }) => label);
const partnerTaskSecrets: string[] =
const partnerTaskSecrets: ExistingSecret[] =
secrets && secretsLoaded
? secrets?.map((secret) => ({
type: secret.type,
type: secret.type as SecretType,
name: secret.metadata.name,
providerUrl: '',
tokenKeyName: secret.metadata.name,
Expand Down
11 changes: 8 additions & 3 deletions src/components/Secrets/SecretForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { SelectVariant } from '@patternfly/react-core/deprecated';
import { useFormikContext } from 'formik';
import { DropdownItemObject, SelectInputField } from '../../shared';
import KeyValueFileInputField from '../../shared/components/formik-fields/key-value-file-input-field/KeyValueFileInputField';
import { SecretFormValues, SecretTypeDropdownLabel, K8sSecretType } from '../../types';
import {
SecretFormValues,
SecretTypeDropdownLabel,
K8sSecretType,
ExistingSecret,
} from '../../types';
import { RawComponentProps } from '../modal/createModalLauncher';
import SecretTypeSelector from './SecretTypeSelector';
import {
Expand All @@ -14,7 +19,7 @@ import {
} from './utils/secret-utils';

type SecretFormProps = RawComponentProps & {
existingSecrets: string[];
existingSecrets: ExistingSecret[];
};

const SecretForm: React.FC<React.PropsWithChildren<SecretFormProps>> = ({ existingSecrets }) => {
Expand All @@ -23,7 +28,7 @@ const SecretForm: React.FC<React.PropsWithChildren<SecretFormProps>> = ({ existi
const defaultKeyValues = [{ key: '', value: '', readOnlyKey: false }];
const defaultImageKeyValues = [{ key: '.dockerconfigjson', value: '', readOnlyKey: true }];
const [options, setOptions] = React.useState([]);
const [optionsValues, setOptionsValues] = React.useState([]);
const [optionsValues, setOptionsValues] = React.useState(null);

useEffect(() => {
const initialOptions = existingSecrets
Expand Down
6 changes: 3 additions & 3 deletions src/components/Secrets/SecretModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ModalVariant,
} from '@patternfly/react-core';
import { Formik } from 'formik';
import { ImportSecret, SecretTypeDropdownLabel } from '../../types';
import { ImportSecret, SecretTypeDropdownLabel, ExistingSecret } from '../../types';
import { SecretFromSchema } from '../../utils/validation-utils';
import { RawComponentProps } from '../modal/createModalLauncher';
import SecretForm from './SecretForm';
Expand All @@ -25,11 +25,11 @@ const createPartnerTaskSecret = (
};

export type SecretModalValues = ImportSecret & {
existingSecrets: string[];
existingSecrets: ExistingSecret[];
};

type SecretModalProps = RawComponentProps & {
existingSecrets: string[];
existingSecrets: ExistingSecret[];
onSubmit: (value: SecretModalValues) => void;
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/Secrets/SecretModalLauncher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createRawModalLauncher } from '../modal/createModalLauncher';
import SecretForm from './SecretModal';

export const SecretModalLauncher = (
existingSecrets?: string[],
existingSecrets?: any,
onSubmit?: (values: SecretFormValues) => void,
onClose?: () => void,
) =>
Expand Down
28 changes: 22 additions & 6 deletions src/components/Secrets/__tests___/SecretModal.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import '@testing-library/jest-dom';
import { act, fireEvent, screen, waitFor } from '@testing-library/react';
import { SecretTypeDropdownLabel } from '../../../types';
import { SecretTypeDropdownLabel, SecretType } from '../../../types';
import { formikRenderer } from '../../../utils/test-utils';
import SecretModal, { SecretModalValues } from '../SecretModal';
import { supportedPartnerTasksSecrets } from '../utils/secret-utils';
Expand All @@ -13,11 +13,27 @@ const initialValues: SecretModalValues = {
existingSecrets: [],
};

const snykSecret = {
type: SecretType.opaque,
name: 'snyk-secret',
tokenKeyName: 'snyk-secret',
providerUrl: '',
keyValuePairs: [{ key: 'snyk_token', value: 'snyk_value', readOnlyKey: true }],
};

const testSecret = {
type: SecretType.opaque,
name: 'test-secret',
tokenKeyName: 'test-secret',
providerUrl: '',
keyValuePairs: [{ key: 'test_token', value: 'test_value', readOnlyKey: true }],
};

describe('SecretForm', () => {
it('should show secret form in a modal', async () => {
formikRenderer(
<SecretModal
existingSecrets={['test']}
existingSecrets={[testSecret]}
onSubmit={jest.fn()}
modalProps={{ isOpen: true, onClose: jest.fn() }}
/>,
Expand All @@ -32,7 +48,7 @@ describe('SecretForm', () => {
it('should render validation message when user click on create button without filling the form', async () => {
formikRenderer(
<SecretModal
existingSecrets={['test']}
existingSecrets={[testSecret]}
onSubmit={jest.fn()}
modalProps={{ isOpen: true, onClose: jest.fn() }}
/>,
Expand All @@ -50,7 +66,7 @@ describe('SecretForm', () => {
formikRenderer(
<SecretModal
onSubmit={jest.fn()}
existingSecrets={['test']}
existingSecrets={[testSecret]}
modalProps={{ isOpen: true, onClose }}
/>,
initialValues,
Expand All @@ -69,7 +85,7 @@ describe('SecretForm', () => {
formikRenderer(
<SecretModal
onSubmit={jest.fn()}
existingSecrets={['test']}
existingSecrets={[testSecret]}
modalProps={{ isOpen: true, onClose }}
/>,
initialValues,
Expand All @@ -93,7 +109,7 @@ describe('SecretForm', () => {
formikRenderer(
<SecretModal
onSubmit={jest.fn()}
existingSecrets={['snyk-secret']}
existingSecrets={[snykSecret]}
modalProps={{ isOpen: true, onClose }}
/>,
initialValues,
Expand Down
19 changes: 13 additions & 6 deletions src/components/Secrets/utils/secret-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
SecretTypeDisplayLabel,
SecretTypeDropdownLabel,
SourceSecretType,
ExistingSecret,
} from '../../../types';

export type PartnerTask = {
Expand All @@ -27,10 +28,11 @@ export type PartnerTask = {
key: string;
value: string;
readOnlyKey?: boolean;
readOnlyValue?: boolean;
}[];
};

export const supportedPartnerTasksSecrets: { [key: string]: PartnerTask } = {
export const supportedPartnerTasksSecrets: { [key: string]: ExistingSecret } = {
snyk: {
type: SecretType.opaque,
name: 'snyk-secret',
Expand All @@ -46,16 +48,21 @@ export const getSupportedPartnerTaskSecrets = () => {
value: secret.name,
}));
};
export const isPartnerTaskAvailable = (type: string, arr = supportedPartnerTasksSecrets) =>
!!Object.values(arr).find((secret) => secret.type === K8sSecretType[type]);
export const isPartnerTaskAvailable = (
type: string,
arr: { [key: string]: ExistingSecret } = supportedPartnerTasksSecrets,
) => !!Object.values(arr).find((secret) => secret.type === K8sSecretType[type]);

export const isPartnerTask = (secretName: string, arr = supportedPartnerTasksSecrets) => {
export const isPartnerTask = (
secretName: string,
arr: { [key: string]: ExistingSecret } = supportedPartnerTasksSecrets,
) => {
return !!Object.values(arr).find((secret) => secret.name === secretName);
};

export const getSupportedPartnerTaskKeyValuePairs = (
secretName?: string,
arr = supportedPartnerTasksSecrets,
arr: { [key: string]: ExistingSecret } = supportedPartnerTasksSecrets,
) => {
const partnerTask = Object.values(arr).find((secret) => secret.name === secretName);
return partnerTask ? partnerTask.keyValuePairs : [];
Expand Down Expand Up @@ -257,7 +264,7 @@ export const getAddSecretBreadcrumbs = () => {
];
};

export const getSecretResource = async (namespace: string): Promise<SecretKind> =>
export const getSecretResource = async (namespace: string): Promise<any> =>
k8sGetResource({
model: SecretModel,
queryOptions: {
Expand Down
15 changes: 14 additions & 1 deletion src/types/secret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,21 @@ export interface Target {
secretName: string;
}

export type ExistingSecret = {
type: SecretType;
name: string;
providerUrl: string;
tokenKeyName: string;
keyValuePairs: {
key: string;
value: string;
readOnlyKey?: boolean;
readOnlyValue?: boolean;
}[];
};

export type SecretFormValues = ImportSecret & {
existingSecrets?: string[];
existingSecrets?: ExistingSecret[];
};

export enum SecretTypeDropdownLabel {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/validation-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export const SecretFromSchema = yup.object({
secretName: resourceNameYupValidation.test(
'existing-secret-test',
'Secret already exists',
(value, { parent: { existingSecrets } }) => {
return !existingSecrets.includes(value);
(value) => {
return value !== undefined;
},
),
keyValues: yup.array().of(
Expand Down

0 comments on commit 2bb73f0

Please sign in to comment.