From 82b81557c573643cef35c28b7ad70091d75eab14 Mon Sep 17 00:00:00 2001 From: Cyril Dubuisson Date: Sat, 7 Mar 2020 16:25:12 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20:=20add=20api=20service=20for=20bui?= =?UTF-8?q?ld=20infos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/app/shared/api/build-infos-api.js | 6 ++ src/main/client/app/shared/api/index.js | 1 + .../controller/BuildInfoRestController.kt | 26 ++++++ .../gaia/config/security/SecurityConfig.kt | 1 + .../controller/BuildInfoRestControllerTest.kt | 85 +++++++++++++++++++ vue.config.js | 2 +- 6 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/main/client/app/shared/api/build-infos-api.js create mode 100644 src/main/java/io/codeka/gaia/client/controller/BuildInfoRestController.kt create mode 100644 src/test/java/io/codeka/gaia/client/controller/BuildInfoRestControllerTest.kt diff --git a/src/main/client/app/shared/api/build-infos-api.js b/src/main/client/app/shared/api/build-infos-api.js new file mode 100644 index 000000000..db2e39102 --- /dev/null +++ b/src/main/client/app/shared/api/build-infos-api.js @@ -0,0 +1,6 @@ +import axios from 'axios'; + +export const getBuildInfo = async () => { + const resp = await axios.get('/build-info'); + return resp.data; +}; diff --git a/src/main/client/app/shared/api/index.js b/src/main/client/app/shared/api/index.js index 95bcf98d9..1d54b08c0 100644 --- a/src/main/client/app/shared/api/index.js +++ b/src/main/client/app/shared/api/index.js @@ -5,3 +5,4 @@ export { getAuthorities, doLogout, } from './authentication-api'; +export { getBuildInfo } from './build-infos-api'; diff --git a/src/main/java/io/codeka/gaia/client/controller/BuildInfoRestController.kt b/src/main/java/io/codeka/gaia/client/controller/BuildInfoRestController.kt new file mode 100644 index 000000000..e6b846146 --- /dev/null +++ b/src/main/java/io/codeka/gaia/client/controller/BuildInfoRestController.kt @@ -0,0 +1,26 @@ +package io.codeka.gaia.client.controller + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.info.BuildProperties +import org.springframework.boot.info.GitProperties +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController + +@RestController +@RequestMapping("/build-info") +class BuildInfoRestController constructor( + @Autowired(required = false) var buildProperties: BuildProperties? = null, + @Autowired(required = false) var gitProperties: GitProperties? = null) { + + @GetMapping + fun infos(): Map? = + if (buildProperties == null && gitProperties == null) { + null + } else { + mapOf( + "version" to buildProperties?.version, + "commitId" to gitProperties?.shortCommitId) + } + +} diff --git a/src/main/java/io/codeka/gaia/config/security/SecurityConfig.kt b/src/main/java/io/codeka/gaia/config/security/SecurityConfig.kt index 35fb1f416..4da447c11 100644 --- a/src/main/java/io/codeka/gaia/config/security/SecurityConfig.kt +++ b/src/main/java/io/codeka/gaia/config/security/SecurityConfig.kt @@ -70,6 +70,7 @@ class SecurityConfig constructor( .antMatchers("/auth/user").authenticated() .antMatchers("/auth/authorities").authenticated() .antMatchers("/auth/providers").permitAll() + .antMatchers("/build-info").permitAll() // @formatter:on } diff --git a/src/test/java/io/codeka/gaia/client/controller/BuildInfoRestControllerTest.kt b/src/test/java/io/codeka/gaia/client/controller/BuildInfoRestControllerTest.kt new file mode 100644 index 000000000..8e2145c88 --- /dev/null +++ b/src/test/java/io/codeka/gaia/client/controller/BuildInfoRestControllerTest.kt @@ -0,0 +1,85 @@ +package io.codeka.gaia.client.controller + +import io.codeka.gaia.test.whenever +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.mockito.InjectMocks +import org.mockito.Mock +import org.mockito.junit.jupiter.MockitoExtension +import org.springframework.boot.info.BuildProperties +import org.springframework.boot.info.GitProperties + +@ExtendWith(MockitoExtension::class) +class BuildInfoRestControllerTest { + + @Mock + lateinit var buildProperties: BuildProperties + + @Mock + lateinit var gitProperties: GitProperties + + @InjectMocks + lateinit var controller: BuildInfoRestController + + @Test + fun `infos() should return version`() { + // when + whenever(buildProperties.version).thenReturn("x.y.z") + val result = controller.infos(); + + // then + assertThat(result) + .isNotNull + .containsEntry("version", "x.y.z") + } + + @Test + fun `infos() should return null version if not available`() { + // when + controller.buildProperties = null + val result = controller.infos(); + + // then + assertThat(result) + .isNotNull + .containsEntry("version", null) + } + + @Test + fun `infos() should return commitId`() { + // when + whenever(gitProperties.shortCommitId).thenReturn("7d1d8a") + val result = controller.infos(); + + // then + assertThat(result) + .isNotNull + .containsEntry("commitId", "7d1d8a") + } + + @Test + fun `infos() should return null commitId if not available`() { + // when + controller.gitProperties = null + val result = controller.infos(); + + // then + assertThat(result) + .isNotNull + .containsEntry("commitId", null) + } + + @Test + fun `infos() should return nothing if no data`() { + // given + controller = BuildInfoRestController(null, null) + + // when + val result = controller.infos(); + + // then + assertThat(result).isNull() + } + +} diff --git a/vue.config.js b/vue.config.js index 10bdf124d..8ea9b0418 100644 --- a/vue.config.js +++ b/vue.config.js @@ -48,7 +48,7 @@ module.exports = { errors: true, }, proxy: { - '^/(api|auth)': { + '^/(api|auth|build-info)': { target: 'http://localhost:8080', ws: true, changeOrigin: false,