后台自动核对REVIEW状态的项目
This commit is contained in:
@@ -28,6 +28,11 @@ public interface FileService {
|
||||
|
||||
CommitView commit(long userId, String code, CommitInput input);
|
||||
|
||||
CommitView commit(long userId, String code, CommitInput input, String operationCode);
|
||||
|
||||
CommitView upload(long userId, String code, String expectedSha, String message, String directory,
|
||||
List<MultipartFile> files, List<String> paths, String operationCode);
|
||||
|
||||
CommitView upload(
|
||||
long userId,
|
||||
String code,
|
||||
|
||||
@@ -15,6 +15,12 @@ public interface GiteaClient {
|
||||
|
||||
record Change(String operation, String path, String blobSha, byte[] bytes) {}
|
||||
|
||||
/** 只从 main 可达的提交中查找,并校验完整尾注及唯一父提交;空结果不是失败证明。 */
|
||||
default String findWriteReceipt(Repository repository, String baseSha, String operationCode,
|
||||
String requestDigest) {
|
||||
throw new UnsupportedOperationException("未实现仓库写入凭据核对");
|
||||
}
|
||||
|
||||
record RemoteRepository(long id, String owner, String name, boolean privateRepo,
|
||||
boolean mirror, boolean empty, String originalUrl, String defaultBranch) {
|
||||
public Repository repository() { return new Repository(id, owner, name); }
|
||||
|
||||
+32
-1
@@ -425,7 +425,7 @@ public class RestGiteaClient implements GiteaClient {
|
||||
items.add(
|
||||
new HistoryEntry(
|
||||
node.path("sha").asText(),
|
||||
commit.path("message").asText(),
|
||||
WriteReceipt.displayMessage(commit.path("message").asText()),
|
||||
commit.path("author").path("name").asText(),
|
||||
commit.path("author").path("date").asText()));
|
||||
}
|
||||
@@ -436,4 +436,35 @@ public class RestGiteaClient implements GiteaClient {
|
||||
.orElse((long) items.size());
|
||||
return new PageResponse<>(items, page, size, total);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String findWriteReceipt(Repository repo, String baseSha, String operationCode,
|
||||
String requestDigest) {
|
||||
// 普通文件写入被项目锁串行化,未知期间禁止下一次写入。限制扫描量防止异常仓库拖垮任务。
|
||||
// 超出扫描范围不推断失败,留在处理中并交给管理员兜底。
|
||||
String suffix = WriteReceipt.suffix(operationCode, requestDigest);
|
||||
String head = head(repo);
|
||||
String found = null;
|
||||
for (int page = 1; page <= 20; page++) {
|
||||
JsonNode entries = parse(request("GET", repoPath(repo)
|
||||
+ "/commits?sha=" + head + "&page=" + page + "&limit=50", null).body(), false);
|
||||
if (!entries.isArray())
|
||||
throw new GiteaFailure(502, "GITEA_INVALID_RESPONSE", "提交记录格式异常", false);
|
||||
for (JsonNode entry : entries) {
|
||||
if (entry.path("commit").path("message").asText().stripTrailing().endsWith(suffix)) {
|
||||
JsonNode parents = entry.path("parents");
|
||||
if (parents.size() != 1 || !baseSha.equals(parents.path(0).path("sha").asText()))
|
||||
throw new GiteaFailure(409, "WRITE_RECEIPT_PARENT_MISMATCH", "提交父版本不匹配", false);
|
||||
String sha = FilePaths.sha(entry.path("sha").asText());
|
||||
if (found != null && !found.equals(sha))
|
||||
throw new GiteaFailure(409, "WRITE_RECEIPT_AMBIGUOUS", "存在多个操作凭据", false);
|
||||
found = sha;
|
||||
}
|
||||
}
|
||||
if (entries.size() < 50) break;
|
||||
}
|
||||
if (!head.equals(head(repo)))
|
||||
throw new GiteaFailure(409, "WRITE_RECEIPT_BRANCH_CHANGED", "核对期间分支版本已变化", false);
|
||||
return found;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.linkerbot.vibemake.service.gitea;
|
||||
|
||||
/** 固定尾注同时绑定操作编码和请求摘要,普通提交信息不会被当作凭据。 */
|
||||
public final class WriteReceipt {
|
||||
private WriteReceipt() {}
|
||||
|
||||
public static String suffix(String code, String digest) {
|
||||
return "\n\nVibemake-Operation: " + code + "\nVibemake-Request: " + digest;
|
||||
}
|
||||
|
||||
public static String displayMessage(String message) {
|
||||
return message.replaceFirst("\\n\\nVibemake-Operation: [a-f0-9]{32}\\nVibemake-Request: [a-f0-9]{64}\\s*$", "");
|
||||
}
|
||||
}
|
||||
+27
-9
@@ -11,6 +11,8 @@ import com.linkerbot.vibemake.service.gitea.*;
|
||||
import com.linkerbot.vibemake.service.state.RateLimits;
|
||||
import com.linkerbot.vibemake.service.support.ProjectActors;
|
||||
import com.linkerbot.vibemake.service.support.ProjectSupport;
|
||||
import com.linkerbot.vibemake.service.write.RepositoryWrites;
|
||||
import com.linkerbot.vibemake.service.write.WriteRequestDigest;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
@@ -33,6 +35,8 @@ public class FileServiceImpl implements FileService {
|
||||
private final UserService users;
|
||||
private final GiteaClient gitea;
|
||||
private final VibemakeProperties config;
|
||||
private final RepositoryWrites writes;
|
||||
private final WriteRequestDigest digests;
|
||||
|
||||
@Override
|
||||
public TreeView ownerTree(long userId, String code, String path, String commitSha) {
|
||||
@@ -137,8 +141,14 @@ public class FileServiceImpl implements FileService {
|
||||
|
||||
@Override
|
||||
public CommitView commit(long userId, String code, CommitInput input) {
|
||||
return commit(userId, code, input, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommitView commit(long userId, String code, CommitInput input, String operationCode) {
|
||||
projects.readyOwner(userId, code);
|
||||
rateLimits.operation("file-write", userId);
|
||||
return projects.write(
|
||||
return writes.execute(userId, code, operationCode, digests.commit(input), request -> projects.write(
|
||||
userId,
|
||||
code,
|
||||
project -> {
|
||||
@@ -191,19 +201,18 @@ public class FileServiceImpl implements FileService {
|
||||
size(bytes, config.getMaxRequestBytes());
|
||||
ProjectActors.Author author = ProjectActors.author(users, userId);
|
||||
CommitView result =
|
||||
gitea.commit(
|
||||
repo,
|
||||
writes.commit(
|
||||
request, project, sha,
|
||||
changes,
|
||||
input.message(),
|
||||
author.nickname(),
|
||||
author.code());
|
||||
author);
|
||||
log.info(
|
||||
"文件提交 projectCode={} commitSha={} fileCount={}",
|
||||
code,
|
||||
result.commitSha(),
|
||||
changes.size());
|
||||
return result;
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -215,6 +224,14 @@ public class FileServiceImpl implements FileService {
|
||||
String directory,
|
||||
List<MultipartFile> files,
|
||||
List<String> paths) {
|
||||
return upload(userId, code, expectedSha, message, directory, files, paths, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommitView upload(long userId, String code, String expectedSha, String message,
|
||||
String directory, List<MultipartFile> files, List<String> paths,
|
||||
String operationCode) {
|
||||
projects.readyOwner(userId, code);
|
||||
rateLimits.operation("file-write", userId);
|
||||
if (message == null || message.isBlank() || message.length() > 1000)
|
||||
throw BusinessException.badRequest("提交说明不合法");
|
||||
@@ -224,7 +241,8 @@ public class FileServiceImpl implements FileService {
|
||||
|| (paths != null && paths.size() != files.size()))
|
||||
throw BusinessException.badRequest("文件和路径数量不合法");
|
||||
String prefix = FilePaths.directory(directory);
|
||||
return projects.write(
|
||||
return writes.execute(userId, code, operationCode,
|
||||
digests.upload(expectedSha, message, directory, files, paths), request -> projects.write(
|
||||
userId,
|
||||
code,
|
||||
project -> {
|
||||
@@ -264,14 +282,14 @@ public class FileServiceImpl implements FileService {
|
||||
}
|
||||
ProjectActors.Author author = ProjectActors.author(users, userId);
|
||||
CommitView result =
|
||||
gitea.commit(repo, changes, message, author.nickname(), author.code());
|
||||
writes.commit(request, project, sha, changes, message, author);
|
||||
log.info(
|
||||
"文件上传 projectCode={} commitSha={} fileCount={}",
|
||||
code,
|
||||
result.commitSha(),
|
||||
changes.size());
|
||||
return result;
|
||||
});
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.linkerbot.vibemake.service.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.linkerbot.vibemake.bean.entity.RepositoryWriteOperation;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface RepositoryWriteOperationMapper extends BaseMapper<RepositoryWriteOperation> {}
|
||||
+6
-2
@@ -28,7 +28,7 @@ import javax.sql.DataSource;
|
||||
|
||||
/** 数据库命名锁不持有业务事务;专用连接在整个项目操作期间保持占用。 */
|
||||
@Component
|
||||
public class ProjectLocks {
|
||||
public class ProjectLocks implements AutoCloseable {
|
||||
private static final org.slf4j.Logger log =
|
||||
org.slf4j.LoggerFactory.getLogger(ProjectLocks.class);
|
||||
private final ReentrantLock[] locks = new ReentrantLock[128];
|
||||
@@ -83,6 +83,10 @@ public class ProjectLocks {
|
||||
() -> {
|
||||
if (writing.get().contains(code)) return action.get();
|
||||
Project guard = guard(code);
|
||||
// 已持有共享项目锁,以数据库为准同步其他进程确认的恢复结果。
|
||||
if (guard != null && "IDLE".equals(guard.getRepoWriteStatus())
|
||||
&& uncertain.remove(code))
|
||||
log.info("同步已恢复的仓库写入状态 projectCode={} writeStatus=IDLE", code);
|
||||
boolean memoryUncertain = uncertain.contains(code);
|
||||
if (memoryUncertain
|
||||
|| guard != null && !"IDLE".equals(guard.getRepoWriteStatus())) {
|
||||
@@ -212,7 +216,7 @@ public class ProjectLocks {
|
||||
}
|
||||
|
||||
private BusinessException blocked() {
|
||||
return new BusinessException(409, "REMOTE_RESULT_UNKNOWN", "上次仓库写入结果待人工核对,暂不能继续写入");
|
||||
return new BusinessException(409, "REMOTE_RESULT_UNKNOWN", "仓库写入结果正在核对,请稍后查询处理结果");
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.linkerbot.vibemake.service.write;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
|
||||
@Configuration
|
||||
@EnableScheduling
|
||||
@RequiredArgsConstructor
|
||||
@ConditionalOnProperty(name = "vibemake.repository-write.worker-enabled", matchIfMissing = true)
|
||||
public class RepositoryWriteWorker {
|
||||
private static final Logger log = LoggerFactory.getLogger(RepositoryWriteWorker.class);
|
||||
private final RepositoryWrites writes;
|
||||
private final java.util.concurrent.ExecutorService executor = java.util.concurrent.Executors
|
||||
.newSingleThreadExecutor(Thread.ofPlatform().name("repository-write-check").factory());
|
||||
private final java.util.concurrent.atomic.AtomicBoolean running = new java.util.concurrent.atomic.AtomicBoolean();
|
||||
|
||||
@Scheduled(fixedDelay = 1000, initialDelay = 15000)
|
||||
public void reconcile() {
|
||||
if (executor.isShutdown() || !running.compareAndSet(false, true)) return;
|
||||
try {
|
||||
executor.execute(() -> {
|
||||
try {
|
||||
writes.reconcileDue();
|
||||
} catch (RuntimeException error) {
|
||||
com.linkerbot.vibemake.common.SafeExceptionLog.write(log, "仓库写入核对任务失败", error);
|
||||
} finally {
|
||||
running.set(false);
|
||||
}
|
||||
});
|
||||
} catch (java.util.concurrent.RejectedExecutionException shutdown) {
|
||||
running.set(false);
|
||||
}
|
||||
}
|
||||
|
||||
@jakarta.annotation.PreDestroy
|
||||
public void close() throws InterruptedException {
|
||||
executor.shutdown();
|
||||
if (!executor.awaitTermination(5, java.util.concurrent.TimeUnit.SECONDS)) executor.shutdownNow();
|
||||
}
|
||||
}
|
||||
+239
@@ -0,0 +1,239 @@
|
||||
package com.linkerbot.vibemake.service.write;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.linkerbot.vibemake.bean.Contracts.CommitView;
|
||||
import com.linkerbot.vibemake.bean.entity.Project;
|
||||
import com.linkerbot.vibemake.bean.entity.RepositoryWriteOperation;
|
||||
import com.linkerbot.vibemake.common.*;
|
||||
import com.linkerbot.vibemake.service.ProjectService;
|
||||
import com.linkerbot.vibemake.service.gitea.*;
|
||||
import com.linkerbot.vibemake.service.mapper.ProjectMapper;
|
||||
import com.linkerbot.vibemake.service.mapper.RepositoryWriteOperationMapper;
|
||||
import com.linkerbot.vibemake.service.support.ProjectActors;
|
||||
import com.linkerbot.vibemake.service.support.ProjectLocks;
|
||||
import com.linkerbot.vibemake.service.support.ProjectSupport;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
|
||||
/** 数据库凭据先于远端写入落盘。未知结果只读取核对,绝不自动重发写请求。 */
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class RepositoryWrites {
|
||||
private static final Logger log = LoggerFactory.getLogger(RepositoryWrites.class);
|
||||
private static final List<String> ACTIVE = List.of("PROCESSING", "NEEDS_REVIEW");
|
||||
private final RepositoryWriteOperationMapper operations;
|
||||
private final ProjectMapper projectMapper;
|
||||
private final ProjectService projects;
|
||||
private final ProjectLocks locks;
|
||||
private final GiteaClient gitea;
|
||||
private final TransactionTemplate transactions;
|
||||
|
||||
public record Request(String code, String digest) {}
|
||||
|
||||
public CommitView execute(long userId, String projectCode, String key, String digest,
|
||||
Function<Request, CommitView> action) {
|
||||
String operationCode = key == null ? Codes.next() : key.replace("-", "").toLowerCase(java.util.Locale.ROOT);
|
||||
if (!operationCode.matches("[a-f0-9]{32}"))
|
||||
throw BusinessException.badRequest("操作编码必须为 UUID 或 32 位十六进制编码");
|
||||
return locks.serial(projectCode, () -> {
|
||||
projects.readyOwner(userId, projectCode); // 重放也必须重新鉴权。
|
||||
RepositoryWriteOperation previous = operations.selectById(operationCode);
|
||||
if (previous != null) {
|
||||
if (!projectCode.equals(previous.getProjectCode()) || !digest.equals(previous.getRequestDigest()))
|
||||
throw new BusinessException(409, "IDEMPOTENCY_KEY_REUSED", "操作编码已用于不同请求,请勿复用");
|
||||
return replay(previous);
|
||||
}
|
||||
RepositoryWriteOperation pending = active(projectCode);
|
||||
if (pending != null)
|
||||
throw pending(pending);
|
||||
try {
|
||||
CommitView result = action.apply(new Request(operationCode, digest));
|
||||
RepositoryWriteOperation operation = operations.selectById(operationCode);
|
||||
if (operation == null) throw new IllegalStateException("仓库写入缺少持久化凭据");
|
||||
complete(operation, result.commitSha(), "SUCCEEDED");
|
||||
return result;
|
||||
} catch (RuntimeException error) {
|
||||
RepositoryWriteOperation operation = operations.selectById(operationCode);
|
||||
if (operation == null) throw error; // 参数校验、读取失败发生在任何远端写入之前。
|
||||
if (!projectCode.equals(operation.getProjectCode()) || !digest.equals(operation.getRequestDigest()))
|
||||
throw new BusinessException(409, "IDEMPOTENCY_KEY_REUSED", "操作编码已用于不同请求,请勿复用");
|
||||
boolean definite = error instanceof GiteaFailure remote && !remote.uncertain();
|
||||
if (definite) {
|
||||
BusinessException business = (BusinessException) error;
|
||||
operation.setStatus("FAILED");
|
||||
operation.setFailureStatus(business.status());
|
||||
operation.setLastErrorCode(business.code());
|
||||
save(operation);
|
||||
throw error;
|
||||
}
|
||||
locks.markUncertain(projectCode);
|
||||
operation.setLastErrorCode(error instanceof BusinessException business
|
||||
? business.code() : "WRITE_RESULT_UNKNOWN");
|
||||
save(operation);
|
||||
log.warn("仓库写入转后台核对 projectCode={} operationCode={} errorCode={} errorType={}",
|
||||
projectCode, operationCode, operation.getLastErrorCode(), error.getClass().getSimpleName());
|
||||
if (!(error instanceof BusinessException)) SafeExceptionLog.write(log, "仓库提交结果未知", error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** 必须在 ProjectLocks.write 内调用;此处是唯一一次发起远端写请求的位置。 */
|
||||
public CommitView commit(Request request, Project project, String baseSha,
|
||||
List<GiteaClient.Change> changes, String message,
|
||||
ProjectActors.Author author) {
|
||||
GiteaClient.Repository repository = projects.repository(project);
|
||||
RepositoryWriteOperation operation = new RepositoryWriteOperation();
|
||||
operation.setOperationCode(request.code());
|
||||
operation.setProjectCode(project.getProjectCode());
|
||||
operation.setRepositoryId(repository.id());
|
||||
operation.setRepositoryOwner(repository.owner());
|
||||
operation.setRepositoryName(repository.name());
|
||||
operation.setBaseCommitSha(baseSha);
|
||||
operation.setRequestDigest(request.digest());
|
||||
operation.setCommitMessage(message);
|
||||
operation.setStatus("PROCESSING");
|
||||
operation.setCheckCount(0);
|
||||
operation.setCreatedAt(ProjectSupport.now());
|
||||
operation.setUpdatedAt(operation.getCreatedAt());
|
||||
operation.setNextCheckAt(operation.getCreatedAt().plusSeconds(15));
|
||||
try {
|
||||
operations.insert(operation);
|
||||
} catch (org.springframework.dao.DuplicateKeyException collision) {
|
||||
throw new BusinessException(409, "IDEMPOTENCY_KEY_REUSED", "操作编码已用于其他请求,请勿复用");
|
||||
}
|
||||
log.info("仓库写入开始 projectCode={} operationCode={} baseCommitSha={} fileCount={}",
|
||||
project.getProjectCode(), request.code(), baseSha, changes.size());
|
||||
try {
|
||||
CommitView result = gitea.commit(repository, changes,
|
||||
message + WriteReceipt.suffix(request.code(), request.digest()), author.nickname(), author.code());
|
||||
return new CommitView(result.commitSha(), message);
|
||||
} catch (GiteaFailure error) {
|
||||
if (!error.uncertain()) throw error;
|
||||
// 尝试立即消除响应丢失的影响,查询失败依旧保留未知,不覆盖原始错误确定性。
|
||||
String receipt = receipt(operation, project);
|
||||
if (receipt != null) return new CommitView(receipt, message);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
private CommitView replay(RepositoryWriteOperation operation) {
|
||||
return switch (operation.getStatus()) {
|
||||
case "SUCCEEDED" -> new CommitView(operation.getResultCommitSha(), operation.getCommitMessage());
|
||||
case "FAILED" -> throw new BusinessException(operation.getFailureStatus(),
|
||||
operation.getLastErrorCode(), "该操作已被仓库拒绝,请刷新后使用新的操作编码提交");
|
||||
default -> throw pending(operation);
|
||||
};
|
||||
}
|
||||
|
||||
private BusinessException pending(RepositoryWriteOperation operation) {
|
||||
return new BusinessException(409, "REMOTE_RESULT_UNKNOWN", "仓库提交结果正在后台核对,请稍后刷新工作区,暂勿重复提交",
|
||||
java.util.Map.of("projectCode", operation.getProjectCode()));
|
||||
}
|
||||
|
||||
public RepositoryWriteOperation active(String code) {
|
||||
return operations.selectOne(new LambdaQueryWrapper<RepositoryWriteOperation>()
|
||||
.eq(RepositoryWriteOperation::getProjectCode, code)
|
||||
.in(RepositoryWriteOperation::getStatus, ACTIVE)
|
||||
.orderByDesc(RepositoryWriteOperation::getCreatedAt).last("LIMIT 1"));
|
||||
}
|
||||
|
||||
private boolean slowCheckDue(RepositoryWriteOperation operation) {
|
||||
return !ProjectSupport.now().isBefore(operation.getCreatedAt().plusMinutes(10));
|
||||
}
|
||||
|
||||
/** 重启后直接扫描数据库,无内存任务队列;多实例由相同项目命名锁串行核对。 */
|
||||
public void reconcileDue() {
|
||||
List<RepositoryWriteOperation> due = operations.selectList(new LambdaQueryWrapper<RepositoryWriteOperation>()
|
||||
.in(RepositoryWriteOperation::getStatus, ACTIVE)
|
||||
.le(RepositoryWriteOperation::getNextCheckAt, ProjectSupport.now())
|
||||
.orderByAsc(RepositoryWriteOperation::getNextCheckAt).last("LIMIT 20"));
|
||||
for (RepositoryWriteOperation candidate : due) {
|
||||
try {
|
||||
locks.serial(candidate.getProjectCode(), () -> {
|
||||
reconcile(candidate.getOperationCode());
|
||||
return null;
|
||||
});
|
||||
} catch (RuntimeException error) {
|
||||
log.warn("仓库后台核对未完成 projectCode={} operationCode={} errorCode={} errorType={}",
|
||||
candidate.getProjectCode(), candidate.getOperationCode(),
|
||||
error instanceof BusinessException business ? business.code() : "CHECK_FAILED",
|
||||
error.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void reconcile(String operationCode) {
|
||||
RepositoryWriteOperation operation = operations.selectById(operationCode);
|
||||
if (operation == null || !ACTIVE.contains(operation.getStatus())
|
||||
|| operation.getNextCheckAt().isAfter(ProjectSupport.now())) return;
|
||||
Project project = projectMapper.selectOne(new LambdaQueryWrapper<Project>()
|
||||
.eq(Project::getProjectCode, operation.getProjectCode()));
|
||||
String receipt = project == null ? null : receipt(operation, project);
|
||||
operation.setCheckCount(operation.getCheckCount() + 1);
|
||||
if (receipt != null) {
|
||||
complete(operation, receipt, "SUCCEEDED");
|
||||
return;
|
||||
}
|
||||
boolean overdue = slowCheckDue(operation);
|
||||
operation.setStatus(overdue ? "NEEDS_REVIEW" : "PROCESSING");
|
||||
operation.setNextCheckAt(ProjectSupport.now().plusSeconds(overdue ? 60 : 15));
|
||||
save(operation);
|
||||
log.info("仓库写入继续核对 projectCode={} operationCode={} status={} checkCount={} errorCode={}",
|
||||
operation.getProjectCode(), operationCode, operation.getStatus(),
|
||||
operation.getCheckCount(), operation.getLastErrorCode());
|
||||
}
|
||||
|
||||
private String receipt(RepositoryWriteOperation operation, Project project) {
|
||||
try {
|
||||
if (!"READY".equals(project.getRepoStatus())
|
||||
|| !Objects.equals(project.getGiteaRepoId(), operation.getRepositoryId())
|
||||
|| !Objects.equals(project.getGiteaOwner(), operation.getRepositoryOwner())
|
||||
|| !Objects.equals(project.getGiteaRepoName(), operation.getRepositoryName()))
|
||||
throw new GiteaFailure(409, "WRITE_REPOSITORY_MISMATCH", "项目仓库身份发生变化", false);
|
||||
GiteaClient.RemoteRepository remote = gitea.inspect(operation.getRepositoryName());
|
||||
if (remote.id() != operation.getRepositoryId()
|
||||
|| !remote.owner().equals(operation.getRepositoryOwner())
|
||||
|| !remote.name().equals(operation.getRepositoryName())
|
||||
|| !remote.privateRepo() || remote.mirror() || remote.empty()
|
||||
|| !"main".equals(remote.defaultBranch()))
|
||||
throw new GiteaFailure(409, "WRITE_REPOSITORY_MISMATCH", "远端仓库身份发生变化", false);
|
||||
String sha = gitea.findWriteReceipt(remote.repository(), operation.getBaseCommitSha(),
|
||||
operation.getOperationCode(), operation.getRequestDigest());
|
||||
if (sha == null) operation.setLastErrorCode("WRITE_RECEIPT_NOT_FOUND");
|
||||
return sha;
|
||||
} catch (RuntimeException error) {
|
||||
operation.setLastErrorCode(error instanceof BusinessException business
|
||||
? business.code() : "WRITE_RECEIPT_READ_FAILED");
|
||||
log.warn("仓库写入凭据读取未确认 projectCode={} operationCode={} errorCode={} errorType={}",
|
||||
operation.getProjectCode(), operation.getOperationCode(), operation.getLastErrorCode(),
|
||||
error.getClass().getSimpleName());
|
||||
if (!(error instanceof BusinessException)) SafeExceptionLog.write(log, "仓库凭据核对异常", error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void complete(RepositoryWriteOperation operation, String sha, String status) {
|
||||
// 项目恢复与操作终态在同一事务提交;任务获得锁后重读终态,旧任务不会清除新操作的保护。
|
||||
transactions.executeWithoutResult(transaction -> {
|
||||
operation.setStatus(status);
|
||||
operation.setResultCommitSha(sha);
|
||||
save(operation);
|
||||
locks.confirmKnownResult(operation.getProjectCode());
|
||||
});
|
||||
log.info("仓库写入结果已落盘 projectCode={} operationCode={} status={} commitSha={}",
|
||||
operation.getProjectCode(), operation.getOperationCode(), status, sha);
|
||||
}
|
||||
|
||||
private void save(RepositoryWriteOperation operation) {
|
||||
operation.setUpdatedAt(ProjectSupport.now());
|
||||
if (operations.updateById(operation) != 1) throw new IllegalStateException("仓库写入记录更新失败");
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package com.linkerbot.vibemake.service.write;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.linkerbot.vibemake.bean.Contracts.CommitInput;
|
||||
import com.linkerbot.vibemake.common.BusinessException;
|
||||
import com.linkerbot.vibemake.common.FilePaths;
|
||||
import com.linkerbot.vibemake.service.config.VibemakeProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HexFormat;
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class WriteRequestDigest {
|
||||
private final ObjectMapper json;
|
||||
private final VibemakeProperties config;
|
||||
|
||||
public String commit(CommitInput input) {
|
||||
return digest(List.of("commit-v1", input));
|
||||
}
|
||||
|
||||
public String upload(String expected, String message, String directory,
|
||||
List<MultipartFile> files, List<String> paths) {
|
||||
if (message == null || files == null || files.isEmpty() || files.size() > 500
|
||||
|| paths != null && paths.size() != files.size())
|
||||
throw BusinessException.badRequest("文件和路径数量不合法");
|
||||
List<Object> items = new ArrayList<>();
|
||||
long total = 0;
|
||||
for (int i = 0; i < files.size(); i++) {
|
||||
MultipartFile file = files.get(i);
|
||||
total += file.getSize();
|
||||
if (file.getSize() > config.getMaxFileBytes() || total > config.getMaxRequestBytes())
|
||||
throw new BusinessException(413, "FILE_TOO_LARGE", "文件或请求超过大小限制");
|
||||
MessageDigest hash = sha256();
|
||||
long bytes = 0;
|
||||
try (InputStream stream = file.getInputStream()) {
|
||||
byte[] buffer = new byte[8192];
|
||||
int count;
|
||||
while ((count = stream.read(buffer)) != -1) {
|
||||
bytes += count;
|
||||
if (bytes > config.getMaxFileBytes())
|
||||
throw new BusinessException(413, "FILE_TOO_LARGE", "文件超过大小限制");
|
||||
hash.update(buffer, 0, count);
|
||||
}
|
||||
} catch (IOException error) {
|
||||
throw BusinessException.badRequest("读取上传文件失败");
|
||||
}
|
||||
items.add(List.of(FilePaths.file(paths == null ? file.getOriginalFilename() : paths.get(i)),
|
||||
bytes, HexFormat.of().formatHex(hash.digest())));
|
||||
}
|
||||
return digest(List.of("upload-v1", FilePaths.sha(expected), message,
|
||||
FilePaths.directory(directory), items));
|
||||
}
|
||||
|
||||
private String digest(Object value) {
|
||||
try {
|
||||
return HexFormat.of().formatHex(sha256().digest(json.writeValueAsBytes(value)));
|
||||
} catch (JsonProcessingException error) {
|
||||
throw new IllegalStateException("无法生成仓库请求摘要", error);
|
||||
}
|
||||
}
|
||||
|
||||
private MessageDigest sha256() {
|
||||
try {
|
||||
return MessageDigest.getInstance("SHA-256");
|
||||
} catch (NoSuchAlgorithmException error) {
|
||||
throw new IllegalStateException(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
package com.linkerbot.vibemake.service.gitea;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.linkerbot.vibemake.service.config.VibemakeProperties;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
class GiteaWriteReceiptTest {
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"valid", "wrongOperation", "wrongDigest", "wrongParent", "duplicate", "branchChanged", "readFailure"})
|
||||
void receiptRequiresExactIdentityAndParentOnStableMainHistory(String mode) throws Exception {
|
||||
String code = "a".repeat(32);
|
||||
String digest = "b".repeat(64);
|
||||
String base = "1".repeat(40);
|
||||
String sha = "2".repeat(40);
|
||||
String head = "3".repeat(40);
|
||||
ObjectMapper json = new ObjectMapper();
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
AtomicInteger heads = new AtomicInteger();
|
||||
server.createContext("/api/v1/repos/vibemake/repo/branches/main", exchange -> {
|
||||
String value = mode.equals("branchChanged") && heads.incrementAndGet() > 1 ? "4".repeat(40) : head;
|
||||
byte[] body = json.writeValueAsBytes(Map.of("commit", Map.of("id", value)));
|
||||
exchange.sendResponseHeaders(200, body.length);
|
||||
exchange.getResponseBody().write(body);
|
||||
exchange.close();
|
||||
});
|
||||
server.createContext("/api/v1/repos/vibemake/repo/commits", exchange -> {
|
||||
assertThat(exchange.getRequestMethod()).isEqualTo("GET");
|
||||
assertThat(exchange.getRequestURI().getQuery()).contains("sha=" + head);
|
||||
String message = "用户提交" + WriteReceipt.suffix(mode.equals("wrongOperation") ? "c".repeat(32) : code,
|
||||
mode.equals("wrongDigest") ? "c".repeat(64) : digest);
|
||||
Map<String, Object> match = Map.of("sha", sha, "commit", Map.of("message", message),
|
||||
"parents", List.of(Map.of("sha", mode.equals("wrongParent") ? head : base)));
|
||||
Map<String, Object> unrelated = Map.of("sha", head, "commit", Map.of("message", "另一次提交"),
|
||||
"parents", List.of(Map.of("sha", sha)));
|
||||
List<Object> entries = new java.util.ArrayList<>(List.of(unrelated, match));
|
||||
if (mode.equals("duplicate")) entries.add(Map.of("sha", "5".repeat(40), "commit", Map.of("message", message),
|
||||
"parents", List.of(Map.of("sha", base))));
|
||||
byte[] body = mode.equals("readFailure") ? "failure".getBytes(StandardCharsets.UTF_8)
|
||||
: json.writeValueAsBytes(entries);
|
||||
exchange.sendResponseHeaders(mode.equals("readFailure") ? 503 : 200, body.length);
|
||||
exchange.getResponseBody().write(body);
|
||||
exchange.close();
|
||||
});
|
||||
server.start();
|
||||
try {
|
||||
VibemakeProperties config = new VibemakeProperties();
|
||||
config.getGitea().setBaseUrl("http://127.0.0.1:" + server.getAddress().getPort());
|
||||
config.getGitea().setToken("unit-test-placeholder");
|
||||
RestGiteaClient client = new RestGiteaClient(config, json);
|
||||
GiteaClient.Repository repo = new GiteaClient.Repository(1, "vibemake", "repo");
|
||||
if (List.of("wrongParent", "duplicate", "branchChanged", "readFailure").contains(mode)) {
|
||||
assertThatThrownBy(() -> client.findWriteReceipt(repo, base, code, digest))
|
||||
.isInstanceOfSatisfying(GiteaFailure.class, error -> {
|
||||
assertThat(error.uncertain()).isFalse();
|
||||
assertThat(error.code()).isEqualTo(switch (mode) {
|
||||
case "wrongParent" -> "WRITE_RECEIPT_PARENT_MISMATCH";
|
||||
case "duplicate" -> "WRITE_RECEIPT_AMBIGUOUS";
|
||||
case "branchChanged" -> "WRITE_RECEIPT_BRANCH_CHANGED";
|
||||
default -> "GITEA_ERROR";
|
||||
});
|
||||
});
|
||||
} else {
|
||||
assertThat(client.findWriteReceipt(repo, base, code, digest))
|
||||
.isEqualTo(mode.equals("valid") ? sha : null);
|
||||
}
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user