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

Fix null-assignee bug #34

Merged
merged 1 commit into from
Dec 25, 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
24 changes: 13 additions & 11 deletions src/main/java/io/github/tttol/mrls/service/MergeRequestService.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
package io.github.tttol.mrls.service;

import io.github.tttol.mrls.dto.GitLabMergeRequestApiResponseDto;
import io.github.tttol.mrls.external.GitLabApiExecutor;
import io.github.tttol.mrls.form.MrDetailForm;
import io.github.tttol.mrls.form.MrInfoForm;
import io.github.tttol.mrls.form.UserForm;
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 org.springframework.stereotype.Service;

import io.github.tttol.mrls.dto.GitLabMergeRequestApiResponseDto;
import io.github.tttol.mrls.external.GitLabApiExecutor;
import io.github.tttol.mrls.form.MrDetailForm;
import io.github.tttol.mrls.form.MrInfoForm;
import io.github.tttol.mrls.form.UserForm;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class MergeRequestService {

private final GitLabApiExecutor gitLabApiExecutor;
private final int NONE_ASSIGNEE = -1;

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()
NONE_ASSIGNEE : e.getAssignee().getId()
)
)
.values().stream().map(this::generateForm)
.values().stream().map(this::generateMrInfoForm)
.sorted(Comparator.comparing(e -> e.assignee().id()))
.toList();
}
Expand All @@ -38,7 +40,7 @@ private List<GitLabMergeRequestApiResponseDto> executeGitLabApi(final String acc
return gitLabApiExecutor.getMergeRequests(accessToken);
}

private MrInfoForm generateForm(final List<GitLabMergeRequestApiResponseDto> responseDtos) {
private MrInfoForm generateMrInfoForm(final List<GitLabMergeRequestApiResponseDto> responseDtos) {
final var responseDto = responseDtos.stream().findAny().orElseThrow();
final var assigneeForm = Optional.ofNullable(responseDto.getAssignee())
.map(assignee -> new UserForm(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package io.github.tttol.mrls.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;

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

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import io.github.tttol.mrls.dto.GitLabMergeRequestApiResponseDto;
import io.github.tttol.mrls.dto.UserDto;
import io.github.tttol.mrls.external.GitLabApiExecutor;
import io.github.tttol.mrls.form.MrDetailForm;
import io.github.tttol.mrls.form.MrInfoForm;
import io.github.tttol.mrls.form.UserForm;
import org.junit.jupiter.api.*;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

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

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;

public class MergeRequestServiceTest {

Expand Down Expand Up @@ -164,25 +169,40 @@ void formTest() {
@Test
@DisplayName("MRのAssigneeが未指定の場合")
void noAssigneeTest() {
final var assignee = UserDto.builder()
.id(11)
.username("author_username11")
.name("author_name11")
.state("active")
.build();
final var mergeRequestInfoDtos = List.of(
// authors are same
// assignee is null
GitLabMergeRequestApiResponseDto.builder()
.assignee(null)
.author(UserDto.builder()
.id(11)
.username("author_username11")
.name("author_name11")
.state("active")
.build())
.author(assignee)
.webUrl("url1")
.title("title1")
.upvotes(0)
.labels(List.of())
.createdAt(OffsetDateTime.of(1970, 1, 1, 9, 0, 0, 0, ZoneOffset.of("+09:00")))
.updatedAt(OffsetDateTime.of(2000, 1, 1, 9, 0, 0, 0, ZoneOffset.of("+09:00")))
.build(),
// assignee is author_username11
GitLabMergeRequestApiResponseDto.builder()
.assignee(assignee)
.author(assignee)
.webUrl("url2")
.title("title2")
.upvotes(0)
.labels(List.of())
.createdAt(OffsetDateTime.of(1970, 1, 1, 9, 0, 0, 0, ZoneOffset.of("+09:00")))
.updatedAt(OffsetDateTime.of(2000, 1, 1, 9, 0, 0, 0, ZoneOffset.of("+09:00")))
.build()
);
doReturn(mergeRequestInfoDtos).when(gitLabApiExecutor).getMergeRequests(anyString());
final var expected = List.of(new MrInfoForm(
final var expected = List.of(
new MrInfoForm(
UserForm.empty(),
List.of(
new MrDetailForm(
Expand All @@ -196,7 +216,23 @@ void noAssigneeTest() {
)
),
1
));
),
new MrInfoForm(
new UserForm(11, "author_username11", "author_name11", "active", null, null),
List.of(
new MrDetailForm(
"title2",
"url2",
new UserForm(11, "author_username11", "author_name11", "active", null, null),
0,
List.of(),
OffsetDateTime.of(1970, 1, 1, 9, 0, 0, 0, ZoneOffset.of("+09:00")),
OffsetDateTime.of(2000, 1, 1, 9, 0, 0, 0, ZoneOffset.of("+09:00"))
)
),
1
)
);

final var actual = mergeRequestService.get(anyString());

Expand Down
Loading