You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 8.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import KFModal from '@/components/KFModal';
  2. import { AvailableRange } from '@/enums';
  3. import { type CodeConfigData } from '@/pages/CodeConfig/List';
  4. import { addCodeConfigReq, updateCodeConfigReq } from '@/services/codeConfig';
  5. import { to } from '@/utils/promise';
  6. import { Form, Input, Radio, message, type FormRule, type ModalProps } from 'antd';
  7. import { omit } from 'lodash';
  8. import { useMemo } from 'react';
  9. export enum VerifyMode {
  10. Password = 0, // 用户名密码
  11. SSH = 1, // SSH Key
  12. }
  13. export enum OperationType {
  14. Create = 0, // 新建
  15. Update = 1, // 更新
  16. }
  17. type FormData = Partial<CodeConfigData>;
  18. interface AddCodeConfigModalProps extends Omit<ModalProps, 'onOk'> {
  19. opType: OperationType;
  20. codeConfigData?: CodeConfigData;
  21. onOk: () => void;
  22. }
  23. function AddCodeConfigModal({ opType, codeConfigData, onOk, ...rest }: AddCodeConfigModalProps) {
  24. const [form] = Form.useForm();
  25. const isPublic = Form.useWatch('code_repo_vis', form) === AvailableRange.Public;
  26. const urlExample = useMemo(
  27. () =>
  28. isPublic
  29. ? 'https://gitlink.org.cn/ci4s/ci4sManagement-cloud.git'
  30. : 'git@code.gitlink.org.cn:ci4s/ci4sManagement-cloud.git',
  31. [isPublic],
  32. );
  33. // /^(git@[\w.-]+:[\w./-]+\.git)$/
  34. const urlRules: FormRule[] = useMemo(
  35. () =>
  36. isPublic
  37. ? [
  38. {
  39. type: 'url',
  40. message: '请输入正确的 Git 地址',
  41. },
  42. ]
  43. : ([] as FormRule[]),
  44. [isPublic],
  45. );
  46. // 创建
  47. const createCodeConfig = async (formData: FormData) => {
  48. const params: FormData & { id?: number } = {
  49. ...formData,
  50. };
  51. // 清除多余的信息
  52. if (formData.code_repo_vis === AvailableRange.Public) {
  53. omit(params, ['verify_mode', 'git_user_name', 'git_password', 'ssh_key']);
  54. }
  55. if (formData.verify_mode === VerifyMode.Password) {
  56. omit(params, ['ssh_key']);
  57. } else if (formData.verify_mode === VerifyMode.SSH) {
  58. omit(params, ['git_user_name', 'git_password']);
  59. }
  60. if (opType === OperationType.Update) {
  61. params.id = codeConfigData?.id;
  62. }
  63. const request = opType === OperationType.Create ? addCodeConfigReq : updateCodeConfigReq;
  64. const [res] = await to(request(params));
  65. if (res) {
  66. message.success(opType === OperationType.Create ? '创建成功' : '修改成功');
  67. onOk?.();
  68. }
  69. };
  70. // 提交
  71. const onFinish = (formData: FormData) => {
  72. createCodeConfig(formData);
  73. };
  74. // 设置初始值
  75. const initialValues: FormData = codeConfigData ?? {
  76. code_repo_vis: AvailableRange.Public,
  77. verify_mode: VerifyMode.Password,
  78. };
  79. if (initialValues.verify_mode === undefined || initialValues.verify_mode === null) {
  80. initialValues.verify_mode = VerifyMode.Password;
  81. }
  82. return (
  83. <KFModal
  84. {...rest}
  85. title="新建代码配置"
  86. image={require('@/assets/img/create-experiment.png')}
  87. width={825}
  88. okButtonProps={{
  89. htmlType: 'submit',
  90. form: 'form',
  91. }}
  92. destroyOnClose
  93. >
  94. <Form
  95. name="form"
  96. form={form}
  97. layout="vertical"
  98. onFinish={onFinish}
  99. initialValues={initialValues}
  100. autoComplete="off"
  101. >
  102. <Form.Item
  103. label="代码仓库名称"
  104. name="code_repo_name"
  105. required
  106. rules={[
  107. {
  108. required: true,
  109. message: '请输入代码仓库名称',
  110. },
  111. ]}
  112. >
  113. <Input placeholder="请输入代码仓库名称" showCount allowClear maxLength={64} />
  114. </Form.Item>
  115. <Form.Item
  116. label="代码仓库可见性"
  117. name="code_repo_vis"
  118. rules={[
  119. {
  120. required: true,
  121. message: '请选择代码仓库可见性',
  122. },
  123. ]}
  124. >
  125. <Radio.Group>
  126. <Radio value={AvailableRange.Public}>公开</Radio>
  127. <Radio value={AvailableRange.Private}>私有</Radio>
  128. </Radio.Group>
  129. </Form.Item>
  130. <Form.Item
  131. label="Git 地址"
  132. name="git_url"
  133. rules={[
  134. {
  135. required: true,
  136. message: '请输入 Git 地址',
  137. },
  138. ...urlRules,
  139. ]}
  140. >
  141. <Input
  142. placeholder={`请输入 Git 地址,如: ${urlExample}`}
  143. showCount
  144. allowClear
  145. maxLength={256}
  146. />
  147. </Form.Item>
  148. <Form.Item
  149. label="代码分支/Tag"
  150. name="git_branch"
  151. rules={[
  152. {
  153. required: true,
  154. message: '请输入代码分支/Tag',
  155. },
  156. ]}
  157. >
  158. <Input placeholder="请输入代码分支/Tag" showCount allowClear maxLength={64} />
  159. </Form.Item>
  160. <Form.Item
  161. noStyle
  162. shouldUpdate={(prevValues, currentValues) =>
  163. prevValues?.code_repo_vis !== currentValues?.code_repo_vis
  164. }
  165. >
  166. {({ getFieldValue }) => {
  167. return getFieldValue('code_repo_vis') === AvailableRange.Private ? (
  168. <>
  169. <Form.Item
  170. label="验证方式"
  171. name="verify_mode"
  172. rules={[
  173. {
  174. required: true,
  175. message: '请选择验证方式',
  176. },
  177. ]}
  178. >
  179. <Radio.Group>
  180. <Radio value={VerifyMode.Password}>用户名/密码</Radio>
  181. <Radio value={VerifyMode.SSH}>SSH Key</Radio>
  182. </Radio.Group>
  183. </Form.Item>
  184. <Form.Item
  185. noStyle
  186. shouldUpdate={(prevValues, currentValues) =>
  187. prevValues?.verify_mode !== currentValues?.verify_mode
  188. }
  189. >
  190. {({ getFieldValue }) => {
  191. return getFieldValue('verify_mode') === VerifyMode.Password ? (
  192. <>
  193. <Form.Item
  194. label="Git 用户名"
  195. name="git_user_name"
  196. rules={[
  197. {
  198. required: true,
  199. message: '请输入 Git 用户名',
  200. },
  201. ]}
  202. >
  203. <Input
  204. placeholder="请输入 Git 用户名"
  205. autoComplete="off"
  206. showCount
  207. allowClear
  208. maxLength={64}
  209. />
  210. </Form.Item>
  211. <Form.Item
  212. label="Git 密码"
  213. name="git_password"
  214. rules={[
  215. {
  216. required: true,
  217. message: '请输入 Git 密码',
  218. },
  219. ]}
  220. >
  221. <Input.Password
  222. autoComplete="new-password"
  223. placeholder="请输入 Git 密码"
  224. allowClear
  225. />
  226. </Form.Item>
  227. </>
  228. ) : (
  229. <Form.Item
  230. label="SSH Key"
  231. name="ssh_key"
  232. rules={[
  233. {
  234. required: true,
  235. message: '请输入 SSH Key',
  236. },
  237. ]}
  238. >
  239. <Input.TextArea
  240. placeholder="请输入 SSH Key"
  241. showCount
  242. maxLength={4096}
  243. autoSize={{ minRows: 4, maxRows: 8 }}
  244. allowClear
  245. />
  246. </Form.Item>
  247. );
  248. }}
  249. </Form.Item>
  250. </>
  251. ) : null;
  252. }}
  253. </Form.Item>
  254. </Form>
  255. </KFModal>
  256. );
  257. }
  258. export default AddCodeConfigModal;