后台自动核对REVIEW状态的项目

This commit is contained in:
lxp
2026-10-28 10:37:02 +08:00
parent c01ef63482
commit 4e7d11d586
23 changed files with 889 additions and 56 deletions
@@ -12,18 +12,29 @@ 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<>();
}
private final Map<Long, RepoData> repositories = new HashMap<>();
private final AtomicLong sequence = new AtomicLong();
boolean hideReceipts;
boolean failReceiptReads;
boolean wrongReceiptParent;
boolean wrongRepositoryIdentity;
int commitCalls;
boolean timeoutNextCommit;
boolean timeoutNextCreate;
boolean failNextCreate;
void reset() {
repositories.clear();
hideReceipts = false;
failReceiptReads = false;
wrongReceiptParent = false;
wrongRepositoryIdentity = false;
commitCalls = 0;
timeoutNextCommit = false;
timeoutNextCreate = false;
failNextCreate = false;
@@ -123,6 +134,7 @@ final class FakeGiteaClient implements GiteaClient {
String message,
String authorName,
String authorCode) {
commitCalls++;
RepoData repo = repo(repository);
Map<String, byte[]> snapshot =
repo.head == null ? new TreeMap<>() : new TreeMap<>(repo.snapshots.get(repo.head));
@@ -136,7 +148,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"));
@@ -153,6 +167,34 @@ 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
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;
}
@Override
public synchronized RemoteRepository inspect(String name) {
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);
}
}
@@ -98,6 +98,7 @@ class QueryEfficiencyTest {
}
for (String table :
List.of(
"repository_write_operation",
"project_import_task",
"project_like",
"project_comment",
@@ -31,6 +31,6 @@ final class SchemaSupport {
jdbc.execute(ddl);
tables++;
}
if (tables != 5) throw new IllegalStateException("必须包含四张基础表和迁移任务表");
if (tables != 6) throw new IllegalStateException("必须包含四张基础表、迁移任务表和仓库写入操作表");
}
}
@@ -38,6 +38,8 @@ import java.util.concurrent.*;
"spring.datasource.password=",
"spring.datasource.driver-class-name=org.h2.Driver",
"logging.level.root=INFO",
"vibemake.gitea.owner=vibemake",
"vibemake.repository-write.worker-enabled=false",
"logging.level.org.springframework=WARN"
})
@ActiveProfiles("test")
@@ -82,6 +84,8 @@ class WorkflowTest {
@Autowired ObjectMapper json;
@Autowired JdbcTemplate jdbc;
@Autowired FakeGiteaClient gitea;
@Autowired com.linkerbot.vibemake.service.FileService fileService;
@Autowired com.linkerbot.vibemake.service.write.RepositoryWrites writes;
@Autowired OssProperties oss;
@Autowired FakeOssObjectStore ossObjects;
@Autowired UrdfProperties urdfLimits;
@@ -1107,7 +1111,7 @@ class WorkflowTest {
"PROJECT",
"PROJECT_COMMENT",
"PROJECT_LIKE",
"PROJECT_IMPORT_TASK");
"PROJECT_IMPORT_TASK", "REPOSITORY_WRITE_OPERATION");
}
@Test
@@ -1487,6 +1491,58 @@ class WorkflowTest {
413);
}
@Test
void editorWriteRecoversInBackgroundWithoutChangingHttpContract() throws Exception {
String email = "pending-editor@example.invalid";
String owner = login(email);
String code = create(owner, "编辑处理中");
String sha = head(owner, code);
Map<String, Object> input = Map.of("expectedCommitSha", sha, "message", "编辑说明",
"changes", List.of(Map.of("action", "UPDATE_FILE", "path", "README.md", "content", "新说明")));
gitea.hideReceipts = true;
gitea.timeoutNextCommit = true;
JsonNode failure = perform(body(auth(post("/api/workspace/projects/" + code + "/commits"), owner), input), 504);
assertThat(failure.path("code").asText()).isEqualTo("REMOTE_RESULT_UNKNOWN");
String operation = jdbc.queryForObject("SELECT operation_code FROM repository_write_operation WHERE project_code=?",
String.class, code);
int calls = gitea.commitCalls;
perform(body(auth(post("/api/workspace/projects/" + code + "/commits"), owner), input), 409);
assertThat(gitea.commitCalls).isEqualTo(calls);
gitea.hideReceipts = false;
jdbc.update("UPDATE repository_write_operation SET next_check_at=? WHERE operation_code=?",
com.linkerbot.vibemake.service.support.ProjectSupport.now().minusSeconds(1), operation);
writes.reconcileDue();
String confirmed = head(owner, code);
assertThat(confirmed).isNotEqualTo(sha);
long userId = jdbc.queryForObject("SELECT id FROM sys_user WHERE email=?", Long.class, email);
assertThat(fileService.commit(userId, code,
json.convertValue(input, com.linkerbot.vibemake.bean.Contracts.CommitInput.class), operation).commitSha())
.isEqualTo(confirmed);
assertThat(gitea.commitCalls).isEqualTo(calls);
}
@Test
void rejectedEditorWriteReplaysFailureWithoutCallingGitea() throws Exception {
String email = "rejected-editor@example.invalid";
String owner = login(email);
String code = create(owner, "拒绝重复创建");
Map<String, Object> input = Map.of("expectedCommitSha", head(owner, code), "message", "不能重复创建",
"changes", List.of(Map.of("action", "CREATE_FILE", "path", "README.md", "content", "重复")));
perform(body(auth(post("/api/workspace/projects/" + code + "/commits"), owner), input), 409);
String key = jdbc.queryForObject("SELECT operation_code FROM repository_write_operation WHERE project_code=?",
String.class, code);
int calls = gitea.commitCalls;
long userId = jdbc.queryForObject("SELECT id FROM sys_user WHERE email=?", Long.class, email);
assertThatThrownBy(() -> fileService.commit(userId, code,
json.convertValue(input, com.linkerbot.vibemake.bean.Contracts.CommitInput.class), key))
.isInstanceOfSatisfying(BusinessException.class, error -> assertThat(error.status()).isEqualTo(409));
assertThat(gitea.commitCalls).isEqualTo(calls);
assertThat(jdbc.queryForObject("SELECT status FROM repository_write_operation WHERE operation_code=?",
String.class, key)).isEqualTo("FAILED");
assertThat(jdbc.queryForObject("SELECT repo_write_status FROM project WHERE project_code=?",
String.class, code)).isEqualTo("IDLE");
}
@Test
void unknownRemoteWritesAreNotRepeatedAndFailedCreatesRemainVisible() throws Exception {
String owner = login("failure@example.com");
@@ -1545,7 +1601,7 @@ class WorkflowTest {
"实际成功")));
perform(
body(auth(post("/api/workspace/projects/" + code + "/commits"), owner), request),
504);
200);
perform(
body(auth(post("/api/workspace/projects/" + code + "/commits"), owner), request),
409);