commit c6cd3253e3c92e204c5fe93d9ae02ff0f3b988ed parent fde2c53908e6d18bc738371de5f8f695ad0f86cc Author: Wim Dupont <wim@wimdupont.com> Date: Sat, 16 Dec 2023 23:26:21 +0100 moved git content to one project Former-commit-id: 12c7ac107532be06aa99bf6d5a96a311c49d713f Diffstat:
11 files changed, 87 insertions(+), 32 deletions(-)
diff --git a/src/main/java/com/wimdupont/personalweb/api/GitlabApi.java b/src/main/java/com/wimdupont/personalweb/api/GitlabApi.java @@ -7,7 +7,10 @@ import com.wimdupont.personalweb.model.AdocContentType; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.util.UriUtils; +import java.net.URI; +import java.nio.charset.StandardCharsets; import java.time.Duration; import java.util.List; import java.util.Optional; @@ -16,43 +19,40 @@ import java.util.Optional; public class GitlabApi { private final WebClient webClient; - private final String guideRepositoryUrl; - private final String blogRepositoryUrl; + private final String repositoryUrl; - public GitlabApi(@Value("${gitlab.guide.repository.url}") String guideRepositoryUrl, - @Value("${gitlab.blog.repository.url}") String blogRepositoryUrl, + public GitlabApi(@Value("${gitlab.repository.url}") String repositoryUrl, WebClient webClient) { - this.guideRepositoryUrl = guideRepositoryUrl; - this.blogRepositoryUrl = blogRepositoryUrl; + this.repositoryUrl = repositoryUrl; this.webClient = webClient; } public Optional<List<RepositoryTreeItem>> getRepositoryTree(AdocContentType adocContentType) { - return webClient.get().uri(getRepositoryUrl(adocContentType) + "/tree").retrieve() + return webClient.get().uri(String.format("%s/tree?ref=main&path=%s", + repositoryUrl, + adocContentType.getPathPrefix())) + .retrieve() .bodyToFlux(RepositoryTreeItem.class) .timeout(Duration.ofSeconds(5)) .collectList().blockOptional(); } - public Optional<RepositoryFile> getRepositoryFile(AdocContentType adocContentType, String filePath) { - return webClient.get().uri(String.format("%s/files/%s?ref=main", getRepositoryUrl(adocContentType), filePath)) + public Optional<RepositoryFile> getRepositoryFile(String filePath) { + return webClient.get().uri(URI.create(String.format("%s/files/%s?ref=main", + repositoryUrl, + UriUtils.encode(filePath, StandardCharsets.UTF_8)))) .retrieve().bodyToMono(RepositoryFile.class) .timeout(Duration.ofSeconds(5)) .blockOptional(); } - public Optional<Commit> getCommit(AdocContentType adocContentType, String commitId){ - return webClient.get().uri(String.format("%s/commits/%s", getRepositoryUrl(adocContentType), commitId)) + public Optional<Commit> getCommit(String commitId) { + return webClient.get().uri(String.format("%s/commits/%s", + repositoryUrl, + commitId)) .retrieve().bodyToMono(Commit.class) .timeout(Duration.ofSeconds(5)) .blockOptional(); } - private String getRepositoryUrl(AdocContentType adocContentType) { - return switch (adocContentType){ - case GUIDE -> guideRepositoryUrl; - case BLOG_ARTICLE -> blogRepositoryUrl; - }; - } - } diff --git a/src/main/java/com/wimdupont/personalweb/converter/AdocContentMetaToDtoConverter.java b/src/main/java/com/wimdupont/personalweb/converter/AdocContentMetaToDtoConverter.java @@ -2,6 +2,7 @@ package com.wimdupont.personalweb.converter; import com.wimdupont.personalweb.model.dao.projection.AdocContentMeta; import com.wimdupont.personalweb.model.dto.AdocContentMetaDto; +import com.wimdupont.personalweb.model.dto.AdocContentMetaDto.Builder; import com.wimdupont.personalweb.util.Constants; import org.springframework.stereotype.Component; @@ -9,8 +10,10 @@ import org.springframework.stereotype.Component; public class AdocContentMetaToDtoConverter { public AdocContentMetaDto convertForMetaData(AdocContentMeta adocContentMeta) { - return AdocContentMetaDto.Builder.newBuilder() - .title(adocContentMeta.getPath().replace(Constants.ADOC_SUFFIX, "")) + return Builder.newBuilder() + .title(adocContentMeta.getPath() + .replace(Constants.ADOC_SUFFIX, "") + .replaceFirst(adocContentMeta.getContentType().getPathPrefix() + "/", "")) .dateTime(adocContentMeta.getCommittedDate().toLocalDate()) .build(); } diff --git a/src/main/java/com/wimdupont/personalweb/model/AdocContentType.java b/src/main/java/com/wimdupont/personalweb/model/AdocContentType.java @@ -2,6 +2,16 @@ package com.wimdupont.personalweb.model; public enum AdocContentType { - GUIDE, - BLOG_ARTICLE + GUIDE("guides"), + BLOG_ARTICLE("blog"); + + private final String pathPrefix; + + public String getPathPrefix() { + return pathPrefix; + } + + AdocContentType(String pathPrefix) { + this.pathPrefix = pathPrefix; + } } diff --git a/src/main/java/com/wimdupont/personalweb/model/dao/projection/AdocContentMeta.java b/src/main/java/com/wimdupont/personalweb/model/dao/projection/AdocContentMeta.java @@ -1,10 +1,13 @@ package com.wimdupont.personalweb.model.dao.projection; +import com.wimdupont.personalweb.model.AdocContentType; + import java.time.LocalDateTime; public interface AdocContentMeta { String getPath(); + AdocContentType getContentType(); LocalDateTime getCommittedDate(); } diff --git a/src/main/java/com/wimdupont/personalweb/repository/AdocContentRepository.java b/src/main/java/com/wimdupont/personalweb/repository/AdocContentRepository.java @@ -12,7 +12,7 @@ import java.util.Optional; @Repository public interface AdocContentRepository extends JpaRepository<AdocContent, String> { - Optional<AdocContent> findByPathAndContentType(String path, AdocContentType contentType); + Optional<AdocContent> findByPath(String path); Optional<AdocContent> findByContentSha256(String contentSha256); diff --git a/src/main/java/com/wimdupont/personalweb/service/AdocContentService.java b/src/main/java/com/wimdupont/personalweb/service/AdocContentService.java @@ -34,7 +34,7 @@ public class AdocContentService { } public Optional<AdocContent> findByByTitle(String title, AdocContentType adocContentType) { - return adocContentRepository.findByPathAndContentType(title + Constants.ADOC_SUFFIX, adocContentType); + return adocContentRepository.findByPath(adocContentType.getPathPrefix() + "/" + title + Constants.ADOC_SUFFIX); } public void removeWhenRemoteNotFound(AdocContentType adocContentType, List<RepositoryFile> repositoryFiles) { diff --git a/src/main/java/com/wimdupont/personalweb/service/RepositoryFileService.java b/src/main/java/com/wimdupont/personalweb/service/RepositoryFileService.java @@ -23,12 +23,12 @@ public class RepositoryFileService { var repositoryFiles = new ArrayList<RepositoryFile>(); gitlabApi.getRepositoryTree(adocContentType) .orElseThrow().stream() - .map(repositoryTreeItem -> gitlabApi.getRepositoryFile(adocContentType, repositoryTreeItem.path())) + .map(repositoryTreeItem -> gitlabApi.getRepositoryFile(repositoryTreeItem.path())) .forEach(repositoryFile -> repositoryFile.ifPresent(repositoryFiles::add)); return repositoryFiles; } - public Optional<Commit> getCommit(AdocContentType adocContentType, String commitId){ - return gitlabApi.getCommit(adocContentType, commitId); + public Optional<Commit> getCommit(String commitId){ + return gitlabApi.getCommit(commitId); } } diff --git a/src/main/java/com/wimdupont/personalweb/service/ScheduledFileGenerator.java b/src/main/java/com/wimdupont/personalweb/service/ScheduledFileGenerator.java @@ -54,7 +54,7 @@ public class ScheduledFileGenerator { .contentSha256(repositoryFile.contentSha256()) .htmlText(toHtml(repositoryFile.contentBase64())) .contentType(contentType) - .committedDate(fetchCommittedDate(contentType, repositoryFile.lastCommitId())) + .committedDate(fetchCommittedDate(repositoryFile.lastCommitId())) .build()) .forEach(adocContentService::save); } @@ -65,9 +65,9 @@ public class ScheduledFileGenerator { .decode(contentBase64.getBytes(StandardCharsets.UTF_8)))); } - private LocalDateTime fetchCommittedDate(AdocContentType contentType, String commitId) { + private LocalDateTime fetchCommittedDate(String commitId) { return LocalDateTime.ofInstant(repositoryFileService - .getCommit(contentType, commitId) + .getCommit(commitId) .orElseThrow() .committedDate(), ZoneId.systemDefault()); } diff --git a/src/test/java/com/wimdupont/personalweb/converter/AdocContentMetaToDtoConverterTest.java b/src/test/java/com/wimdupont/personalweb/converter/AdocContentMetaToDtoConverterTest.java @@ -1,5 +1,6 @@ package com.wimdupont.personalweb.converter; +import com.wimdupont.personalweb.model.AdocContentType; import com.wimdupont.personalweb.model.dao.projection.AdocContentMeta; import com.wimdupont.personalweb.model.dto.AdocContentMetaDto; import org.junit.jupiter.api.Assertions; @@ -27,6 +28,11 @@ class AdocContentMetaToDtoConverterTest { } @Override + public AdocContentType getContentType() { + return AdocContentType.GUIDE; + } + + @Override public LocalDateTime getCommittedDate() { return dateTime; } @@ -45,6 +51,11 @@ class AdocContentMetaToDtoConverterTest { } @Override + public AdocContentType getContentType() { + return AdocContentType.GUIDE; + } + + @Override public LocalDateTime getCommittedDate() { return dateTime; } @@ -63,6 +74,34 @@ class AdocContentMetaToDtoConverterTest { } @Override + public AdocContentType getContentType() { + return AdocContentType.GUIDE; + } + + @Override + public LocalDateTime getCommittedDate() { + return dateTime; + } + }); + Assertions.assertEquals("guide", result.title()); + Assertions.assertEquals(dateTime.toLocalDate(), result.date()); + } + + @Test + void convertWithTypePrefixShouldRemove() { + var dateTime = LocalDateTime.now(); + AdocContentMetaDto result = converter.convertForMetaData(new AdocContentMeta() { + @Override + public String getPath() { + return AdocContentType.GUIDE.getPathPrefix() + "/guide"; + } + + @Override + public AdocContentType getContentType() { + return AdocContentType.GUIDE; + } + + @Override public LocalDateTime getCommittedDate() { return dateTime; } diff --git a/src/test/java/com/wimdupont/personalweb/service/RepositoryFileServiceTest.java b/src/test/java/com/wimdupont/personalweb/service/RepositoryFileServiceTest.java @@ -37,7 +37,7 @@ class RepositoryFileServiceTest { public void findRepositoryFiles() { Mockito.when(gitlabApi.getRepositoryTree(AdocContentType.GUIDE)) .thenReturn(Optional.of(repositoryTreeItems)); - Mockito.when(gitlabApi.getRepositoryFile(AdocContentType.GUIDE, "path")) + Mockito.when(gitlabApi.getRepositoryFile("path")) .thenReturn(Optional.of(repositoryFile)); var result = repositoryFileService.findRepositoryFiles(AdocContentType.GUIDE); diff --git a/src/test/java/com/wimdupont/personalweb/service/ScheduledFileGeneratorTest.java b/src/test/java/com/wimdupont/personalweb/service/ScheduledFileGeneratorTest.java @@ -80,7 +80,7 @@ class ScheduledFileGeneratorTest { .thenReturn(Optional.empty()); Mockito.when(adocConverter.convert(Mockito.anyString())) .thenReturn("content"); - Mockito.when(repositoryFileService.getCommit(AdocContentType.BLOG_ARTICLE, repositoryFile.lastCommitId())) + Mockito.when(repositoryFileService.getCommit(repositoryFile.lastCommitId())) .thenReturn(Optional.of(commit)); scheduledFileGenerator.generate();