/** * Copyright (c) OpenSpug Organization. https://github.com/openspug/spug * Copyright (c) * Released under the AGPL-3.0 License. */ import React, { useState, useEffect } from 'react'; import { observer } from 'mobx-react'; import { Modal, Form, Input, Select, DatePicker, Button, message } from 'antd'; import { LoadingOutlined, SyncOutlined } from '@ant-design/icons'; import HostSelector from './HostSelector'; import { http, history, includes } from 'libs'; import store from './store'; import lds from 'lodash'; import moment from 'moment'; function NoVersions() { return (
未找到符合条件的版本,
) } export default observer(function () { const [form] = Form.useForm(); const [visible, setVisible] = useState(false); const [loading, setLoading] = useState(false); const [repositories, setRepositories] = useState([]); const [host_ids, setHostIds] = useState([]); const [plan, setPlan] = useState(store.record.plan); const [fetching, setFetching] = useState(false); const [git_type, setGitType] = useState(); const [extra, setExtra] = useState([]); const [extra1, setExtra1] = useState(); const [extra2, setExtra2] = useState(); const [versions, setVersions] = useState({}); useEffect(() => { const {app_host_ids, host_ids} = store.record; setHostIds(lds.clone(host_ids || app_host_ids)); fetchVersions() // eslint-disable-next-line react-hooks/exhaustive-deps }, []) function fetchVersions() { setFetching(true); const {app_id, deploy_id} = store.record const p1 = http.get(`/api/app/deploy/${deploy_id}/versions/`, {timeout: 300000}) const p2 = http.get('/api/repository/', {params: {app_id}}) Promise.all([p1, p2]) .then(([res1, res2]) => { if (!versions.branches) _initial(res1, res2) setVersions(res1) setRepositories(res2) }) .finally(() => setFetching(false)) } function handleSubmit() { if (host_ids.length === 0) { return message.error('请至少选择一个要发布的主机') } setLoading(true); const formData = form.getFieldsValue(); formData['id'] = store.record.id; formData['deploy_id'] = store.record.deploy_id; formData['host_ids'] = host_ids; formData['type'] = store.record.type; formData['extra'] = [git_type, extra1, extra2]; if (plan) formData.plan = plan.format('YYYY-MM-DD HH:mm:00'); http.post('/api/deploy/request/ext1/', formData) .then(res => { message.success('操作成功'); store.ext1Visible = false; store.fetchRecords() }, () => setLoading(false)) } function _setDefault(type, new_extra, new_versions, new_repositories) { const now_extra = new_extra || extra; const now_versions = new_versions || versions; const now_repositories = new_repositories || repositories; const {branches, tags} = now_versions; if (type === 'branch') { let [branch, commit] = [now_extra[1], null]; if (branches[branch]) { commit = lds.get(branches[branch], '0.id') } else { branch = lds.get(Object.keys(branches), 0) commit = lds.get(branches, [branch, 0, 'id']) } setExtra1(branch) setExtra2(commit) } else if (type === 'tag') { setExtra1(lds.get(Object.keys(tags), 0)) setExtra2(null) } else { setExtra1(lds.get(now_repositories, '0.id')) setExtra2(null) } } function _initial(versions, repositories) { const {branches, tags} = versions; if (branches && tags) { for (let item of store.records) { if (item.extra && item.deploy_id === store.record.deploy_id) { const type = item.extra[0]; setExtra(item.extra); setGitType(type); return _setDefault(type, item.extra, versions, repositories); } } setGitType('branch'); const branch = lds.get(Object.keys(branches), 0); const commit = lds.get(branches, [branch, 0, 'id']) setExtra1(branch); setExtra2(commit) } } function switchType(v) { setGitType(v); _setDefault(v) } function switchExtra1(v) { setExtra1(v) if (git_type === 'branch') { setExtra2(lds.get(versions.branches[v], '0.id')) } } const {app_host_ids, type, rb_id} = store.record; const {branches, tags} = versions; return ( store.ext1Visible = false} confirmLoading={loading} onOk={handleSubmit}>
根据网络情况,首次刷新可能会很慢,请耐心等待。 clone 失败? }> {fetching ? : } {git_type === 'branch' && ( )} {host_ids.length > 0 && ( 已选择 {host_ids.length} 台(可选{app_host_ids.length}) )} {type !== '2' && ( {plan ? 大约 {plan.fromNow()} : null} )}
{visible && setVisible(false)} onOk={ids => setHostIds(ids)}/>}
) })