diff --git a/build.gradle b/build.gradle index b0ba5b9..005f70f 100644 --- a/build.gradle +++ b/build.gradle @@ -1,12 +1,16 @@ plugins { id 'java' - id 'org.springframework.boot' version '3.0.1' + id 'org.springframework.boot' version '3.2.5' id 'io.spring.dependency-management' version '1.1.0' } group = 'io.github.tttol' version = "1.1.6" -sourceCompatibility = '17' + +java { + sourceCompatibility = JavaVersion.VERSION_21 + targetCompatibility = JavaVersion.VERSION_21 +} configurations { compileOnly { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 070cb70..17655d0 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/java/io/github/tttol/mrls/config/RestClientConfig.java b/src/main/java/io/github/tttol/mrls/config/RestClientConfig.java new file mode 100644 index 0000000..8ce8ea2 --- /dev/null +++ b/src/main/java/io/github/tttol/mrls/config/RestClientConfig.java @@ -0,0 +1,43 @@ +package io.github.tttol.mrls.config; + +import io.micrometer.common.util.StringUtils; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClients; +import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.util.Timeout; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.web.client.RestClient; + +import java.net.InetSocketAddress; +import java.net.Proxy; + +@Configuration +public class RestClientConfig { + + @Value("${app.proxy.host}") + private String proxyHost; + + @Value("${app.proxy.port}") + private int proxyPort; + + @Bean + public RestClient restClient() { + if (StringUtils.isBlank(proxyHost)) { + return RestClient.create(); + } + + var httpClient = HttpClients.custom() + .setProxy(new HttpHost(proxyHost, proxyPort)) + .build(); + var httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); + + return RestClient.builder() + .requestFactory(httpComponentsClientHttpRequestFactory) + .build(); + } +} diff --git a/src/main/java/io/github/tttol/mrls/external/GitLabApiExecutor.java b/src/main/java/io/github/tttol/mrls/external/GitLabApiExecutor.java index ffd9e69..e997062 100644 --- a/src/main/java/io/github/tttol/mrls/external/GitLabApiExecutor.java +++ b/src/main/java/io/github/tttol/mrls/external/GitLabApiExecutor.java @@ -1,26 +1,27 @@ package io.github.tttol.mrls.external; -import io.github.tttol.mrls.dto.GitLabMergeRequestApiResponseDto; -import io.github.tttol.mrls.exception.GitLabApiException; import java.net.URI; import java.util.Arrays; import java.util.List; import java.util.Objects; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; + import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.MediaType; -import org.springframework.http.RequestEntity; import org.springframework.stereotype.Component; -import org.springframework.web.client.RestTemplate; +import org.springframework.web.client.RestClient; + +import io.github.tttol.mrls.dto.GitLabMergeRequestApiResponseDto; +import io.github.tttol.mrls.exception.GitLabApiException; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; @Component @RequiredArgsConstructor @Slf4j public class GitLabApiExecutor { - private final RestTemplate restTemplate; + private final RestClient restClient; @Value("${app.gitlab.api.endpoint}") private String endpoint; @@ -28,21 +29,21 @@ public class GitLabApiExecutor { private String projectAccessToken; public List getMergeRequests(String accessToken) { - // TODO 例外処理 try { - var requestEntity = RequestEntity - .get(new URI(endpoint)) + var token = StringUtils.isNotBlank(accessToken) ? accessToken : projectAccessToken; + var responseEntity = restClient.get() + .uri(new URI(endpoint)) .accept(MediaType.APPLICATION_JSON) - .header("Authorization", - "Bearer %s".formatted(StringUtils.isNotBlank(accessToken) ? - accessToken : projectAccessToken)) - .build(); - log.info("api url -> {}", requestEntity.getUrl()); - var responseEntity = restTemplate.exchange(requestEntity, - GitLabMergeRequestApiResponseDto[].class); - log.debug("status code -> {}", responseEntity.getStatusCode()); - var body = responseEntity.getBody(); - return Objects.isNull(body) ? List.of() : Arrays.asList(body); + .header("Authorization", "Bearer %s".formatted(token)) + .retrieve() + .toEntity(GitLabMergeRequestApiResponseDto[].class); + + if (responseEntity != null) { + log.info("status code -> {}", responseEntity.getStatusCode()); + var body = responseEntity.getBody(); + return Objects.isNull(body) ? List.of() : Arrays.asList(body); + } + return List.of(); } catch (Exception e) { throw new GitLabApiException(e); } diff --git a/src/main/resources/application-env.yml b/src/main/resources/application-env.yml index 3344c18..661d3ca 100644 --- a/src/main/resources/application-env.yml +++ b/src/main/resources/application-env.yml @@ -1,4 +1,7 @@ app: + proxy: + host: + port: gitlab: project: id: diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index c507b62..1584992 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,4 +1,9 @@ +server: + port: 8081 app: + proxy: + host: ${PROXY_HOST} + port: ${PROXY_PORT} gitlab: host: ${GITLAB_HOST} project: