迁移任务补好恢复逻辑
This commit is contained in:
+6
-3
@@ -1014,7 +1014,7 @@ URDF 缺失或大小不符时拒绝创建,不留下项目和任务。收到 20
|
||||
|
||||
```ts
|
||||
type ImportStatus = 'QUEUED' | 'RUNNING' | 'SUCCEEDED' | 'FAILED' | 'REVIEW';
|
||||
type ImportPhase = 'QUEUED' | 'CLEANING' | 'PRECHECK' | 'MIGRATING' | 'MIGRATED'
|
||||
type ImportPhase = 'QUEUED' | 'CLEANING' | 'PRECHECK' | 'MIGRATING' | 'VERIFYING' | 'MIGRATED'
|
||||
| 'NORMALIZING' | 'BRANCH_READY' | 'LINKED';
|
||||
|
||||
interface TaskView {
|
||||
@@ -1079,6 +1079,7 @@ interface TaskView {
|
||||
| CLEANING | 核验并清理失败仓库,准备重新迁移 |
|
||||
| PRECHECK | 校验来源和项目信息 |
|
||||
| MIGRATING | 迁移 Git 仓库与 LFS |
|
||||
| VERIFYING | 核验已有迁移结果,确认后接续原任务 |
|
||||
| MIGRATED | 仓库迁移已确认 |
|
||||
| NORMALIZING | 适配社区主分支 |
|
||||
| BRANCH_READY | 主分支已准备 |
|
||||
@@ -1131,7 +1132,9 @@ async function watchImport(
|
||||
- canRetry=true 时显示“重试迁移”按钮,用 retryMessage 说明操作并确认;点击后禁用重复提交,收到 202 后恢复轮询。409 表示状态已变化或核验未通过,刷新任务并显示 message,不循环重发。
|
||||
- 对于明确失败的 `GITEA_MIGRATION_REJECTED`,新任务显示 FAILED。重试后经过 CLEANING:后台核验残留仓库的任务标识、组织、名称、来源及仓库身份,清理后确认不存在,再重新迁移。沿用原 taskCode/projectCode,保留项目资料、封面及 URDF。
|
||||
- 已取得成功迁移收据时,重试只核验并继续后续步骤,不删除已迁移仓库。项目已 READY、发布过、下架或删除时拒绝重试。若项目已删除并希望重新拉取 GitHub 内容,应返回“新建迁移”重新预检、提交,创建新的项目和任务;不要对旧任务反复调用 retry。管理员查询仍不显示已删除项目。
|
||||
- 超时、进程在远端迁移期间中断等未知结果,不自动删除仓库或重发迁移;canRetry=false 时由维护人员处理。清理本身超时或回写中断,可通过重试重新核验目标是否已删除后恢复。
|
||||
- 超时、进程在远端迁移期间中断、成功响应丢失且尚未保存收据时,原任务进入 REVIEW。若项目仍符合恢复条件,canRetry=true,点击同一“重试迁移”接口返回 202,进入 VERIFYING;后台只读核验任务归属标识、GitHub 稳定来源、组织、仓库名称、私有属性、非镜像、非空状态、已记录仓库身份及稳定的来源分支提交。通过后原子保存恢复收据和冻结 SHA,再继续分支适配和项目关联;不删除仓库、不重新发起 GitHub 迁移,不自动发布,原封面和 URDF 保留。VERIFYING 是结果核验,不表示重新拉取文件。
|
||||
- 核验时仓库不存在、仍为空、无法读取、归属/来源不符或身份变化时,保持 REVIEW,项目仍被阻断;前端展示 errorCode、retryMessage,按 canRetry 决定是否允许再次核验。MIGRATION_NOT_FINISHED 可等待 Gitea 完成后再次点击;MIGRATION_TARGET_UNSAFE、MIGRATION_TARGET_REPLACED 需要先人工核对,不能改数据库标记成功或绕过身份校验。来源稳定标识发生变化时返回 SOURCE_CHANGED 并停止重试。
|
||||
- REVIEW 迁移不会被定时扫描自动核验;由管理员点击重试触发后台核验及续跑。不要为 REVIEW 自动循环发送 retry,也不要用列表“刷新状态”代替恢复操作。清理本身超时或回写中断仍沿用 CLEANING 的原有核验流程。
|
||||
- 旧版本显示 REVIEW 且错误为 GITEA_MIGRATION_REJECTED 的任务,也可以显示重试按钮。若仓库已不存在可以重迁;若仍有残留,但缺少新版本的任务归属标识,则停止清理并返回 REVIEW,避免误删。
|
||||
- 如果返回 CLEANUP_REPOSITORY_UNSAFE 或 CLEANUP_REPOSITORY_REPLACED,说明残留仓库归属无法核实或身份变化,未删除仓库。项目仍允许恢复时 canRetry=true,retryMessage 提示先人工核对清理;人工删除目标仓库后,刷新并点击同一“重试迁移”按钮即可,无需修改数据库或新建任务。重试仍先核验:仓库依然存在且无法确认归属时继续阻断,确认不存在才重新迁移。canRetry=true 表示允许再次核验,不表示仓库已清理或必然迁移成功。
|
||||
- 同一来源存在未删除项目时返回 409 `DUPLICATE_SOURCE`,错误 data 中的 projectCode 可用于跳转现有项目。若旧项目已全部删除,则允许创建全新的迁移;不要改项目名或循环创建来绕过有效项目的去重。
|
||||
@@ -1203,7 +1206,7 @@ requestId 同时可能出现在响应头 `X-Request-Id`。错误提示可附“
|
||||
- 迁移 202 显示已受理;终态停止轮询;不把 FAILED/REVIEW 显示为成功。
|
||||
- 在迁移表单上传封面和 URDF 后即可创建任务;不必等待仓库 READY 再绑定 URDF;未完整上传时不能创建任务。
|
||||
- 重复来源提供现有项目入口;任务失败、超时及待核对不自动重建迁移任务。
|
||||
- 重试按钮使用 canRetry 控制,确认框显示 retryMessage;明确失败时可清理重迁,结果未知时不盲目删库;CLEANING 阶段展示清理提示。
|
||||
- 重试按钮使用 canRetry 控制,确认框显示 retryMessage;明确失败时可清理重迁,结果未知时核验已有仓库再续跑;CLEANING 展示清理提示,VERIFYING 展示结果核验提示。
|
||||
|
||||
## Redis 接入后的兼容与错误处理
|
||||
|
||||
|
||||
@@ -87,6 +87,8 @@ CREATE TABLE project_import_task (
|
||||
|
||||
REVIEW 或遗留 WRITING 不应直接改为 IDLE:先核对 Gitea 写入、迁移收据、LFS 完成情况、仓库身份及提交。已持久化成功收据时提供只读核对后的续跑;明确 GITEA_MIGRATION_REJECTED 的任务允许通过 retry 进入 CLEANING,核验并清理归属于该任务的失败仓库后重迁。清理身份使用现有 project.gitea_repo_id 保存,保留原任务与项目,不新增字段或表。已有 phase 为 VARCHAR,无需因新增 CLEANING 阶段执行数据库变更。不提供强制成功操作。
|
||||
|
||||
迁移响应丢失、没有成功收据的 REVIEW 任务,管理员点击 retry 后进入 VERIFYING,核验通过才在同一事务保存恢复收据、仓库身份和冻结提交,接续原任务。核验不通过继续 REVIEW,不删除仓库、不重新迁移。VERIFYING 沿用现有 phase 的 VARCHAR 字段,本次恢复能力不需要新增表或字段,也无需手工修改任务状态。
|
||||
|
||||
## 已经执行过上一版管理端 SQL 的数据库
|
||||
|
||||
本节只供已经存在旧字段/旧任务表的数据库手工升级;尚未执行旧版 SQL 的数据库不要执行本节。先暂停写入并备份,不自动删除旧字段。
|
||||
|
||||
@@ -69,6 +69,9 @@ project 表只新增 moderation_reason 和 repo_write_status,来源标识、
|
||||
- 清理操作写入前持久化 CLEANING 阶段及仓库身份;删除超时或数据库回写失败时保持阻断,下次重试先核验是否已删除再继续。仓库已替换、来源不符、缺少任务归属标识时不删除;旧任务的无标识残留仓库仍需人工核对。已就绪、发布过的项目及已有成功收据的迁移不会走删除重迁流程。
|
||||
- 人工核对后已删除目标仓库的任务,可在原任务页刷新并重试,无需修改状态或清空仓库 ID。后台重新确认仓库不存在后才清除旧仓库关联和写入阻断,保留原项目与资源继续迁移;再次点击不能绕过仍存在仓库的身份校验。
|
||||
- REMOTE_RESULT_UNKNOWN 需要按 requestId、项目编码、任务编码核对日志和 Gitea,不应删除目标仓库后直接重跑。
|
||||
- MIGRATING 阶段缺少成功收据、项目仍未就绪且未发布/删除的 REVIEW 任务,可通过原 retry 接口排队进入 VERIFYING。后台核验任务标识、来源稳定身份、目标组织和名称、私有非镜像属性、非空状态、两次读取的仓库身份及来源分支提交;核验通过后在同一事务保存仓库关联标识、恢复收据和冻结 SHA,再继续原有分支适配与关联流程。不删除、不重新迁移、不自动发布;任何核验失败都保持 REVIEW,原项目资源保留。
|
||||
- 完成判断针对当前 Gitea 1.26.4 原生 `service=git` 非镜像迁移:初建库的 `empty=true`,Git/LFS 导入成功后才持久化非空状态。参见 [Gitea 原生迁移实现](https://github.com/go-gitea/gitea/blob/v1.26.4/services/repository/migrate.go#L121-L163) 与 [迁移收尾实现](https://github.com/go-gitea/gitea/blob/v1.26.4/services/repository/migrate.go#L218-L265)。缺少 empty 布尔属性的响应直接拒绝核验,不能使用 JSON 缺省值推断完成;升级 Gitea 或改用其他迁移方式前须重新验证该约定。
|
||||
- VERIFYING 由管理员点击触发,沿用现有任务消费者;没有自动扫描 REVIEW 的定时恢复任务。核验期间服务重启后仍进入 REVIEW,可再次点击核验。仓库未完成可稍后再核验,标识/来源/仓库身份不匹配时由维护人员先核对;禁止手工把 REVIEW 改成成功。
|
||||
- 已持久化成功收据且停在 MIGRATED 的任务,retry 会只读验证仓库身份、来源等后续跑。
|
||||
- 停在 NORMALIZING/BRANCH_READY 且有冻结目标 SHA 的任务,retry 会核对已适配分支和收据后续跑;验证不通过继续阻断。
|
||||
- 无强制成功、直接清除未知写入状态、任务取消接口。不能直接把 REVIEW 改成成功。
|
||||
|
||||
+92
-5
@@ -1050,9 +1050,13 @@ class AdminWorkflowTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void uncertainMigrationIsNotRepeatedAfterRestartRecovery() throws Exception {
|
||||
JsonNode task = createImport();
|
||||
void lostMigrationResponseCanBeVerifiedAndResumedWithoutRepeatingMigration() throws Exception {
|
||||
enableOss();
|
||||
JsonNode signature = importUrdfSignature();
|
||||
storeUrdfFiles(signature);
|
||||
JsonNode task = request(signed(body(post("/admin-api/project-imports"), importWithUrdf(signature))), 202).path("data");
|
||||
String code = task.path("taskCode").asText();
|
||||
String projectCode = task.path("projectCode").asText();
|
||||
gitea.migrationTimeout = true;
|
||||
imports.run(code);
|
||||
imports.recover();
|
||||
@@ -1062,18 +1066,89 @@ class AdminWorkflowTest {
|
||||
.path("status")
|
||||
.asText())
|
||||
.isEqualTo("REVIEW");
|
||||
request(signed(post("/admin-api/project-imports/" + code + "/retry")), 409);
|
||||
JsonNode queued = request(signed(post("/admin-api/project-imports/" + code + "/retry")), 202).path("data");
|
||||
assertThat(queued.path("phase").asText()).isEqualTo("VERIFYING");
|
||||
assertThat(gitea.migrations).isEqualTo(1);
|
||||
imports.run(code);
|
||||
assertThat(
|
||||
request(signed(get("/admin-api/project-imports/" + code)), 200)
|
||||
.path("data")
|
||||
.path("canRetry")
|
||||
.asBoolean())
|
||||
.isFalse();
|
||||
JsonNode recovered = request(signed(get("/admin-api/project-imports/" + code)), 200).path("data");
|
||||
assertThat(recovered.path("status").asText()).isEqualTo("SUCCEEDED");
|
||||
assertThat(jdbc.queryForObject("SELECT receipt_json FROM project_import_task WHERE task_code=?", String.class, code))
|
||||
.isNotBlank();
|
||||
JsonNode project = request(signed(get("/admin-api/projects/" + projectCode)), 200).path("data").path("project");
|
||||
assertThat(project.path("publishStatus").asText()).isEqualTo("UNPUBLISHED");
|
||||
assertThat(project.path("urdfMainUrl").asText()).contains(signature.path("batchCode").asText());
|
||||
assertThat(writeState(projectCode)).isEqualTo("IDLE");
|
||||
imports.run(code);
|
||||
assertThat(gitea.migrations).isEqualTo(1);
|
||||
assertThat(gitea.deletions).isZero();
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"marker", "source", "source-id", "owner", "name", "public", "mirror", "identity", "replaced", "empty", "read", "missing"})
|
||||
void unconfirmedMigrationTargetsStayInReviewWithoutWritingOrRemigrating(String fault) throws Exception {
|
||||
JsonNode task = createImport();
|
||||
String code = task.path("taskCode").asText();
|
||||
String projectCode = task.path("projectCode").asText();
|
||||
String name = "p_" + projectCode;
|
||||
gitea.migrationTimeout = true;
|
||||
imports.run(code);
|
||||
if (fault.equals("source-id")) SOURCES.incrementAndGet();
|
||||
com.linkerbot.vibemake.service.gitea.GiteaClient.RemoteRepository remote = gitea.inspect(name);
|
||||
if (fault.equals("marker")) gitea.migrationDescriptions.put(name, "vibemake-import:another-task");
|
||||
if (fault.equals("identity"))
|
||||
jdbc.update("UPDATE project SET gitea_repo_id=? WHERE project_code=?", remote.id() + 1, projectCode);
|
||||
gitea.replaceMigrationTargetAfterRead = fault.equals("replaced");
|
||||
gitea.failMigrationTargetRead = fault.equals("read");
|
||||
gitea.migrated.put(name, new com.linkerbot.vibemake.service.gitea.GiteaClient.RemoteRepository(
|
||||
remote.id(), fault.equals("owner") ? "other" : remote.owner(),
|
||||
fault.equals("name") ? "other" : remote.name(), !fault.equals("public"),
|
||||
fault.equals("mirror"), fault.equals("empty"),
|
||||
fault.equals("source") ? "https://github.com/example/other.git" : remote.originalUrl(), remote.defaultBranch()));
|
||||
if (fault.equals("missing")) gitea.migrated.remove(name);
|
||||
JsonNode before = request(signed(get("/admin-api/project-imports/" + code)), 200).path("data");
|
||||
assertThat(before.path("canRetry").asBoolean()).isTrue();
|
||||
assertThat(before.path("retryMessage").asText()).contains("只读核验");
|
||||
request(signed(post("/admin-api/project-imports/" + code + "/retry")), 202);
|
||||
imports.run(code);
|
||||
JsonNode after = request(signed(get("/admin-api/project-imports/" + code)), 200).path("data");
|
||||
assertThat(after.path("status").asText()).isEqualTo("REVIEW");
|
||||
assertThat(after.path("phase").asText()).isEqualTo("VERIFYING");
|
||||
assertThat(jdbc.queryForObject("SELECT receipt_json FROM project_import_task WHERE task_code=?", String.class, code)).isNull();
|
||||
assertThat(writeState(projectCode)).isEqualTo("REVIEW");
|
||||
assertThat(gitea.migrations).isEqualTo(1);
|
||||
assertThat(gitea.deletions).isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
void migrationStillInProgressCanBeVerifiedAgainOnceGiteaFinishes() throws Exception {
|
||||
JsonNode task = createImport();
|
||||
String code = task.path("taskCode").asText();
|
||||
String name = "p_" + task.path("projectCode").asText();
|
||||
gitea.migrationTimeout = true;
|
||||
imports.run(code);
|
||||
com.linkerbot.vibemake.service.gitea.GiteaClient.RemoteRepository finished = gitea.inspect(name);
|
||||
gitea.migrated.put(name, new com.linkerbot.vibemake.service.gitea.GiteaClient.RemoteRepository(
|
||||
finished.id(), finished.owner(), finished.name(), true, false, true, finished.originalUrl(), finished.defaultBranch()));
|
||||
request(signed(post("/admin-api/project-imports/" + code + "/retry")), 202);
|
||||
imports.run(code);
|
||||
JsonNode pending = request(signed(get("/admin-api/project-imports/" + code)), 200).path("data");
|
||||
assertThat(pending.path("errorCode").asText()).isEqualTo("MIGRATION_NOT_FINISHED");
|
||||
assertThat(pending.path("canRetry").asBoolean()).isTrue();
|
||||
gitea.migrated.put(name, finished);
|
||||
request(signed(post("/admin-api/project-imports/" + code + "/retry")), 202);
|
||||
imports.run(code);
|
||||
assertThat(request(signed(get("/admin-api/project-imports/" + code)), 200)
|
||||
.path("data").path("status").asText()).isEqualTo("SUCCEEDED");
|
||||
assertThat(gitea.migrations).isEqualTo(1);
|
||||
assertThat(gitea.deletions).isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectedMigrationCleansOwnedRepositoryAndRetriesWithExistingAssets() throws Exception {
|
||||
enableOss();
|
||||
@@ -1390,7 +1465,7 @@ class AdminWorkflowTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void receiptPersistenceFailureCannotBeRetriedAsSuccessfulMigration() throws Exception {
|
||||
void recoveryReceiptPersistenceFailureKeepsReviewAndRollsBackRepositoryBinding() throws Exception {
|
||||
JsonNode task = createImport();
|
||||
String code = task.path("taskCode").asText();
|
||||
jdbc.execute("ALTER TABLE project_import_task ALTER COLUMN receipt_json VARCHAR(1)");
|
||||
@@ -1403,7 +1478,19 @@ class AdminWorkflowTest {
|
||||
assertThat(failed.getStatus()).isEqualTo("REVIEW");
|
||||
assertThat(failed.getReceiptJson()).isNull();
|
||||
assertThat(gitea.migrations).isEqualTo(1);
|
||||
request(signed(post("/admin-api/project-imports/" + code + "/retry")), 409);
|
||||
request(signed(post("/admin-api/project-imports/" + code + "/retry")), 202);
|
||||
imports.run(code);
|
||||
assertThat(request(signed(get("/admin-api/project-imports/" + code)), 200)
|
||||
.path("data").path("status").asText()).isEqualTo("REVIEW");
|
||||
assertThat(jdbc.queryForObject("SELECT gitea_repo_id FROM project WHERE project_code=?", Long.class,
|
||||
task.path("projectCode").asText())).isNull();
|
||||
assertThat(gitea.migrations).isEqualTo(1);
|
||||
assertThat(gitea.deletions).isZero();
|
||||
jdbc.execute("ALTER TABLE project_import_task ALTER COLUMN receipt_json TEXT");
|
||||
request(signed(post("/admin-api/project-imports/" + code + "/retry")), 202);
|
||||
imports.run(code);
|
||||
assertThat(request(signed(get("/admin-api/project-imports/" + code)), 200)
|
||||
.path("data").path("status").asText()).isEqualTo("SUCCEEDED");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+14
-2
@@ -27,7 +27,7 @@ final class FakeGiteaClient implements GiteaClient {
|
||||
boolean deletionRejected;
|
||||
int deletions;
|
||||
boolean mainConflict;
|
||||
private final Map<String, RemoteRepository> migrated = new HashMap<>();
|
||||
final Map<String, RemoteRepository> migrated = new HashMap<>();
|
||||
private final Set<String> migrationNames = new HashSet<>();
|
||||
final Map<String, String> migrationDescriptions = new HashMap<>();
|
||||
private final Set<String> mainReady = new HashSet<>();
|
||||
@@ -40,6 +40,9 @@ final class FakeGiteaClient implements GiteaClient {
|
||||
boolean timeoutNextHead;
|
||||
boolean timeoutNextCreate;
|
||||
boolean failNextCreate;
|
||||
boolean failMigrationTargetRead;
|
||||
boolean replaceMigrationTargetAfterRead;
|
||||
int migrationTargetReads;
|
||||
|
||||
void reset() {
|
||||
repositories.clear();
|
||||
@@ -63,6 +66,9 @@ final class FakeGiteaClient implements GiteaClient {
|
||||
timeoutNextHead = false;
|
||||
timeoutNextCreate = false;
|
||||
failNextCreate = false;
|
||||
failMigrationTargetRead = false;
|
||||
replaceMigrationTargetAfterRead = false;
|
||||
migrationTargetReads = 0;
|
||||
}
|
||||
|
||||
private String sha() {
|
||||
@@ -229,7 +235,13 @@ final class FakeGiteaClient implements GiteaClient {
|
||||
|
||||
@Override
|
||||
public synchronized MigrationTarget inspectMigrationTarget(String name) {
|
||||
return new MigrationTarget(inspect(name), migrationDescriptions.get(name));
|
||||
if (failMigrationTargetRead)
|
||||
throw new GiteaFailure(504, "REMOTE_RESULT_UNKNOWN", "读取迁移目标超时", false);
|
||||
RemoteRepository remote = inspect(name);
|
||||
if (replaceMigrationTargetAfterRead && ++migrationTargetReads > 1)
|
||||
remote = new RemoteRepository(remote.id() + 1, remote.owner(), remote.name(), remote.privateRepo(),
|
||||
remote.mirror(), remote.empty(), remote.originalUrl(), remote.defaultBranch());
|
||||
return new MigrationTarget(remote, migrationDescriptions.get(name));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+91
-1
@@ -300,6 +300,9 @@ public class AdminImportService {
|
||||
if (recreate) {
|
||||
// 持久化清理意图后交给后台处理,接口不等待远端删除或迁移。
|
||||
task.setPhase("CLEANING");
|
||||
} else if (canVerifyMissingReceipt(task)) {
|
||||
// 只排队核验已有目标,不能因收据丢失而重新调用迁移或清理接口。
|
||||
task.setPhase("VERIFYING");
|
||||
} else if ("REVIEW".equals(task.getStatus())) {
|
||||
if (task.getReceiptJson() == null)
|
||||
throw BusinessException.conflict("迁移结果不明,需要运维核对,禁止重发");
|
||||
@@ -360,6 +363,7 @@ public class AdminImportService {
|
||||
.eq(Project::getProjectCode, task.getProjectCode()));
|
||||
if (!retryableProject(project)) return false;
|
||||
if (recreateOnRetry(task)) return true;
|
||||
if (canVerifyMissingReceipt(task)) return true;
|
||||
if ("CLEANING".equals(task.getPhase())) return false;
|
||||
if ("FAILED".equals(task.getStatus())) return true;
|
||||
return task.getReceiptJson() != null
|
||||
@@ -368,6 +372,13 @@ public class AdminImportService {
|
||||
&& task.getTargetSha() != null);
|
||||
}
|
||||
|
||||
private boolean canVerifyMissingReceipt(ProjectImportTask task) {
|
||||
return "REVIEW".equals(task.getStatus())
|
||||
&& task.getReceiptJson() == null
|
||||
&& Set.of("MIGRATING", "VERIFYING").contains(task.getPhase())
|
||||
&& !"SOURCE_CHANGED".equals(task.getErrorCode());
|
||||
}
|
||||
|
||||
private boolean retryableProject(Project project) {
|
||||
return project != null
|
||||
&& !Boolean.TRUE.equals(project.getDeleted())
|
||||
@@ -385,6 +396,8 @@ public class AdminImportService {
|
||||
.contains(task.getErrorCode() == null ? "" : task.getErrorCode()))
|
||||
return "残留仓库身份核验未通过;若已手工删除目标仓库,可重试核验并继续迁移,保留项目资料、封面和 URDF";
|
||||
if (recreateOnRetry(task)) return "将核验并清理本任务的失败仓库后重新迁移,保留项目资料、封面和 URDF";
|
||||
if (canVerifyMissingReceipt(task))
|
||||
return "将只读核验本任务已创建仓库的归属、来源及迁移结果,确认后继续关联;不会删除仓库或重新拉取 GitHub";
|
||||
if (task.getReceiptJson() != null) return "将核验已迁移的仓库并继续后续步骤,不会删除仓库";
|
||||
return "将重新检查来源并继续迁移,保留项目资料、封面和 URDF";
|
||||
}
|
||||
@@ -393,6 +406,76 @@ public class AdminImportService {
|
||||
return "vibemake-import:" + task.getTaskCode();
|
||||
}
|
||||
|
||||
private GiteaClient.RemoteRepository verifiedMigrationTarget(ProjectImportTask task, Project project) {
|
||||
GiteaClient.MigrationTarget target = gitea.inspectMigrationTarget(project.getGiteaRepoName());
|
||||
GiteaClient.RemoteRepository remote = target.repository();
|
||||
boolean sourceMatches;
|
||||
try {
|
||||
sourceMatches = GitHubSourceChecks.repositoryKey(remote.originalUrl())
|
||||
.equalsIgnoreCase(GitHubSourceChecks.repositoryKey(task.getSourceRepositoryUrl()));
|
||||
} catch (RuntimeException error) {
|
||||
sourceMatches = false;
|
||||
}
|
||||
if (!migrationMarker(task).equals(target.description())
|
||||
|| !properties.getGitea().getOwner().equals(project.getGiteaOwner())
|
||||
|| !project.getGiteaOwner().equals(remote.owner())
|
||||
|| !project.getGiteaRepoName().equals(remote.name())
|
||||
|| remote.id() <= 0 || !remote.privateRepo() || remote.mirror() || !sourceMatches)
|
||||
throw new MigrationFailure("MIGRATION_TARGET_UNSAFE", "已有仓库的任务标识、来源或归属不匹配,停止恢复");
|
||||
if (project.getGiteaRepoId() != null && project.getGiteaRepoId() != remote.id())
|
||||
throw new MigrationFailure("MIGRATION_TARGET_REPLACED", "已有仓库身份已变化,停止恢复");
|
||||
// Gitea 原生 service=git 非镜像迁移在 LFS 下载成功后才持久化非空状态。
|
||||
// 仅有目录、提交或分支可读不作为完成证明;缺少 empty 属性由客户端拒绝。
|
||||
if (remote.empty())
|
||||
throw new MigrationFailure("MIGRATION_NOT_FINISHED", "仓库尚未完成迁移,请稍后重新核验");
|
||||
return remote;
|
||||
}
|
||||
|
||||
private void recoverMissingReceipt(ProjectImportTask task, Project project) throws IOException {
|
||||
GitHubSourceChecks.Repository source = check(task.getSourceRepositoryUrl());
|
||||
if (source.id() != task.getSourceRepositoryId())
|
||||
throw new MigrationFailure("SOURCE_CHANGED", "来源仓库身份已变化,停止恢复");
|
||||
GiteaClient.RemoteRepository remote = verifiedMigrationTarget(task, project);
|
||||
String sha = gitea.branch(remote.repository(), task.getSourceDefaultBranch());
|
||||
if (sha == null || !sha.matches("[a-fA-F0-9]{40}|[a-fA-F0-9]{64}"))
|
||||
throw new MigrationFailure("MIGRATION_NOT_FINISHED", "来源分支提交尚不可核验,请稍后重试");
|
||||
GiteaClient.RemoteRepository confirmed = verifiedMigrationTarget(task, project);
|
||||
if (confirmed.id() != remote.id())
|
||||
throw new MigrationFailure("MIGRATION_TARGET_REPLACED", "核验期间仓库身份变化,停止恢复");
|
||||
if (!sha.equals(gitea.branch(confirmed.repository(), task.getSourceDefaultBranch())))
|
||||
throw new MigrationFailure("SOURCE_BRANCH_CHANGED", "核验期间来源分支发生变化,停止恢复");
|
||||
String data = json.writeValueAsString(confirmed);
|
||||
transactions.executeWithoutResult(status -> {
|
||||
int updated = projects.update(null, new LambdaUpdateWrapper<Project>()
|
||||
.eq(Project::getProjectCode, task.getProjectCode())
|
||||
.eq(Project::getDeleted, false)
|
||||
.in(Project::getRepoStatus, List.of("CREATING", "FAILED"))
|
||||
.eq(Project::getPublishStatus, "UNPUBLISHED")
|
||||
.isNull(Project::getPublishedCommitSha)
|
||||
.isNull(Project::getPublishedAt)
|
||||
.set(Project::getGiteaRepoId, confirmed.id())
|
||||
.set(Project::getUpdatedAt, ProjectSupport.now()));
|
||||
if (updated != 1) throw BusinessException.conflict("项目状态已变化,停止恢复");
|
||||
int saved = tasks.update(null, new LambdaUpdateWrapper<ProjectImportTask>()
|
||||
.eq(ProjectImportTask::getTaskCode, task.getTaskCode())
|
||||
.eq(ProjectImportTask::getStatus, "RUNNING")
|
||||
.eq(ProjectImportTask::getPhase, "VERIFYING")
|
||||
.isNull(ProjectImportTask::getReceiptJson)
|
||||
.set(ProjectImportTask::getReceiptJson, data)
|
||||
.set(ProjectImportTask::getTargetSha, sha)
|
||||
.set(ProjectImportTask::getPhase, "MIGRATED")
|
||||
.set(ProjectImportTask::getUpdatedAt, ProjectSupport.now()));
|
||||
if (saved != 1) throw BusinessException.conflict("任务状态已变化,停止恢复");
|
||||
});
|
||||
task.setReceiptJson(data);
|
||||
task.setTargetSha(sha);
|
||||
task.setPhase("MIGRATED");
|
||||
project.setGiteaRepoId(confirmed.id());
|
||||
locks.confirmKnownResult(task.getProjectCode());
|
||||
log.info("迁移结果核验通过 taskCode={} projectCode={} commitSha={}",
|
||||
task.getTaskCode(), task.getProjectCode(), sha);
|
||||
}
|
||||
|
||||
private GiteaClient.RemoteRepository failedRepository(ProjectImportTask task, Project project) {
|
||||
GiteaClient.MigrationTarget target;
|
||||
try {
|
||||
@@ -568,6 +651,12 @@ public class AdminImportService {
|
||||
throw new BusinessException(403, "OPERATOR_UNAVAILABLE", "任务创建人已无后台权限");
|
||||
if ("CLEANING".equals(task.getPhase())) cleanupFailedMigration(task, project);
|
||||
try {
|
||||
if ("VERIFYING".equals(task.getPhase())) {
|
||||
if (task.getReceiptJson() != null)
|
||||
throw BusinessException.conflict("任务已有迁移收据,请刷新后按原收据续跑");
|
||||
log.info("开始核验已有迁移结果 taskCode={} projectCode={}", task.getTaskCode(), task.getProjectCode());
|
||||
recoverMissingReceipt(task, project);
|
||||
}
|
||||
RepositoryMigration.Result result;
|
||||
if ("BRANCH_READY".equals(task.getPhase()) && task.getReceiptJson() != null) {
|
||||
result =
|
||||
@@ -693,7 +782,7 @@ public class AdminImportService {
|
||||
try {
|
||||
ProjectImportTask task = find(code);
|
||||
boolean review =
|
||||
Set.of("CLEANING", "MIGRATING", "MIGRATED", "NORMALIZING", "BRANCH_READY")
|
||||
Set.of("CLEANING", "MIGRATING", "VERIFYING", "MIGRATED", "NORMALIZING", "BRANCH_READY")
|
||||
.contains(task.getPhase());
|
||||
if (error instanceof GiteaFailure failure && failure.uncertain()) review = true;
|
||||
if (error instanceof MigrationFailure failure
|
||||
@@ -744,6 +833,7 @@ public class AdminImportService {
|
||||
errorCode,
|
||||
review,
|
||||
error.getClass().getSimpleName());
|
||||
SafeExceptionLog.write(log, "后台迁移异常 taskCode=" + code + " projectCode=" + task.getProjectCode(), error);
|
||||
} finally {
|
||||
if (interrupted) Thread.currentThread().interrupt();
|
||||
}
|
||||
|
||||
+8
-3
@@ -96,14 +96,18 @@ public class RestGiteaClient implements GiteaClient {
|
||||
return response;
|
||||
} catch (InterruptedException error) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new GiteaFailure(
|
||||
GiteaFailure failure = new GiteaFailure(
|
||||
504, "REMOTE_RESULT_UNKNOWN", "仓库调用被中断,写入结果需核对", !method.equals("GET"));
|
||||
failure.initCause(error);
|
||||
throw failure;
|
||||
} catch (IOException error) {
|
||||
for (Throwable cause = error; cause != null; cause = cause.getCause())
|
||||
if (cause instanceof LimitedHttpBody.TooLarge && method.equals("GET"))
|
||||
throw new GiteaFailure(502, "GITEA_RESPONSE_TOO_LARGE", "仓库响应超过读取限制", false);
|
||||
throw new GiteaFailure(
|
||||
GiteaFailure failure = new GiteaFailure(
|
||||
504, "REMOTE_RESULT_UNKNOWN", "仓库连接异常或超时,写入结果需核对", !method.equals("GET"));
|
||||
failure.initCause(error);
|
||||
throw failure;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,7 +234,8 @@ public class RestGiteaClient implements GiteaClient {
|
||||
null)
|
||||
.body(),
|
||||
false);
|
||||
if (!data.path("private").isBoolean() || !data.path("mirror").isBoolean())
|
||||
if (!data.path("private").isBoolean() || !data.path("mirror").isBoolean()
|
||||
|| !data.path("empty").isBoolean())
|
||||
throw new GiteaFailure(502, "GITEA_INVALID_RESPONSE", "无法核验迁移仓库属性", false);
|
||||
return new MigrationTarget(remote(data), data.path("description").asText(null));
|
||||
}
|
||||
|
||||
+37
-1
@@ -8,6 +8,8 @@ import com.linkerbot.vibemake.service.config.VibemakeProperties;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -132,7 +134,41 @@ class GiteaMigrationTest {
|
||||
"p_111111111111",
|
||||
Duration.ofMillis(50)))
|
||||
.isInstanceOfSatisfying(
|
||||
GiteaFailure.class, error -> assertThat(error.uncertain()).isTrue());
|
||||
GiteaFailure.class, error -> {
|
||||
assertThat(error.uncertain()).isTrue();
|
||||
assertThat(error.getCause()).isInstanceOf(java.net.http.HttpTimeoutException.class);
|
||||
});
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"private", "mirror", "empty"})
|
||||
void migrationInspectionRejectsMissingBooleanAttributes(String missingField) throws Exception {
|
||||
ObjectMapper json = new ObjectMapper();
|
||||
com.fasterxml.jackson.databind.node.ObjectNode metadata = json.createObjectNode()
|
||||
.put("id", 77).put("name", "p_fixture").put("private", true).put("mirror", false).put("empty", false);
|
||||
metadata.putObject("owner").put("login", "vibemake");
|
||||
metadata.remove(missingField);
|
||||
byte[] response = json.writeValueAsBytes(metadata);
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/api/v1/repos/vibemake/p_fixture", exchange -> {
|
||||
exchange.sendResponseHeaders(200, response.length);
|
||||
exchange.getResponseBody().write(response);
|
||||
exchange.close();
|
||||
});
|
||||
server.start();
|
||||
try {
|
||||
VibemakeProperties config = new VibemakeProperties();
|
||||
config.getGitea().setBaseUrl("http://127.0.0.1:" + server.getAddress().getPort());
|
||||
config.getGitea().setToken("fixture-token");
|
||||
GiteaClient client = new RestGiteaClient(config, json);
|
||||
assertThatThrownBy(() -> client.inspectMigrationTarget("p_fixture"))
|
||||
.isInstanceOfSatisfying(GiteaFailure.class, error -> {
|
||||
assertThat(error.code()).isEqualTo("GITEA_INVALID_RESPONSE");
|
||||
assertThat(error.uncertain()).isFalse();
|
||||
});
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user