refactor git provider and site structures
This commit is contained in:
@@ -23,12 +23,21 @@ public class GitConfig {
|
||||
|
||||
@PostConstruct
|
||||
public void init() throws IOException {
|
||||
if (reposPath == null) return;
|
||||
Path path = Paths.get(reposPath);
|
||||
if (!Files.exists(path)) {
|
||||
Files.createDirectories(path);
|
||||
}
|
||||
}
|
||||
|
||||
public void setAuthorName(String authorName) {
|
||||
this.authorName = authorName;
|
||||
}
|
||||
|
||||
public void setAuthorEmail(String authorEmail) {
|
||||
this.authorEmail = authorEmail;
|
||||
}
|
||||
|
||||
public Path getReposPath() {
|
||||
return Paths.get(reposPath);
|
||||
}
|
||||
|
||||
@@ -34,17 +34,6 @@ public class SiteStructure {
|
||||
private String seoTitle;
|
||||
private String seoDescription;
|
||||
private int orderIndex;
|
||||
private List<ComponentStructure> components;
|
||||
}
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public static class ComponentStructure {
|
||||
private String id;
|
||||
private String type;
|
||||
private int orderIndex;
|
||||
private Map<String, Object> config;
|
||||
private Map<String, Object> styles;
|
||||
private String rawHtml;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.krrishg.service;
|
||||
|
||||
import com.krrishg.model.GitRemote;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
class GitHubPagesProvider implements PagesProvider {
|
||||
|
||||
@Override
|
||||
public String branchName() {
|
||||
return "gh-pages";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path contentRoot(Path tempDir) {
|
||||
return tempDir;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeProviderFiles(Path tempDir) throws IOException {
|
||||
Files.writeString(tempDir.resolve(".nojekyll"), "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String buildPagesUrl(GitRemote remote) {
|
||||
String url = GitService.normalizeHttpsUrl(remote.getRemoteUrl());
|
||||
String path = url.substring(url.indexOf("github.com") + "github.com".length() + 1);
|
||||
if (path.endsWith(".git")) path = path.substring(0, path.length() - 4);
|
||||
if (path.endsWith("/")) path = path.substring(0, path.length() - 1);
|
||||
String[] parts = path.split("/", 2);
|
||||
if (parts.length == 2) {
|
||||
return "https://" + parts[0] + ".github.io/" + parts[1] + "/";
|
||||
}
|
||||
return url;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.krrishg.service;
|
||||
|
||||
import com.krrishg.model.GitRemote;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
class GitLabPagesProvider implements PagesProvider {
|
||||
|
||||
@Override
|
||||
public String branchName() {
|
||||
return "gl-pages";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path contentRoot(Path tempDir) {
|
||||
return tempDir.resolve("public");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeProviderFiles(Path tempDir) throws IOException {
|
||||
Files.writeString(tempDir.resolve(".gitlab-ci.yml"),
|
||||
"pages:\n" +
|
||||
" stage: deploy\n" +
|
||||
" script:\n" +
|
||||
" - echo \"Deploying GitLab Pages\"\n" +
|
||||
" artifacts:\n" +
|
||||
" paths:\n" +
|
||||
" - public\n" +
|
||||
" rules:\n" +
|
||||
" - if: '$CI_COMMIT_BRANCH == \"gl-pages\"'\n");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String buildPagesUrl(GitRemote remote) {
|
||||
String url = GitService.normalizeHttpsUrl(remote.getRemoteUrl());
|
||||
String path = url.substring(url.indexOf("gitlab.com") + "gitlab.com".length() + 1);
|
||||
if (path.endsWith(".git")) path = path.substring(0, path.length() - 4);
|
||||
if (path.endsWith("/")) path = path.substring(0, path.length() - 1);
|
||||
String[] parts = path.split("/", 2);
|
||||
if (parts.length == 2) {
|
||||
return "https://" + parts[0] + ".gitlab.io/" + parts[1] + "/";
|
||||
}
|
||||
return url;
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
@@ -194,7 +195,7 @@ public class GitService {
|
||||
|
||||
public record CommitInfo(String hash, String message, String timestamp, String author) {}
|
||||
|
||||
private String normalizeHttpsUrl(String url) {
|
||||
static String normalizeHttpsUrl(String url) {
|
||||
if (url.startsWith("https://")) {
|
||||
return url;
|
||||
}
|
||||
@@ -218,38 +219,15 @@ public class GitService {
|
||||
String authUrl = httpsUrl.replace("https://",
|
||||
"https://git:" + remote.getEncryptedToken() + "@");
|
||||
|
||||
boolean isGitLab = remote.getRemoteUrl().contains("gitlab.com");
|
||||
String branch = isGitLab ? "gl-pages" : "gh-pages";
|
||||
|
||||
PagesProvider provider = PagesProvider.forRemote(remote);
|
||||
Path tempDir = Files.createTempDirectory("indie-pages-" + siteId);
|
||||
try {
|
||||
Path contentRoot = isGitLab ? tempDir.resolve("public") : tempDir;
|
||||
Path contentRoot = provider.contentRoot(tempDir);
|
||||
Files.createDirectories(contentRoot);
|
||||
pushPagesBranch(tempDir, authUrl, provider.branchName(), contentRoot, provider, files, "Deploy site");
|
||||
|
||||
for (Map.Entry<String, byte[]> entry : files.entrySet()) {
|
||||
Path filePath = contentRoot.resolve(entry.getKey());
|
||||
Files.createDirectories(filePath.getParent());
|
||||
Files.write(filePath, entry.getValue());
|
||||
}
|
||||
|
||||
if (isGitLab) {
|
||||
Files.writeString(tempDir.resolve(".gitlab-ci.yml"),
|
||||
"pages:\n" +
|
||||
" stage: deploy\n" +
|
||||
" script:\n" +
|
||||
" - echo \"Deploying GitLab Pages\"\n" +
|
||||
" artifacts:\n" +
|
||||
" paths:\n" +
|
||||
" - public\n" +
|
||||
" rules:\n" +
|
||||
" - if: '$CI_COMMIT_BRANCH == \"gl-pages\"'\n");
|
||||
} else {
|
||||
Files.writeString(tempDir.resolve(".nojekyll"), "");
|
||||
}
|
||||
pushOrphanBranch(tempDir, authUrl, branch);
|
||||
|
||||
log.info("Pushed site {} to {} branch {}", siteId, remote.getRemoteUrl(), branch);
|
||||
return buildPagesUrl(remote);
|
||||
log.info("Pushed site {} to {} branch {}", siteId, remote.getRemoteUrl(), provider.branchName());
|
||||
return provider.buildPagesUrl(remote);
|
||||
} finally {
|
||||
deleteDirectory(tempDir);
|
||||
}
|
||||
@@ -258,19 +236,82 @@ public class GitService {
|
||||
}
|
||||
}
|
||||
|
||||
void pushPagesBranch(Path tempDir, String authUrl, String branch,
|
||||
Path contentRoot, PagesProvider provider, Map<String, byte[]> files,
|
||||
String commitMessage) throws Exception {
|
||||
String authorConfig = "-c user.name='" + gitConfig.getAuthorName()
|
||||
+ "' -c user.email='" + gitConfig.getAuthorEmail() + "'";
|
||||
|
||||
runGit(tempDir, "git init", "git remote add origin " + authUrl);
|
||||
|
||||
private void pushOrphanBranch(Path tempDir, String authUrl, String branch) throws Exception {
|
||||
String authorConfig = "-c user.name='" + gitConfig.getAuthorName() + "' -c user.email='" + gitConfig.getAuthorEmail() + "'";
|
||||
runGit(tempDir,
|
||||
"git init",
|
||||
"git " + authorConfig + " commit --allow-empty -m 'Initial commit'",
|
||||
"git push --force " + authUrl + " HEAD:main",
|
||||
"git checkout --orphan " + branch,
|
||||
"git add -A",
|
||||
"git " + authorConfig + " commit -m 'Deploy site'",
|
||||
"git push --force " + authUrl + " HEAD:" + branch
|
||||
);
|
||||
if (branchExistsOnRemote(authUrl, branch)) {
|
||||
runGit(tempDir,
|
||||
"git fetch origin " + branch,
|
||||
"git checkout -b " + branch + " origin/" + branch,
|
||||
"git rm -rf ."
|
||||
);
|
||||
writeSiteFilesTo(contentRoot, files);
|
||||
provider.writeProviderFiles(tempDir);
|
||||
runGit(tempDir,
|
||||
"git add -A",
|
||||
"git " + authorConfig + " commit -m '" + commitMessage + "'",
|
||||
"git fetch origin " + branch,
|
||||
"git merge origin/" + branch + " -X ours",
|
||||
"git push origin " + branch
|
||||
);
|
||||
} else {
|
||||
runGit(tempDir, "git checkout --orphan " + branch);
|
||||
writeSiteFilesTo(contentRoot, files);
|
||||
provider.writeProviderFiles(tempDir);
|
||||
runGit(tempDir,
|
||||
"git add -A",
|
||||
"git " + authorConfig + " commit -m '" + commitMessage + "'",
|
||||
"git push origin " + branch
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private void writeSiteFilesTo(Path contentRoot, Map<String, byte[]> files) throws IOException {
|
||||
for (Map.Entry<String, byte[]> entry : files.entrySet()) {
|
||||
Path filePath = contentRoot.resolve(entry.getKey());
|
||||
Files.createDirectories(filePath.getParent());
|
||||
Files.write(filePath, entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private boolean branchExistsOnRemote(String authUrl, String branch) throws Exception {
|
||||
ProcessBuilder pb = new ProcessBuilder("sh", "-c",
|
||||
"git ls-remote --exit-code " + authUrl + " " + branch);
|
||||
Process process = pb.start();
|
||||
return process.waitFor() == 0;
|
||||
}
|
||||
|
||||
private Map<String, byte[]> createRemovalPage() {
|
||||
String html = """
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Site Removed</title>
|
||||
<style>
|
||||
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
display: flex; justify-content: center; align-items: center;
|
||||
min-height: 100vh; margin: 0; background: #f5f5f5; color: #333;
|
||||
text-align: center; }
|
||||
h1 { font-size: 2rem; margin-bottom: 0.5rem; }
|
||||
p { color: #666; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<h1>This site has been unpublished</h1>
|
||||
<p>The owner has removed this site.</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
""";
|
||||
return Map.of("index.html", html.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private void runGit(Path workDir, String... commands) throws Exception {
|
||||
@@ -287,36 +328,6 @@ public class GitService {
|
||||
}
|
||||
}
|
||||
|
||||
private String buildPagesUrl(GitRemote remote) {
|
||||
String url = normalizeHttpsUrl(remote.getRemoteUrl());
|
||||
String owner = "";
|
||||
String repo = "";
|
||||
|
||||
if (url.contains("github.com")) {
|
||||
String path = url.substring(url.indexOf("github.com") + "github.com".length() + 1);
|
||||
if (path.endsWith(".git")) path = path.substring(0, path.length() - 4);
|
||||
if (path.endsWith("/")) path = path.substring(0, path.length() - 1);
|
||||
String[] parts = path.split("/", 2);
|
||||
if (parts.length == 2) {
|
||||
owner = parts[0];
|
||||
repo = parts[1];
|
||||
}
|
||||
return "https://" + owner + ".github.io/" + repo + "/";
|
||||
} else if (url.contains("gitlab.com")) {
|
||||
String path = url.substring(url.indexOf("gitlab.com") + "gitlab.com".length() + 1);
|
||||
if (path.endsWith(".git")) path = path.substring(0, path.length() - 4);
|
||||
if (path.endsWith("/")) path = path.substring(0, path.length() - 1);
|
||||
String[] parts = path.split("/", 2);
|
||||
if (parts.length == 2) {
|
||||
owner = parts[0];
|
||||
repo = parts[1];
|
||||
}
|
||||
return "https://" + owner + ".gitlab.io/" + repo + "/";
|
||||
}
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
public void removePages(String siteId) {
|
||||
GitRemote remote = gitRemoteRepository.findBySiteId(UUID.fromString(siteId))
|
||||
.orElse(null);
|
||||
@@ -330,20 +341,21 @@ public class GitService {
|
||||
String authUrl = httpsUrl.replace("https://",
|
||||
"https://git:" + remote.getEncryptedToken() + "@");
|
||||
|
||||
boolean isGitLab = remote.getRemoteUrl().contains("gitlab.com");
|
||||
String branch = isGitLab ? "gl-pages" : "gh-pages";
|
||||
PagesProvider provider = PagesProvider.forRemote(remote);
|
||||
String branch = provider.branchName();
|
||||
|
||||
if (!branchExistsOnRemote(authUrl, branch)) {
|
||||
log.info("No {} branch found for site {}, nothing to remove", branch, siteId);
|
||||
return;
|
||||
}
|
||||
|
||||
Path tempDir = Files.createTempDirectory("indie-pages-remove-" + siteId);
|
||||
try {
|
||||
String authorConfig = "-c user.name='" + gitConfig.getAuthorName() + "' -c user.email='" + gitConfig.getAuthorEmail() + "'";
|
||||
runGit(tempDir,
|
||||
"git init",
|
||||
"git " + authorConfig + " commit --allow-empty -m 'Remove site'",
|
||||
"git checkout --orphan " + branch,
|
||||
"git " + authorConfig + " commit --allow-empty -m 'Remove site'",
|
||||
"git push --force " + authUrl + " HEAD:" + branch
|
||||
);
|
||||
log.info("Removed pages for site {} from {} branch {}", siteId, remote.getProvider(), branch);
|
||||
Path contentRoot = provider.contentRoot(tempDir);
|
||||
Files.createDirectories(contentRoot);
|
||||
Map<String, byte[]> removalFiles = createRemovalPage();
|
||||
pushPagesBranch(tempDir, authUrl, branch, contentRoot, provider, removalFiles, "Unpublish site");
|
||||
log.info("Unpublished site {} from {} branch {}", siteId, remote.getProvider(), branch);
|
||||
} finally {
|
||||
deleteDirectory(tempDir);
|
||||
}
|
||||
|
||||
@@ -142,14 +142,9 @@ public class LLMResponseParser {
|
||||
if (pageNode.has("orderIndex")) {
|
||||
builder.orderIndex(pageNode.get("orderIndex").asInt());
|
||||
}
|
||||
|
||||
List<SiteStructure.ComponentStructure> components = new ArrayList<>();
|
||||
if (pageNode.has("components") && pageNode.get("components").isArray()) {
|
||||
for (JsonNode compNode : pageNode.get("components")) {
|
||||
components.add(parseComponent(compNode));
|
||||
}
|
||||
if (pageNode.has("rawHtml")) {
|
||||
builder.rawHtml(pageNode.get("rawHtml").asText());
|
||||
}
|
||||
builder.components(components);
|
||||
|
||||
SiteStructure.PageStructure page = builder.build();
|
||||
if (page.getTitle() == null || page.getSlug() == null) {
|
||||
@@ -158,34 +153,4 @@ public class LLMResponseParser {
|
||||
|
||||
return page;
|
||||
}
|
||||
|
||||
private SiteStructure.ComponentStructure parseComponent(JsonNode compNode) {
|
||||
SiteStructure.ComponentStructure.ComponentStructureBuilder builder = SiteStructure.ComponentStructure.builder();
|
||||
|
||||
if (compNode.has("id")) {
|
||||
builder.id(compNode.get("id").asText());
|
||||
} else {
|
||||
builder.id(UUID.randomUUID().toString());
|
||||
}
|
||||
|
||||
if (compNode.has("type")) {
|
||||
builder.type(compNode.get("type").asText());
|
||||
}
|
||||
if (compNode.has("orderIndex")) {
|
||||
builder.orderIndex(compNode.get("orderIndex").asInt());
|
||||
}
|
||||
if (compNode.has("config")) {
|
||||
builder.config(objectMapper.convertValue(compNode.get("config"), Map.class));
|
||||
}
|
||||
if (compNode.has("styles")) {
|
||||
builder.styles(objectMapper.convertValue(compNode.get("styles"), Map.class));
|
||||
}
|
||||
|
||||
SiteStructure.ComponentStructure component = builder.build();
|
||||
if (component.getType() == null) {
|
||||
throw new IllegalArgumentException("Component must have a type");
|
||||
}
|
||||
|
||||
return component;
|
||||
}
|
||||
}
|
||||
|
||||
24
backend/src/main/java/com/krrishg/service/PagesProvider.java
Normal file
24
backend/src/main/java/com/krrishg/service/PagesProvider.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.krrishg.service;
|
||||
|
||||
import com.krrishg.model.GitRemote;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
|
||||
interface PagesProvider {
|
||||
|
||||
String branchName();
|
||||
|
||||
Path contentRoot(Path tempDir);
|
||||
|
||||
void writeProviderFiles(Path tempDir) throws IOException;
|
||||
|
||||
String buildPagesUrl(GitRemote remote);
|
||||
|
||||
static PagesProvider forRemote(GitRemote remote) {
|
||||
if (remote.getRemoteUrl().contains("gitlab.com")) {
|
||||
return new GitLabPagesProvider();
|
||||
}
|
||||
return new GitHubPagesProvider();
|
||||
}
|
||||
}
|
||||
@@ -7,8 +7,8 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class RenderService {
|
||||
@@ -58,52 +58,16 @@ public class RenderService {
|
||||
}
|
||||
}
|
||||
|
||||
List<Section> sections = buildSections(page.getComponents());
|
||||
Set<Integer> lastIndices = sections.stream()
|
||||
.map(s -> s.indices.get(s.indices.size() - 1))
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
StringBuilder sectionsHtml = new StringBuilder();
|
||||
for (Section section : sections) {
|
||||
String bgStyle = section.bg != null && !section.bg.isEmpty()
|
||||
? " style=\"background:" + section.bg + "\"" : "";
|
||||
sectionsHtml.append("<section").append(bgStyle).append(">\n <div class=\"container\">\n");
|
||||
for (int idx : section.indices) {
|
||||
SiteStructure.ComponentStructure comp = page.getComponents().get(idx);
|
||||
sectionsHtml.append(" ").append(renderComponent(comp, idx)).append("\n");
|
||||
}
|
||||
sectionsHtml.append(" </div>\n</section>\n");
|
||||
}
|
||||
|
||||
StringBuilder compStyles = new StringBuilder();
|
||||
for (int i = 0; i < page.getComponents().size(); i++) {
|
||||
SiteStructure.ComponentStructure c = page.getComponents().get(i);
|
||||
if (c.getStyles() == null || c.getStyles().isEmpty()) continue;
|
||||
List<Map.Entry<String, Object>> entries = c.getStyles().entrySet().stream()
|
||||
.filter(e -> !"backgroundColor".equals(e.getKey()))
|
||||
.collect(Collectors.toList());
|
||||
if (lastIndices.contains(i)) {
|
||||
entries = entries.stream()
|
||||
.filter(e -> !"marginBottom".equals(e.getKey()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
if (entries.isEmpty()) continue;
|
||||
compStyles.append(".comp-").append(i).append(" {\n");
|
||||
for (Map.Entry<String, Object> e : entries) {
|
||||
compStyles.append(" ").append(cssProperty(e.getKey())).append(": ").append(e.getValue()).append(";\n");
|
||||
}
|
||||
compStyles.append("}\n");
|
||||
}
|
||||
|
||||
String title = page.getSeoTitle() != null && !page.getSeoTitle().isEmpty()
|
||||
? page.getSeoTitle() : page.getTitle();
|
||||
String bodyContent = page.getRawHtml() != null ? page.getRawHtml() : "";
|
||||
|
||||
return "<!DOCTYPE html>\n"
|
||||
+ "<html lang=\"en\">\n"
|
||||
+ "<head>\n"
|
||||
+ " <meta charset=\"UTF-8\">\n"
|
||||
+ " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n"
|
||||
+ " <title>" + esc(title) + "</title>\n"
|
||||
+ " <title>" + title + "</title>\n"
|
||||
+ " <link rel=\"stylesheet\" href=\"style.css\">\n"
|
||||
+ " <link href=\"https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Playfair+Display:wght@400;500;600;700&family=Merriweather:wght@300;400;700&family=Lato:wght@300;400;700&family=Open+Sans:wght@300;400;600;700&family=DM+Sans:wght@300;400;500;600;700&family=Plus+Jakarta+Sans:wght@300;400;500;600;700&display=swap\" rel=\"stylesheet\">\n"
|
||||
+ " <style>\n"
|
||||
@@ -118,200 +82,14 @@ public class RenderService {
|
||||
+ " line-height: 1.6;\n"
|
||||
+ " -webkit-font-smoothing: antialiased;\n"
|
||||
+ " }\n"
|
||||
+ " .container { max-width: var(--spacing-containerWidth, 1100px); margin: 0 auto; padding: 0 1.5rem; }\n"
|
||||
+ " section { padding: var(--spacing-sectionPadding, 3rem) 0; }\n"
|
||||
+ compStyles.toString()
|
||||
+ " </style>\n"
|
||||
+ "</head>\n"
|
||||
+ "<body>\n"
|
||||
+ sectionsHtml.toString()
|
||||
+ "</body>\n"
|
||||
+ bodyContent
|
||||
+ "\n</body>\n"
|
||||
+ "</html>";
|
||||
}
|
||||
|
||||
private String renderComponent(SiteStructure.ComponentStructure c, int index) {
|
||||
Map<String, Object> config = c.getConfig() != null ? c.getConfig() : new HashMap<>();
|
||||
String type = (c.getType() != null ? c.getType() : "").toUpperCase();
|
||||
String cls = "comp-" + index;
|
||||
|
||||
return switch (type) {
|
||||
case "HEADING" -> renderHeading(config, cls);
|
||||
case "TEXT" -> renderText(config, cls);
|
||||
case "IMAGE" -> renderImage(config, cls);
|
||||
case "BUTTON" -> renderButton(config, cls);
|
||||
case "DIVIDER" -> renderDivider(config, cls);
|
||||
case "GALLERY" -> renderGallery(config, cls);
|
||||
case "VIDEO" -> renderVideo(config, cls);
|
||||
case "CONTACT_FORM" -> renderContactForm(config, cls);
|
||||
case "MAP" -> renderMap(config, cls);
|
||||
case "CUSTOM_HTML" -> renderCustomHtml(config, cls);
|
||||
default -> "<div class=\"" + cls + "\">[" + esc(type) + "]</div>";
|
||||
};
|
||||
}
|
||||
|
||||
private String renderHeading(Map<String, Object> config, String cls) {
|
||||
String level = str(config.get("level"), "h2");
|
||||
String text = str(config.get("text"), "Heading");
|
||||
String tag = List.of("h1", "h2", "h3", "h4", "h5", "h6").contains(level) ? level : "h2";
|
||||
return "<" + tag + " class=\"" + cls + "\">" + esc(text) + "</" + tag + ">";
|
||||
}
|
||||
|
||||
private String renderText(Map<String, Object> config, String cls) {
|
||||
String content = str(config.get("content"), "");
|
||||
return "<div class=\"" + cls + "\">" + content + "</div>";
|
||||
}
|
||||
|
||||
private String renderImage(Map<String, Object> config, String cls) {
|
||||
String src = str(config.get("src"), "");
|
||||
String alt = str(config.get("alt"), "");
|
||||
String caption = str(config.get("caption"), "");
|
||||
String linkTo = str(config.get("linkTo"), "");
|
||||
String img = "<img src=\"" + esc(src) + "\" alt=\"" + esc(alt) + "\" class=\"" + cls + "\" style=\"max-width:100%;height:auto\">";
|
||||
String imgHtml = linkTo.isEmpty() ? img : "<a href=\"" + esc(linkTo) + "\">" + img + "</a>";
|
||||
if (!caption.isEmpty()) {
|
||||
return "<figure style=\"margin:0\">" + imgHtml
|
||||
+ "<figcaption style=\"text-align:center;font-size:0.875rem;color:#6b7280;margin-top:0.5rem\">"
|
||||
+ esc(caption) + "</figcaption></figure>";
|
||||
}
|
||||
return imgHtml;
|
||||
}
|
||||
|
||||
private String renderButton(Map<String, Object> config, String cls) {
|
||||
String text = str(config.get("text"), "Button");
|
||||
String link = str(config.get("link"), "#");
|
||||
String variant = str(config.get("variant"), "primary");
|
||||
String btnStyle = "display:inline-block;text-decoration:none;font-weight:500;font-size:1rem;padding:0.75rem 2rem;border-radius:8px;transition:all 0.2s;cursor:pointer;border:2px solid transparent";
|
||||
btnStyle += switch (variant) {
|
||||
case "outline" -> ";background:transparent;color:var(--color-primary,#4f46e5);border-color:var(--color-primary,#4f46e5)";
|
||||
case "secondary" -> ";background:var(--color-secondary,#6366f1);color:#ffffff";
|
||||
default -> ";background:var(--color-primary,#4f46e5);color:#ffffff";
|
||||
};
|
||||
return "<a href=\"" + esc(link) + "\" class=\"" + cls + "\" style=\"" + btnStyle + "\">" + esc(text) + "</a>";
|
||||
}
|
||||
|
||||
private String renderDivider(Map<String, Object> config, String cls) {
|
||||
String style = str(config.get("style"), "solid");
|
||||
String color = str(config.get("color"), "var(--color-text, #d1d5db)");
|
||||
return "<hr class=\"" + cls + "\" style=\"border:none;border-top:1px " + style + " " + color + "\">";
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private String renderGallery(Map<String, Object> config, String cls) {
|
||||
List<String> images = (List<String>) config.getOrDefault("images", List.of());
|
||||
int columns = Math.min(Math.max(toInt(config.get("columns"), 3), 1), 6);
|
||||
if (images.isEmpty()) {
|
||||
return "<div class=\"" + cls + "\">Gallery (no images)</div>";
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("<div class=\"").append(cls).append("\" style=\"display:grid;grid-template-columns:repeat(")
|
||||
.append(columns).append(",1fr);gap:1rem\">\n");
|
||||
for (String src : images) {
|
||||
sb.append(" <img src=\"").append(esc(src))
|
||||
.append("\" style=\"width:100%;height:250px;object-fit:cover;border-radius:8px\">\n");
|
||||
}
|
||||
sb.append(" </div>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private String renderVideo(Map<String, Object> config, String cls) {
|
||||
String src = str(config.get("src"), "");
|
||||
boolean controls = config.get("controls") != Boolean.FALSE;
|
||||
boolean autoplay = config.get("autoplay") == Boolean.TRUE;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("<video src=\"").append(esc(src)).append("\" class=\"").append(cls)
|
||||
.append("\"");
|
||||
if (controls) sb.append(" controls");
|
||||
if (autoplay) sb.append(" autoplay");
|
||||
sb.append(" muted style=\"max-width:100%\"></video>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private String renderContactForm(Map<String, Object> config, String cls) {
|
||||
List<String> fields = (List<String>) config.getOrDefault("fields", List.of("name", "email", "message"));
|
||||
String submitText = str(config.get("submitText"), "Send");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("<form class=\"").append(cls).append("\">\n");
|
||||
for (String field : fields) {
|
||||
sb.append(" <div style=\"margin-bottom:1rem\">\n");
|
||||
sb.append(" <label style=\"display:block;font-size:0.875rem;font-weight:600;margin-bottom:0.375rem;color:var(--color-text,#374151)\">")
|
||||
.append(esc(capitalize(field))).append("</label>\n");
|
||||
if ("message".equals(field)) {
|
||||
sb.append(" <textarea rows=\"4\" style=\"width:100%;padding:0.75rem;border:1px solid #d1d5db;border-radius:8px;font-family:inherit;font-size:0.95rem;transition:border-color 0.2s;outline:none\" onfocus=\"this.style.borderColor='var(--color-primary,#4f46e5)'\" onblur=\"this.style.borderColor='#d1d5db'\"></textarea>\n");
|
||||
} else {
|
||||
String inputType = "email".equals(field) ? "email" : "text";
|
||||
sb.append(" <input type=\"").append(inputType)
|
||||
.append("\" style=\"width:100%;padding:0.75rem;border:1px solid #d1d5db;border-radius:8px;font-family:inherit;font-size:0.95rem;transition:border-color 0.2s;outline:none\" onfocus=\"this.style.borderColor='var(--color-primary,#4f46e5)'\" onblur=\"this.style.borderColor='#d1d5db'\">\n");
|
||||
}
|
||||
sb.append(" </div>\n");
|
||||
}
|
||||
sb.append(" <button type=\"submit\" style=\"padding:0.75rem 2rem;background:var(--color-primary,#4f46e5);color:#fff;border:none;border-radius:8px;cursor:pointer;font-size:1rem;font-weight:500;transition:opacity 0.2s\" onmouseover=\"this.style.opacity='0.9'\" onmouseout=\"this.style.opacity='1'\">")
|
||||
.append(esc(submitText)).append("</button>\n");
|
||||
sb.append(" </form>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private String renderMap(Map<String, Object> config, String cls) {
|
||||
String address = str(config.get("address"), "New York, NY");
|
||||
return "<div class=\"" + cls + "\" style=\"display:flex;align-items:center;justify-content:center;background:#f3f4f6\">\n"
|
||||
+ " <span style=\"color:#9ca3af\">Map: " + esc(address) + "</span>\n"
|
||||
+ " </div>";
|
||||
}
|
||||
|
||||
private String renderCustomHtml(Map<String, Object> config, String cls) {
|
||||
String html = str(config.get("html"), "");
|
||||
return "<div class=\"" + cls + "\">" + html + "</div>";
|
||||
}
|
||||
|
||||
private List<Section> buildSections(List<SiteStructure.ComponentStructure> components) {
|
||||
List<Section> sections = new ArrayList<>();
|
||||
if (components == null) return sections;
|
||||
for (int i = 0; i < components.size(); i++) {
|
||||
SiteStructure.ComponentStructure c = components.get(i);
|
||||
String bg = "";
|
||||
if (c.getStyles() != null && c.getStyles().get("backgroundColor") != null) {
|
||||
bg = String.valueOf(c.getStyles().get("backgroundColor"));
|
||||
}
|
||||
if ((bg != null && !bg.isEmpty()) || i == 0) {
|
||||
sections.add(new Section(bg, new ArrayList<>()));
|
||||
}
|
||||
sections.get(sections.size() - 1).indices.add(i);
|
||||
}
|
||||
return sections;
|
||||
}
|
||||
|
||||
private String cssProperty(String key) {
|
||||
return key.replaceAll("([a-z])([A-Z])", "$1-$2").toLowerCase();
|
||||
}
|
||||
|
||||
private String esc(String s) {
|
||||
if (s == null) return "";
|
||||
return s.replace("&", "&")
|
||||
.replace("<", "<")
|
||||
.replace(">", ">")
|
||||
.replace("\"", """)
|
||||
.replace("'", "'");
|
||||
}
|
||||
|
||||
private String capitalize(String s) {
|
||||
if (s == null || s.isEmpty()) return s;
|
||||
return Character.toUpperCase(s.charAt(0)) + s.substring(1);
|
||||
}
|
||||
|
||||
private String str(Object value, String defaultValue) {
|
||||
return value != null ? String.valueOf(value) : defaultValue;
|
||||
}
|
||||
|
||||
private int toInt(Object value, int defaultValue) {
|
||||
if (value instanceof Number n) return n.intValue();
|
||||
if (value instanceof String s) {
|
||||
try { return Integer.parseInt(s); } catch (NumberFormatException e) { return defaultValue; }
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
private record Section(String bg, List<Integer> indices) {}
|
||||
|
||||
public record SiteOutput(String css, String js, Map<String, String> pages) {
|
||||
public Map<String, byte[]> asFileMap() {
|
||||
Map<String, byte[]> files = new LinkedHashMap<>();
|
||||
@@ -321,7 +99,7 @@ public class RenderService {
|
||||
for (Map.Entry<String, String> entry : pages.entrySet()) {
|
||||
String slug = entry.getKey();
|
||||
String html = entry.getValue();
|
||||
if (slug == null || slug.isEmpty() || "home".equals(slug)) {
|
||||
if (slug == null || slug.isEmpty() || "home".equals(slug) || "index".equals(slug)) {
|
||||
files.put("index.html", html.getBytes());
|
||||
} else {
|
||||
files.put(slug + "/index.html", html.getBytes());
|
||||
|
||||
@@ -72,9 +72,7 @@ class LLMControllerTest {
|
||||
.pages(List.of(
|
||||
SiteStructure.PageStructure.builder()
|
||||
.title("Home").slug("home")
|
||||
.components(List.of(
|
||||
SiteStructure.ComponentStructure.builder().type("hero").build()
|
||||
))
|
||||
.rawHtml("<h1>Home</h1>")
|
||||
.build()
|
||||
))
|
||||
.build();
|
||||
@@ -101,9 +99,7 @@ class LLMControllerTest {
|
||||
.pages(List.of(
|
||||
SiteStructure.PageStructure.builder()
|
||||
.title("Home").slug("home")
|
||||
.components(List.of(
|
||||
SiteStructure.ComponentStructure.builder().type("hero").build()
|
||||
))
|
||||
.rawHtml("<h1>Home</h1>")
|
||||
.build()
|
||||
))
|
||||
.build();
|
||||
@@ -215,9 +211,7 @@ class LLMControllerTest {
|
||||
.pages(List.of(
|
||||
SiteStructure.PageStructure.builder()
|
||||
.title("Old").slug("old")
|
||||
.components(List.of(
|
||||
SiteStructure.ComponentStructure.builder().type("text").build()
|
||||
))
|
||||
.rawHtml("<p>Old</p>")
|
||||
.build()
|
||||
))
|
||||
.build();
|
||||
@@ -225,9 +219,7 @@ class LLMControllerTest {
|
||||
.pages(List.of(
|
||||
SiteStructure.PageStructure.builder()
|
||||
.title("Refined").slug("refined")
|
||||
.components(List.of(
|
||||
SiteStructure.ComponentStructure.builder().type("hero").build()
|
||||
))
|
||||
.rawHtml("<h1>Refined</h1>")
|
||||
.build()
|
||||
))
|
||||
.build();
|
||||
@@ -354,9 +346,7 @@ class LLMControllerTest {
|
||||
.pages(List.of(
|
||||
SiteStructure.PageStructure.builder()
|
||||
.title("Restored").slug("restored")
|
||||
.components(List.of(
|
||||
SiteStructure.ComponentStructure.builder().type("hero").build()
|
||||
))
|
||||
.rawHtml("<h1>Restored</h1>")
|
||||
.build()
|
||||
))
|
||||
.build();
|
||||
|
||||
364
backend/src/test/java/com/krrishg/service/GitServiceTest.java
Normal file
364
backend/src/test/java/com/krrishg/service/GitServiceTest.java
Normal file
@@ -0,0 +1,364 @@
|
||||
package com.krrishg.service;
|
||||
|
||||
import com.krrishg.config.GitConfig;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class GitServiceTest {
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private Path remoteDir;
|
||||
private GitService gitService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
remoteDir = tempDir.resolve("remote");
|
||||
Files.createDirectories(remoteDir);
|
||||
|
||||
GitConfig config = new GitConfig();
|
||||
config.setAuthorName("Test Author");
|
||||
config.setAuthorEmail("test@example.com");
|
||||
gitService = new GitService(config, null, null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void initialPublishCreatesBranchOnRemote() throws Exception {
|
||||
initRemote();
|
||||
Path workDir = tempDir.resolve("work");
|
||||
Files.createDirectories(workDir);
|
||||
Path contentRoot = workDir;
|
||||
|
||||
Map<String, byte[]> files = new HashMap<>();
|
||||
files.put("index.html", "<h1>Hello</h1>".getBytes());
|
||||
files.put("style.css", "body { color: red; }".getBytes());
|
||||
|
||||
gitService.pushPagesBranch(workDir, "file://" + remoteDir.toString(), "gh-pages",
|
||||
contentRoot, new GitHubPagesProvider(), files, "Deploy site");
|
||||
|
||||
assertBranchExists("gh-pages");
|
||||
assertRemoteFileContent("gh-pages", "index.html", "<h1>Hello</h1>");
|
||||
assertRemoteFileContent("gh-pages", "style.css", "body { color: red; }");
|
||||
assertRemoteHasNojekyll("gh-pages");
|
||||
}
|
||||
|
||||
@Test
|
||||
void initialPublishForGitLabCreatesPublicDirectory() throws Exception {
|
||||
initRemote();
|
||||
Path workDir = tempDir.resolve("work");
|
||||
Files.createDirectories(workDir);
|
||||
Path contentRoot = workDir.resolve("public");
|
||||
Files.createDirectories(contentRoot);
|
||||
|
||||
Map<String, byte[]> files = new HashMap<>();
|
||||
files.put("index.html", "<h1>GitLab</h1>".getBytes());
|
||||
|
||||
gitService.pushPagesBranch(workDir, "file://" + remoteDir.toString(), "gl-pages",
|
||||
contentRoot, new GitLabPagesProvider(), files, "Deploy site");
|
||||
|
||||
assertBranchExists("gl-pages");
|
||||
assertRemoteFileContent("gl-pages", "public/index.html", "<h1>GitLab</h1>");
|
||||
assertRemoteHasGitlabCi("gl-pages");
|
||||
}
|
||||
|
||||
@Test
|
||||
void secondPublishPreservesHistory() throws Exception {
|
||||
initRemote();
|
||||
Path workDir = tempDir.resolve("work");
|
||||
Files.createDirectories(workDir);
|
||||
Path contentRoot = workDir;
|
||||
|
||||
Map<String, byte[]> firstFiles = new HashMap<>();
|
||||
firstFiles.put("index.html", "<h1>First</h1>".getBytes());
|
||||
|
||||
gitService.pushPagesBranch(workDir, "file://" + remoteDir.toString(), "gh-pages",
|
||||
contentRoot, new GitHubPagesProvider(), firstFiles, "Deploy site");
|
||||
|
||||
Path workDir2 = tempDir.resolve("work2");
|
||||
Files.createDirectories(workDir2);
|
||||
Path contentRoot2 = workDir2;
|
||||
|
||||
Map<String, byte[]> secondFiles = new HashMap<>();
|
||||
secondFiles.put("index.html", "<h1>Second</h1>".getBytes());
|
||||
|
||||
gitService.pushPagesBranch(workDir2, "file://" + remoteDir.toString(), "gh-pages",
|
||||
contentRoot2, new GitHubPagesProvider(), secondFiles, "Deploy site");
|
||||
|
||||
assertRemoteFileContent("gh-pages", "index.html", "<h1>Second</h1>");
|
||||
|
||||
int commitCount = countCommits("gh-pages");
|
||||
assertEquals(2, commitCount);
|
||||
}
|
||||
|
||||
@Test
|
||||
void divergenceUsesLocalContentOnConflict() throws Exception {
|
||||
initRemote();
|
||||
Path workDir = tempDir.resolve("work");
|
||||
Files.createDirectories(workDir);
|
||||
Path contentRoot = workDir;
|
||||
|
||||
Map<String, byte[]> initialFiles = new HashMap<>();
|
||||
initialFiles.put("index.html", "<h1>Original</h1>".getBytes());
|
||||
gitService.pushPagesBranch(workDir, "file://" + remoteDir.toString(), "gh-pages",
|
||||
contentRoot, new GitHubPagesProvider(), initialFiles, "Deploy site");
|
||||
|
||||
addFileToRemote("gh-pages", "index.html", "<h1>Remote change</h1>");
|
||||
addFileToRemote("gh-pages", "remote-only.html", "<p>Remote only</p>");
|
||||
|
||||
Path workDir2 = tempDir.resolve("work2");
|
||||
Files.createDirectories(workDir2);
|
||||
Path contentRoot2 = workDir2;
|
||||
|
||||
Map<String, byte[]> localFiles = new HashMap<>();
|
||||
localFiles.put("index.html", "<h1>Local new version</h1>".getBytes());
|
||||
localFiles.put("local-only.html", "<p>Local only</p>".getBytes());
|
||||
|
||||
gitService.pushPagesBranch(workDir2, "file://" + remoteDir.toString(), "gh-pages",
|
||||
contentRoot2, new GitHubPagesProvider(), localFiles, "Deploy site");
|
||||
|
||||
assertRemoteFileContent("gh-pages", "index.html", "<h1>Local new version</h1>");
|
||||
assertRemoteFileContent("gh-pages", "local-only.html", "<p>Local only</p>");
|
||||
int commitCount = countCommits("gh-pages");
|
||||
assertEquals(4, commitCount, "Should preserve history from both sides");
|
||||
}
|
||||
|
||||
@Test
|
||||
void publishWithoutNojekyllForGitHub() throws Exception {
|
||||
initRemote();
|
||||
Path workDir = tempDir.resolve("work");
|
||||
Files.createDirectories(workDir);
|
||||
Path contentRoot = workDir;
|
||||
|
||||
Map<String, byte[]> files = new HashMap<>();
|
||||
files.put("index.html", "<h1>Test</h1>".getBytes());
|
||||
|
||||
gitService.pushPagesBranch(workDir, "file://" + remoteDir.toString(), "gh-pages",
|
||||
contentRoot, new GitHubPagesProvider(), files, "Deploy site");
|
||||
|
||||
assertRemoteFileContent("gh-pages", "index.html", "<h1>Test</h1>");
|
||||
}
|
||||
|
||||
@Test
|
||||
void unpublishReplacesContentWithRemovalPage() throws Exception {
|
||||
initRemote();
|
||||
Path workDir = tempDir.resolve("work");
|
||||
Files.createDirectories(workDir);
|
||||
Path contentRoot = workDir;
|
||||
|
||||
Map<String, byte[]> siteFiles = new HashMap<>();
|
||||
siteFiles.put("index.html", "<h1>My Site</h1>".getBytes());
|
||||
siteFiles.put("about.html", "<h1>About</h1>".getBytes());
|
||||
gitService.pushPagesBranch(workDir, "file://" + remoteDir.toString(), "gh-pages",
|
||||
contentRoot, new GitHubPagesProvider(), siteFiles, "Deploy site");
|
||||
|
||||
Path workDir2 = tempDir.resolve("work2");
|
||||
Files.createDirectories(workDir2);
|
||||
Path contentRoot2 = workDir2;
|
||||
|
||||
Map<String, byte[]> removalFiles = new HashMap<>();
|
||||
removalFiles.put("index.html", "<!DOCTYPE html>".getBytes());
|
||||
gitService.pushPagesBranch(workDir2, "file://" + remoteDir.toString(), "gh-pages",
|
||||
contentRoot2, new GitHubPagesProvider(), removalFiles, "Unpublish site");
|
||||
|
||||
assertRemoteFileContentStart("gh-pages", "index.html", "<!DOCTYPE html>");
|
||||
assertRemoteFileMissing("gh-pages", "about.html");
|
||||
}
|
||||
|
||||
@Test
|
||||
void unpublishPreservesHistory() throws Exception {
|
||||
initRemote();
|
||||
Path workDir = tempDir.resolve("work");
|
||||
Files.createDirectories(workDir);
|
||||
Path contentRoot = workDir;
|
||||
|
||||
Map<String, byte[]> siteFiles = new HashMap<>();
|
||||
siteFiles.put("index.html", "<h1>My Site</h1>".getBytes());
|
||||
gitService.pushPagesBranch(workDir, "file://" + remoteDir.toString(), "gh-pages",
|
||||
contentRoot, new GitHubPagesProvider(), siteFiles, "Deploy site");
|
||||
|
||||
Path workDir2 = tempDir.resolve("work2");
|
||||
Files.createDirectories(workDir2);
|
||||
Path contentRoot2 = workDir2;
|
||||
|
||||
Map<String, byte[]> removalFiles = new HashMap<>();
|
||||
removalFiles.put("index.html", "<!DOCTYPE html>".getBytes());
|
||||
gitService.pushPagesBranch(workDir2, "file://" + remoteDir.toString(), "gh-pages",
|
||||
contentRoot2, new GitHubPagesProvider(), removalFiles, "Unpublish site");
|
||||
|
||||
int commitCount = countCommits("gh-pages");
|
||||
assertEquals(2, commitCount, "Unpublish should add a commit, not replace history");
|
||||
}
|
||||
|
||||
@Test
|
||||
void unpublishForGitLabUsesPublicDirectory() throws Exception {
|
||||
initRemote();
|
||||
Path workDir = tempDir.resolve("work");
|
||||
Files.createDirectories(workDir);
|
||||
Path contentRoot = workDir.resolve("public");
|
||||
Files.createDirectories(contentRoot);
|
||||
|
||||
Map<String, byte[]> siteFiles = new HashMap<>();
|
||||
siteFiles.put("index.html", "<h1>GitLab Site</h1>".getBytes());
|
||||
gitService.pushPagesBranch(workDir, "file://" + remoteDir.toString(), "gl-pages",
|
||||
contentRoot, new GitLabPagesProvider(), siteFiles, "Deploy site");
|
||||
|
||||
Path workDir2 = tempDir.resolve("work2");
|
||||
Files.createDirectories(workDir2);
|
||||
Path contentRoot2 = workDir2.resolve("public");
|
||||
Files.createDirectories(contentRoot2);
|
||||
|
||||
Map<String, byte[]> removalFiles = new HashMap<>();
|
||||
removalFiles.put("index.html", "<!DOCTYPE html>".getBytes());
|
||||
gitService.pushPagesBranch(workDir2, "file://" + remoteDir.toString(), "gl-pages",
|
||||
contentRoot2, new GitLabPagesProvider(), removalFiles, "Unpublish site");
|
||||
|
||||
assertRemoteFileContentStart("gl-pages", "public/index.html", "<!DOCTYPE html>");
|
||||
assertRemoteHasGitlabCi("gl-pages");
|
||||
}
|
||||
|
||||
private void assertRemoteFileContentStart(String branch, String path, String expectedPrefix) throws Exception {
|
||||
Path cloneDir = tempDir.resolve("prefix-" + path.hashCode());
|
||||
Files.createDirectories(cloneDir);
|
||||
runGit(cloneDir,
|
||||
"git init",
|
||||
"git remote add origin file://" + remoteDir.toString(),
|
||||
"git fetch origin " + branch,
|
||||
"git checkout -b " + branch + " origin/" + branch
|
||||
);
|
||||
Path filePath = cloneDir.resolve(path);
|
||||
assertTrue(Files.exists(filePath), "File " + path + " should exist in " + branch);
|
||||
String actual = Files.readString(filePath);
|
||||
assertTrue(actual.startsWith(expectedPrefix),
|
||||
"Expected content starting with \"" + expectedPrefix + "\" but got: " + actual);
|
||||
}
|
||||
|
||||
private void assertRemoteFileMissing(String branch, String path) throws Exception {
|
||||
Path cloneDir = tempDir.resolve("missing-" + path.hashCode());
|
||||
Files.createDirectories(cloneDir);
|
||||
runGit(cloneDir,
|
||||
"git init",
|
||||
"git remote add origin file://" + remoteDir.toString(),
|
||||
"git fetch origin " + branch,
|
||||
"git checkout -b " + branch + " origin/" + branch
|
||||
);
|
||||
Path filePath = cloneDir.resolve(path);
|
||||
assertFalse(Files.exists(filePath), "File " + path + " should NOT exist in " + branch);
|
||||
}
|
||||
|
||||
private void initRemote() throws Exception {
|
||||
runGit(remoteDir, "git init --bare");
|
||||
}
|
||||
|
||||
private void assertBranchExists(String branch) throws Exception {
|
||||
ProcessBuilder pb = new ProcessBuilder("sh", "-c",
|
||||
"git ls-remote --exit-code file://" + remoteDir.toString() + " " + branch);
|
||||
pb.directory(tempDir.toFile());
|
||||
assertEquals(0, pb.start().waitFor(), "Branch " + branch + " should exist on remote");
|
||||
}
|
||||
|
||||
private void assertRemoteFileContent(String branch, String path, String expectedContent) throws Exception {
|
||||
Path cloneDir = tempDir.resolve("assert-" + path.hashCode());
|
||||
Files.createDirectories(cloneDir);
|
||||
runGit(cloneDir,
|
||||
"git init",
|
||||
"git remote add origin file://" + remoteDir.toString(),
|
||||
"git fetch origin " + branch,
|
||||
"git checkout -b " + branch + " origin/" + branch
|
||||
);
|
||||
Path filePath = cloneDir.resolve(path);
|
||||
assertTrue(Files.exists(filePath), "File " + path + " should exist in " + branch);
|
||||
String actual = Files.readString(filePath);
|
||||
assertEquals(expectedContent, actual);
|
||||
}
|
||||
|
||||
private void assertRemoteHasNojekyll(String branch) throws Exception {
|
||||
try {
|
||||
assertRemoteFileContent(branch, ".nojekyll", "");
|
||||
} catch (AssertionError e) {
|
||||
Path cloneDir = tempDir.resolve("nojekyll-check");
|
||||
Files.createDirectories(cloneDir);
|
||||
runGit(cloneDir,
|
||||
"git init",
|
||||
"git remote add origin file://" + remoteDir.toString(),
|
||||
"git fetch origin " + branch,
|
||||
"git checkout -b " + branch + " origin/" + branch
|
||||
);
|
||||
assertTrue(Files.exists(cloneDir.resolve(".nojekyll")),
|
||||
".nojekyll file should exist on " + branch);
|
||||
}
|
||||
}
|
||||
|
||||
private void assertRemoteHasGitlabCi(String branch) throws Exception {
|
||||
assertRemoteFileContent(branch, ".gitlab-ci.yml",
|
||||
"pages:\n" +
|
||||
" stage: deploy\n" +
|
||||
" script:\n" +
|
||||
" - echo \"Deploying GitLab Pages\"\n" +
|
||||
" artifacts:\n" +
|
||||
" paths:\n" +
|
||||
" - public\n" +
|
||||
" rules:\n" +
|
||||
" - if: '$CI_COMMIT_BRANCH == \"gl-pages\"'\n");
|
||||
}
|
||||
|
||||
private void addFileToRemote(String branch, String path, String content) throws Exception {
|
||||
Path cloneDir = tempDir.resolve("remote-add-" + path.hashCode());
|
||||
Files.createDirectories(cloneDir);
|
||||
runGit(cloneDir,
|
||||
"git init",
|
||||
"git remote add origin file://" + remoteDir.toString(),
|
||||
"git fetch origin " + branch,
|
||||
"git checkout -b " + branch + " origin/" + branch
|
||||
);
|
||||
Path filePath = cloneDir.resolve(path);
|
||||
Files.createDirectories(filePath.getParent());
|
||||
Files.writeString(filePath, content);
|
||||
runGit(cloneDir,
|
||||
"git add -A",
|
||||
"git -c user.name='Remote' -c user.email='remote@example.com' commit -m 'Remote change: " + path + "'",
|
||||
"git push origin " + branch
|
||||
);
|
||||
}
|
||||
|
||||
private int countCommits(String branch) throws Exception {
|
||||
Path cloneDir = tempDir.resolve("count-commits");
|
||||
Files.createDirectories(cloneDir);
|
||||
runGit(cloneDir,
|
||||
"git init",
|
||||
"git remote add origin file://" + remoteDir.toString(),
|
||||
"git fetch origin " + branch
|
||||
);
|
||||
ProcessBuilder pb = new ProcessBuilder("sh", "-c",
|
||||
"git rev-list --count origin/" + branch);
|
||||
pb.directory(cloneDir.toFile());
|
||||
pb.redirectErrorStream(true);
|
||||
Process process = pb.start();
|
||||
String output = new String(process.getInputStream().readAllBytes()).trim();
|
||||
int exitCode = process.waitFor();
|
||||
if (exitCode != 0) return 0;
|
||||
return Integer.parseInt(output);
|
||||
}
|
||||
|
||||
private void runGit(Path workDir, String... commands) throws Exception {
|
||||
for (String cmd : commands) {
|
||||
ProcessBuilder pb = new ProcessBuilder("sh", "-c", cmd);
|
||||
pb.directory(workDir.toFile());
|
||||
pb.redirectErrorStream(true);
|
||||
Process process = pb.start();
|
||||
int exitCode = process.waitFor();
|
||||
if (exitCode != 0) {
|
||||
String output = new String(process.getInputStream().readAllBytes());
|
||||
throw new RuntimeException("git command failed (exit " + exitCode + "): " + cmd + "\n" + output);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -95,9 +95,7 @@ class LLMResponseParserTest {
|
||||
{
|
||||
"title": "Home",
|
||||
"slug": "home",
|
||||
"components": [
|
||||
{"type": "hero", "orderIndex": 0, "config": {"title": "Welcome"}}
|
||||
]
|
||||
"rawHtml": "<h1>Welcome</h1>"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -111,8 +109,7 @@ class LLMResponseParserTest {
|
||||
assertEquals(1, structure.getPages().size());
|
||||
assertEquals("Home", structure.getPages().get(0).getTitle());
|
||||
assertEquals("home", structure.getPages().get(0).getSlug());
|
||||
assertEquals(1, structure.getPages().get(0).getComponents().size());
|
||||
assertEquals("hero", structure.getPages().get(0).getComponents().get(0).getType());
|
||||
assertEquals("<h1>Welcome</h1>", structure.getPages().get(0).getRawHtml());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -120,7 +117,7 @@ class LLMResponseParserTest {
|
||||
String json = """
|
||||
{
|
||||
"pages": [
|
||||
{"title": "Home", "slug": "home", "components": [{"type": "text"}]}
|
||||
{"title": "Home", "slug": "home", "rawHtml": "<p>Hello</p>"}
|
||||
]
|
||||
}
|
||||
""";
|
||||
@@ -147,7 +144,7 @@ class LLMResponseParserTest {
|
||||
String json = """
|
||||
{
|
||||
"pages": [
|
||||
{"slug": "home", "components": [{"type": "hero"}]}
|
||||
{"slug": "home", "rawHtml": "<p>test</p>"}
|
||||
]
|
||||
}
|
||||
""";
|
||||
@@ -159,19 +156,7 @@ class LLMResponseParserTest {
|
||||
String json = """
|
||||
{
|
||||
"pages": [
|
||||
{"title": "Home", "components": [{"type": "hero"}]}
|
||||
]
|
||||
}
|
||||
""";
|
||||
assertThrows(IllegalArgumentException.class, () -> parser.parse(json));
|
||||
}
|
||||
|
||||
@Test
|
||||
void throwsOnComponentWithoutType() {
|
||||
String json = """
|
||||
{
|
||||
"pages": [
|
||||
{"title": "Home", "slug": "home", "components": [{"orderIndex": 0}]}
|
||||
{"title": "Home", "rawHtml": "<p>test</p>"}
|
||||
]
|
||||
}
|
||||
""";
|
||||
@@ -183,7 +168,7 @@ class LLMResponseParserTest {
|
||||
String json = """
|
||||
{
|
||||
"pages": [
|
||||
{"title": "Home", "slug": "home", "components": [{"type": "hero"}]}
|
||||
{"title": "Home", "slug": "home", "rawHtml": "<h1>Hello</h1>"}
|
||||
]
|
||||
}
|
||||
""";
|
||||
@@ -192,20 +177,6 @@ class LLMResponseParserTest {
|
||||
assertNotNull(structure.getPages().get(0).getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void assignsGeneratedIdWhenComponentIdMissing() {
|
||||
String json = """
|
||||
{
|
||||
"pages": [
|
||||
{"title": "Home", "slug": "home", "components": [{"type": "hero"}]}
|
||||
]
|
||||
}
|
||||
""";
|
||||
|
||||
SiteStructure structure = parser.parse(json);
|
||||
assertNotNull(structure.getPages().get(0).getComponents().get(0).getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesPageWithAllOptionalFields() {
|
||||
String json = """
|
||||
@@ -218,15 +189,7 @@ class LLMResponseParserTest {
|
||||
"seoTitle": "About Us",
|
||||
"seoDescription": "Learn about our company",
|
||||
"orderIndex": 1,
|
||||
"components": [
|
||||
{
|
||||
"id": "comp-1",
|
||||
"type": "text",
|
||||
"orderIndex": 0,
|
||||
"config": {"body": "Hello"},
|
||||
"styles": {"color": "red"}
|
||||
}
|
||||
]
|
||||
"rawHtml": "<section><h2>About</h2><p>Our story</p></section>"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -240,13 +203,7 @@ class LLMResponseParserTest {
|
||||
assertEquals("About Us", page.getSeoTitle());
|
||||
assertEquals("Learn about our company", page.getSeoDescription());
|
||||
assertEquals(1, page.getOrderIndex());
|
||||
|
||||
SiteStructure.ComponentStructure comp = page.getComponents().get(0);
|
||||
assertEquals("comp-1", comp.getId());
|
||||
assertEquals("text", comp.getType());
|
||||
assertEquals(0, comp.getOrderIndex());
|
||||
assertEquals("Hello", comp.getConfig().get("body"));
|
||||
assertEquals("red", comp.getStyles().get("color"));
|
||||
assertEquals("<section><h2>About</h2><p>Our story</p></section>", page.getRawHtml());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -255,7 +212,7 @@ class LLMResponseParserTest {
|
||||
{
|
||||
"unknownField": true,
|
||||
"pages": [
|
||||
{"title": "Home", "slug": "home", "components": [{"type": "hero", "extra": 1}]}
|
||||
{"title": "Home", "slug": "home", "rawHtml": "<p>test</p>", "extra": 1}
|
||||
]
|
||||
}
|
||||
""";
|
||||
@@ -268,5 +225,19 @@ class LLMResponseParserTest {
|
||||
String json = "{invalid json here}";
|
||||
assertThrows(IllegalArgumentException.class, () -> parser.parse(json));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsesPageWithoutRawHtml() {
|
||||
String json = """
|
||||
{
|
||||
"pages": [
|
||||
{"title": "Empty", "slug": "empty"}
|
||||
]
|
||||
}
|
||||
""";
|
||||
|
||||
SiteStructure structure = parser.parse(json);
|
||||
assertNull(structure.getPages().get(0).getRawHtml());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ class LLMServiceTest {
|
||||
activeProvider.withContent("""
|
||||
{
|
||||
"pages": [
|
||||
{"title": "Home", "slug": "home", "components": [{"type": "hero"}]}
|
||||
{"title": "Home", "slug": "home", "rawHtml": "<h1>Welcome</h1>"}
|
||||
]
|
||||
}
|
||||
""");
|
||||
@@ -97,7 +97,7 @@ class LLMServiceTest {
|
||||
activeProvider.withContent("""
|
||||
{
|
||||
"pages": [
|
||||
{"title": "About", "slug": "about", "components": [{"type": "text"}]}
|
||||
{"title": "About", "slug": "about", "rawHtml": "<p>About</p>"}
|
||||
]
|
||||
}
|
||||
""");
|
||||
@@ -122,7 +122,7 @@ class LLMServiceTest {
|
||||
activeProvider.withContent("""
|
||||
{
|
||||
"pages": [
|
||||
{"title": "Home", "slug": "home", "components": [{"type": "hero"}]}
|
||||
{"title": "Home", "slug": "home", "rawHtml": "<p>Hero</p>"}
|
||||
]
|
||||
}
|
||||
""");
|
||||
@@ -147,7 +147,7 @@ class LLMServiceTest {
|
||||
activeProvider.withContent("""
|
||||
{
|
||||
"pages": [
|
||||
{"title": "Home", "slug": "home", "components": [{"type": "hero"}]}
|
||||
{"title": "Home", "slug": "home", "rawHtml": "<p>Hero</p>"}
|
||||
]
|
||||
}
|
||||
""");
|
||||
@@ -174,7 +174,7 @@ class LLMServiceTest {
|
||||
fallbackProvider.withContent("""
|
||||
{
|
||||
"pages": [
|
||||
{"title": "Fallback", "slug": "fallback", "components": [{"type": "hero"}]}
|
||||
{"title": "Fallback", "slug": "fallback", "rawHtml": "<p>Hero</p>"}
|
||||
]
|
||||
}
|
||||
""");
|
||||
@@ -204,9 +204,7 @@ class LLMServiceTest {
|
||||
SiteStructure.PageStructure.builder()
|
||||
.title("Old")
|
||||
.slug("old")
|
||||
.components(List.of(
|
||||
SiteStructure.ComponentStructure.builder().type("text").build()
|
||||
))
|
||||
.rawHtml("<p>Old content</p>")
|
||||
.build()
|
||||
))
|
||||
.build();
|
||||
@@ -214,7 +212,7 @@ class LLMServiceTest {
|
||||
activeProvider.withContent("""
|
||||
{
|
||||
"pages": [
|
||||
{"title": "Refined", "slug": "refined", "components": [{"type": "hero"}]}
|
||||
{"title": "Refined", "slug": "refined", "rawHtml": "<p>Hero</p>"}
|
||||
]
|
||||
}
|
||||
""");
|
||||
@@ -258,7 +256,7 @@ class LLMServiceTest {
|
||||
GenerationVersion version = GenerationVersion.builder()
|
||||
.id("v1")
|
||||
.fullStructure("""
|
||||
{"pages": [{"title": "Restored", "slug": "restored", "components": [{"type": "hero"}]}]}
|
||||
{"pages": [{"title": "Restored", "slug": "restored", "rawHtml": "<p>Hero</p>"}]}
|
||||
""")
|
||||
.build();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user