Files
devops-platform-web/src/views/ReleaseCenterView.vue
T
湛兮 8910ee6be3 feat: 精简发布确认并支持回滚和助手操作
- 删除三个留档必填项与重复检查卡片,提交前核对真实锁定版本。

- 增加待确认操作卡片、发布回执与历史成功版本回滚入口,保护会话隔离和迟到响应。

- 折叠助手来源并清理系统设置的无效实现提示,保留并行通知配置改动。
2026-10-28 10:22:19 +08:00

494 lines
15 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { computed, reactive, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Promotion, Refresh } from '@element-plus/icons-vue'
import { devopsApi } from '../services/devopsApi'
import { confirmOperation } from '../services/operationConfirmation'
import StatusPill from '../components/StatusPill.vue'
import { usePlatformStore } from '../stores/platform'
import { formatTime } from '../utils/releases'
import type { EnvironmentName, OperationPlan } from '../types/devops'
const platform = usePlatformStore()
const route = useRoute()
const router = useRouter()
const form = reactive({
projectKey: '',
environment: 'test' as EnvironmentName,
ref: '',
})
const confirmation = reactive({ reviewed: false })
const submitting = ref(false)
const refsLoading = ref(false)
const refsError = ref('')
const submittedRunId = ref('')
let refsRequest = 0
let preparedPlan: OperationPlan | undefined
const selectedProject = computed(() =>
platform.projects.find((project) => project.key === form.projectKey),
)
const selectedEnvironment = computed(() =>
selectedProject.value?.environments.find((environment) => environment.name === form.environment),
)
const refs = computed(() => platform.projectRefs[form.projectKey])
const useTags = computed(
() => form.environment === 'production' && selectedEnvironment.value?.branchPolicy !== 'master',
)
const refOptions = computed(() =>
useTags.value ? refs.value?.tags || [] : refs.value?.branches || [],
)
const selectedRef = computed(() => refOptions.value.find((item) => item.name === form.ref))
const sha = computed(() => selectedRef.value?.commit?.sha || selectedRef.value?.commitSha || '')
const latestRun = computed(() =>
platform.runs.find(
(run) => run.projectKey === form.projectKey && run.environment === form.environment,
),
)
const submittedRun = computed(() => platform.runs.find((run) => run.id === submittedRunId.value))
const productionReady = computed(() => form.environment !== 'production' || confirmation.reviewed)
const disabledReason = computed(() => {
if (!selectedProject.value || !selectedEnvironment.value) return '请选择项目与发布环境'
if (!platform.canBuildProject(form.projectKey)) return '当前项目为只读权限,请联系管理员授权'
if (!selectedEnvironment.value.jenkinsJob) return '当前环境尚未配置构建任务'
if (refsLoading.value) return '正在读取可发布版本…'
if (refsError.value) return '版本读取失败,请重试'
if (refs.value?.source !== 'gitea') return '无法实时核对版本,请检查仓库接入后重试'
if (!refOptions.value.length)
return useTags.value
? '暂无可发布标签,请先在仓库创建版本标签'
: '暂无可发布分支,请检查仓库接入'
if (!selectedRef.value) return `请选择${useTags.value ? '版本标签' : '分支'}`
if (!productionReady.value) return '请核对目标版本并确认生产发布'
return ''
})
function resetConfirmation() {
confirmation.reviewed = false
preparedPlan = undefined
}
function applyRoute() {
const projectKey = typeof route.query.projectKey === 'string' ? route.query.projectKey : ''
const project = platform.projects.find((item) => item.key === projectKey) || platform.projects[0]
if (!project) return
form.projectKey = project.key
const environment = route.query.environment
form.environment =
project.environments.find((item) => item.name === environment)?.name ||
project.environments[0]?.name ||
'test'
}
watch(
() => [
route.query.projectKey,
route.query.environment,
platform.projects.map((project) => project.key).join('|'),
],
applyRoute,
{ immediate: true },
)
watch(
() => form.projectKey,
() => {
if (
!selectedProject.value?.environments.some(
(environment) => environment.name === form.environment,
)
)
form.environment = selectedProject.value?.environments[0]?.name || 'test'
form.ref = ''
submittedRunId.value = ''
void loadRefs()
},
)
watch(
() => [form.projectKey, form.environment],
() => {
resetConfirmation()
form.ref = ''
void router.replace({
name: 'releases',
query: { projectKey: form.projectKey, environment: form.environment },
})
},
)
watch(
[refOptions, useTags],
() => {
if (!refOptions.value.some((item) => item.name === form.ref))
form.ref = useTags.value
? ''
: refOptions.value.find((item) => item.name === selectedProject.value?.defaultBranch)
?.name ||
refOptions.value[0]?.name ||
''
},
{ immediate: true },
)
watch(() => [form.ref], resetConfirmation)
// 首次进入时 applyRoute 可能早于项目 watcher 注册。
if (form.projectKey) void loadRefs()
async function loadRefs() {
const projectKey = form.projectKey
if (!projectKey) return
const request = ++refsRequest
refsLoading.value = true
refsError.value = ''
try {
await platform.loadProjectRefs(projectKey)
} catch (error) {
if (request === refsRequest)
refsError.value = error instanceof Error ? error.message : '无法读取仓库版本'
} finally {
if (request === refsRequest) refsLoading.value = false
}
}
async function triggerRelease() {
if (submitting.value || disabledReason.value) return
submitting.value = true
try {
const plan =
preparedPlan && Date.parse(preparedPlan.expiresAt) > Date.now()
? preparedPlan
: await devopsApi.prepareOperation({
kind: 'deploy',
projectKey: form.projectKey,
environment: form.environment,
ref: form.ref,
})
preparedPlan = plan
if (!(await confirmOperation(plan))) return
const run = await platform.executeOperation(plan.confirmationToken!)
submittedRunId.value = run.id
preparedPlan = undefined
if (run.status === 'failed') ElMessage.error('发布未成功,请查看日志')
else ElMessage.success('发布已提交,可查看实际进度与日志')
} catch (error) {
ElMessage.error(error instanceof Error ? error.message : '发布提交失败,请重试')
} finally {
submitting.value = false
}
}
</script>
<template>
<div class="page-shell">
<div class="page-header">
<div>
<h1>新建发布</h1>
<p>选择项目、环境和版本,确认后开始发布。</p>
</div>
<RouterLink class="text-link" to="/runs">查看发布记录</RouterLink>
</div>
<ElAlert
v-if="submittedRun"
:type="submittedRun.status === 'failed' ? 'error' : 'success'"
:closable="false"
show-icon
:title="submittedRun.status === 'failed' ? '发布失败' : '发布已提交'"
>
{{ submittedRun.projectName }} · {{ submittedRun.ref
}}<RouterLink
class="text-link submitted-link"
:to="{ name: 'runs', query: { runId: submittedRun.id } }"
>查看进度与日志 →</RouterLink
>
</ElAlert>
<div class="release-layout">
<section class="panel release-form">
<div class="panel-header">
<h2>发布内容</h2>
</div>
<div class="panel-body">
<ElForm label-position="top" @submit.prevent="triggerRelease">
<ElFormItem label="项目"
><ElSelect
v-model="form.projectKey"
filterable
aria-label="发布项目"
placeholder="选择项目…"
><ElOption
v-for="project in platform.projects"
:key="project.key"
:label="project.name"
:value="project.key" /></ElSelect
></ElFormItem>
<ElFormItem label="发布环境"
><ElRadioGroup v-model="form.environment" aria-label="发布环境"
><ElRadioButton
v-for="environment in selectedProject?.environments || []"
:key="environment.name"
:value="environment.name"
>{{ environment.label }}</ElRadioButton
></ElRadioGroup
></ElFormItem
>
<ElFormItem :label="useTags ? '版本标签' : '分支'">
<div class="ref-control">
<ElSelect
v-model="form.ref"
filterable
:loading="refsLoading"
:disabled="refsLoading || !refOptions.length"
:aria-label="useTags ? '版本标签' : '分支'"
:placeholder="useTags ? '选择已验证的版本标签…' : '选择分支…'"
><ElOption
v-for="item in refOptions"
:key="item.name"
:label="item.name"
:value="item.name" /></ElSelect
><ElButton
:icon="Refresh"
:loading="refsLoading"
:disabled="!form.projectKey"
aria-label="重新读取版本"
title="重新读取版本"
@click="loadRefs"
/>
</div>
<div v-if="refsError" class="inline-error" role="alert">
{{ refsError }}
</div>
<div v-if="sha" class="commit-preview">
<span class="mono">{{ sha.slice(0, 10) }}</span
><span>{{ selectedRef?.commit?.message?.split('\n')[0] || '无提交说明' }}</span>
</div>
<a
v-if="selectedRef?.pullRequest?.url"
class="text-link"
:href="selectedRef.pullRequest.url"
target="_blank"
rel="noopener noreferrer"
>PR #{{ selectedRef.pullRequest.number }} · {{ selectedRef.pullRequest.title }}</a
>
<div v-if="refs?.source === 'fallback'" class="field-hint">
当前为后端备用版本列表,请确认仓库接入后再发布。
</div>
</ElFormItem>
<section v-if="form.environment === 'production'" class="production-confirmation">
<h2>生产发布确认</h2>
<p>将部署所选版本,提交前还会显示锁定的具体提交。</p>
<ElCheckbox v-model="confirmation.reviewed"
>已核对目标版本,确认发布到生产环境</ElCheckbox
>
</section>
<div class="submit-row">
<ElButton
type="primary"
:icon="Promotion"
:disabled="Boolean(disabledReason) || submitting"
:loading="submitting"
native-type="submit"
>{{ form.environment === 'production' ? '发布到生产环境' : '开始发布' }}</ElButton
><span v-if="disabledReason" class="field-hint" role="status">{{
disabledReason
}}</span>
</div>
</ElForm>
</div>
</section>
<aside class="release-context">
<section class="panel">
<div class="panel-header"><h2>目标环境</h2></div>
<div class="panel-body">
<strong class="context-project">{{ selectedProject?.name || '尚未选择项目' }}</strong>
<p class="context-environment">
{{ selectedEnvironment?.label || '—' }}
</p>
<dl>
<dt>最近发布</dt>
<dd v-if="latestRun"><StatusPill :state="latestRun.status" /></dd>
<dd v-else>尚无记录</dd>
<dt>版本</dt>
<dd class="mono">{{ latestRun?.ref || '—' }}</dd>
<dt>时间</dt>
<dd>{{ formatTime(latestRun?.startedAt) }}</dd>
</dl>
<RouterLink
v-if="latestRun"
class="text-link"
:to="{ name: 'runs', query: { runId: latestRun.id } }"
>查看上次发布 →</RouterLink
>
</div>
</section>
</aside>
</div>
</div>
</template>
<style scoped>
.release-layout {
display: grid;
grid-template-columns: minmax(0, 1fr) 290px;
align-items: start;
gap: 24px;
max-width: 1100px;
}
.release-form .panel-body {
padding: 26px;
}
.release-form :deep(.el-form-item) {
margin-bottom: 24px;
}
.release-form :deep(.el-form-item__label) {
color: #596070;
font-size: 12px;
}
.ref-control {
display: flex;
gap: 8px;
width: 100%;
}
.commit-preview {
display: flex;
flex-wrap: wrap;
gap: 8px;
width: 100%;
margin-top: 10px;
color: var(--text-muted);
font-size: 11px;
line-height: 1.6;
overflow-wrap: anywhere;
}
.commit-preview .mono {
padding: 1px 6px;
border-radius: 4px;
background: #f3f2f7;
color: #71678e;
}
.inline-error {
color: #b54a4a;
font-size: 12px;
line-height: 1.7;
margin-top: 8px;
}
.production-confirmation {
padding: 20px;
margin-top: 4px;
border: 1px solid #eadfcc;
border-radius: 7px;
background: #fdfbf7;
}
.production-confirmation h2 {
margin: 0;
font-size: 13px;
font-weight: 600;
}
.production-confirmation > p {
margin: 7px 0 18px;
font-size: 11px;
color: var(--text-muted);
}
.confirmation-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 14px;
}
.production-confirmation :deep(.el-checkbox) {
white-space: normal;
height: auto;
}
.production-confirmation :deep(.el-checkbox__label) {
font-size: 11px;
line-height: 1.8;
}
.submit-row {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 14px;
margin-top: 24px;
padding-top: 22px;
border-top: 1px solid var(--border);
}
.submit-row .field-hint {
margin: 0;
}
.context-project {
font-size: 14px;
font-weight: 550;
overflow-wrap: anywhere;
}
.context-environment {
margin: 6px 0 24px;
color: var(--text-muted);
font-size: 12px;
}
.release-context dl {
display: grid;
grid-template-columns: 64px minmax(0, 1fr);
align-items: center;
gap: 16px;
font-size: 11px;
margin: 0 0 24px;
}
.release-context dt {
color: var(--text-muted);
}
.release-context dd {
margin: 0;
overflow-wrap: anywhere;
}
.checks {
margin-top: 22px;
padding: 0 4px;
}
.checks summary {
cursor: pointer;
font-size: 12px;
color: #747b88;
margin-bottom: 15px;
}
.check-row {
margin: 12px 0;
font-size: 11px;
color: var(--text-muted);
}
.check-row > span {
display: block;
margin-bottom: 6px;
}
.check-row :deep(.el-tag) {
height: auto;
white-space: normal;
overflow-wrap: anywhere;
line-height: 1.6;
padding: 4px 7px;
}
.submitted-link {
margin-left: 15px;
}
@media (max-width: 1000px) {
.release-layout {
grid-template-columns: minmax(0, 1fr) 240px;
gap: 18px;
}
}
@media (max-width: 880px) {
.release-layout {
grid-template-columns: 1fr;
}
.release-context {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
}
.checks {
margin-top: 5px;
}
}
@media (max-width: 520px) {
.release-context,
.confirmation-grid {
grid-template-columns: 1fr;
}
.release-form .panel-body {
padding: 20px 16px;
}
.production-confirmation {
padding: 16px;
}
}
</style>