|
| 1 | +import { useCallback } from 'react'; |
| 2 | +import jsYaml from 'js-yaml'; |
| 3 | + |
| 4 | +import { useNotifications } from 'hooks'; |
| 5 | +import { useInitRepoMutation, useLazyGetRepoQuery } from 'services/repo'; |
| 6 | + |
| 7 | +import { getPathWithoutProtocol, getRepoDirFromUrl, getRepoName, slugify } from '../../../../libs/repo'; |
| 8 | +import { getRunSpecConfigurationResources } from '../helpers/getRunSpecConfigurationResources'; |
| 9 | + |
| 10 | +// TODO add next fields: volumes, repos, |
| 11 | +const supportedFields: (keyof TDevEnvironmentConfiguration)[] = [ |
| 12 | + 'type', |
| 13 | + 'init', |
| 14 | + 'inactivity_duration', |
| 15 | + 'image', |
| 16 | + 'user', |
| 17 | + 'privileged', |
| 18 | + 'entrypoint', |
| 19 | + 'working_dir', |
| 20 | + 'registry_auth', |
| 21 | + 'python', |
| 22 | + 'nvcc', |
| 23 | + 'env', |
| 24 | + 'docker', |
| 25 | + 'backends', |
| 26 | + 'regions', |
| 27 | + 'instance_types', |
| 28 | + 'spot_policy', |
| 29 | + 'retry', |
| 30 | + 'max_duration', |
| 31 | + 'max_price', |
| 32 | + 'idle_duration', |
| 33 | + 'utilization_policy', |
| 34 | + 'fleets', |
| 35 | + 'repos', |
| 36 | +]; |
| 37 | + |
| 38 | +export const useGetRunSpecFromYaml = ({ projectName = '' }) => { |
| 39 | + const [pushNotification] = useNotifications(); |
| 40 | + const [getRepo] = useLazyGetRepoQuery(); |
| 41 | + const [initRepo] = useInitRepoMutation(); |
| 42 | + |
| 43 | + const getRepoData = useCallback( |
| 44 | + async (repos: TEnvironmentConfigurationRepo[]) => { |
| 45 | + const [firstRepo] = repos; |
| 46 | + |
| 47 | + if (!firstRepo || !firstRepo.url) { |
| 48 | + return {}; |
| 49 | + } |
| 50 | + |
| 51 | + const prefix = getRepoName(firstRepo.url); |
| 52 | + const uniqKey = getPathWithoutProtocol(firstRepo.url); |
| 53 | + const repoId = await slugify(prefix, uniqKey); |
| 54 | + const repoDir = getRepoDirFromUrl(firstRepo.url); |
| 55 | + |
| 56 | + try { |
| 57 | + await getRepo({ project_name: projectName, repo_id: repoId, include_creds: true }).unwrap(); |
| 58 | + } catch (_) { |
| 59 | + initRepo({ |
| 60 | + project_name: projectName, |
| 61 | + repo_id: repoId, |
| 62 | + repo_info: { |
| 63 | + repo_type: 'remote', |
| 64 | + repo_name: prefix, |
| 65 | + }, |
| 66 | + repo_creds: { clone_url: firstRepo.url, private_key: null, oauth_token: null }, |
| 67 | + }) |
| 68 | + .unwrap() |
| 69 | + .catch(console.error); |
| 70 | + } |
| 71 | + |
| 72 | + return { |
| 73 | + repo_id: repoId, |
| 74 | + repo_data: { |
| 75 | + repo_type: 'remote', |
| 76 | + repo_name: prefix, |
| 77 | + repo_branch: firstRepo.branch ?? null, |
| 78 | + repo_hash: firstRepo.hash ?? null, |
| 79 | + repo_config_name: null, |
| 80 | + repo_config_email: null, |
| 81 | + }, |
| 82 | + repo_code_hash: null, |
| 83 | + repo_dir: repoDir ?? null, |
| 84 | + }; |
| 85 | + }, |
| 86 | + [projectName, getRepo, initRepo], |
| 87 | + ); |
| 88 | + |
| 89 | + const getRunSpecFromYaml = useCallback( |
| 90 | + async (yaml: string) => { |
| 91 | + let parsedYaml; |
| 92 | + |
| 93 | + try { |
| 94 | + parsedYaml = (await jsYaml.load(yaml)) as { [key: string]: unknown }; |
| 95 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 96 | + } catch (_) { |
| 97 | + pushNotification({ |
| 98 | + type: 'error', |
| 99 | + content: 'Invalid YAML', |
| 100 | + }); |
| 101 | + |
| 102 | + window.scrollTo(0, 0); |
| 103 | + |
| 104 | + throw new Error('Invalid YAML'); |
| 105 | + } |
| 106 | + |
| 107 | + const { name, ...otherFields } = parsedYaml; |
| 108 | + |
| 109 | + const runSpec: TRunSpec = { |
| 110 | + run_name: name as string, |
| 111 | + configuration: {} as TDevEnvironmentConfiguration, |
| 112 | + }; |
| 113 | + |
| 114 | + for (const fieldName of Object.keys(otherFields)) { |
| 115 | + switch (fieldName) { |
| 116 | + case 'ide': |
| 117 | + runSpec.configuration.ide = otherFields[fieldName] as TIde; |
| 118 | + break; |
| 119 | + case 'resources': |
| 120 | + runSpec.configuration.resources = getRunSpecConfigurationResources(otherFields[fieldName]); |
| 121 | + break; |
| 122 | + case 'repos': { |
| 123 | + const repoData = await getRepoData(otherFields['repos'] as TEnvironmentConfigurationRepo[]); |
| 124 | + Object.assign(runSpec, repoData); |
| 125 | + break; |
| 126 | + } |
| 127 | + default: |
| 128 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 129 | + // @ts-expect-error |
| 130 | + if (!supportedFields.includes(fieldName)) { |
| 131 | + throw new Error(`Unsupported field: ${fieldName}`); |
| 132 | + } |
| 133 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 134 | + // @ts-expect-error |
| 135 | + runSpec.configuration[fieldName] = otherFields[fieldName]; |
| 136 | + break; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return runSpec; |
| 141 | + }, |
| 142 | + [pushNotification, getRepoData], |
| 143 | + ); |
| 144 | + |
| 145 | + return [getRunSpecFromYaml]; |
| 146 | +}; |
0 commit comments