Skip to content

Commit

Permalink
Version added
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavGarasym committed Nov 22, 2023
1 parent da8f256 commit 83385eb
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,13 @@
<version>3.4.3</version>
</dependency>

<!-- - - - - - Git - - - - - -->
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>6.7.0.202309050840-r</version>
</dependency>

</dependencies>


Expand All @@ -366,7 +373,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.2.0</version>
<version>3.3.1</version>
<executions>
<execution>
<id>check</id>
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/softserve/teachua/dto/version/VersionDto.java
Original file line number Diff line number Diff line change
@@ -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;
}
24 changes: 24 additions & 0 deletions src/main/java/com/softserve/teachua/dto/version/VersionEnum.java
Original file line number Diff line number Diff line change
@@ -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 + '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.softserve.teachua.service;

import java.util.Map;

public interface PropertiesService {
Map<String, String> readProperties(String fileName);

void writeProperties(String fileName, String commitName, int commitDateTime);
}
12 changes: 12 additions & 0 deletions src/main/java/com/softserve/teachua/service/VersionService.java
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
@@ -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<String, String> readProperties(String fileName) {
Map<String, String> propertiesMap = new HashMap<>();
Properties appProps = new Properties();
try (FileInputStream fileInputStream = new FileInputStream(getFullPath(fileName))) {
appProps.load(fileInputStream);
for (Map.Entry<Object, Object> entry : appProps.entrySet()) {
propertiesMap.put((String) entry.getKey(), (String) entry.getValue());
}
log.debug("Map<String, String> = " + 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;
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> 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;
}
}
15 changes: 15 additions & 0 deletions src/test/java/com/softserve/teachua/config/VersionCreateTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 83385eb

Please sign in to comment.