后台自动核对REVIEW状态的项目
This commit is contained in:
+127
-31
@@ -42,6 +42,8 @@ import java.util.regex.*;
|
||||
"spring.datasource.driver-class-name=org.h2.Driver",
|
||||
"vibemake.admin.worker-enabled=false",
|
||||
"vibemake.oss.enabled=false",
|
||||
"vibemake.gitea.owner=vibemake",
|
||||
"vibemake.repository-write.worker-enabled=false",
|
||||
"logging.level.root=WARN"
|
||||
})
|
||||
@ActiveProfiles("test")
|
||||
@@ -95,8 +97,13 @@ class AdminWorkflowTest {
|
||||
@Autowired ObjectMapper json;
|
||||
@Autowired FakeGiteaClient gitea;
|
||||
@Autowired ProjectService projectService;
|
||||
@Autowired FileService fileService;
|
||||
@Autowired UserService userService;
|
||||
@Autowired ProjectLocks locks;
|
||||
@Autowired com.linkerbot.vibemake.service.write.RepositoryWrites writes;
|
||||
@Autowired RepositoryWriteOperationMapper writeOperations;
|
||||
@Autowired javax.sql.DataSource dataSource;
|
||||
@Autowired org.springframework.transaction.support.TransactionTemplate transactions;
|
||||
@Autowired OssProperties oss;
|
||||
@Autowired FakeOssObjectStore ossObjects;
|
||||
private User admin;
|
||||
@@ -209,47 +216,136 @@ class AdminWorkflowTest {
|
||||
assertThat(projectService.published(code).getPublishedCommitSha()).isEqualTo(current);
|
||||
}
|
||||
|
||||
private String initialSha(String code) throws Exception {
|
||||
return request(signed(get("/admin-api/projects/" + code + "/tree")), 200)
|
||||
.path("data").path("commitSha").asText();
|
||||
}
|
||||
|
||||
private void due(String operationCode, boolean overdue) {
|
||||
java.time.LocalDateTime now = ProjectSupport.now();
|
||||
jdbc.update("UPDATE repository_write_operation SET next_check_at=?, created_at=? WHERE operation_code=?",
|
||||
now.minusSeconds(1), now.minusMinutes(overdue ? 11 : 1), operationCode);
|
||||
}
|
||||
|
||||
private String operationState(String operationCode) {
|
||||
return writeOperations.selectById(operationCode).getStatus();
|
||||
}
|
||||
|
||||
private String operationCode(String projectCode) {
|
||||
return jdbc.queryForObject("SELECT operation_code FROM repository_write_operation WHERE project_code=?",
|
||||
String.class, projectCode);
|
||||
}
|
||||
|
||||
private com.linkerbot.vibemake.bean.Contracts.CommitView replayUpload(String code, String base, String key,
|
||||
String directory) {
|
||||
try (ProjectActors.Scope scope = ProjectActors.enter(new ProjectActors.AdminActor(admin.getUserCode(), admin.getNickname()))) {
|
||||
return fileService.upload(0, code, base, "测试上传", directory,
|
||||
List.of(new MockMultipartFile("files", "sample.txt", "text/plain", "hello".getBytes())),
|
||||
List.of("models/sample.txt"), key);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void uploadedCommitWithLostResponseStillBlocksPublicationAtLatestSha() throws Exception {
|
||||
void lostUploadResponseIsConfirmedAndSameOperationNeverRepeatsWrite() throws Exception {
|
||||
String code = createProject();
|
||||
String initial = request(signed(get("/admin-api/projects/" + code + "/tree")), 200)
|
||||
.path("data").path("commitSha").asText();
|
||||
String initial = initialSha(code);
|
||||
gitea.timeoutNextCommit = true;
|
||||
JsonNode failure = request(uploadRequest(code, initial), 504);
|
||||
assertThat(failure.path("code").asText()).isEqualTo("REMOTE_RESULT_UNKNOWN");
|
||||
assertThat(failure.path("data").path("projectCode").asText()).isEqualTo(code);
|
||||
assertThat(writeState(code)).isEqualTo("REVIEW");
|
||||
String current = request(signed(get("/admin-api/projects/" + code + "/tree")), 200)
|
||||
.path("data").path("commitSha").asText();
|
||||
JsonNode result = request(uploadRequest(code, initial), 200);
|
||||
String key = operationCode(code);
|
||||
String current = result.path("data").path("commitSha").asText();
|
||||
assertThat(current).isNotEqualTo(initial);
|
||||
assertThat(request(signed(get("/admin-api/projects/" + code + "/files")
|
||||
.param("path", "models/sample.txt")), 200)
|
||||
.path("data").path("content").asText()).isEqualTo("hello");
|
||||
JsonNode blocked = request(signed(body(
|
||||
put("/admin-api/projects/" + code + "/publication"), Map.of("commitSha", current))), 409);
|
||||
assertThat(blocked.path("code").asText()).isEqualTo("REMOTE_RESULT_UNKNOWN");
|
||||
assertThat(writeState(code)).isEqualTo("REVIEW");
|
||||
assertThat(projects.selectOne(new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<Project>()
|
||||
.eq(Project::getProjectCode, code)).getPublishedCommitSha()).isNull();
|
||||
|
||||
// 只改数据库不能清除当前进程的待核对标记,下一次写入会重新持久化 REVIEW。
|
||||
jdbc.update("UPDATE project SET repo_write_status='IDLE' WHERE project_code=?", code);
|
||||
assertThat(result.path("data").path("message").asText()).isEqualTo("测试上传");
|
||||
assertThat(writeState(code)).isEqualTo("IDLE");
|
||||
JsonNode blockedAgain = request(signed(body(
|
||||
put("/admin-api/projects/" + code + "/publication"), Map.of("commitSha", current))), 409);
|
||||
assertThat(blockedAgain.path("code").asText()).isEqualTo("REMOTE_RESULT_UNKNOWN");
|
||||
assertThat(writeState(code)).isEqualTo("REVIEW");
|
||||
assertThat(operationState(key)).isEqualTo("SUCCEEDED");
|
||||
int calls = gitea.commitCalls;
|
||||
assertThat(replayUpload(code, initial, key, "").commitSha()).isEqualTo(current);
|
||||
assertThat(gitea.commitCalls).isEqualTo(calls);
|
||||
assertThatThrownBy(() -> replayUpload(code, initial, key, "other"))
|
||||
.isInstanceOfSatisfying(BusinessException.class,
|
||||
error -> assertThat(error.code()).isEqualTo("IDEMPOTENCY_KEY_REUSED"));
|
||||
assertThat(request(signed(get("/admin-api/projects/" + code + "/commits")), 200)
|
||||
.path("data").path("items").get(0).path("message").asText()).isEqualTo("测试上传");
|
||||
assertThat(jdbc.queryForObject("SELECT published_commit_sha FROM project WHERE project_code=?",
|
||||
String.class, code)).isNull();
|
||||
}
|
||||
|
||||
// 模拟核对远端结果后持锁恢复,同时清除数据库和本进程标记。
|
||||
locks.serial(code, () -> {
|
||||
locks.confirmKnownResult(code);
|
||||
return null;
|
||||
});
|
||||
@Test
|
||||
void pendingWriteKeepsOriginalErrorAndRecoversAfterRestartWithoutFrontendChanges() throws Exception {
|
||||
String code = createProject();
|
||||
String initial = initialSha(code);
|
||||
gitea.timeoutNextCommit = true;
|
||||
gitea.hideReceipts = true;
|
||||
JsonNode error = request(uploadRequest(code, initial), 504);
|
||||
assertThat(error.path("code").asText()).isEqualTo("REMOTE_RESULT_UNKNOWN");
|
||||
assertThat(error.path("data").path("projectCode").asText()).isEqualTo(code);
|
||||
String key = operationCode(code);
|
||||
assertThat(operationState(key)).isEqualTo("PROCESSING");
|
||||
assertThat(writeState(code)).isEqualTo("REVIEW");
|
||||
int calls = gitea.commitCalls;
|
||||
assertThat(request(uploadRequest(code, initial), 409).path("code").asText())
|
||||
.isEqualTo("REMOTE_RESULT_UNKNOWN");
|
||||
assertThatThrownBy(() -> replayUpload(code, initial, key, ""))
|
||||
.isInstanceOfSatisfying(BusinessException.class, business -> assertThat(business.status()).isEqualTo(409));
|
||||
assertThat(gitea.commitCalls).isEqualTo(calls);
|
||||
String current = initialSha(code);
|
||||
request(signed(body(put("/admin-api/projects/" + code + "/publication"),
|
||||
Map.of("commitSha", current))), 409);
|
||||
due(key, false);
|
||||
gitea.hideReceipts = false;
|
||||
try (ProjectLocks restarted = new ProjectLocks(dataSource, projects)) {
|
||||
com.linkerbot.vibemake.service.write.RepositoryWrites worker =
|
||||
new com.linkerbot.vibemake.service.write.RepositoryWrites(writeOperations, projects,
|
||||
projectService, restarted, gitea, transactions);
|
||||
worker.reconcileDue();
|
||||
}
|
||||
assertThat(operationState(key)).isEqualTo("SUCCEEDED");
|
||||
assertThat(writeState(code)).isEqualTo("IDLE");
|
||||
assertThat(writeOperations.selectById(key).getResultCommitSha()).isEqualTo(current);
|
||||
assertThat(gitea.commitCalls).isEqualTo(calls);
|
||||
assertThat(jdbc.queryForObject("SELECT published_commit_sha FROM project WHERE project_code=?",
|
||||
String.class, code)).isNull();
|
||||
// 原实例仍保留 uncertain,下一次正常写入必须按数据库 IDLE 清理,无需重启。
|
||||
request(signed(body(put("/admin-api/projects/" + code + "/publication"),
|
||||
Map.of("commitSha", current))), 200);
|
||||
assertThat(writeState(code)).isEqualTo("IDLE");
|
||||
assertThat(projectService.published(code).getPublishedCommitSha()).isEqualTo(current);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"missing", "parent", "identity", "read"})
|
||||
void missingOrInvalidReceiptNeverBecomesSuccessOrResendsWrite(String fault) throws Exception {
|
||||
String code = createProject();
|
||||
String initial = initialSha(code);
|
||||
gitea.hideReceipts = fault.equals("missing");
|
||||
gitea.wrongReceiptParent = fault.equals("parent");
|
||||
gitea.wrongRepositoryIdentity = fault.equals("identity");
|
||||
gitea.failReceiptReads = fault.equals("read");
|
||||
gitea.timeoutNextCommit = true;
|
||||
request(uploadRequest(code, initial), 504);
|
||||
String key = operationCode(code);
|
||||
int calls = gitea.commitCalls;
|
||||
due(key, false);
|
||||
writes.reconcileDue();
|
||||
assertThat(operationState(key)).isEqualTo("PROCESSING");
|
||||
assertThat(writeState(code)).isEqualTo("REVIEW");
|
||||
assertThat(writeOperations.selectById(key).getNextCheckAt())
|
||||
.isAfter(ProjectSupport.now().plusSeconds(10));
|
||||
due(key, true);
|
||||
writes.reconcileDue();
|
||||
assertThat(operationState(key)).isEqualTo("NEEDS_REVIEW");
|
||||
assertThat(writeOperations.selectById(key).getNextCheckAt())
|
||||
.isAfter(ProjectSupport.now().plusSeconds(50));
|
||||
assertThat(gitea.commitCalls).isEqualTo(calls);
|
||||
assertThat(writeState(code)).isEqualTo("REVIEW");
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyReviewWithoutOperationIsNeverGuessedSuccessful() throws Exception {
|
||||
String code = createProject();
|
||||
locks.serial(code, () -> { locks.markUncertain(code); return null; });
|
||||
int calls = gitea.commitCalls;
|
||||
writes.reconcileDue();
|
||||
assertThat(writeState(code)).isEqualTo("REVIEW");
|
||||
assertThat(gitea.commitCalls).isEqualTo(calls);
|
||||
}
|
||||
|
||||
private JsonNode createImport() throws Exception {
|
||||
|
||||
+46
-2
@@ -12,6 +12,7 @@ final class FakeGiteaClient implements GiteaClient {
|
||||
private static final class RepoData {
|
||||
Repository repository;
|
||||
String head;
|
||||
Map<String, String> parents = new HashMap<>();
|
||||
Map<String, Map<String, byte[]>> snapshots = new HashMap<>();
|
||||
List<HistoryEntry> history = new ArrayList<>();
|
||||
}
|
||||
@@ -27,8 +28,14 @@ final class FakeGiteaClient implements GiteaClient {
|
||||
int deletions;
|
||||
boolean mainConflict;
|
||||
private 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<>();
|
||||
boolean hideReceipts;
|
||||
boolean failReceiptReads;
|
||||
boolean wrongReceiptParent;
|
||||
boolean wrongRepositoryIdentity;
|
||||
int commitCalls;
|
||||
boolean timeoutNextCommit;
|
||||
boolean timeoutNextHead;
|
||||
boolean timeoutNextCreate;
|
||||
@@ -36,7 +43,13 @@ final class FakeGiteaClient implements GiteaClient {
|
||||
|
||||
void reset() {
|
||||
repositories.clear();
|
||||
hideReceipts = false;
|
||||
failReceiptReads = false;
|
||||
wrongReceiptParent = false;
|
||||
wrongRepositoryIdentity = false;
|
||||
commitCalls = 0;
|
||||
migrated.clear();
|
||||
migrationNames.clear();
|
||||
mainReady.clear();
|
||||
migrations = 0;
|
||||
deletions = 0;
|
||||
@@ -151,6 +164,7 @@ final class FakeGiteaClient implements GiteaClient {
|
||||
String authorName,
|
||||
String authorCode) {
|
||||
lastAuthorCode = authorCode;
|
||||
commitCalls++;
|
||||
RepoData repo = repo(repository);
|
||||
Map<String, byte[]> snapshot =
|
||||
repo.head == null ? new TreeMap<>() : new TreeMap<>(repo.snapshots.get(repo.head));
|
||||
@@ -164,7 +178,9 @@ final class FakeGiteaClient implements GiteaClient {
|
||||
if (change.operation().equals("delete")) snapshot.remove(change.path());
|
||||
else snapshot.put(change.path(), change.bytes().clone());
|
||||
}
|
||||
String parent = repo.head;
|
||||
repo.head = sha();
|
||||
repo.parents.put(repo.head, parent);
|
||||
repo.snapshots.put(repo.head, snapshot);
|
||||
repo.history.addFirst(
|
||||
new HistoryEntry(repo.head, message, authorName, "2026-10-13T00:00:00Z"));
|
||||
@@ -181,7 +197,9 @@ final class FakeGiteaClient implements GiteaClient {
|
||||
List<HistoryEntry> history = repo(repository).history;
|
||||
int from = (int) Math.min(history.size(), (page - 1) * size);
|
||||
int to = (int) Math.min(history.size(), from + size);
|
||||
return new PageResponse<>(history.subList(from, to), page, size, history.size());
|
||||
return new PageResponse<>(history.subList(from, to).stream().map(item ->
|
||||
new HistoryEntry(item.commitSha(), WriteReceipt.displayMessage(item.message()),
|
||||
item.authorName(), item.authorDate())).toList(), page, size, history.size());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -197,6 +215,7 @@ final class FakeGiteaClient implements GiteaClient {
|
||||
public synchronized RemoteRepository migrate(
|
||||
String source, String name, java.time.Duration timeout, String description) {
|
||||
migrations++;
|
||||
migrationNames.add(name);
|
||||
Repository repository = createRepository(name, true);
|
||||
RemoteRepository remote =
|
||||
new RemoteRepository(
|
||||
@@ -232,7 +251,16 @@ final class FakeGiteaClient implements GiteaClient {
|
||||
@Override
|
||||
public synchronized RemoteRepository inspect(String name) {
|
||||
RemoteRepository remote = migrated.get(name);
|
||||
if (remote == null) throw new GiteaFailure(404, "NOT_FOUND", "仓库不存在", false);
|
||||
if (remote == null && migrationNames.contains(name))
|
||||
throw new GiteaFailure(404, "NOT_FOUND", "仓库不存在", false);
|
||||
if (remote == null) {
|
||||
for (RepoData data : repositories.values()) {
|
||||
if (data.repository.name().equals(name))
|
||||
return new RemoteRepository(data.repository.id() + (wrongRepositoryIdentity ? 1 : 0),
|
||||
data.repository.owner(), name, true, false, false, null, "main");
|
||||
}
|
||||
throw new GiteaFailure(404, "NOT_FOUND", "仓库不存在", false);
|
||||
}
|
||||
return remote;
|
||||
}
|
||||
|
||||
@@ -264,4 +292,20 @@ final class FakeGiteaClient implements GiteaClient {
|
||||
remote.originalUrl(),
|
||||
branch));
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized String findWriteReceipt(Repository repository, String baseSha,
|
||||
String operationCode, String digest) {
|
||||
if (failReceiptReads) throw new GiteaFailure(504, "READ_TIMEOUT", "核对读取超时", false);
|
||||
if (hideReceipts) return null;
|
||||
RepoData data = repo(repository);
|
||||
for (HistoryEntry entry : data.history) {
|
||||
if (entry.message().endsWith(WriteReceipt.suffix(operationCode, digest))) {
|
||||
if (wrongReceiptParent || !baseSha.equals(data.parents.get(entry.commitSha())))
|
||||
throw new GiteaFailure(409, "WRITE_RECEIPT_PARENT_MISMATCH", "父提交不匹配", false);
|
||||
return entry.commitSha();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user