Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class GitProjectsController extends AbstractLogger {
private final GitDetailsService gitDetailsService;

protected static final String IRONOC_GIT_USER = "conorheffron";
private static final String OPEN_ISSUE_STATE = "open";

// Cache for issue counts: key is "username/repo"
private final ConcurrentHashMap<String, Integer> issueCountCache = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -104,7 +105,7 @@ private ResponseEntity<List<RepositoryDetailDomain>> getReposSortedAndWithIssueC
Integer cachedCount = issueCountCache.get(cacheKey);
if (cachedCount == null) {
List<RepositoryIssueDto> issues = gitDetailsService.getIssues(userId, domain.getName(), false);
cachedCount = issues != null ? issues.size() : 0;
cachedCount = countOpenIssues(issues);
issueCountCache.put(cacheKey, cachedCount);
}
domain.setIssueCount(cachedCount);
Expand Down Expand Up @@ -152,6 +153,18 @@ private List<String> sanitizeValues(String... values) {
return List.of(sanitizedValueUserId, sanitizedValueRepo);
}

private int countOpenIssues(List<RepositoryIssueDto> issues) {
if (issues == null) {
return 0;
}
return (int) issues.stream()
.filter(Objects::nonNull)
.map(RepositoryIssueDto::getState)
.filter(Objects::nonNull)
.filter(OPEN_ISSUE_STATE::equalsIgnoreCase)
.count();
}

private String sanitizeValue(String value) {
// trim leading and trailing whitespace
String sanitizedValue = value.trim();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import module java.base;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.ironoc.portfolio.dto.RepositoryDetailDto;
import net.ironoc.portfolio.dto.RepositoryIssueDto;
Expand Down Expand Up @@ -169,6 +170,52 @@ public void test_getReposByUsernameReqParam_success() throws Exception {
assertThat(response.getContentAsString(), is(JSON_RESPONSE));
}

@Test
public void test_getReposByUsernameReqParam_counts_only_open_issues_and_sorts_by_open_count() throws Exception {
// given
List<RepositoryDetailDto> dtos = List.of(
RepositoryDetailDto.builder()
.name("repo-open-1")
.fullName("conorheffron/repo-open-1")
.htmlUrl("https://github.com/conorheffron/repo-open-1")
.build(),
RepositoryDetailDto.builder()
.name("repo-open-2")
.fullName("conorheffron/repo-open-2")
.htmlUrl("https://github.com/conorheffron/repo-open-2")
.build()
);

when(gitDetailsServiceMock.getRepoDetails("conorheffron", false)).thenReturn(dtos);
when(gitDetailsServiceMock.mapRepositoriesToResponse(anyList()))
.thenReturn(new GitDetailsService(null, null, null, null, null).mapRepositoriesToResponse(dtos));
when(gitDetailsServiceMock.getIssues("conorheffron", "repo-open-1", false))
.thenReturn(List.of(
RepositoryIssueDto.builder().state("open").build(),
RepositoryIssueDto.builder().state("closed").build(),
RepositoryIssueDto.builder().state("CLOSED").build()
));
when(gitDetailsServiceMock.getIssues("conorheffron", "repo-open-2", false))
.thenReturn(List.of(
RepositoryIssueDto.builder().state("OPEN").build(),
RepositoryIssueDto.builder().state("open").build(),
RepositoryIssueDto.builder().state("closed").build()
));

// when
MockHttpServletResponse response = mockMvc.perform(get("/api/get-repo-detail?username=conorheffron")
.accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andReturn().getResponse();

// then
assertThat(response.getStatus(), is(HttpStatus.OK.value()));
JsonNode responseJson = objectMapper.readTree(response.getContentAsString());
assertThat(responseJson.get(0).get("name").asText(), is("repo-open-2"));
assertThat(responseJson.get(0).get("issueCount").asInt(), is(2));
assertThat(responseJson.get(1).get("name").asText(), is("repo-open-1"));
assertThat(responseJson.get(1).get("issueCount").asInt(), is(1));
}

@Test
public void test_getReposByUsernameReqParam_empty_response_success() throws Exception {
// when
Expand Down
Loading