Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add proxy & update gradle, spring boot, jdk #32

Merged
merged 3 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle

name: Test and deploy

on: [push]

# on:
# push:
# branches: [ "main" ]
# pull_request:
# branches: [ "main" ]

# permissions:
# contents: read

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
java-version: '17'
java-version: '21'
distribution: 'corretto'
- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b
Expand Down
10 changes: 7 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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'
version = "1.1.7"

java {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}

configurations {
compileOnly {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions src/main/java/io/github/tttol/mrls/config/RestClientConfig.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
41 changes: 21 additions & 20 deletions src/main/java/io/github/tttol/mrls/external/GitLabApiExecutor.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
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;
@Value("${app.gitlab.project.accessToken}")
private String projectAccessToken;

public List<GitLabMergeRequestApiResponseDto> 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);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application-env.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
app:
proxy:
host:
port:
gitlab:
project:
id:
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
app:
proxy:
host: ${PROXY_HOST}
port: ${PROXY_PORT}
gitlab:
host: ${GITLAB_HOST}
project:
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/io/github/tttol/mrls/MrlsApplicationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest(properties = {"GITLAB_PROJECT_ID=dummy",
@SpringBootTest(properties = {
"PROXY_HOST=dummy.com",
"PROXY_PORT=80",
"GITLAB_PROJECT_ID=dummy",
"GITLAB_ACCESS_TOKEN=dummy",
"GITLAB_HOST=dummy"})
class MrlsApplicationTests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

@SpringBootTest(properties = {"GITLAB_PROJECT_ID=dummy",
@SpringBootTest(properties = {
"PROXY_HOST=dummy.com",
"PROXY_PORT=80",
"GITLAB_PROJECT_ID=dummy",
"GITLAB_ACCESS_TOKEN=dummy"})
public class MrListControllerTest {

Expand Down
Loading