Skip to content

Commit

Permalink
Revert "chore: merge changes from alternative branch"
Browse files Browse the repository at this point in the history
This reverts commit 2ee7598, reversing
changes made to dbbdf42.
  • Loading branch information
JoaoPedroPP committed Oct 25, 2024
1 parent 2c028d4 commit bc6a3d4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/components/ImportForm/SecretSection/SecretSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const SecretSection = () => {
secrets && secretsLoaded
? secrets
?.filter((rs) => partnerTaskNames.includes(rs.metadata.name))
?.map((s) => s.metadata.name)
?.map((s) => s.metadata.name) || []
: [];

const onSubmit = React.useCallback(
Expand Down
37 changes: 14 additions & 23 deletions src/components/Secrets/SecretForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,32 @@ import React from 'react';
import { Form } from '@patternfly/react-core';
import { SelectVariant } from '@patternfly/react-core/deprecated';
import { useFormikContext } from 'formik';
import { useSecrets } from '../../hooks/useSecrets';
import { DropdownItemObject, SelectInputField } from '../../shared';
import KeyValueFileInputField from '../../shared/components/formik-fields/key-value-file-input-field/KeyValueFileInputField';
import { SecretFormValues, SecretTypeDropdownLabel } from '../../types';
import { useWorkspaceInfo } from '../../utils/workspace-context-utils';
import { RawComponentProps } from '../modal/createModalLauncher';
import SecretTypeSelector from './SecretTypeSelector';
import { secretsList } from './utils/secret-utils';
import {
getSupportedPartnerTaskKeyValuePairs,
isPartnerTask,
getSupportedPartnerTaskSecrets,
} from './utils/secret-utils';

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

const SecretForm: React.FC<React.PropsWithChildren<SecretFormProps>> = ({ existingSecrets }) => {
const { values, setFieldValue } = useFormikContext<SecretFormValues>();
const { namespace } = useWorkspaceInfo();
const [secrets, secretsLoaded] = useSecrets(namespace);

const defaultKeyValues = [{ key: '', value: '', readOnlyKey: false }];
const defaultImageKeyValues = [{ key: '.dockerconfigjson', value: '', readOnlyKey: true }];

const sl = secretsLoaded ? secretsList(secrets) : [];
const initialOptions = Object.values(sl)
.map((secret) => ({ value: secret.name, label: secret.name }))
.filter((secret) => !existingSecrets.includes(secret.value));
const initialOptions = getSupportedPartnerTaskSecrets().filter(
(secret) => !existingSecrets.includes(secret.value),
);
const [options, setOptions] = React.useState(initialOptions);
const currentTypeRef = React.useRef(values.type);

const isSecretTask = (secretName: string) => {
return !!Object.values(sl).find((secret) => secret.name === secretName);
};

const getSecretsTaskKeyValuePairs = (secretName?: string) => {
const secretTask = Object.values(sl).find((secret) => secret.name === secretName);
return secretTask ? secretTask.keyValuePairs : [];
};

const clearKeyValues = () => {
const newKeyValues = values.keyValues.filter((kv) => !kv.readOnlyKey);
setFieldValue('keyValues', [...(newKeyValues.length ? newKeyValues : defaultKeyValues)]);
Expand Down Expand Up @@ -68,7 +57,9 @@ const SecretForm: React.FC<React.PropsWithChildren<SecretFormProps>> = ({ existi
currentTypeRef.current = type;
if (type === SecretTypeDropdownLabel.image) {
resetKeyValues();
values.secretName && isSecretTask(values.secretName) && setFieldValue('secretName', '');
values.secretName &&
isPartnerTask(values.secretName) &&
setFieldValue('secretName', '');
} else {
setOptions(initialOptions);
clearKeyValues();
Expand All @@ -89,15 +80,15 @@ const SecretForm: React.FC<React.PropsWithChildren<SecretFormProps>> = ({ existi
toggleId="secret-name-toggle"
toggleAriaLabel="secret-name-dropdown"
onClear={() => {
if (currentTypeRef.current !== values.type || isSecretTask(values.secretName)) {
if (currentTypeRef.current !== values.type || isPartnerTask(values.secretName)) {
clearKeyValues();
}
}}
onSelect={(e, value) => {
if (isSecretTask(value)) {
if (isPartnerTask(value)) {
setFieldValue('keyValues', [
...values.keyValues.filter((kv) => !kv.readOnlyKey && (!!kv.key || !!kv.value)),
...getSecretsTaskKeyValuePairs(value),
...getSupportedPartnerTaskKeyValuePairs(value),
]);
}
setFieldValue('secretName', value);
Expand Down
26 changes: 1 addition & 25 deletions src/components/Secrets/utils/secret-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,10 @@ export const supportedPartnerTasksSecrets: { [key: string]: PartnerTask } = {
name: 'snyk-secret',
providerUrl: 'https://snyk.io/',
tokenKeyName: 'snyk_token',
keyValuePairs: [{ key: 'snyk_token', value: '', readOnlyKey: true, readOnlyValue: false }],
keyValuePairs: [{ key: 'snyk_token', value: '', readOnlyKey: true }],
},
};

export const secretsList = (secrets) => {
const partnerSecretNames = {
'snyk-secret': supportedPartnerTasksSecrets.snyk,
};
secrets
.filter((secret) => secret.type === 'Opaque')
.forEach((secret) => {
partnerSecretNames[secret.metadata.name] = {
type: secret.type,
name: secret.metadata.name,
providerUrl:
secret.metadata.name === 'snyk-secret' ? 'https://snyk.io/' : 'https://kube.io',
tokenKeyName:
secret.metadata.name === 'snyk-secret' ? 'secret_token' : secret.metadata.name,
keyValuePairs: Object.keys(secret.data).map((key) => ({
key,
value: Base64.decode(secret.data[key]),
readOnlyKey: true,
})),
};
});
return partnerSecretNames;
};

export const getSupportedPartnerTaskSecrets = () => {
return Object.values(supportedPartnerTasksSecrets).map((secret) => ({
label: secret.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ const KeyValueFileInputField: React.FC<
label="Value"
name={`${name}.${idx.toString()}.value`}
filenamePlaceholder="Drag a file here or upload one"
isDisabled={v.readOnlyValue ?? v.readOnlyKey}
onDataChange={(ev, data: string) => {
setFieldValue(`${name}.${idx.toString()}.value`, data);
onChange && onChange(data, `${name}.${idx.toString()}.value`);
Expand Down

0 comments on commit bc6a3d4

Please sign in to comment.