后台管理迁移开源项目失败重试按钮

This commit is contained in:
lxp
2026-10-21 13:13:47 +08:00
parent 2f5949b0ab
commit 40d3ce0662
11 changed files with 479 additions and 19 deletions
@@ -559,8 +559,152 @@ class AdminWorkflowTest {
.asText())
.isEqualTo("REVIEW");
request(signed(post("/admin-api/project-imports/" + code + "/retry")), 409);
assertThat(request(signed(get("/admin-api/project-imports/" + code)), 200)
.path("data").path("canRetry").asBoolean()).isFalse();
imports.run(code);
assertThat(gitea.migrations).isEqualTo(1);
assertThat(gitea.deletions).isZero();
}
@Test
void rejectedMigrationCleansOwnedRepositoryAndRetriesWithExistingAssets() throws Exception {
enableOss();
JsonNode signature = importUrdfSignature();
storeUrdfFiles(signature);
Map<String, Object> input = new HashMap<>(importWithUrdf(signature));
Map<String, Object> project = new HashMap<>(metadata());
project.put("coverUrl", "https://images.example.com/cover.png");
input.put("project", project);
JsonNode initial = request(signed(body(post("/admin-api/project-imports"), input)), 202).path("data");
String taskCode = initial.path("taskCode").asText();
String projectCode = initial.path("projectCode").asText();
gitea.migrationRejected = true;
imports.run(taskCode);
JsonNode failed = request(signed(get("/admin-api/project-imports/" + taskCode)), 200).path("data");
assertThat(failed.path("status").asText()).isEqualTo("FAILED");
assertThat(failed.path("errorCode").asText()).isEqualTo("GITEA_MIGRATION_REJECTED");
assertThat(failed.path("canRetry").asBoolean()).isTrue();
JsonNode before = request(signed(get("/admin-api/projects/" + projectCode)), 200).path("data").path("project");
JsonNode queued = request(signed(post("/admin-api/project-imports/" + taskCode + "/retry")), 202).path("data");
assertThat(queued.path("phase").asText()).isEqualTo("CLEANING");
assertThat(queued.path("canRetry").asBoolean()).isFalse();
request(signed(post("/admin-api/project-imports/" + taskCode + "/retry")), 409);
assertThat(gitea.deletions).isZero(); // HTTP 接口只排队,由后台持锁清理。
gitea.migrationRejected = false;
imports.run(taskCode);
JsonNode done = request(signed(get("/admin-api/project-imports/" + taskCode)), 200).path("data");
assertThat(done.path("status").asText()).isEqualTo("SUCCEEDED");
assertThat(done.path("projectCode").asText()).isEqualTo(projectCode);
assertThat(gitea.migrations).isEqualTo(2);
assertThat(gitea.deletions).isEqualTo(1);
JsonNode after = request(signed(get("/admin-api/projects/" + projectCode)), 200).path("data").path("project");
assertThat(after.path("coverUrl")).isEqualTo(before.path("coverUrl"));
assertThat(after.path("urdfMainUrl")).isEqualTo(before.path("urdfMainUrl"));
assertThat(after.path("publishStatus").asText()).isEqualTo("UNPUBLISHED");
request(signed(post("/admin-api/project-imports/" + taskCode + "/retry")), 409);
assertThat(gitea.deletions).isEqualTo(1);
}
@Test
void legacyRejectedMigrationWithNoRemoteRepositoryCanRetry() throws Exception {
JsonNode task = createImport();
String code = task.path("taskCode").asText();
jdbc.update("UPDATE project_import_task SET status='REVIEW',phase='MIGRATING',"
+ "error_code='GITEA_MIGRATION_REJECTED' WHERE task_code=?", code);
jdbc.update("UPDATE project SET repo_write_status='REVIEW' WHERE project_code=?", task.path("projectCode").asText());
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.deletions).isZero();
assertThat(gitea.migrations).isEqualTo(1);
}
@Test
void cleanupNeverDeletesRepositoryWithoutTaskOwnershipMarker() throws Exception {
JsonNode task = createImport();
String code = task.path("taskCode").asText();
gitea.migrationRejected = true;
imports.run(code);
String repoName = "p_" + task.path("projectCode").asText();
gitea.migrationDescriptions.put(repoName, "manually-created repository");
request(signed(post("/admin-api/project-imports/" + code + "/retry")), 202);
imports.run(code);
JsonNode blocked = request(signed(get("/admin-api/project-imports/" + code)), 200).path("data");
assertThat(blocked.path("status").asText()).isEqualTo("REVIEW");
assertThat(blocked.path("errorCode").asText()).isEqualTo("CLEANUP_REPOSITORY_UNSAFE");
assertThat(blocked.path("canRetry").asBoolean()).isFalse();
assertThat(gitea.deletions).isZero();
assertThat(gitea.migrations).isEqualTo(1);
assertThat(gitea.inspect(repoName)).isNotNull();
}
@Test
void cleanupNeverDeletesReplacementEvenWithSameMarker() throws Exception {
JsonNode task = createImport();
String code = task.path("taskCode").asText();
String name = "p_" + task.path("projectCode").asText();
gitea.migrationRejected = true;
imports.run(code);
gitea.migrationRejected = false;
gitea.migrate("https://github.com/example/robot.git", name, java.time.Duration.ofSeconds(1),
"community-import:" + code);
int before = gitea.migrations;
request(signed(post("/admin-api/project-imports/" + code + "/retry")), 202);
imports.run(code);
JsonNode blocked = request(signed(get("/admin-api/project-imports/" + code)), 200).path("data");
assertThat(blocked.path("errorCode").asText()).isEqualTo("CLEANUP_REPOSITORY_REPLACED");
assertThat(blocked.path("canRetry").asBoolean()).isFalse();
assertThat(gitea.deletions).isZero();
assertThat(gitea.migrations).isEqualTo(before);
}
@Test
void cleanupTimeoutCanResumeAfterConfirmingDeletionWithoutDeletingAgain() throws Exception {
JsonNode task = createImport();
String code = task.path("taskCode").asText();
gitea.migrationRejected = true;
imports.run(code);
gitea.migrationRejected = false;
gitea.timeoutNextDelete = true;
request(signed(post("/admin-api/project-imports/" + code + "/retry")), 202);
imports.run(code);
JsonNode unknown = request(signed(get("/admin-api/project-imports/" + code)), 200).path("data");
assertThat(unknown.path("status").asText()).isEqualTo("REVIEW");
assertThat(unknown.path("phase").asText()).isEqualTo("CLEANING");
assertThat(unknown.path("canRetry").asBoolean()).isTrue();
assertThat(gitea.migrations).isEqualTo(1);
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.deletions).isEqualTo(1);
assertThat(gitea.migrations).isEqualTo(2);
}
@Test
void cleanupDatabaseFailureRetainsIdentityAndResumesAfterRestart() throws Exception {
JsonNode task = createImport();
String code = task.path("taskCode").asText();
gitea.migrationRejected = true;
imports.run(code);
request(signed(post("/admin-api/project-imports/" + code + "/retry")), 202);
jdbc.execute("ALTER TABLE project_import_task ADD CONSTRAINT simulate_cleanup_failure CHECK (phase <> 'PRECHECK')");
gitea.migrationRejected = false;
imports.run(code);
assertThat(gitea.deletions).isEqualTo(1);
assertThat(gitea.migrations).isEqualTo(1);
assertThat(jdbc.queryForObject("SELECT gitea_repo_id FROM project WHERE project_code=?", Long.class,
task.path("projectCode").asText())).isNotNull();
jdbc.execute("ALTER TABLE project_import_task DROP CONSTRAINT simulate_cleanup_failure");
jdbc.update("UPDATE project_import_task SET status='RUNNING',phase='CLEANING' WHERE task_code=?", code);
imports.recover();
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.deletions).isEqualTo(1);
assertThat(gitea.migrations).isEqualTo(2);
}
@Test
@@ -593,6 +737,7 @@ class AdminWorkflowTest {
.asText())
.isEqualTo("SUCCEEDED");
assertThat(gitea.migrations).isEqualTo(1);
assertThat(gitea.deletions).isZero();
String sha = gitea.head(remote.repository());
request(
signed(
@@ -21,8 +21,13 @@ final class FakeGiteaClient implements GiteaClient {
String lastAuthorCode;
int migrations;
boolean migrationTimeout;
boolean migrationRejected;
boolean timeoutNextDelete;
boolean deletionRejected;
int deletions;
boolean mainConflict;
private final Map<String, RemoteRepository> migrated = new HashMap<>();
final Map<String, String> migrationDescriptions = new HashMap<>();
private final Set<String> mainReady = new HashSet<>();
boolean timeoutNextCommit;
boolean timeoutNextCreate;
@@ -33,7 +38,12 @@ final class FakeGiteaClient implements GiteaClient {
migrated.clear();
mainReady.clear();
migrations = 0;
deletions = 0;
migrationDescriptions.clear();
migrationTimeout = false;
migrationRejected = false;
timeoutNextDelete = false;
deletionRejected = false;
mainConflict = false;
timeoutNextCommit = false;
timeoutNextCreate = false;
@@ -174,16 +184,45 @@ final class FakeGiteaClient implements GiteaClient {
@Override
public synchronized RemoteRepository migrate(
String source, String name, java.time.Duration timeout) {
return migrate(source, name, timeout, null);
}
@Override
public synchronized RemoteRepository migrate(
String source, String name, java.time.Duration timeout, String description) {
migrations++;
Repository repository = createRepository(name, true);
RemoteRepository remote =
new RemoteRepository(
repository.id(), "community", name, true, false, false, source, "master");
migrated.put(name, remote);
migrationDescriptions.put(name, description);
if (migrationTimeout) throw new GiteaFailure(504, "REMOTE_RESULT_UNKNOWN", "迁移结果不明", true);
if (migrationRejected) throw new GiteaFailure(422, "GITEA_MIGRATION_REJECTED", "迁移失败", false);
return remote;
}
@Override
public synchronized MigrationTarget inspectMigrationTarget(String name) {
return new MigrationTarget(inspect(name), migrationDescriptions.get(name));
}
@Override
public synchronized void deleteRepository(Repository repository) {
if (inspect(repository.name()).id() != repository.id())
throw new GiteaFailure(409, "GITEA_CONFLICT", "仓库身份已变化", false);
if (deletionRejected) throw new GiteaFailure(502, "GITEA_ERROR", "清理失败", false);
deletions++;
migrated.remove(repository.name());
migrationDescriptions.remove(repository.name());
mainReady.remove(repository.name());
repositories.remove(repository.id());
if (timeoutNextDelete) {
timeoutNextDelete = false;
throw new GiteaFailure(504, "REMOTE_RESULT_UNKNOWN", "删除结果不明", true);
}
}
@Override
public synchronized RemoteRepository inspect(String name) {
RemoteRepository remote = migrated.get(name);
@@ -75,6 +75,8 @@ public final class AdminContracts {
String status,
String phase,
String errorCode,
boolean canRetry,
String retryMessage,
List<String> warnings,
LocalDateTime createdAt,
LocalDateTime startedAt,
@@ -213,6 +213,7 @@ public class AdminImportService {
public TaskView view(ProjectImportTask task) {
try {
boolean retry = canRetry(task);
List<String> warnings =
json.readValue(
task.getWarningsJson() == null ? "[]" : task.getWarningsJson(),
@@ -223,6 +224,8 @@ public class AdminImportService {
task.getStatus(),
task.getPhase(),
task.getErrorCode(),
retry,
retryMessage(task, retry),
warnings,
task.getCreatedAt(),
task.getStartedAt(),
@@ -264,9 +267,15 @@ public class AdminImportService {
new LambdaQueryWrapper<Project>()
.eq(Project::getProjectCode, task.getProjectCode())
.eq(Project::getDeleted, false));
if (project == null || !"UNPUBLISHED".equals(project.getPublishStatus()))
throw BusinessException.conflict("项目已删除、发布或下架,不能重试迁移");
if ("REVIEW".equals(task.getStatus())) {
if (!retryableProject(project))
throw BusinessException.conflict("项目已就绪、发布过、下架或删除,不能重试迁移");
if (!canRetry(task))
throw BusinessException.conflict("当前任务无法安全重试,请核对仓库状态");
boolean recreate = recreateOnRetry(task);
if (recreate) {
// 持久化清理意图后交给后台处理,接口不等待远端删除或迁移。
task.setPhase("CLEANING");
} else if ("REVIEW".equals(task.getStatus())) {
if (task.getReceiptJson() == null)
throw BusinessException.conflict("迁移结果不明,需要运维核对,禁止重发");
if ("MIGRATED".equals(task.getPhase())) {
@@ -288,7 +297,7 @@ public class AdminImportService {
} else if (!"FAILED".equals(task.getStatus()))
throw BusinessException.conflict("仅失败或可核验的待核对任务允许续跑");
if (!"IDLE".equals(project.getRepoWriteStatus())
&& !"REVIEW".equals(task.getStatus()))
&& !"REVIEW".equals(task.getStatus()) && !recreate)
throw BusinessException.conflict("仓库写入状态需要人工核对");
tasks.update(
null,
@@ -299,10 +308,129 @@ public class AdminImportService {
.set(ProjectImportTask::getErrorCode, null)
.set(ProjectImportTask::getFinishedAt, null)
.set(ProjectImportTask::getUpdatedAt, ProjectSupport.now()));
log.info("迁移重试已排队 operatorUserCode={} taskCode={} projectCode={} cleanup={}",
ProjectActors.admin().userCode(), code, task.getProjectCode(), recreate);
return view(task(code));
});
}
private boolean recreateOnRetry(ProjectImportTask task) {
if (task.getReceiptJson() != null) return false;
if ("MIGRATING".equals(task.getPhase()))
return "GITEA_MIGRATION_REJECTED".equals(task.getErrorCode());
return "CLEANING".equals(task.getPhase())
&& !Set.of("CLEANUP_REPOSITORY_UNSAFE", "CLEANUP_REPOSITORY_REPLACED", "SOURCE_CHANGED")
.contains(task.getErrorCode() == null ? "" : task.getErrorCode());
}
private boolean canRetry(ProjectImportTask task) {
if (!Set.of("FAILED", "REVIEW").contains(task.getStatus())) return false;
Project project = projects.selectOne(new LambdaQueryWrapper<Project>()
.eq(Project::getProjectCode, task.getProjectCode()));
if (!retryableProject(project)) return false;
if (recreateOnRetry(task)) return true;
if ("CLEANING".equals(task.getPhase())) return false;
if ("FAILED".equals(task.getStatus())) return true;
return task.getReceiptJson() != null
&& ("MIGRATED".equals(task.getPhase())
|| Set.of("NORMALIZING", "BRANCH_READY").contains(task.getPhase())
&& task.getTargetSha() != null);
}
private boolean retryableProject(Project project) {
return project != null && !Boolean.TRUE.equals(project.getDeleted())
&& "UNPUBLISHED".equals(project.getPublishStatus())
&& Set.of("CREATING", "FAILED").contains(project.getRepoStatus())
&& project.getPublishedCommitSha() == null && project.getPublishedAt() == null;
}
private String retryMessage(ProjectImportTask task, boolean retry) {
if (!Set.of("FAILED", "REVIEW").contains(task.getStatus())) return null;
if (!retry) return "当前项目状态或远端结果不允许安全重试,需要维护人员核对";
if (recreateOnRetry(task))
return "将核验并清理本任务的失败仓库后重新迁移,保留项目资料、封面和 URDF";
if (task.getReceiptJson() != null) return "将核验已迁移的仓库并继续后续步骤,不会删除仓库";
return "将重新检查来源并继续迁移,保留项目资料、封面和 URDF";
}
private String migrationMarker(ProjectImportTask task) {
return "community-import:" + task.getTaskCode();
}
private GiteaClient.RemoteRepository failedRepository(ProjectImportTask task, Project project) {
GiteaClient.MigrationTarget target;
try {
target = gitea.inspectMigrationTarget(project.getGiteaRepoName());
} catch (GiteaFailure missing) {
if (missing.status() == 404) return null;
throw missing;
}
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.privateRepo() || remote.mirror() || !sourceMatches)
throw new MigrationFailure("CLEANUP_REPOSITORY_UNSAFE", "无法确认残留仓库属于本次失败迁移,未执行删除");
if (project.getGiteaRepoId() != null && project.getGiteaRepoId() != remote.id())
throw new MigrationFailure("CLEANUP_REPOSITORY_REPLACED", "目标仓库身份已变化,未执行删除");
return remote;
}
private void rememberFailedRepository(ProjectImportTask task, Project project,
GiteaClient.RemoteRepository remote) {
if (remote == null) return;
if (project.getGiteaRepoId() == null) {
int updated = projects.update(null, new LambdaUpdateWrapper<Project>()
.eq(Project::getProjectCode, task.getProjectCode())
.eq(Project::getDeleted, false)
.in(Project::getRepoStatus, List.of("CREATING", "FAILED"))
.isNull(Project::getGiteaRepoId)
.set(Project::getGiteaRepoId, remote.id()));
if (updated != 1) throw BusinessException.conflict("项目状态已变化,停止清理");
project.setGiteaRepoId(remote.id());
}
}
private void cleanupFailedMigration(ProjectImportTask task, Project project) {
if (task.getReceiptJson() != null || !retryableProject(project))
throw new MigrationFailure("CLEANUP_REPOSITORY_UNSAFE", "项目已就绪或已有发布记录,禁止清理仓库");
GitHubSourceChecks.Repository source = check(task.getSourceRepositoryUrl());
if (source.id() != task.getSourceRepositoryId())
throw new MigrationFailure("SOURCE_CHANGED", "来源仓库身份已变化,停止清理");
phase(task, "CLEANING");
GiteaClient.RemoteRepository remote = failedRepository(task, project);
rememberFailedRepository(task, project, remote);
if (remote != null) {
log.info("清理失败迁移仓库 taskCode={} projectCode={}", task.getTaskCode(), task.getProjectCode());
gitea.deleteRepository(remote.repository());
}
// 删除响应成功也必须再次确认目标不存在;超时或回写失败可从 CLEANING 恢复。
engine().ensureAbsent(project.getGiteaRepoName());
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")
.set(Project::getGiteaRepoId, null).set(Project::getRepoStatus, "CREATING")
.set(Project::getRepoWriteStatus, "IDLE"));
if (updated != 1) throw BusinessException.conflict("项目状态已变化,停止重新迁移");
task.setPhase("PRECHECK");
task.setTargetSha(null);
task.setErrorCode(null);
save(task);
});
locks.confirmKnownResult(task.getProjectCode());
log.info("失败迁移仓库清理已确认 taskCode={} projectCode={}", task.getTaskCode(), task.getProjectCode());
}
public List<String> queued() {
return tasks
.selectList(
@@ -380,6 +508,7 @@ public class AdminImportService {
|| !"ADMIN".equals(operator.getRole())
|| !"ACTIVE".equals(operator.getStatus()))
throw new BusinessException(403, "OPERATOR_UNAVAILABLE", "任务创建人已无后台权限");
if ("CLEANING".equals(task.getPhase())) cleanupFailedMigration(task, project);
try {
RepositoryMigration.Result result;
if ("BRANCH_READY".equals(task.getPhase()) && task.getReceiptJson() != null) {
@@ -414,6 +543,10 @@ public class AdminImportService {
return task.getTargetSha();
}
public String migrationDescription() {
return migrationMarker(task);
}
public void receipt(
GiteaClient.RemoteRepository receipt)
throws IOException {
@@ -502,16 +635,31 @@ public class AdminImportService {
try {
ProjectImportTask task = find(code);
boolean review =
Set.of("MIGRATING", "MIGRATED", "NORMALIZING", "BRANCH_READY")
Set.of("CLEANING", "MIGRATING", "MIGRATED", "NORMALIZING", "BRANCH_READY")
.contains(task.getPhase());
if (error instanceof GiteaFailure failure && failure.uncertain()) review = true;
if (error instanceof MigrationFailure failure
&& "REMOTE_ALREADY_EXISTS".equals(failure.code())) review = true;
boolean rejected = "MIGRATING".equals(task.getPhase())
&& task.getReceiptJson() == null
&& error instanceof GiteaFailure failure
&& "GITEA_MIGRATION_REJECTED".equals(failure.code()) && !failure.uncertain();
if (rejected) {
review = false;
try {
Project project = projects.selectOne(new LambdaQueryWrapper<Project>()
.eq(Project::getProjectCode, task.getProjectCode()));
if (project != null) rememberFailedRepository(task, project, failedRepository(task, project));
} catch (RuntimeException inspectError) {
log.warn("失败迁移仓库身份未记录 taskCode={} projectCode={} errorType={}",
code, task.getProjectCode(), inspectError.getClass().getSimpleName());
}
}
String errorCode =
error instanceof BusinessException business
? business.code()
: "IMPORT_INTERNAL_ERROR";
if (review) locks.markUncertain(task.getProjectCode());
if (review || rejected) locks.markUncertain(task.getProjectCode());
tasks.update(
null,
new LambdaUpdateWrapper<ProjectImportTask>()
@@ -540,7 +688,7 @@ public class AdminImportService {
status -> {
task.setPhase(phase);
save(task);
if (Set.of("MIGRATING", "NORMALIZING").contains(phase)) {
if (Set.of("CLEANING", "MIGRATING", "NORMALIZING").contains(phase)) {
int updated =
projects.update(
null,
@@ -24,6 +24,20 @@ public interface GiteaClient {
throw new UnsupportedOperationException("未实现迁移");
}
default RemoteRepository migrate(String cloneUrl, String name, Duration timeout, String description) {
throw new UnsupportedOperationException("未实现带任务标识的迁移");
}
record MigrationTarget(RemoteRepository repository, String description) {}
default MigrationTarget inspectMigrationTarget(String name) {
throw new UnsupportedOperationException("未实现失败迁移仓库核验");
}
default void deleteRepository(Repository repository) {
throw new UnsupportedOperationException("未实现仓库删除");
}
default RemoteRepository inspect(String name) { throw new UnsupportedOperationException("未实现仓库检查"); }
default String branch(Repository repository, String branch) { throw new UnsupportedOperationException("未实现分支检查"); }
@@ -153,6 +153,11 @@ public class RestGiteaClient implements GiteaClient {
@Override
public RemoteRepository migrate(String cloneUrl, String name, Duration timeout) {
return migrate(cloneUrl, name, timeout, null);
}
@Override
public RemoteRepository migrate(String cloneUrl, String name, Duration timeout, String description) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("clone_addr", cloneUrl);
body.put("repo_owner", properties.getGitea().getOwner());
@@ -161,6 +166,7 @@ public class RestGiteaClient implements GiteaClient {
body.put("private", true);
body.put("mirror", false);
body.put("lfs", true);
if (description != null) body.put("description", description);
for (String option : List.of("issues", "labels", "milestones", "pull_requests", "releases", "wiki"))
body.put(option, false);
HttpResponse<byte[]> response = request("POST", "/repos/migrate", body, timeout);
@@ -174,6 +180,27 @@ public class RestGiteaClient implements GiteaClient {
return remote(parse(request("GET", "/repos/" + encode(properties.getGitea().getOwner()) + "/" + encode(name), null).body(), false));
}
@Override
public MigrationTarget inspectMigrationTarget(String name) {
JsonNode data = parse(request("GET", "/repos/" + encode(properties.getGitea().getOwner())
+ "/" + encode(name), null).body(), false);
if (!data.path("private").isBoolean() || !data.path("mirror").isBoolean())
throw new GiteaFailure(502, "GITEA_INVALID_RESPONSE", "无法核验迁移仓库属性", false);
return new MigrationTarget(remote(data), data.path("description").asText(null));
}
@Override
public void deleteRepository(Repository repository) {
if (!properties.getGitea().getOwner().equals(repository.owner()))
throw BusinessException.conflict("仓库组织与当前配置不一致,禁止删除");
RemoteRepository current = inspect(repository.name());
if (current.id() != repository.id())
throw BusinessException.conflict("仓库身份已变化,禁止删除");
HttpResponse<byte[]> response = request("DELETE", repoPath(repository), null);
if (response.statusCode() != 204)
throw new GiteaFailure(502, "MIGRATION_CLEANUP_UNCONFIRMED", "仓库清理结果尚未确认", true);
}
@Override
public void createBranch(Repository repo, String name, String sha) {
request("POST", repoPath(repo) + "/branches", Map.of("new_branch_name", name, "old_ref_name", FilePaths.sha(sha)));
@@ -12,6 +12,8 @@ public final class RepositoryMigration {
String targetSha();
default String migrationDescription() { return null; }
void receipt(GiteaClient.RemoteRepository receipt) throws IOException;
void targetSha(String sha) throws IOException;
@@ -40,7 +42,10 @@ public final class RepositoryMigration {
if (progress.receipt() == null) {
ensureAbsent(name);
progress.phase("MIGRATING");
GiteaClient.RemoteRepository receipt = gitea.migrate(sourceUrl + ".git", name, timeout);
String description = progress.migrationDescription();
GiteaClient.RemoteRepository receipt = description == null
? gitea.migrate(sourceUrl + ".git", name, timeout)
: gitea.migrate(sourceUrl + ".git", name, timeout, description);
progress.receipt(receipt);
}
GiteaClient.RemoteRepository remote = gitea.inspect(name);
@@ -43,7 +43,8 @@ class GiteaMigrationTest {
client.migrate(
"https://github.com/example/robot.git",
"p_111111111111",
Duration.ofSeconds(2));
Duration.ofSeconds(2),
"community-import:fixture-task");
assertThat(result.id()).isEqualTo(77);
assertThat(result.defaultBranch()).isEqualTo("master");
assertThat(body.get().path("repo_owner").asText()).isEqualTo("community");
@@ -51,6 +52,7 @@ class GiteaMigrationTest {
assertThat(body.get().path("service").asText()).isEqualTo("git");
assertThat(body.get().path("private").asBoolean()).isTrue();
assertThat(body.get().path("lfs").asBoolean()).isTrue();
assertThat(body.get().path("description").asText()).isEqualTo("community-import:fixture-task");
for (String key :
new String[] {
"mirror",
@@ -135,4 +137,67 @@ class GiteaMigrationTest {
server.stop(0);
}
}
@Test
void deletionChecksRepositoryIdAndUsesAuthenticatedDelete() throws Exception {
java.util.concurrent.atomic.AtomicInteger deleted = new java.util.concurrent.atomic.AtomicInteger();
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/api/v1/repos/community/p_fixture", exchange -> {
assertThat(exchange.getRequestHeaders().getFirst("Authorization")).isEqualTo("token fixture-token");
if (exchange.getRequestMethod().equals("DELETE")) {
deleted.incrementAndGet();
exchange.sendResponseHeaders(204, -1);
} else {
byte[] response = ("{\"id\":77,\"owner\":{\"login\":\"community\"},\"name\":\"p_fixture\","
+ "\"private\":true,\"mirror\":false,\"empty\":true,"
+ "\"description\":\"community-import:fixture-task\","
+ "\"original_url\":\"https://github.com/example/robot.git\"}")
.getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(200, response.length);
exchange.getResponseBody().write(response);
}
exchange.close();
});
server.start();
try {
CommunityProperties config = new CommunityProperties();
config.getGitea().setBaseUrl("http://127.0.0.1:" + server.getAddress().getPort());
config.getGitea().setToken("fixture-token");
GiteaClient client = new RestGiteaClient(config, new ObjectMapper());
GiteaClient.MigrationTarget target = client.inspectMigrationTarget("p_fixture");
assertThat(target.description()).isEqualTo("community-import:fixture-task");
assertThatThrownBy(() -> client.deleteRepository(new GiteaClient.Repository(78, "community", "p_fixture")))
.isInstanceOf(com.linkerbot.community.common.BusinessException.class);
assertThat(deleted.get()).isZero();
client.deleteRepository(target.repository().repository());
assertThat(deleted.get()).isEqualTo(1);
} finally {
server.stop(0);
}
}
@Test
void asynchronousDeleteResponseDoesNotConfirmCleanup() throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/api/v1/repos/community/p_fixture", exchange -> {
byte[] response = "{\"id\":77}".getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(exchange.getRequestMethod().equals("DELETE") ? 202 : 200, response.length);
exchange.getResponseBody().write(response);
exchange.close();
});
server.start();
try {
CommunityProperties config = new CommunityProperties();
config.getGitea().setBaseUrl("http://127.0.0.1:" + server.getAddress().getPort());
config.getGitea().setToken("fixture-token");
GiteaClient client = new RestGiteaClient(config, new ObjectMapper());
assertThatThrownBy(() -> client.deleteRepository(new GiteaClient.Repository(77, "community", "p_fixture")))
.isInstanceOfSatisfying(GiteaFailure.class, error -> {
assertThat(error.code()).isEqualTo("MIGRATION_CLEANUP_UNCONFIRMED");
assertThat(error.uncertain()).isTrue();
});
} finally {
server.stop(0);
}
}
}
+19 -8
View File
@@ -918,7 +918,7 @@ URDF 缺失或大小不符时拒绝创建,不留下项目和任务。收到 20
```ts
type ImportStatus = 'QUEUED' | 'RUNNING' | 'SUCCEEDED' | 'FAILED' | 'REVIEW';
type ImportPhase = 'QUEUED' | 'PRECHECK' | 'MIGRATING' | 'MIGRATED'
type ImportPhase = 'QUEUED' | 'CLEANING' | 'PRECHECK' | 'MIGRATING' | 'MIGRATED'
| 'NORMALIZING' | 'BRANCH_READY' | 'LINKED';
interface TaskView {
@@ -927,6 +927,8 @@ interface TaskView {
status: ImportStatus;
phase: ImportPhase;
errorCode: string | null;
canRetry: boolean;
retryMessage: string | null;
warnings: string[];
createdAt: UtcDateTime;
startedAt: UtcDateTime | null;
@@ -946,6 +948,8 @@ interface TaskView {
"status": "QUEUED",
"phase": "QUEUED",
"errorCode": null,
"canRetry": false,
"retryMessage": null,
"warnings": [],
"createdAt": "2026-10-20T02:30:00",
"startedAt": null,
@@ -961,7 +965,7 @@ interface TaskView {
| --- | --- | --- |
| `GET /admin-api/project-imports` | Query:page、size、status、projectCode,可选 | `PageResponse<TaskView>` |
| `GET /admin-api/project-imports/{taskCode}` | 无 | `TaskView` |
| `POST /admin-api/project-imports/{taskCode}/retry` | 无 body | HTTP 202,`TaskView` |
| `POST /admin-api/project-imports/{taskCode}/retry` | 无 body;先展示 retryMessage 供用户确认 | HTTP 202,`TaskView`;后台清理或续跑 |
状态决定页面行为,阶段用于文字说明:
@@ -970,12 +974,13 @@ interface TaskView {
| QUEUED | 排队中 | 持续轮询,不重复提交 |
| RUNNING | 迁移中 | 持续轮询,展示阶段和 warnings |
| SUCCEEDED | 迁移完成,待检查发布 | 停止轮询,跳转项目检查;不会自动发布 |
| FAILED | 迁移失败 | 停止轮询,展示 errorCode、warnings,可由用户点击重试 |
| REVIEW | 结果待核对 | 停止轮询,提示联系维护人员,不自动重试 |
| FAILED | 迁移失败 | 停止轮询,展示提醒;canRetry=true 时显示“重试迁移” |
| REVIEW | 结果待核对 | 停止轮询;canRetry=true 时可重试核验/续跑,否则提示联系维护人员 |
| phase | 建议文案 |
| --- | --- |
| QUEUED | 等待处理 |
| CLEANING | 核验并清理失败仓库,准备重新迁移 |
| PRECHECK | 校验来源和项目信息 |
| MIGRATING | 迁移 Git 仓库与 LFS |
| MIGRATED | 仓库迁移已确认 |
@@ -983,7 +988,7 @@ interface TaskView {
| BRANCH_READY | 主分支已准备 |
| LINKED | 已关联社区项目 |
不展示百分比进度,接口没有 progress、duration、canRetry 字段。耗时可根据 startedAt、finishedAt 计算,空值显示 `—`。任务查询 HTTP 200、code=OK 只表示查询成功,必须再判断 data.status 是否失败或待核对。
不展示百分比进度,接口没有 progress、duration 字段。canRetry 由后端判断,前端不要仅按 status/phase 猜测是否可重试;重试仍会校验项目最新状态,可能返回 409。retryMessage 是重试确认框或无法重试时的说明,正常执行中/成功时为 null。耗时可根据 startedAt、finishedAt 计算,空值显示 `—`。任务查询 HTTP 200、code=OK 只表示查询成功,必须再判断 data.status。
### 9.5 轮询示例
@@ -1027,10 +1032,14 @@ async function watchImport(
### 9.6 重试和功能边界
- FAILED 点击重试后,按返回任务恢复轮询;后端拒绝时显示 409 message,不循环重发。
- REVIEW 可能涉及远端结果不明,需要维护人员核对。部分已确认阶段支持同一 retry 接口安全续跑,但判断所需信息不对前端暴露;默认不显示常规重试按钮,也不要只根据 phase 推断可重试。
- canRetry=true 时显示“重试迁移”按钮,用 retryMessage 说明操作并确认;点击后禁用重复提交,收到 202 后恢复轮询。409 表示状态已变化或核验未通过,刷新任务并显示 message,不循环重发。
- 对于明确失败的 `GITEA_MIGRATION_REJECTED`,新任务显示 FAILED。重试后经过 CLEANING:后台核验残留仓库的任务标识、组织、名称、来源及仓库身份,清理后确认不存在,再重新迁移。沿用原 taskCode/projectCode,保留项目资料、封面及 URDF。
- 已取得成功迁移收据时,重试只核验并继续后续步骤,不删除已迁移仓库。项目已 READY、发布过、下架或删除时拒绝重试。
- 超时、进程在远端迁移期间中断等未知结果,不自动删除仓库或重发迁移;canRetry=false 时由维护人员处理。清理本身超时或回写中断,可通过重试重新核验目标是否已删除后恢复。
- 旧版本显示 REVIEW 且错误为 GITEA_MIGRATION_REJECTED 的任务,也可以显示重试按钮。若仓库已不存在可以重迁;若仍有残留,但缺少新版本的任务归属标识,则停止清理并返回 REVIEW,避免误删。
- 如果返回 CLEANUP_REPOSITORY_UNSAFE 或 CLEANUP_REPOSITORY_REPLACED,说明仓库归属无法核实或身份变化,未删除仓库,canRetry=false,提示联系维护人员。
- 重复来源可能返回 409 `DUPLICATE_SOURCE`,错误 data 中的 projectCode 可用于跳转现有项目。不要改项目名或重新预检后循环创建。
- 没有取消任务、强行成功、删除目标仓库、定期同步源仓库的接口。
- 没有取消任务、强行成功、任意删除仓库、定期同步源仓库的接口;失败仓库清理只能通过上述受保护的 retry 流程执行。
- 迁移保留 Git 历史、分支、标签并迁移 LFS;不迁移 Issue、PR、Wiki、Release 附件,不递归复制子模块依赖。保留分支不代表后台可切换分支。
- 原默认分支非 main 时进行适配;已有 main 内容冲突会停止,前端展示任务错误和提醒即可。
- 成功后项目仍未发布;需要管理员查看文件并手动发布。迁移表单已提交的封面和 URDF 会保留,无需再次绑定;后续替换资源仍可使用普通项目接口。不会自动扫描 Git 仓库中的 URDF 并绑定。
@@ -1052,6 +1061,7 @@ async function watchImport(
| 409 PROJECT_BUSY | 项目正在执行其他写操作,稍后刷新状态 |
| 409 PROJECT_BLOCKED | 项目被管理员下架,刷新详情,不继续发布 |
| 409 DUPLICATE_SOURCE | 使用错误 data.projectCode 引导查看已有项目 |
| 409 CLEANUP_REPOSITORY_UNSAFE / CLEANUP_REPOSITORY_REPLACED | 残留仓库核验不通过,停止清理;任务中的同名 errorCode 也按此提示 |
| REMOTE_RESULT_UNKNOWN(状态码以响应为准) | 写入结果待核对,停止重复提交,展示 requestId |
| 413 FILE_TOO_LARGE / UPLOAD_TOO_LARGE / URDF_UPLOAD_TOO_LARGE | 检查对应上传类型限额,拆批或减少文件 |
| 429 ADMIN_LOGIN_LIMITED | 提示 15 分钟后重试,不循环发登录请求 |
@@ -1095,3 +1105,4 @@ requestId 同时可能出现在响应头 `X-Request-Id`。错误提示可附“
- 迁移 202 显示已受理;终态停止轮询;不把 FAILED/REVIEW 显示为成功。
- 在迁移表单上传封面和 URDF 后即可创建任务;不必等待仓库 READY 再绑定 URDF;未完整上传时不能创建任务。
- 重复来源提供现有项目入口;任务失败、超时及待核对不自动重建迁移任务。
- 重试按钮使用 canRetry 控制,确认框显示 retryMessage;明确失败时可清理重迁,结果未知时不盲目删库;CLEANING 阶段展示清理提示。
+2 -2
View File
@@ -32,7 +32,7 @@ CREATE TABLE project_import_task (
source_default_branch VARCHAR(255) NOT NULL COMMENT 'GitHub仓库原默认分支,用于分支适配和续跑校验',
source_license VARCHAR(128) NULL COMMENT 'GitHub许可证标识,无法识别时为空',
status VARCHAR(16) NOT NULL DEFAULT 'QUEUED' COMMENT '任务状态 QUEUED排队 RUNNING执行 SUCCEEDED成功 FAILED失败 REVIEW待核对',
phase VARCHAR(32) NOT NULL DEFAULT 'QUEUED' COMMENT '迁移阶段 QUEUED PRECHECK MIGRATING MIGRATED NORMALIZING BRANCH_READY LINKED',
phase VARCHAR(32) NOT NULL DEFAULT 'QUEUED' COMMENT '迁移阶段 QUEUED CLEANING PRECHECK MIGRATING MIGRATED NORMALIZING BRANCH_READY LINKED',
receipt_json TEXT NULL COMMENT '已确认迁移完成收据JSON,仅内部使用,不包含认证凭据',
target_sha VARCHAR(64) NULL COMMENT '迁移后冻结的目标提交SHA',
error_code VARCHAR(64) NULL COMMENT '最近失败错误码',
@@ -53,7 +53,7 @@ CREATE TABLE project_import_task (
没有管理员账号表、管理员会话表、审计表或仓库状态表。第一位管理员使用初始化命令设置,禁止手写明文密码入库。后续手工修改角色时必须同时递增 session_version,且 ADMIN 必须具有通过 BCrypt 编码的密码。官方托管账号由后台首次创建或迁移项目时自动建立,保留邮箱为 community-official@internal.invalid。
REVIEW 或遗留 WRITING 不应直接改为 IDLE:先核对 Gitea 写入、迁移收据、LFS 完成情况、仓库身份及提交。后台仅为已持久化成功收据的明确阶段提供只读核对后的续跑,不提供强制成功操作。
REVIEW 或遗留 WRITING 不应直接改为 IDLE:先核对 Gitea 写入、迁移收据、LFS 完成情况、仓库身份及提交。已持久化成功收据时提供只读核对后的续跑;明确 GITEA_MIGRATION_REJECTED 的任务允许通过 retry 进入 CLEANING,核验并清理归属于该任务的失败仓库后重迁。清理身份使用现有 project.gitea_repo_id 保存,保留原任务与项目,不新增字段或表。已有 phase 为 VARCHAR,无需因新增 CLEANING 阶段执行数据库变更。不提供强制成功操作。
## 已经执行过上一版管理端 SQL 的数据库
+4
View File
@@ -51,6 +51,8 @@ project 表只新增 moderation_reason 和 repo_write_status,来源标识、
上线前确认目标 Gitea 已开启迁移、HTTP Git、LFS,技术账号具有组织仓库写权限。GitHub 预检优先读取 API;受限时严格核对公开网页身份、可见性和默认分支,无法确认时失败。网页回退拿不到的简介、许可证可能为空,需要管理员检查原仓库文件。
失败迁移重试需要技术账号及 Token 具有目标私有仓库读取、迁移创建及删除权限。后台迁移时在 Gitea 仓库描述中写入任务归属标识;不要手工修改该标识。它仅用于失败仓库清理核验,不改变社区项目的名称和描述。
迁移成功收据先持久化,再处理分支、关联和就绪状态。迁移保留 Git 历史、分支、标签并请求迁移 LFS;不导入 Issue、PR、Wiki、Release 附件,不递归复制子模块或外链。原默认分支不是 main 时补建 main;已有 main 与源默认分支内容不同则停止,不覆盖。
迁移表单支持预上传封面及 URDF。URDF 使用现有 OSS 配置与限额,创建任务前检查对象,并使用 project 既有 URDF 字段保存,不需要新增数据库变更。上传后未提交表单的 OSS 对象不会自动清理,沿用现有资源生命周期。
@@ -58,6 +60,8 @@ project 表只新增 moderation_reason 和 repo_write_status,来源标识、
## 异常核对
- 超时、进程在 MIGRATING 阶段中断或远端结果不明确时进入 REVIEW。禁止自动重发,也不能仅凭仓库存在判定成功。
- Gitea 明确返回 GITEA_MIGRATION_REJECTED 时显示 FAILED,提供重试。原有同错误的 REVIEW 任务也可申请重试;后台持有项目共享锁,在 CLEANING 阶段核验任务标识、组织、名称、来源及已记录的仓库 ID,清理后确认不存在再迁移。没有残留仓库时直接继续,不删除项目记录或 OSS 资源。
- 清理操作写入前持久化 CLEANING 阶段及仓库身份;删除超时或数据库回写失败时保持阻断,下次重试先核验是否已删除再继续。仓库已替换、来源不符、缺少任务归属标识时不删除;旧任务的无标识残留仓库仍需人工核对。已就绪、发布过的项目及已有成功收据的迁移不会走删除重迁流程。
- REMOTE_RESULT_UNKNOWN 需要按 requestId、项目编码、任务编码核对日志和 Gitea,不应删除目标仓库后直接重跑。
- 已持久化成功收据且停在 MIGRATED 的任务,retry 会只读验证仓库身份、来源等后续跑。
- 停在 NORMALIZING/BRANCH_READY 且有冻结目标 SHA 的任务,retry 会核对已适配分支和收据后续跑;验证不通过继续阻断。