Skip to content

Commit

Permalink
state=openのMRに絞る、LocalDateTime -> OffsetDateTimeにリファクタ (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
tttol authored Feb 1, 2023
1 parent a3378c9 commit 6d4b03f
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.List;

@Data
Expand All @@ -30,22 +30,22 @@ public class GitLabMergeRequestApiResponseDto {
String state;

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
LocalDateTime createdAt;
OffsetDateTime createdAt;

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
LocalDateTime updatedAt;
OffsetDateTime updatedAt;

String mergedBy;

String mergeUser;

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
LocalDateTime mergedAt;
OffsetDateTime mergedAt;

String closedBy;

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
LocalDateTime closedAt;
OffsetDateTime closedAt;

String targetBranch;

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/io/github/tttol/mrls/form/MrDetailForm.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.tttol.mrls.form;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;

public record MrDetailForm(String title, String webUrl, UserForm author, LocalDateTime createdAt,
LocalDateTime updatedAt) {
public record MrDetailForm(String title, String webUrl, UserForm author, OffsetDateTime createdAt,
OffsetDateTime updatedAt) {
}
108 changes: 51 additions & 57 deletions src/main/java/io/github/tttol/mrls/service/MergeRequestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,69 @@
import io.github.tttol.mrls.form.MrDetailForm;
import io.github.tttol.mrls.form.MrInfoForm;
import io.github.tttol.mrls.form.UserForm;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.ZoneOffset;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class MergeRequestService {

private final GitLabApiExecutor gitLabApiExecutor;
private final GitLabApiExecutor gitLabApiExecutor;

public List<MrInfoForm> get(String accessToken) {
var mergeRequestInfoDtos = executeGitLabApi(accessToken);
return mergeRequestInfoDtos.stream()
.collect(Collectors.groupingBy(
e -> Objects.isNull(e.getAssignee()) ?
e.getAuthor().getId() : e.getAssignee().getId()
)
)
.values().stream().map(this::generateForm)
.sorted(Comparator.comparing(e -> e.assignee().id()))
.toList();
}
public List<MrInfoForm> get(final String accessToken) {
final var mergeRequestInfoDtos = executeGitLabApi(accessToken);
return mergeRequestInfoDtos.stream()
.collect(Collectors.groupingBy(
e -> Objects.isNull(e.getAssignee()) ?
e.getAuthor().getId() : e.getAssignee().getId()
)
)
.values().stream().map(this::generateForm)
.sorted(Comparator.comparing(e -> e.assignee().id()))
.toList();
}

private List<GitLabMergeRequestApiResponseDto> executeGitLabApi(String accessToken) {
return gitLabApiExecutor.getMergeRequests(accessToken);
}
private List<GitLabMergeRequestApiResponseDto> executeGitLabApi(final String accessToken) {
return gitLabApiExecutor.getMergeRequests(accessToken);
}

private MrInfoForm generateForm(List<GitLabMergeRequestApiResponseDto> responseDtos) {
var responseDto = responseDtos.stream().findAny().orElseThrow();
var assigneeForm = Optional.ofNullable(responseDto.getAssignee())
.map(assignee -> new UserForm(
assignee.getId(),
assignee.getUsername(),
assignee.getName(),
assignee.getState(),
assignee.getAvatarUrl(),
assignee.getWebUrl()
)
).orElse(UserForm.empty());
var linkForms = responseDtos.stream().map(e ->
new MrDetailForm(
e.getTitle(),
e.getWebUrl(),
Optional.ofNullable(e.getAuthor())
private MrInfoForm generateForm(final List<GitLabMergeRequestApiResponseDto> responseDtos) {
final var responseDto = responseDtos.stream().findAny().orElseThrow();
final var assigneeForm = Optional.ofNullable(responseDto.getAssignee())
.map(assignee -> new UserForm(
assignee.getId(),
assignee.getUsername(),
assignee.getName(),
assignee.getState(),
assignee.getAvatarUrl(),
assignee.getWebUrl()
)
)
.orElse(UserForm.empty()),
toJst(e.getCreatedAt()),
toJst(e.getUpdatedAt())
)).toList();
return new MrInfoForm(assigneeForm, linkForms, linkForms.size());
}

private LocalDateTime toJst(LocalDateTime utc) {
return ZonedDateTime.of(utc, ZoneId.of("UTC"))
.withZoneSameInstant(ZoneId.of("Asia/Tokyo")).toLocalDateTime();
}
assignee.getId(),
assignee.getUsername(),
assignee.getName(),
assignee.getState(),
assignee.getAvatarUrl(),
assignee.getWebUrl()
)
).orElse(UserForm.empty());
final var linkForms = responseDtos.stream().map(e ->
new MrDetailForm(
e.getTitle(),
e.getWebUrl(),
Optional.ofNullable(e.getAuthor())
.map(assignee -> new UserForm(
assignee.getId(),
assignee.getUsername(),
assignee.getName(),
assignee.getState(),
assignee.getAvatarUrl(),
assignee.getWebUrl()
)
)
.orElse(UserForm.empty()),
e.getCreatedAt().withOffsetSameInstant(ZoneOffset.of("+09:00")),
e.getUpdatedAt().withOffsetSameInstant(ZoneOffset.of("+09:00"))
)).toList();
return new MrInfoForm(assigneeForm, linkForms, linkForms.size());
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ app:
id: ${GITLAB_PROJECT_ID}
accessToken: ${GITLAB_ACCESS_TOKEN}
api:
endpoint: https://${app.gitlab.host}/api/v4/projects/${app.gitlab.project.id}/merge_requests
endpoint: https://${app.gitlab.host}/api/v4/projects/${app.gitlab.project.id}/merge_requests?state=opened
logging:
level:
root: INFO
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/fragment/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<body>
<footer th:fragment="footer">
<div class="footerBox">
<span class="appVer">version 1.0.0</span>
<span class="appVer">version 1.1.3</span>
</div>
</footer>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.List;

import static org.mockito.ArgumentMatchers.anyString;
Expand All @@ -36,13 +37,13 @@ public class MrListControllerTest {
public void init() {
mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();

var assignee = UserDto.builder()
final var assignee = UserDto.builder()
.id(1)
.username("assignee_username1")
.name("assignee_name1")
.state("active")
.build();
var mergeRequestInfoDtos = List.of(
final var mergeRequestInfoDtos = List.of(
GitLabMergeRequestApiResponseDto.builder()
.assignee(assignee)
.author(UserDto.builder()
Expand All @@ -53,8 +54,8 @@ public void init() {
.build())
.webUrl("url1")
.title("title1")
.createdAt(LocalDateTime.of(1970, 1, 1, 0, 0, 0))
.updatedAt(LocalDateTime.of(2000, 1, 1, 0, 0, 0))
.createdAt(OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC))
.updatedAt(OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC))
.build(),
GitLabMergeRequestApiResponseDto.builder()
.assignee(assignee)
Expand All @@ -66,8 +67,8 @@ public void init() {
.build())
.webUrl("url2")
.title("title2")
.createdAt(LocalDateTime.of(1970, 1, 1, 0, 0, 0))
.updatedAt(LocalDateTime.of(2000, 1, 1, 0, 0, 0))
.createdAt(OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC))
.updatedAt(OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC))
.build(),
GitLabMergeRequestApiResponseDto.builder()
.assignee(assignee)
Expand All @@ -79,8 +80,8 @@ public void init() {
.build())
.webUrl("url3")
.title("title3")
.createdAt(LocalDateTime.of(1970, 1, 1, 0, 0, 0))
.updatedAt(LocalDateTime.of(2000, 1, 1, 0, 0, 0))
.createdAt(OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC))
.updatedAt(OffsetDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC))
.build()
);
doReturn(mergeRequestInfoDtos).when(gitLabApiExecutor).getMergeRequests(anyString());
Expand Down
Loading

0 comments on commit 6d4b03f

Please sign in to comment.