git clone功能
网站登录 → 创建项目 Git Token → clone → 本地修改、commit → push main → 网站工作区刷新后显示最新内容 → 网站手动发布到社区
This commit is contained in:
@@ -0,0 +1,728 @@
|
||||
package com.linkerbot.vibemake.api;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.linkerbot.vibemake.common.SecurityHashes;
|
||||
import com.linkerbot.vibemake.service.git.*;
|
||||
import com.linkerbot.vibemake.service.write.RepositoryWrites;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.*;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.context.*;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
|
||||
import org.springframework.test.web.servlet.*;
|
||||
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
@SpringBootTest(
|
||||
properties = {
|
||||
"spring.datasource.url=jdbc:h2:mem:gitapi;MODE=MySQL;DB_CLOSE_DELAY=-1",
|
||||
"spring.datasource.username=sa",
|
||||
"spring.datasource.password=",
|
||||
"spring.datasource.driver-class-name=org.h2.Driver",
|
||||
"vibemake.ai.enabled=false",
|
||||
"vibemake.oss.enabled=false",
|
||||
"vibemake.limits.enabled=false",
|
||||
"vibemake.gitea.owner=vibemake",
|
||||
"vibemake.repository-write.worker-enabled=false",
|
||||
"vibemake.git.enabled=true",
|
||||
"vibemake.git.public-base-url=https://git.example/git",
|
||||
"vibemake.git.upstream-username=fixture-bot",
|
||||
"vibemake.git.upstream-token=fixture-only-secret",
|
||||
"vibemake.git.auth-failures-per-minute=1000",
|
||||
"logging.level.root=WARN"
|
||||
})
|
||||
@ActiveProfiles("test")
|
||||
@AutoConfigureMockMvc(print = MockMvcPrint.NONE)
|
||||
@Import(WorkflowTest.Config.class)
|
||||
class GitApiTest {
|
||||
static HttpServer server;
|
||||
static AtomicReference<String> mode = new AtomicReference<>("ok");
|
||||
static AtomicReference<byte[]> received = new AtomicReference<>();
|
||||
static java.util.concurrent.CountDownLatch pushEntered;
|
||||
static java.util.concurrent.CountDownLatch pushRelease;
|
||||
static String upstream;
|
||||
static final byte[] OBJECT = "LFS binary 中文\0fixture".getBytes(StandardCharsets.UTF_8);
|
||||
static final String OID = java.util.HexFormat.of().formatHex(hash(OBJECT));
|
||||
|
||||
private static byte[] hash(byte[] bytes) {
|
||||
try {
|
||||
return java.security.MessageDigest.getInstance("SHA-256").digest(bytes);
|
||||
} catch (Exception error) {
|
||||
throw new IllegalStateException(error);
|
||||
}
|
||||
}
|
||||
|
||||
@DynamicPropertySource
|
||||
static void upstream(DynamicPropertyRegistry registry) throws Exception {
|
||||
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
upstream = "http://127.0.0.1:" + server.getAddress().getPort();
|
||||
server.createContext(
|
||||
"/",
|
||||
exchange -> {
|
||||
String path = exchange.getRequestURI().getPath();
|
||||
byte[] result;
|
||||
String type = "application/x-git-upload-pack-advertisement";
|
||||
if (path.endsWith("objects/batch")) {
|
||||
type = LfsGateway.MEDIA;
|
||||
JsonNode input = new ObjectMapper().readTree(exchange.getRequestBody());
|
||||
String op = input.path("operation").asText();
|
||||
String prefix = path.substring(0, path.length() - "objects/batch".length());
|
||||
String href =
|
||||
upstream
|
||||
+ prefix
|
||||
+ "objects/"
|
||||
+ OID
|
||||
+ (op.equals("upload") ? "/" + OBJECT.length : "");
|
||||
if (mode.get().equals("evil")) href = "https://untrusted.invalid/" + OID;
|
||||
result =
|
||||
("{\"transfer\":\"basic\",\"objects\":[{\"oid\":\""
|
||||
+ OID
|
||||
+ "\",\"size\":"
|
||||
+ OBJECT.length
|
||||
+ ",\"actions\":{\""
|
||||
+ op
|
||||
+ "\":{\"href\":\""
|
||||
+ href
|
||||
+ "\",\"header\":{\"Authorization\":\"Bearer"
|
||||
+ " upstream-secret\"}}}}]}")
|
||||
.getBytes();
|
||||
} else if (path.contains("/info/lfs/objects/")) {
|
||||
type = "application/octet-stream";
|
||||
if (exchange.getRequestMethod().equals("PUT")) {
|
||||
received.set(exchange.getRequestBody().readAllBytes());
|
||||
result = new byte[0];
|
||||
} else result = OBJECT;
|
||||
} else if (path.endsWith("git-receive-pack")) {
|
||||
received.set(exchange.getRequestBody().readAllBytes());
|
||||
if (mode.get().equals("block")) {
|
||||
pushEntered.countDown();
|
||||
try {
|
||||
pushRelease.await(15, java.util.concurrent.TimeUnit.SECONDS);
|
||||
} catch (InterruptedException error) {
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
}
|
||||
type = "application/x-git-receive-pack-result";
|
||||
result =
|
||||
mode.get().equals("broken")
|
||||
? "not status".getBytes()
|
||||
: packets(
|
||||
"unpack ok\n",
|
||||
"ng refs/heads/main non-fast-forward\n",
|
||||
null);
|
||||
} else
|
||||
result =
|
||||
packets(
|
||||
"# service=git-upload-pack\n",
|
||||
null,
|
||||
"a".repeat(40) + " refs/heads/main\0multi_ack\n",
|
||||
null);
|
||||
exchange.getResponseHeaders().set("Content-Type", type);
|
||||
exchange.sendResponseHeaders(200, result.length == 0 ? -1 : result.length);
|
||||
exchange.getResponseBody().write(result);
|
||||
exchange.close();
|
||||
});
|
||||
server.start();
|
||||
registry.add("vibemake.gitea.base-url", () -> upstream);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void closeUpstream() {
|
||||
server.stop(0);
|
||||
}
|
||||
|
||||
@Autowired MockMvc mvc;
|
||||
@Autowired ObjectMapper json;
|
||||
@Autowired JdbcTemplate jdbc;
|
||||
@MockitoSpyBean FakeGiteaClient gitea;
|
||||
@Autowired RepositoryWrites writes;
|
||||
@Autowired com.linkerbot.vibemake.common.state.TransientStateStore state;
|
||||
@MockitoBean GitPolicy policy;
|
||||
|
||||
@BeforeEach
|
||||
void schema() throws Exception {
|
||||
SchemaSupport.reset(jdbc);
|
||||
reset(gitea);
|
||||
gitea.reset();
|
||||
mode.set("ok");
|
||||
received.set(null);
|
||||
pushEntered = new java.util.concurrent.CountDownLatch(1);
|
||||
pushRelease = new java.util.concurrent.CountDownLatch(1);
|
||||
}
|
||||
|
||||
static byte[] packets(String... lines) throws IOException {
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
for (String line : lines)
|
||||
if (line == null) bytes.write("0000".getBytes());
|
||||
else GitIo.packet(bytes, line.getBytes(StandardCharsets.UTF_8));
|
||||
return bytes.toByteArray();
|
||||
}
|
||||
|
||||
JsonNode ok(MockHttpServletRequestBuilder request) throws Exception {
|
||||
return json.readTree(
|
||||
mvc.perform(request)
|
||||
.andExpect(status().isOk())
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getContentAsByteArray())
|
||||
.path("data");
|
||||
}
|
||||
|
||||
MockHttpServletRequestBuilder body(MockHttpServletRequestBuilder request, Object value)
|
||||
throws Exception {
|
||||
return request.contentType("application/json").content(json.writeValueAsBytes(value));
|
||||
}
|
||||
|
||||
MockHttpServletRequestBuilder auth(MockHttpServletRequestBuilder request, String token) {
|
||||
return request.header("Authorization", "Bearer " + token);
|
||||
}
|
||||
|
||||
record Account(String token, String username) {}
|
||||
|
||||
Account login(String suffix) throws Exception {
|
||||
String captcha = ok(get("/api/auth/captcha")).path("captchaCode").asText();
|
||||
JsonNode response =
|
||||
ok(
|
||||
body(
|
||||
post("/api/auth/login"),
|
||||
Map.of(
|
||||
"email",
|
||||
suffix + "@example.invalid",
|
||||
"captchaCode",
|
||||
captcha,
|
||||
"captchaAnswer",
|
||||
"ABCDE")));
|
||||
return new Account(
|
||||
response.path("token").asText(), response.path("user").path("userCode").asText());
|
||||
}
|
||||
|
||||
String create(Account account) throws Exception {
|
||||
return ok(body(
|
||||
auth(post("/api/workspace/projects"), account.token()),
|
||||
Map.of("name", "Git 项目", "projectType", "GENERAL")))
|
||||
.path("projectCode")
|
||||
.asText();
|
||||
}
|
||||
|
||||
JsonNode issue(Account account, String code, String scope) throws Exception {
|
||||
return ok(
|
||||
body(
|
||||
auth(post(api(code) + "/tokens"), account.token()),
|
||||
Map.of("name", "笔记本", "scope", scope)));
|
||||
}
|
||||
|
||||
String api(String code) {
|
||||
return "/api/workspace/projects/" + code + "/git";
|
||||
}
|
||||
|
||||
String protocol(String code) {
|
||||
return "/git/projects/" + code + ".git";
|
||||
}
|
||||
|
||||
MockHttpServletRequestBuilder basic(
|
||||
MockHttpServletRequestBuilder request, Account account, String secret) {
|
||||
return request.header(
|
||||
"Authorization",
|
||||
"Basic "
|
||||
+ Base64.getEncoder()
|
||||
.encodeToString(
|
||||
(account.username() + ":" + secret)
|
||||
.getBytes(StandardCharsets.UTF_8)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void credentialsAreProjectBoundHashedAndSeparateFromWebsiteSessions() throws Exception {
|
||||
Account owner = login("git-owner"), other = login("git-other");
|
||||
String code = create(owner), second = create(owner);
|
||||
JsonNode issued = issue(owner, code, "READ_ONLY");
|
||||
String secret = issued.path("token").asText();
|
||||
assertThat(secret).startsWith("vbg_");
|
||||
assertThat(jdbc.queryForObject("SELECT token_hash FROM project_git_token", String.class))
|
||||
.isEqualTo(SecurityHashes.sha256(secret));
|
||||
String list = ok(auth(get(api(code) + "/tokens"), owner.token())).toString();
|
||||
assertThat(list).doesNotContain(secret, "tokenHash", "ownerUserId", "\"id\"");
|
||||
mvc.perform(
|
||||
basic(
|
||||
get(protocol(code) + "/info/refs")
|
||||
.param("service", "git-upload-pack"),
|
||||
owner,
|
||||
secret))
|
||||
.andExpect(status().isOk());
|
||||
mvc.perform(
|
||||
basic(
|
||||
get(protocol(second) + "/info/refs")
|
||||
.param("service", "git-upload-pack"),
|
||||
owner,
|
||||
secret))
|
||||
.andExpect(status().isNotFound());
|
||||
mvc.perform(
|
||||
basic(
|
||||
get(protocol(code) + "/info/refs")
|
||||
.param("service", "git-upload-pack"),
|
||||
other,
|
||||
secret))
|
||||
.andExpect(status().isUnauthorized());
|
||||
mvc.perform(auth(get(api(code)), secret)).andExpect(status().isUnauthorized());
|
||||
mvc.perform(
|
||||
auth(
|
||||
get(protocol(code) + "/info/refs")
|
||||
.param("service", "git-upload-pack"),
|
||||
owner.token()))
|
||||
.andExpect(status().isUnauthorized())
|
||||
.andExpect(
|
||||
header().string(
|
||||
"WWW-Authenticate",
|
||||
org.hamcrest.Matchers.containsString("Basic")));
|
||||
mvc.perform(
|
||||
basic(
|
||||
get(protocol(code) + "/info/refs")
|
||||
.param("service", "git-receive-pack"),
|
||||
owner,
|
||||
secret))
|
||||
.andExpect(status().isForbidden());
|
||||
mvc.perform(auth(get(api(code) + "/tokens"), other.token()))
|
||||
.andExpect(status().isNotFound());
|
||||
// 普通退出只清网页会话。
|
||||
ok(auth(post("/api/auth/logout"), owner.token()));
|
||||
mvc.perform(
|
||||
basic(
|
||||
get(protocol(code) + "/info/refs")
|
||||
.param("service", "git-upload-pack"),
|
||||
owner,
|
||||
secret))
|
||||
.andExpect(status().isOk());
|
||||
jdbc.update(
|
||||
"UPDATE sys_user SET session_version=session_version+1 WHERE user_code=?",
|
||||
owner.username());
|
||||
mvc.perform(
|
||||
basic(
|
||||
get(protocol(code) + "/info/refs")
|
||||
.param("service", "git-upload-pack"),
|
||||
owner,
|
||||
secret))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
void expirationRevocationAndTokenQuota() throws Exception {
|
||||
Account owner = login("git-token-lifecycle");
|
||||
String code = create(owner);
|
||||
JsonNode first = issue(owner, code, "READ_WRITE");
|
||||
String tokenCode = first.path("credential").path("tokenCode").asText();
|
||||
for (int index = 1; index < 10; index++) issue(owner, code, "READ_ONLY");
|
||||
mvc.perform(
|
||||
body(
|
||||
auth(post(api(code) + "/tokens"), owner.token()),
|
||||
Map.of("name", "超过", "scope", "READ_ONLY")))
|
||||
.andExpect(status().isConflict());
|
||||
ok(auth(delete(api(code) + "/tokens/" + tokenCode), owner.token()));
|
||||
ok(auth(delete(api(code) + "/tokens/" + tokenCode), owner.token()));
|
||||
mvc.perform(
|
||||
basic(
|
||||
get(protocol(code) + "/info/refs")
|
||||
.param("service", "git-upload-pack"),
|
||||
owner,
|
||||
first.path("token").asText()))
|
||||
.andExpect(status().isUnauthorized());
|
||||
String replacement = issue(owner, code, "READ_ONLY").path("token").asText();
|
||||
jdbc.update("UPDATE project_git_token SET expires_at=DATEADD('DAY',-1,CURRENT_TIMESTAMP)");
|
||||
mvc.perform(
|
||||
basic(
|
||||
get(protocol(code) + "/info/refs")
|
||||
.param("service", "git-upload-pack"),
|
||||
owner,
|
||||
replacement))
|
||||
.andExpect(status().isUnauthorized());
|
||||
for (Map<String, Object> invalid :
|
||||
List.of(
|
||||
Map.<String, Object>of("name", " ", "scope", "READ_ONLY"),
|
||||
Map.<String, Object>of(
|
||||
"name", "x", "scope", "READ_ONLY", "expiresInDays", 366)))
|
||||
mvc.perform(body(auth(post(api(code) + "/tokens"), owner.token()), invalid))
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
void lfsActionsHideUpstreamBindProjectAndRecheckRevocation() throws Exception {
|
||||
Account owner = login("git-lfs");
|
||||
String code = create(owner);
|
||||
JsonNode issued = issue(owner, code, "READ_WRITE");
|
||||
String secret = issued.path("token").asText();
|
||||
Map<String, Object> batch =
|
||||
Map.of(
|
||||
"operation",
|
||||
"upload",
|
||||
"objects",
|
||||
List.of(Map.of("oid", OID, "size", OBJECT.length)));
|
||||
MvcResult result =
|
||||
mvc.perform(
|
||||
basic(
|
||||
post(protocol(code) + "/info/lfs/objects/batch"),
|
||||
owner,
|
||||
secret)
|
||||
.contentType(LfsGateway.MEDIA)
|
||||
.content(json.writeValueAsBytes(batch)))
|
||||
.andExpect(status().isOk())
|
||||
.andReturn();
|
||||
String raw = result.getResponse().getContentAsString();
|
||||
assertThat(raw).doesNotContain(upstream, "upstream-secret", "fixture-only-secret");
|
||||
JsonNode action = json.readTree(raw).path("objects").get(0).path("actions").path("upload");
|
||||
String path = URI.create(action.path("href").asText()).getPath();
|
||||
String auth = action.path("header").path("Authorization").asText();
|
||||
mvc.perform(
|
||||
put(path)
|
||||
.header("Authorization", auth)
|
||||
.contentType("application/octet-stream")
|
||||
.content(OBJECT))
|
||||
.andExpect(status().isOk());
|
||||
assertThat(received.get()).isEqualTo(OBJECT);
|
||||
String readonly = issue(owner, code, "READ_ONLY").path("token").asText();
|
||||
mvc.perform(
|
||||
basic(post(protocol(code) + "/info/lfs/objects/batch"), owner, readonly)
|
||||
.contentType(LfsGateway.MEDIA)
|
||||
.content(json.writeValueAsBytes(batch)))
|
||||
.andExpect(status().isForbidden());
|
||||
mode.set("evil");
|
||||
mvc.perform(
|
||||
basic(post(protocol(code) + "/info/lfs/objects/batch"), owner, secret)
|
||||
.contentType(LfsGateway.MEDIA)
|
||||
.content(json.writeValueAsBytes(batch)))
|
||||
.andExpect(status().isBadGateway());
|
||||
ok(
|
||||
auth(
|
||||
delete(
|
||||
api(code)
|
||||
+ "/tokens/"
|
||||
+ issued.path("credential").path("tokenCode").asText()),
|
||||
owner.token()));
|
||||
mvc.perform(put(path).header("Authorization", auth).content(OBJECT))
|
||||
.andExpect(status().isUnauthorized());
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitGitRejectionUnlocksButUnknownStatusRequiresReview() throws Exception {
|
||||
Account owner = login("git-push");
|
||||
String code = create(owner);
|
||||
String secret = issue(owner, code, "READ_WRITE").path("token").asText();
|
||||
String old =
|
||||
ok(auth(get("/api/workspace/projects/" + code + "/tree"), owner.token()))
|
||||
.path("commitSha")
|
||||
.asText();
|
||||
byte[] body =
|
||||
packets(
|
||||
old + " " + "b".repeat(40) + " refs/heads/main\0report-status ofs-delta",
|
||||
null);
|
||||
mvc.perform(
|
||||
basic(post(protocol(code) + "/git-receive-pack"), owner, secret)
|
||||
.contentType("application/x-git-receive-pack-request")
|
||||
.content(body))
|
||||
.andExpect(status().isOk());
|
||||
assertThat(
|
||||
jdbc.queryForObject(
|
||||
"SELECT status FROM repository_write_operation", String.class))
|
||||
.isEqualTo("FAILED");
|
||||
assertThat(jdbc.queryForObject("SELECT repo_write_status FROM project", String.class))
|
||||
.isEqualTo("IDLE");
|
||||
mode.set("broken");
|
||||
mvc.perform(
|
||||
basic(post(protocol(code) + "/git-receive-pack"), owner, secret)
|
||||
.contentType("application/x-git-receive-pack-request")
|
||||
.content(body))
|
||||
.andExpect(status().isBadGateway());
|
||||
assertThat(jdbc.queryForObject("SELECT repo_write_status FROM project", String.class))
|
||||
.isEqualTo("REVIEW");
|
||||
jdbc.update(
|
||||
"UPDATE repository_write_operation SET"
|
||||
+ " next_check_at=DATEADD('SECOND',-1,CURRENT_TIMESTAMP),created_at=DATEADD('MINUTE',-11,CURRENT_TIMESTAMP)"
|
||||
+ " WHERE status='PROCESSING'");
|
||||
writes.reconcileDue();
|
||||
assertThat(
|
||||
jdbc.queryForObject(
|
||||
"SELECT status FROM repository_write_operation WHERE"
|
||||
+ " status<>'FAILED'",
|
||||
String.class))
|
||||
.isEqualTo("NEEDS_REVIEW");
|
||||
assertThat(jdbc.queryForObject("SELECT repo_write_status FROM project", String.class))
|
||||
.isEqualTo("REVIEW");
|
||||
doReturn("b".repeat(40)).when(gitea).head(any());
|
||||
jdbc.update(
|
||||
"UPDATE repository_write_operation SET"
|
||||
+ " next_check_at=DATEADD('SECOND',-1,CURRENT_TIMESTAMP) WHERE"
|
||||
+ " status='NEEDS_REVIEW'");
|
||||
writes.reconcileDue();
|
||||
assertThat(
|
||||
jdbc.queryForObject(
|
||||
"SELECT status FROM repository_write_operation WHERE"
|
||||
+ " status<>'FAILED'",
|
||||
String.class))
|
||||
.isEqualTo("SUCCEEDED");
|
||||
assertThat(jdbc.queryForObject("SELECT repo_write_status FROM project", String.class))
|
||||
.isEqualTo("IDLE");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nativePushHoldsSameLockAsWebsiteWritesAndPersistsBeforeForwarding() throws Exception {
|
||||
Account owner = login("git-shared-lock");
|
||||
String code = create(owner);
|
||||
String secret = issue(owner, code, "READ_WRITE").path("token").asText();
|
||||
String old =
|
||||
ok(auth(get("/api/workspace/projects/" + code + "/tree"), owner.token()))
|
||||
.path("commitSha")
|
||||
.asText();
|
||||
byte[] payload =
|
||||
packets(
|
||||
old + " " + "b".repeat(40) + " refs/heads/main\0 report-status ofs-delta",
|
||||
null);
|
||||
mode.set("block");
|
||||
try (java.util.concurrent.ExecutorService executor =
|
||||
java.util.concurrent.Executors.newSingleThreadExecutor()) {
|
||||
java.util.concurrent.Future<MvcResult> pushing =
|
||||
executor.submit(
|
||||
() ->
|
||||
mvc.perform(
|
||||
basic(
|
||||
post(
|
||||
protocol(code)
|
||||
+ "/git-receive-pack"),
|
||||
owner,
|
||||
secret)
|
||||
.contentType(
|
||||
"application/x-git-receive-pack-request")
|
||||
.content(payload))
|
||||
.andReturn());
|
||||
try {
|
||||
assertThat(pushEntered.await(5, java.util.concurrent.TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(
|
||||
jdbc.queryForObject(
|
||||
"SELECT repo_write_status FROM project", String.class))
|
||||
.isEqualTo("WRITING");
|
||||
assertThat(
|
||||
jdbc.queryForObject(
|
||||
"SELECT operation_kind FROM repository_write_operation",
|
||||
String.class))
|
||||
.isEqualTo("GIT_PUSH");
|
||||
ok(auth(get("/api/workspace/projects/" + code + "/tree"), owner.token()));
|
||||
mvc.perform(
|
||||
body(
|
||||
auth(
|
||||
post(
|
||||
"/api/workspace/projects/"
|
||||
+ code
|
||||
+ "/commits"),
|
||||
owner.token()),
|
||||
Map.of(
|
||||
"expectedCommitSha",
|
||||
old,
|
||||
"message",
|
||||
"竞争网页提交",
|
||||
"changes",
|
||||
List.of(
|
||||
Map.of(
|
||||
"action",
|
||||
"UPDATE_FILE",
|
||||
"path",
|
||||
"README.md",
|
||||
"content",
|
||||
"保留输入")))))
|
||||
.andExpect(status().isConflict());
|
||||
assertThat(
|
||||
jdbc.queryForObject(
|
||||
"SELECT COUNT(*) FROM repository_write_operation",
|
||||
Integer.class))
|
||||
.isEqualTo(1);
|
||||
} finally {
|
||||
pushRelease.countDown();
|
||||
}
|
||||
assertThat(
|
||||
pushing.get(5, java.util.concurrent.TimeUnit.SECONDS)
|
||||
.getResponse()
|
||||
.getStatus())
|
||||
.isEqualTo(200);
|
||||
assertThat(jdbc.queryForObject("SELECT repo_write_status FROM project", String.class))
|
||||
.isEqualTo("IDLE");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void acceptsGzipAndFlushProbeButRejectsDisabledDeletedAndNonMainUpdates() throws Exception {
|
||||
Account owner = login("git-wire");
|
||||
String code = create(owner);
|
||||
String secret = issue(owner, code, "READ_WRITE").path("token").asText();
|
||||
mvc.perform(
|
||||
basic(post(protocol(code) + "/git-receive-pack"), owner, secret)
|
||||
.contentType("application/x-git-receive-pack-request")
|
||||
.content("0000"))
|
||||
.andExpect(status().isOk());
|
||||
assertThat(
|
||||
jdbc.queryForObject(
|
||||
"SELECT COUNT(*) FROM repository_write_operation", Integer.class))
|
||||
.isZero();
|
||||
String old =
|
||||
ok(auth(get("/api/workspace/projects/" + code + "/tree"), owner.token()))
|
||||
.path("commitSha")
|
||||
.asText();
|
||||
byte[] payload =
|
||||
packets(
|
||||
old + " " + "b".repeat(40) + " refs/heads/main\0 report-status ofs-delta",
|
||||
null);
|
||||
ByteArrayOutputStream compressed = new ByteArrayOutputStream();
|
||||
try (java.util.zip.GZIPOutputStream gzip = new java.util.zip.GZIPOutputStream(compressed)) {
|
||||
gzip.write(payload);
|
||||
}
|
||||
mvc.perform(
|
||||
basic(post(protocol(code) + "/git-receive-pack"), owner, secret)
|
||||
.contentType("application/x-git-receive-pack-request")
|
||||
.header("Content-Encoding", "gzip")
|
||||
.content(compressed.toByteArray()))
|
||||
.andExpect(status().isOk());
|
||||
assertThat(received.get()).isEqualTo(payload);
|
||||
jdbc.update("UPDATE sys_user SET status='DISABLED' WHERE user_code=?", owner.username());
|
||||
assertThat(
|
||||
mvc.perform(
|
||||
basic(
|
||||
get(protocol(code) + "/info/refs")
|
||||
.param("service", "git-upload-pack"),
|
||||
owner,
|
||||
secret))
|
||||
.andReturn()
|
||||
.getResponse()
|
||||
.getStatus())
|
||||
.isIn(401, 403);
|
||||
jdbc.update("UPDATE sys_user SET status='ACTIVE' WHERE user_code=?", owner.username());
|
||||
jdbc.update("UPDATE project SET repo_status='CREATING'");
|
||||
mvc.perform(
|
||||
basic(
|
||||
get(protocol(code) + "/info/refs")
|
||||
.param("service", "git-upload-pack"),
|
||||
owner,
|
||||
secret))
|
||||
.andExpect(status().isConflict());
|
||||
jdbc.update("UPDATE project SET repo_status='READY',deleted=1");
|
||||
mvc.perform(
|
||||
basic(
|
||||
get(protocol(code) + "/info/refs")
|
||||
.param("service", "git-upload-pack"),
|
||||
owner,
|
||||
secret))
|
||||
.andExpect(status().isNotFound());
|
||||
}
|
||||
|
||||
@Test
|
||||
void lfsValidatesDownloadActionsIntegrityExpiryAndMetadataLimits() throws Exception {
|
||||
Account owner = login("git-lfs-boundaries");
|
||||
String code = create(owner), other = create(owner);
|
||||
String rw = issue(owner, code, "READ_WRITE").path("token").asText();
|
||||
String ro = issue(owner, code, "READ_ONLY").path("token").asText();
|
||||
byte[] batch =
|
||||
json.writeValueAsBytes(
|
||||
Map.of(
|
||||
"operation",
|
||||
"download",
|
||||
"objects",
|
||||
List.of(Map.of("oid", OID, "size", OBJECT.length))));
|
||||
MvcResult reply =
|
||||
mvc.perform(
|
||||
basic(post(protocol(code) + "/info/lfs/objects/batch"), owner, ro)
|
||||
.contentType(LfsGateway.MEDIA)
|
||||
.content(batch))
|
||||
.andExpect(status().isOk())
|
||||
.andReturn();
|
||||
JsonNode action =
|
||||
json.readTree(reply.getResponse().getContentAsByteArray())
|
||||
.path("objects")
|
||||
.get(0)
|
||||
.path("actions")
|
||||
.path("download");
|
||||
String path = URI.create(action.path("href").asText()).getPath(),
|
||||
authorization = action.path("header").path("Authorization").asText();
|
||||
mvc.perform(get(path).header("Authorization", authorization))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().bytes(OBJECT));
|
||||
mvc.perform(get(path.replace(code, other)).header("Authorization", authorization))
|
||||
.andExpect(status().isUnauthorized());
|
||||
mvc.perform(put(path).header("Authorization", authorization).content(OBJECT))
|
||||
.andExpect(status().isUnauthorized());
|
||||
String key = "git:lfs-action:" + path.substring(path.lastIndexOf('/') + 1);
|
||||
com.fasterxml.jackson.databind.node.ObjectNode stored =
|
||||
(com.fasterxml.jackson.databind.node.ObjectNode) json.readTree(state.get(key));
|
||||
stored.put("expiresAt", java.time.Instant.now().minusSeconds(1).toString());
|
||||
state.remove(key);
|
||||
state.putIfAbsent(key, json.writeValueAsString(stored), java.time.Duration.ofSeconds(30));
|
||||
mvc.perform(get(path).header("Authorization", authorization))
|
||||
.andExpect(status().isUnauthorized());
|
||||
reply =
|
||||
mvc.perform(
|
||||
basic(post(protocol(code) + "/info/lfs/objects/batch"), owner, rw)
|
||||
.contentType(LfsGateway.MEDIA)
|
||||
.content(
|
||||
json.writeValueAsBytes(
|
||||
Map.of(
|
||||
"operation",
|
||||
"upload",
|
||||
"objects",
|
||||
List.of(
|
||||
Map.of(
|
||||
"oid",
|
||||
OID,
|
||||
"size",
|
||||
OBJECT.length))))))
|
||||
.andExpect(status().isOk())
|
||||
.andReturn();
|
||||
action =
|
||||
json.readTree(reply.getResponse().getContentAsByteArray())
|
||||
.path("objects")
|
||||
.get(0)
|
||||
.path("actions")
|
||||
.path("upload");
|
||||
path = URI.create(action.path("href").asText()).getPath();
|
||||
authorization = action.path("header").path("Authorization").asText();
|
||||
mvc.perform(
|
||||
put(path)
|
||||
.header("Authorization", authorization)
|
||||
.content(new byte[OBJECT.length - 1]))
|
||||
.andExpect(status().isBadRequest());
|
||||
mvc.perform(
|
||||
put(path)
|
||||
.header("Authorization", authorization)
|
||||
.content(new byte[OBJECT.length]))
|
||||
.andExpect(status().isBadRequest());
|
||||
mvc.perform(
|
||||
basic(post(protocol(code) + "/info/lfs/objects/batch"), owner, rw)
|
||||
.contentType(LfsGateway.MEDIA)
|
||||
.content("{"))
|
||||
.andExpect(status().isBadRequest());
|
||||
mvc.perform(
|
||||
basic(post(protocol(code) + "/info/lfs/objects/batch"), owner, rw)
|
||||
.contentType(LfsGateway.MEDIA)
|
||||
.content(
|
||||
json.writeValueAsBytes(
|
||||
Map.of(
|
||||
"operation",
|
||||
"upload",
|
||||
"objects",
|
||||
List.of(
|
||||
Map.of(
|
||||
"oid",
|
||||
OID,
|
||||
"size",
|
||||
1073741825L))))))
|
||||
.andExpect(status().isPayloadTooLarge());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,515 @@
|
||||
package com.linkerbot.vibemake.api;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.linkerbot.vibemake.service.config.*;
|
||||
import com.linkerbot.vibemake.service.config.RedisStateProperties;
|
||||
import com.linkerbot.vibemake.service.gitea.GiteaClient;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.server.LocalServerPort;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.data.redis.core.*;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
|
||||
import java.net.*;
|
||||
import java.net.http.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.sql.*;
|
||||
import java.time.Duration;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
/** 只使用本测试创建的用户、仓库与临时 H2;不访问业务项目。 */
|
||||
@SpringBootTest(
|
||||
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
|
||||
properties = {
|
||||
"spring.datasource.url=jdbc:h2:mem:gitlive;MODE=MySQL;DB_CLOSE_DELAY=-1",
|
||||
"spring.datasource.username=sa",
|
||||
"spring.datasource.password=",
|
||||
"spring.datasource.driver-class-name=org.h2.Driver",
|
||||
"vibemake.ai.enabled=false",
|
||||
"vibemake.oss.enabled=false",
|
||||
"vibemake.limits.enabled=false",
|
||||
"vibemake.repository-write.worker-enabled=false",
|
||||
"vibemake.git.enabled=true",
|
||||
"vibemake.git.public-base-url=https://git.example/git",
|
||||
"vibemake.git.upstream-username=fixture",
|
||||
"vibemake.git.upstream-token=fixture",
|
||||
"vibemake.redis.real-test=true",
|
||||
"vibemake.redis.key-prefix=vibemake:test:git-live-${random.uuid}",
|
||||
"logging.level.root=WARN"
|
||||
})
|
||||
@ActiveProfiles("test")
|
||||
@Import(GiteaLiveTest.Config.class)
|
||||
@EnabledIfEnvironmentVariable(named = "VIBEMAKE_LIVE_GITEA", matches = "true")
|
||||
class GitGatewayLiveTest {
|
||||
@LocalServerPort int port;
|
||||
@Autowired ObjectMapper json;
|
||||
@Autowired JdbcTemplate jdbc;
|
||||
@Autowired VibemakeProperties config;
|
||||
@Autowired GitGatewayProperties gitConfig;
|
||||
@Autowired GiteaClient gitea;
|
||||
@Autowired StringRedisTemplate redis;
|
||||
@Autowired RedisStateProperties namespace;
|
||||
static String mysqlDatabase;
|
||||
static String mysqlUrl;
|
||||
|
||||
@DynamicPropertySource
|
||||
static void isolatedMysql(DynamicPropertyRegistry registry) throws Exception {
|
||||
if (!"true".equals(System.getenv("GIT_GATEWAY_MYSQL"))) return;
|
||||
String original = System.getenv("DB_URL");
|
||||
if (original == null || !original.startsWith("jdbc:mysql:"))
|
||||
throw new IllegalStateException("Git 联调需要 DB_URL");
|
||||
String requestedDatabase =
|
||||
"vibemake_git_verify_" + UUID.randomUUID().toString().replace("-", "");
|
||||
try (Connection connection =
|
||||
DriverManager.getConnection(
|
||||
original,
|
||||
System.getenv("DB_USERNAME"),
|
||||
System.getenv("DB_PASSWORD"));
|
||||
Statement statement = connection.createStatement()) {
|
||||
statement.execute(
|
||||
"CREATE DATABASE "
|
||||
+ requestedDatabase
|
||||
+ " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
|
||||
mysqlDatabase = requestedDatabase;
|
||||
}
|
||||
String plain = original.split("\\?", 2)[0];
|
||||
mysqlUrl =
|
||||
plain.substring(0, plain.lastIndexOf('/') + 1)
|
||||
+ mysqlDatabase
|
||||
+ (original.contains("?") ? "?" + original.split("\\?", 2)[1] : "");
|
||||
registry.add("spring.datasource.url", () -> mysqlUrl);
|
||||
registry.add("spring.datasource.username", () -> System.getenv("DB_USERNAME"));
|
||||
registry.add("spring.datasource.password", () -> System.getenv("DB_PASSWORD"));
|
||||
registry.add("spring.datasource.driver-class-name", () -> "com.mysql.cj.jdbc.Driver");
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
static void dropOnlyTemporaryDatabase() throws Exception {
|
||||
if (mysqlDatabase == null) return;
|
||||
if (!mysqlDatabase.matches("vibemake_git_verify_[a-f0-9]{32}"))
|
||||
throw new IllegalStateException("拒绝清理非测试库");
|
||||
try (Connection connection =
|
||||
DriverManager.getConnection(
|
||||
System.getenv("DB_URL"),
|
||||
System.getenv("DB_USERNAME"),
|
||||
System.getenv("DB_PASSWORD"));
|
||||
Statement statement = connection.createStatement()) {
|
||||
statement.execute("DROP DATABASE " + mysqlDatabase);
|
||||
}
|
||||
}
|
||||
|
||||
@TempDir Path directory;
|
||||
final HttpClient http =
|
||||
HttpClient.newBuilder()
|
||||
.followRedirects(HttpClient.Redirect.NEVER)
|
||||
.connectTimeout(Duration.ofSeconds(10))
|
||||
.build();
|
||||
String bot;
|
||||
String siteToken;
|
||||
String username;
|
||||
String project;
|
||||
String gitToken;
|
||||
String gitAuth;
|
||||
Path askpass;
|
||||
boolean botCreated;
|
||||
|
||||
@BeforeEach
|
||||
void setup() throws Exception {
|
||||
if (mysqlUrl == null) SchemaSupport.reset(jdbc);
|
||||
else
|
||||
for (String doc : List.of("database.md", "admin-database.md")) {
|
||||
Matcher tables =
|
||||
Pattern.compile("CREATE TABLE [\\s\\S]*?;")
|
||||
.matcher(Files.readString(Path.of("../docs/" + doc)));
|
||||
while (tables.find()) jdbc.execute(tables.group());
|
||||
}
|
||||
String suffix = UUID.randomUUID().toString().replace("-", "").substring(0, 16);
|
||||
bot = "vbg_test_" + suffix;
|
||||
String password = "Vbg!" + UUID.randomUUID() + "a9";
|
||||
admin(
|
||||
"POST",
|
||||
"/admin/users",
|
||||
Map.of(
|
||||
"username",
|
||||
bot,
|
||||
"email",
|
||||
bot + "@example.invalid",
|
||||
"password",
|
||||
password,
|
||||
"must_change_password",
|
||||
false,
|
||||
"send_notify",
|
||||
false),
|
||||
201);
|
||||
botCreated = true;
|
||||
HttpRequest mint =
|
||||
HttpRequest.newBuilder(
|
||||
URI.create(
|
||||
config.getGitea().getBaseUrl()
|
||||
+ "/api/v1/users/"
|
||||
+ bot
|
||||
+ "/tokens"))
|
||||
.header(
|
||||
"Authorization",
|
||||
"Basic "
|
||||
+ Base64.getEncoder()
|
||||
.encodeToString(
|
||||
(bot + ":" + password)
|
||||
.getBytes(StandardCharsets.UTF_8)))
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(
|
||||
HttpRequest.BodyPublishers.ofByteArray(
|
||||
json.writeValueAsBytes(
|
||||
Map.of(
|
||||
"name",
|
||||
"gateway-live",
|
||||
"scopes",
|
||||
List.of("read:user", "write:repository")))))
|
||||
.build();
|
||||
HttpResponse<byte[]> minted = http.send(mint, HttpResponse.BodyHandlers.ofByteArray());
|
||||
assertThat(minted.statusCode()).as("创建测试技术账号PAT").isEqualTo(201);
|
||||
gitConfig.setUpstreamUsername(bot);
|
||||
gitConfig.setUpstreamToken(json.readTree(minted.body()).path("sha1").asText());
|
||||
// 仅回环联调使用明文 HTTP;生产配置启动校验仍强制公网 HTTPS。
|
||||
gitConfig.setPublicBaseUrl("http://127.0.0.1:" + port + "/git");
|
||||
JsonNode captcha = api("GET", "/api/auth/captcha", null, null);
|
||||
JsonNode login =
|
||||
api(
|
||||
"POST",
|
||||
"/api/auth/login",
|
||||
Map.of(
|
||||
"email",
|
||||
"gateway-live-" + suffix + "@example.invalid",
|
||||
"captchaCode",
|
||||
captcha.path("captchaCode").asText(),
|
||||
"captchaAnswer",
|
||||
"ABCDE"),
|
||||
null);
|
||||
siteToken = login.path("token").asText();
|
||||
username = login.path("user").path("userCode").asText();
|
||||
project =
|
||||
api(
|
||||
"POST",
|
||||
"/api/workspace/projects",
|
||||
Map.of("name", "网关真实联调", "projectType", "GENERAL"),
|
||||
siteToken)
|
||||
.path("projectCode")
|
||||
.asText();
|
||||
gitToken =
|
||||
api(
|
||||
"POST",
|
||||
"/api/workspace/projects/" + project + "/git/tokens",
|
||||
Map.of("name", "测试本机", "scope", "READ_WRITE"),
|
||||
siteToken)
|
||||
.path("token")
|
||||
.asText();
|
||||
gitAuth =
|
||||
"Basic "
|
||||
+ Base64.getEncoder()
|
||||
.encodeToString(
|
||||
(username + ":" + gitToken)
|
||||
.getBytes(StandardCharsets.UTF_8));
|
||||
askpass = directory.resolve("askpass.sh");
|
||||
Files.writeString(
|
||||
askpass,
|
||||
"#!/bin/sh\n"
|
||||
+ "case \"$1\" in *Username*) printf '%s' \"$VBG_USERNAME\";; *) printf '%s'"
|
||||
+ " \"$VBG_SECRET\";; esac\n");
|
||||
Files.setPosixFilePermissions(
|
||||
askpass, java.nio.file.attribute.PosixFilePermissions.fromString("rwx------"));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanup() throws Exception {
|
||||
for (String name : jdbc.queryForList("SELECT gitea_repo_name FROM project", String.class)) {
|
||||
if (!name.matches("p_([0-9a-f]{32}|[0-9abcdefghjkmnpqrstvwxyz]{12})"))
|
||||
throw new IllegalStateException("拒绝清理非测试仓库");
|
||||
admin("DELETE", "/repos/" + config.getGitea().getOwner() + "/" + name, null, 204);
|
||||
}
|
||||
if (botCreated) admin("DELETE", "/admin/users/" + bot, null, 204);
|
||||
assertThat(namespace.getKeyPrefix()).startsWith("vibemake:test:git-live-");
|
||||
try (Cursor<String> cursor =
|
||||
redis.scan(
|
||||
ScanOptions.scanOptions()
|
||||
.match(namespace.prefix() + "*")
|
||||
.count(100)
|
||||
.build())) {
|
||||
while (cursor.hasNext()) redis.delete(cursor.next());
|
||||
}
|
||||
}
|
||||
|
||||
JsonNode admin(String method, String path, Object body, int expected) throws Exception {
|
||||
HttpRequest.Builder builder =
|
||||
HttpRequest.newBuilder(
|
||||
URI.create(config.getGitea().getBaseUrl() + "/api/v1" + path))
|
||||
.timeout(Duration.ofSeconds(40))
|
||||
.header("Authorization", "token " + config.getGitea().getToken())
|
||||
.header("Content-Type", "application/json");
|
||||
HttpResponse<byte[]> result =
|
||||
http.send(
|
||||
builder.method(
|
||||
method,
|
||||
body == null
|
||||
? HttpRequest.BodyPublishers.noBody()
|
||||
: HttpRequest.BodyPublishers.ofByteArray(
|
||||
json.writeValueAsBytes(body)))
|
||||
.build(),
|
||||
HttpResponse.BodyHandlers.ofByteArray());
|
||||
assertThat(result.statusCode()).as("Gitea 测试管理请求 %s %s", method, path).isEqualTo(expected);
|
||||
return result.body().length == 0 ? json.nullNode() : json.readTree(result.body());
|
||||
}
|
||||
|
||||
JsonNode api(String method, String path, Object body, String token) throws Exception {
|
||||
HttpRequest.Builder builder =
|
||||
HttpRequest.newBuilder(URI.create("http://127.0.0.1:" + port + path))
|
||||
.timeout(Duration.ofSeconds(90))
|
||||
.header("Content-Type", "application/json");
|
||||
if (token != null) builder.header("Authorization", "Bearer " + token);
|
||||
HttpResponse<byte[]> result =
|
||||
http.send(
|
||||
builder.method(
|
||||
method,
|
||||
body == null
|
||||
? HttpRequest.BodyPublishers.noBody()
|
||||
: HttpRequest.BodyPublishers.ofByteArray(
|
||||
json.writeValueAsBytes(body)))
|
||||
.build(),
|
||||
HttpResponse.BodyHandlers.ofByteArray());
|
||||
JsonNode parsed = json.readTree(result.body());
|
||||
assertThat(result.statusCode())
|
||||
.as("网站请求 %s %s code=%s", method, path, parsed.path("code").asText())
|
||||
.isEqualTo(200);
|
||||
return parsed.path("data");
|
||||
}
|
||||
|
||||
record Command(int status, String output) {}
|
||||
|
||||
Command command(Path cwd, String... args) throws Exception {
|
||||
List<String> command =
|
||||
new ArrayList<>(
|
||||
List.of(
|
||||
"git",
|
||||
"-c",
|
||||
"credential.helper=",
|
||||
"-c",
|
||||
"credential.useHttpPath=true"));
|
||||
command.addAll(List.of(args));
|
||||
ProcessBuilder builder =
|
||||
new ProcessBuilder(command).directory(cwd.toFile()).redirectErrorStream(true);
|
||||
// 凭据只在环境中传递;不出现在命令行、URL、断言正文或日志中。
|
||||
builder.environment().put("GIT_ASKPASS", askpass.toString());
|
||||
builder.environment().put("VBG_USERNAME", username);
|
||||
builder.environment().put("VBG_SECRET", gitToken);
|
||||
builder.environment().put("GIT_TERMINAL_PROMPT", "0");
|
||||
builder.environment().put("GIT_AUTHOR_NAME", "Gateway fixture");
|
||||
builder.environment().put("GIT_AUTHOR_EMAIL", "fixture@example.invalid");
|
||||
builder.environment().put("GIT_COMMITTER_NAME", "Gateway fixture");
|
||||
builder.environment().put("GIT_COMMITTER_EMAIL", "fixture@example.invalid");
|
||||
Process process = builder.start();
|
||||
CompletableFuture<byte[]> output =
|
||||
CompletableFuture.supplyAsync(
|
||||
() -> {
|
||||
try {
|
||||
return process.getInputStream().readAllBytes();
|
||||
} catch (Exception error) {
|
||||
throw new IllegalStateException(error);
|
||||
}
|
||||
});
|
||||
if (!process.waitFor(180, TimeUnit.SECONDS)) {
|
||||
process.destroyForcibly();
|
||||
throw new IllegalStateException("Git 联调超时");
|
||||
}
|
||||
return new Command(
|
||||
process.exitValue(),
|
||||
new String(output.get(5, TimeUnit.SECONDS), StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
String git(Path cwd, String... args) throws Exception {
|
||||
Command result = command(cwd, args);
|
||||
assertThat(result.status())
|
||||
.as(
|
||||
"Git %s: %s",
|
||||
args[0],
|
||||
result.output()
|
||||
.replace(gitToken, "[redacted]")
|
||||
.replace(gitConfig.getUpstreamToken(), "[redacted]")
|
||||
.replaceAll(
|
||||
"(?i)(Basic|Bearer) [A-Za-z0-9_+./=-]+", "$1 [redacted]"))
|
||||
.isZero();
|
||||
return result.output().trim();
|
||||
}
|
||||
|
||||
@Test
|
||||
void nativeGitAndLargeLfsRoundTripWithRefProtectionAndExplicitPublication() throws Exception {
|
||||
String url = "http://127.0.0.1:" + port + "/git/projects/" + project + ".git";
|
||||
String tree = "/api/workspace/projects/" + project + "/tree";
|
||||
String first = api("GET", tree, null, siteToken).path("commitSha").asText();
|
||||
api(
|
||||
"PUT",
|
||||
"/api/workspace/projects/" + project + "/publication",
|
||||
Map.of("commitSha", first),
|
||||
siteToken);
|
||||
git(
|
||||
directory,
|
||||
"-c",
|
||||
"protocol.version=0",
|
||||
"clone",
|
||||
"--single-branch",
|
||||
"--branch",
|
||||
"main",
|
||||
url,
|
||||
"one");
|
||||
Path one = directory.resolve("one");
|
||||
git(one, "config", "credential.useHttpPath", "true");
|
||||
git(one, "lfs", "install", "--local");
|
||||
git(one, "lfs", "track", "*.bin");
|
||||
byte[] bytes = new byte[51 * 1024 * 1024 + 17];
|
||||
new Random(71).nextBytes(bytes);
|
||||
Files.write(one.resolve("large.bin"), bytes);
|
||||
Files.createDirectories(one.resolve("子目录"));
|
||||
Files.writeString(one.resolve("子目录/说明.md"), "本地提交\n");
|
||||
git(one, "add", ".");
|
||||
git(one, "commit", "-m", "本地 Git 和 LFS 更新");
|
||||
String second = git(one, "rev-parse", "HEAD");
|
||||
git(one, "push", "origin", "main");
|
||||
assertThat(api("GET", tree, null, siteToken).path("commitSha").asText()).isEqualTo(second);
|
||||
assertThat(
|
||||
api("GET", "/api/community/projects/" + project + "/tree", null, null)
|
||||
.path("commitSha")
|
||||
.asText())
|
||||
.isEqualTo(first);
|
||||
assertThat(
|
||||
api(
|
||||
"GET",
|
||||
"/api/workspace/projects/" + project + "/commits",
|
||||
null,
|
||||
siteToken)
|
||||
.path("items")
|
||||
.get(0)
|
||||
.path("message")
|
||||
.asText())
|
||||
.contains("本地 Git 和 LFS 更新");
|
||||
git(directory, "-c", "protocol.version=2", "clone", "--no-checkout", url, "two");
|
||||
Path two = directory.resolve("two");
|
||||
git(two, "lfs", "install", "--local");
|
||||
git(two, "checkout", "main");
|
||||
assertThat(Arrays.equals(Files.readAllBytes(two.resolve("large.bin")), bytes))
|
||||
.as("LFS 实体逐字节一致")
|
||||
.isTrue();
|
||||
assertThat(Files.readString(two.resolve("子目录/说明.md"))).isEqualTo("本地提交\n");
|
||||
assertThat(command(one, "push", "origin", "HEAD:refs/heads/other").status()).isNotZero();
|
||||
assertThat(command(one, "push", "origin", "HEAD:refs/tags/test").status()).isNotZero();
|
||||
assertThat(command(one, "push", "origin", ":main").status()).isNotZero();
|
||||
git(one, "reset", "--hard", first);
|
||||
assertThat(command(one, "push", "--force", "origin", "main").status()).isNotZero();
|
||||
assertThat(api("GET", tree, null, siteToken).path("commitSha").asText()).isEqualTo(second);
|
||||
api(
|
||||
"PUT",
|
||||
"/api/workspace/projects/" + project + "/publication",
|
||||
Map.of("commitSha", second),
|
||||
siteToken);
|
||||
assertThat(
|
||||
api("GET", "/api/community/projects/" + project + "/tree", null, null)
|
||||
.path("commitSha")
|
||||
.asText())
|
||||
.isEqualTo(second);
|
||||
Files.writeString(two.resolve("pull.txt"), "下一次更新");
|
||||
git(two, "add", "pull.txt");
|
||||
git(two, "commit", "-m", "继续更新");
|
||||
git(two, "push", "origin", "main");
|
||||
git(one, "pull", "--ff-only");
|
||||
assertThat(Files.readString(one.resolve("pull.txt"))).isEqualTo("下一次更新");
|
||||
// 非 LFS 的大 pack + 极小 postBuffer 强制走 HTTP 分块及认证 probe。
|
||||
byte[] packData = new byte[2 * 1024 * 1024 + 7];
|
||||
new Random(19).nextBytes(packData);
|
||||
Files.write(one.resolve("native.dat"), packData);
|
||||
git(one, "add", "native.dat");
|
||||
git(one, "commit", "-m", "大 pack 更新");
|
||||
git(one, "-c", "http.postBuffer=1024", "push", "origin", "main");
|
||||
git(two, "pull", "--ff-only");
|
||||
assertThat(Arrays.equals(Files.readAllBytes(two.resolve("native.dat")), packData)).isTrue();
|
||||
git(two, "checkout", "-b", "local-only");
|
||||
Files.writeString(two.resolve("left.txt"), "left");
|
||||
git(two, "add", "left.txt");
|
||||
git(two, "commit", "-m", "本地分支提交");
|
||||
git(two, "checkout", "main");
|
||||
Files.writeString(two.resolve("right.txt"), "right");
|
||||
git(two, "add", "right.txt");
|
||||
git(two, "commit", "-m", "main 提交");
|
||||
git(two, "merge", "--no-ff", "local-only", "-m", "合并本地提交");
|
||||
git(two, "push", "origin", "main");
|
||||
Path signing = directory.resolve("signing-key");
|
||||
Process keygen =
|
||||
new ProcessBuilder(
|
||||
"ssh-keygen",
|
||||
"-q",
|
||||
"-t",
|
||||
"ed25519",
|
||||
"-N",
|
||||
"",
|
||||
"-f",
|
||||
signing.toString())
|
||||
.redirectErrorStream(true)
|
||||
.start();
|
||||
assertThat(keygen.waitFor(10, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(keygen.exitValue()).isZero();
|
||||
git(
|
||||
two,
|
||||
"-c",
|
||||
"gpg.format=ssh",
|
||||
"-c",
|
||||
"user.signingkey=" + signing,
|
||||
"commit",
|
||||
"--allow-empty",
|
||||
"-S",
|
||||
"-m",
|
||||
"签名提交");
|
||||
String signed = git(two, "rev-parse", "HEAD");
|
||||
git(two, "push", "origin", "main");
|
||||
assertThat(api("GET", tree, null, siteToken).path("commitSha").asText()).isEqualTo(signed);
|
||||
assertThat(git(two, "cat-file", "-p", "HEAD"))
|
||||
.contains("gpgsig -----BEGIN SSH SIGNATURE-----");
|
||||
String originalToken = gitToken;
|
||||
String other =
|
||||
api(
|
||||
"POST",
|
||||
"/api/workspace/projects",
|
||||
Map.of("name", "第二项目", "projectType", "GENERAL"),
|
||||
siteToken)
|
||||
.path("projectCode")
|
||||
.asText();
|
||||
gitToken =
|
||||
api(
|
||||
"POST",
|
||||
"/api/workspace/projects/" + other + "/git/tokens",
|
||||
Map.of("name", "第二项目凭据", "scope", "READ_WRITE"),
|
||||
siteToken)
|
||||
.path("token")
|
||||
.asText();
|
||||
String otherUrl = "http://127.0.0.1:" + port + "/git/projects/" + other + ".git";
|
||||
git(directory, "clone", otherUrl, "other");
|
||||
Path otherRepo = directory.resolve("other");
|
||||
git(otherRepo, "lfs", "install", "--local");
|
||||
git(otherRepo, "lfs", "track", "*.bin");
|
||||
Files.write(otherRepo.resolve("other.bin"), Arrays.copyOf(bytes, 1024));
|
||||
git(otherRepo, "add", ".");
|
||||
git(otherRepo, "commit", "-m", "第二项目 LFS");
|
||||
git(otherRepo, "push", "origin", "main");
|
||||
assertThat(command(one, "fetch", "origin").status()).isNotZero();
|
||||
gitToken = originalToken;
|
||||
git(one, "fetch", "origin");
|
||||
}
|
||||
}
|
||||
@@ -26,11 +26,12 @@ final class SchemaSupport {
|
||||
"JSON NOT NULL DEFAULT (JSON_ARRAY())",
|
||||
"CLOB NOT NULL DEFAULT '[]'")
|
||||
.replace(" CHARACTER SET utf8mb4 COLLATE utf8mb4_bin", "")
|
||||
.replace(" CHARACTER SET ascii COLLATE ascii_bin", "")
|
||||
.replaceAll(" COMMENT '[^']*'", "")
|
||||
.replaceAll("\\) ENGINE=InnoDB[^;]*;", ");");
|
||||
jdbc.execute(ddl);
|
||||
tables++;
|
||||
}
|
||||
if (tables != 6) throw new IllegalStateException("必须包含四张基础表、迁移任务表和仓库写入操作表");
|
||||
if (tables != 7) throw new IllegalStateException("必须包含七张业务与审计表");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1263,7 +1263,7 @@ class WorkflowTest {
|
||||
"PROJECT",
|
||||
"PROJECT_COMMENT",
|
||||
"PROJECT_LIKE",
|
||||
"PROJECT_IMPORT_TASK", "REPOSITORY_WRITE_OPERATION");
|
||||
"PROJECT_IMPORT_TASK", "REPOSITORY_WRITE_OPERATION", "PROJECT_GIT_TOKEN");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user