generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
da8f256
commit 83385eb
Showing
9 changed files
with
270 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/com/softserve/teachua/controller/version/VersionController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
src/main/java/com/softserve/teachua/dto/version/VersionDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
src/main/java/com/softserve/teachua/dto/version/VersionEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + '\'' | ||
+ '}'; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/softserve/teachua/service/PropertiesService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
src/main/java/com/softserve/teachua/service/VersionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/com/softserve/teachua/service/impl/PropertiesServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/com/softserve/teachua/service/impl/VersionServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
src/test/java/com/softserve/teachua/config/VersionCreateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |