diff --git a/pom.xml b/pom.xml index 78af1157..e6f97f76 100644 --- a/pom.xml +++ b/pom.xml @@ -356,6 +356,13 @@ 3.4.3 + + + org.eclipse.jgit + org.eclipse.jgit + 6.7.0.202309050840-r + + @@ -366,7 +373,7 @@ org.apache.maven.plugins maven-checkstyle-plugin - 3.2.0 + 3.3.1 check diff --git a/src/main/java/com/softserve/teachua/controller/version/VersionController.java b/src/main/java/com/softserve/teachua/controller/version/VersionController.java new file mode 100644 index 00000000..e2cf4595 --- /dev/null +++ b/src/main/java/com/softserve/teachua/controller/version/VersionController.java @@ -0,0 +1,35 @@ +package com.softserve.teachua.controller.version; + +import com.softserve.teachua.controller.marker.Api; +import com.softserve.teachua.dto.version.VersionDto; +import com.softserve.teachua.service.VersionService; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@Slf4j +@RestController +@Tag(name = "version", description = "the build version API") +@SecurityRequirement(name = "api") +public class VersionController implements Api { + private VersionService versionService; + + @Autowired + public VersionController(VersionService versionService) { + this.versionService = versionService; + } + + /** + * Use this endpoint to get the version information. The controller returns {@code VersionDto}. + * + * @return {@code VersionDto}. + */ + @GetMapping("/version") + public VersionDto getDate() { + log.debug("VersionController start"); + return versionService.getVersion(); + } +} diff --git a/src/main/java/com/softserve/teachua/dto/version/VersionDto.java b/src/main/java/com/softserve/teachua/dto/version/VersionDto.java new file mode 100644 index 00000000..0a6278ee --- /dev/null +++ b/src/main/java/com/softserve/teachua/dto/version/VersionDto.java @@ -0,0 +1,32 @@ +package com.softserve.teachua.dto.version; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.With; + +@AllArgsConstructor +@NoArgsConstructor +@Data +@Builder +@With +public class VersionDto { + @NotNull + @NotEmpty + @NotBlank(message = "commitNumber cannot be empty") + private String backendCommitNumber; + + @NotNull + @NotEmpty + @NotBlank(message = "commitDate cannot be empty") + private String backendCommitDate; + + @NotNull + @NotEmpty + @NotBlank(message = "buildDate cannot be empty") + private String buildDate; +} diff --git a/src/main/java/com/softserve/teachua/dto/version/VersionEnum.java b/src/main/java/com/softserve/teachua/dto/version/VersionEnum.java new file mode 100644 index 00000000..26ee38a9 --- /dev/null +++ b/src/main/java/com/softserve/teachua/dto/version/VersionEnum.java @@ -0,0 +1,24 @@ +package com.softserve.teachua.dto.version; + +public enum VersionEnum { + BACKEND_COMMIT_NUMBER("backendCommitNumber"), + BACKEND_COMMIT_DATE("backendCommitDate"), + BUILD_DATE("buildDate"); + + private String fieldName; + + private VersionEnum(String fieldName) { + this.fieldName = fieldName; + } + + public String getFieldName() { + return fieldName; + } + + @Override + public String toString() { + return "VersionEnum{" + + "fieldName='" + fieldName + '\'' + + '}'; + } +} diff --git a/src/main/java/com/softserve/teachua/service/PropertiesService.java b/src/main/java/com/softserve/teachua/service/PropertiesService.java new file mode 100644 index 00000000..f2990461 --- /dev/null +++ b/src/main/java/com/softserve/teachua/service/PropertiesService.java @@ -0,0 +1,9 @@ +package com.softserve.teachua.service; + +import java.util.Map; + +public interface PropertiesService { + Map readProperties(String fileName); + + void writeProperties(String fileName, String commitName, int commitDateTime); +} diff --git a/src/main/java/com/softserve/teachua/service/VersionService.java b/src/main/java/com/softserve/teachua/service/VersionService.java new file mode 100644 index 00000000..87c769ad --- /dev/null +++ b/src/main/java/com/softserve/teachua/service/VersionService.java @@ -0,0 +1,12 @@ +package com.softserve.teachua.service; + +import com.softserve.teachua.dto.version.VersionDto; +import org.eclipse.jgit.revwalk.RevCommit; + +public interface VersionService { + VersionDto getVersion(); + + void setVersion(); + + RevCommit getCommit(); +} diff --git a/src/main/java/com/softserve/teachua/service/impl/PropertiesServiceImpl.java b/src/main/java/com/softserve/teachua/service/impl/PropertiesServiceImpl.java new file mode 100644 index 00000000..f08b7b99 --- /dev/null +++ b/src/main/java/com/softserve/teachua/service/impl/PropertiesServiceImpl.java @@ -0,0 +1,73 @@ +package com.softserve.teachua.service.impl; + +import com.softserve.teachua.dto.version.VersionEnum; +import com.softserve.teachua.service.PropertiesService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +@Slf4j +@Service +public class PropertiesServiceImpl implements PropertiesService { + public static final String PATH_SEPARATOR = "/"; + public static final String PATH_MAIN_CLASS = "/../classes/"; + public static final String LOCAL_DATE_TIME_TEMPLATE = "HH:mm:ss dd.MM.yyyy"; + public static final String LOCAL_DATE_TEMPLATE = "dd.MM.yyyy"; + private static final String BUILD_DATE = " Build Date is "; + + public Map readProperties(String fileName) { + Map propertiesMap = new HashMap<>(); + Properties appProps = new Properties(); + try (FileInputStream fileInputStream = new FileInputStream(getFullPath(fileName))) { + appProps.load(fileInputStream); + for (Map.Entry entry : appProps.entrySet()) { + propertiesMap.put((String) entry.getKey(), (String) entry.getValue()); + } + log.debug("Map = " + propertiesMap); + } catch (Exception e) { + throw new RuntimeException(e); + } + return propertiesMap; + } + + public void writeProperties(String fileName, String commitName, int commitDateTime) { + DateTimeFormatter formatter = DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_TEMPLATE); + LocalDateTime localDateTime = LocalDateTime.now(); + LocalDateTime commitLocalDateTime = LocalDateTime.ofEpochSecond(commitDateTime, + 0, ZoneOffset.ofHours(2)); + String commitDate = commitLocalDateTime.format(DateTimeFormatter.ofPattern(LOCAL_DATE_TEMPLATE)); + log.debug("commitDate = " + commitDate); + + String currentDateTime = localDateTime.format(formatter); + Properties appProps = new Properties(); + appProps.setProperty(VersionEnum.BACKEND_COMMIT_NUMBER.getFieldName(), commitName); + appProps.setProperty(VersionEnum.BACKEND_COMMIT_DATE.getFieldName(), commitDate); + appProps.setProperty(VersionEnum.BUILD_DATE.getFieldName(), currentDateTime); + + try (FileOutputStream fileOutputStream = new FileOutputStream(getClassPath() + fileName)) { + appProps.store(fileOutputStream, BUILD_DATE + currentDateTime); + } catch (Exception e) { + throw new RuntimeException(e); + } + log.info("writeProperties = " + appProps + " saved"); + } + + private String getFullPath(String fileName) { + String fullPath = this.getClass().getResource(PATH_SEPARATOR + fileName).getPath(); + log.debug("fullPath = " + fullPath); + return fullPath; + } + + private String getClassPath() { + String fullPath = PropertiesServiceImpl.class.getResource(PATH_MAIN_CLASS).getPath(); + log.debug("fullPath = " + fullPath); + return fullPath; + } +} diff --git a/src/main/java/com/softserve/teachua/service/impl/VersionServiceImpl.java b/src/main/java/com/softserve/teachua/service/impl/VersionServiceImpl.java new file mode 100644 index 00000000..c2d45a29 --- /dev/null +++ b/src/main/java/com/softserve/teachua/service/impl/VersionServiceImpl.java @@ -0,0 +1,62 @@ +package com.softserve.teachua.service.impl; + +import com.softserve.teachua.dto.version.VersionDto; +import com.softserve.teachua.dto.version.VersionEnum; +import com.softserve.teachua.service.PropertiesService; +import com.softserve.teachua.service.VersionService; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revwalk.RevWalk; +import org.eclipse.jgit.storage.file.FileRepositoryBuilder; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import java.io.File; +import java.io.IOException; +import java.util.Map; + +@Slf4j +@Service +public class VersionServiceImpl implements VersionService { + private static final String VERSION_FILE_NAME = "version.properties"; + private static final String BACKAND_PATH = "./.git"; + private static final String BRANCH_HEAD = "HEAD"; + private PropertiesService propertiesService; + + @Autowired + public VersionServiceImpl(PropertiesService propertiesService) { + this.propertiesService = propertiesService; + } + + public VersionDto getVersion() { + Map versionProperties = propertiesService.readProperties(VERSION_FILE_NAME); + VersionDto versionDto = VersionDto.builder() + .backendCommitNumber(versionProperties.get(VersionEnum.BACKEND_COMMIT_NUMBER.getFieldName())) + .backendCommitDate(versionProperties.get(VersionEnum.BACKEND_COMMIT_DATE.getFieldName())) + .buildDate(versionProperties.get(VersionEnum.BUILD_DATE.getFieldName()).replace("\\","")) + .build(); + log.debug("VersionService = " + versionDto); + return versionDto; + } + + public void setVersion() { + RevCommit commit = getCommit(); + propertiesService.writeProperties(VERSION_FILE_NAME, commit.getName(), commit.getCommitTime()); + } + + public RevCommit getCommit() { + RevCommit commit = null; + try { + Repository existingRepo = new FileRepositoryBuilder() + .setGitDir(new File(BACKAND_PATH)) + .build(); + ObjectId head = existingRepo.resolve(BRANCH_HEAD); + RevWalk walk = new RevWalk(existingRepo); + commit = walk.parseCommit(head); + } catch (IOException e) { + throw new RuntimeException(e); + } + return commit; + } +} diff --git a/src/test/java/com/softserve/teachua/config/VersionCreateTest.java b/src/test/java/com/softserve/teachua/config/VersionCreateTest.java new file mode 100644 index 00000000..e5b2589c --- /dev/null +++ b/src/test/java/com/softserve/teachua/config/VersionCreateTest.java @@ -0,0 +1,15 @@ +package com.softserve.teachua.config; + +import com.softserve.teachua.service.VersionService; +import com.softserve.teachua.service.impl.PropertiesServiceImpl; +import com.softserve.teachua.service.impl.VersionServiceImpl; +import org.junit.jupiter.api.*; + +public class VersionCreateTest { + + @Test + public void createVersion() { + VersionService versionService = new VersionServiceImpl(new PropertiesServiceImpl()); + versionService.setVersion(); + } +}