From 529b7aebc6f7bf0ab34b69d903dd7639037b9ef5 Mon Sep 17 00:00:00 2001 From: Miquel de Domingo Date: Wed, 14 Dec 2022 22:58:00 +0100 Subject: [PATCH 1/3] refactor: replace `for:each` for class methods We currently had many nested for each in order to loop over the different entities and construct our organization. It had reached a point in which it was really nested, lots of indentation and it was not sustaniable/scalable. --- .../controller/UserOptionController.java | 133 +++++++++++------- 1 file changed, 84 insertions(+), 49 deletions(-) diff --git a/src/main/java/io/pakland/mdas/githubstats/infrastructure/shell/controller/UserOptionController.java b/src/main/java/io/pakland/mdas/githubstats/infrastructure/shell/controller/UserOptionController.java index 7330e082..40b0f02e 100644 --- a/src/main/java/io/pakland/mdas/githubstats/infrastructure/shell/controller/UserOptionController.java +++ b/src/main/java/io/pakland/mdas/githubstats/infrastructure/shell/controller/UserOptionController.java @@ -7,19 +7,17 @@ import io.pakland.mdas.githubstats.domain.repository.*; import io.pakland.mdas.githubstats.infrastructure.github.repository.*; import io.pakland.mdas.githubstats.infrastructure.shell.model.UserOptionRequest; +import java.util.List; import lombok.NoArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; -import java.util.List; - @Component @NoArgsConstructor public class UserOptionController { Logger logger = LoggerFactory.getLogger(UserOptionController.class); - private UserOptionRequest userOptionRequest; private OrganizationExternalRepository organizationExternalRepository; private TeamExternalRepository teamExternalRepository; private UserExternalRepository userExternalRepository; @@ -28,14 +26,15 @@ public class UserOptionController { private CommitExternalRepository commitExternalRepository; public UserOptionController(UserOptionRequest userOptionRequest) { - this.userOptionRequest = userOptionRequest; WebClientConfiguration webClientConfiguration = new WebClientConfiguration( - "https://api.github.com", userOptionRequest.getApiKey()); - this.organizationExternalRepository = new OrganizationGitHubRepository(webClientConfiguration); + "https://api.github.com", userOptionRequest.getApiKey()); + this.organizationExternalRepository = new OrganizationGitHubRepository( + webClientConfiguration); this.teamExternalRepository = new TeamGitHubRepository(webClientConfiguration); this.userExternalRepository = new UserGitHubRepository(webClientConfiguration); this.repositoryExternalRepository = new RepositoryGitHubRepository(webClientConfiguration); - this.pullRequestExternalRepository = new PullRequestGitHubRepository(webClientConfiguration); + this.pullRequestExternalRepository = new PullRequestGitHubRepository( + webClientConfiguration); this.commitExternalRepository = new CommitGitHubRepository(webClientConfiguration); } @@ -44,54 +43,90 @@ public void execute() { // TODO: If the execution succeeds, we should make an entry to the historic_queries table. // Fetch the API key's available organizations. List organizationList = new FetchAvailableOrganizations( - this.organizationExternalRepository) - .execute(); - // Start building the github-stats relational schema. - for (Organization organization : organizationList) { - // Fetch the teams belonging to the available organization. - List teamList = new FetchTeamsFromOrganization(teamExternalRepository) - .execute(organization.getLogin()); + this.organizationExternalRepository) + .execute(); + organizationList.forEach(this::fetchTeamsFromOrganization); + } catch (HttpException e) { + throw new RuntimeException(e); + } + } + + private void fetchTeamsFromOrganization(Organization organization) { + try { + List teamList = new FetchTeamsFromOrganization(teamExternalRepository) + .execute(organization.getLogin()); + teamList.forEach(team -> { + this.fetchRepositoriesFromTeam(organization, team); + this.fetchUsersFromTeam(organization, team); + }); + } catch (HttpException e) { + throw new RuntimeException(e); + } + + } + + private void fetchRepositoriesFromTeam(Organization organization, Team team) { + organization.addTeam(team); + try { + // Fetch the repositories for each team. + List repositoryList = new FetchRepositoriesFromTeam( + repositoryExternalRepository) + .execute(organization.getLogin(), team.getSlug()); + // Add the team to the repository + repositoryList.forEach(repository -> { + repository.setTeam(team); + this.fetchPullRequestsFromRepository(team, repository); + }); + } catch (HttpException e) { + throw new RuntimeException(e); + } + } - for (Team team : teamList) { - organization.addTeam(team); - // Fetch the members of each team. - List userList = new FetchUsersFromTeam(userExternalRepository) - .execute(organization.getLogin(), team.getSlug()); - // Fetch the repositories for each team. - List repositoryList = new FetchRepositoriesFromTeam( - repositoryExternalRepository) - .execute(organization.getLogin(), team.getSlug()); - // Add the team to the repository - repositoryList.forEach(r -> r.setTeam(team)); + private void fetchUsersFromTeam(Organization organization, Team team) { + try { + // Fetch the members of each team. + List userList = new FetchUsersFromTeam(userExternalRepository) + .execute(organization.getLogin(), team.getSlug()); + + team.setUsers(userList); + } catch (HttpException e) { + throw new RuntimeException(e); + } + } - for (Repository repository : repositoryList) { - team.addRepository(repository); - // Fetch pull requests from each team. - List pullRequestList = new FetchPullRequestsFromRepository( - pullRequestExternalRepository) - .execute(repository.getOwnerLogin(), repository.getName()); + private void fetchPullRequestsFromRepository(Team team, Repository repository) { + team.addRepository(repository); + try { + // Fetch pull requests from each team. + List pullRequestList = new FetchPullRequestsFromRepository( + pullRequestExternalRepository) + .execute(repository.getOwnerLogin(), repository.getName()); - for (PullRequest pullRequest : pullRequestList) { - // Add the repository to the pull request - pullRequest.setRepository(repository); - /* - TODO: if the user of the PR belongs to the team, increment the prs executed inside the team, - TODO: else increment the prs executed outside the team. - */ - // TODO: Save for later calculate the additions, deletions and commit num. from PR aggregation. - List commitList = new FetchCommitsFromPullRequest(commitExternalRepository) - .execute(repository.getOwnerLogin(), repository.getName(), pullRequest.getNumber()); - for (Commit commit : commitList) { - // TODO: Fetch PR reviews. + pullRequestList.forEach(pullRequest -> this.fetchCommitsFromPullRequest(repository, pullRequest)); + repository.setPullRequests(pullRequestList); + } catch (HttpException e) { + throw new RuntimeException(e); + } + } - } - } - repository.setPullRequests(pullRequestList); - } + private void fetchCommitsFromPullRequest(Repository repository, PullRequest pullRequest) { + // Add the repository to the pull request + pullRequest.setRepository(repository); + /* + TODO: if the user of the PR belongs to the team, increment the prs executed inside the team + TODO: else increment the prs executed outside the team + TODO: Save for later calculate the additions, deletions and commit num. from PR aggregation + */ + // + try { + List commitList = new FetchCommitsFromPullRequest( + commitExternalRepository) + .execute(repository.getOwnerLogin(), repository.getName(), + pullRequest.getNumber()); + for (Commit commit : commitList) { + // TODO: Fetch PR reviews. - team.setUsers(userList); - } } } catch (HttpException e) { throw new RuntimeException(e); From d95fbce644a1b9f6bef35362a86e2fac333800a9 Mon Sep 17 00:00:00 2001 From: Miquel de Domingo Date: Sun, 18 Dec 2022 11:53:02 +0100 Subject: [PATCH 2/3] fix: create custom github user option request We currently had our `UserOptionRequest` inside the `shell` package and we were coupling the `github` pacakge to such request. --- .../controller/UserOptionController.java | 4 ++-- .../github/model/GitHubUserOptionRequest.java | 16 ++++++++++++++++ .../shell/components/UserOptionComponent.java | 5 ++++- 3 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 src/main/java/io/pakland/mdas/githubstats/infrastructure/github/model/GitHubUserOptionRequest.java diff --git a/src/main/java/io/pakland/mdas/githubstats/infrastructure/controller/UserOptionController.java b/src/main/java/io/pakland/mdas/githubstats/infrastructure/controller/UserOptionController.java index 757ac825..3a0d3a20 100644 --- a/src/main/java/io/pakland/mdas/githubstats/infrastructure/controller/UserOptionController.java +++ b/src/main/java/io/pakland/mdas/githubstats/infrastructure/controller/UserOptionController.java @@ -4,8 +4,8 @@ import io.pakland.mdas.githubstats.application.external.*; import io.pakland.mdas.githubstats.domain.entity.*; import io.pakland.mdas.githubstats.domain.repository.*; +import io.pakland.mdas.githubstats.infrastructure.github.model.GitHubUserOptionRequest; import io.pakland.mdas.githubstats.infrastructure.github.repository.*; -import io.pakland.mdas.githubstats.infrastructure.shell.model.UserOptionRequest; import java.util.List; import lombok.NoArgsConstructor; import org.slf4j.Logger; @@ -24,7 +24,7 @@ public class UserOptionController { private PullRequestExternalRepository pullRequestExternalRepository; private CommitExternalRepository commitExternalRepository; - public UserOptionController(UserOptionRequest userOptionRequest) { + public UserOptionController(GitHubUserOptionRequest userOptionRequest) { WebClientConfiguration webClientConfiguration = new WebClientConfiguration( "https://api.github.com", userOptionRequest.getApiKey()); this.organizationExternalRepository = new OrganizationGitHubRepository( diff --git a/src/main/java/io/pakland/mdas/githubstats/infrastructure/github/model/GitHubUserOptionRequest.java b/src/main/java/io/pakland/mdas/githubstats/infrastructure/github/model/GitHubUserOptionRequest.java new file mode 100644 index 00000000..97a47dd8 --- /dev/null +++ b/src/main/java/io/pakland/mdas/githubstats/infrastructure/github/model/GitHubUserOptionRequest.java @@ -0,0 +1,16 @@ +package io.pakland.mdas.githubstats.infrastructure.github.model; + +import java.util.Date; +import lombok.Builder; +import lombok.Data; +import lombok.Getter; + +@Data +@Getter +@Builder +public class GitHubUserOptionRequest { + private String userName; + private String apiKey; + private Date from; + private Date to; +} diff --git a/src/main/java/io/pakland/mdas/githubstats/infrastructure/shell/components/UserOptionComponent.java b/src/main/java/io/pakland/mdas/githubstats/infrastructure/shell/components/UserOptionComponent.java index cd3fce4b..3b4ee405 100644 --- a/src/main/java/io/pakland/mdas/githubstats/infrastructure/shell/components/UserOptionComponent.java +++ b/src/main/java/io/pakland/mdas/githubstats/infrastructure/shell/components/UserOptionComponent.java @@ -2,6 +2,7 @@ import io.pakland.mdas.githubstats.infrastructure.controller.UserOptionController; +import io.pakland.mdas.githubstats.infrastructure.github.model.GitHubUserOptionRequest; import io.pakland.mdas.githubstats.infrastructure.shell.model.UserOptionRequest; import io.pakland.mdas.githubstats.infrastructure.shell.validation.DateValidator; import io.pakland.mdas.githubstats.infrastructure.shell.validation.UserNameValidator; @@ -45,7 +46,9 @@ private boolean user( } UserOptionController userControllerFromGithub = new UserOptionController( - this.userOptionRequest); + GitHubUserOptionRequest.builder().userName( + userOptionRequest.getUserName()).apiKey(userOptionRequest.getApiKey()) + .from(userOptionRequest.getFrom()).to(userOptionRequest.getTo()).build()); userControllerFromGithub.execute(); // ... Perform request ... From 231fa9c9c94e955a52c8c5b84aec055a1fefa43b Mon Sep 17 00:00:00 2001 From: Miquel de Domingo Date: Sun, 18 Dec 2022 11:55:04 +0100 Subject: [PATCH 3/3] refactor: place user option controller back to `github` pkg --- .../controller/GitHubUserOptionController.java} | 8 ++++---- .../shell/components/UserOptionComponent.java | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) rename src/main/java/io/pakland/mdas/githubstats/infrastructure/{controller/UserOptionController.java => github/controller/GitHubUserOptionController.java} (95%) diff --git a/src/main/java/io/pakland/mdas/githubstats/infrastructure/controller/UserOptionController.java b/src/main/java/io/pakland/mdas/githubstats/infrastructure/github/controller/GitHubUserOptionController.java similarity index 95% rename from src/main/java/io/pakland/mdas/githubstats/infrastructure/controller/UserOptionController.java rename to src/main/java/io/pakland/mdas/githubstats/infrastructure/github/controller/GitHubUserOptionController.java index 3a0d3a20..cb82c4d5 100644 --- a/src/main/java/io/pakland/mdas/githubstats/infrastructure/controller/UserOptionController.java +++ b/src/main/java/io/pakland/mdas/githubstats/infrastructure/github/controller/GitHubUserOptionController.java @@ -1,4 +1,4 @@ -package io.pakland.mdas.githubstats.infrastructure.controller; +package io.pakland.mdas.githubstats.infrastructure.github.controller; import io.pakland.mdas.githubstats.application.exceptions.HttpException; import io.pakland.mdas.githubstats.application.external.*; @@ -14,9 +14,9 @@ @Component @NoArgsConstructor -public class UserOptionController { +public class GitHubUserOptionController { - Logger logger = LoggerFactory.getLogger(UserOptionController.class); + Logger logger = LoggerFactory.getLogger(GitHubUserOptionController.class); private OrganizationExternalRepository organizationExternalRepository; private TeamExternalRepository teamExternalRepository; private UserExternalRepository userExternalRepository; @@ -24,7 +24,7 @@ public class UserOptionController { private PullRequestExternalRepository pullRequestExternalRepository; private CommitExternalRepository commitExternalRepository; - public UserOptionController(GitHubUserOptionRequest userOptionRequest) { + public GitHubUserOptionController(GitHubUserOptionRequest userOptionRequest) { WebClientConfiguration webClientConfiguration = new WebClientConfiguration( "https://api.github.com", userOptionRequest.getApiKey()); this.organizationExternalRepository = new OrganizationGitHubRepository( diff --git a/src/main/java/io/pakland/mdas/githubstats/infrastructure/shell/components/UserOptionComponent.java b/src/main/java/io/pakland/mdas/githubstats/infrastructure/shell/components/UserOptionComponent.java index 3b4ee405..dbc4ef7f 100644 --- a/src/main/java/io/pakland/mdas/githubstats/infrastructure/shell/components/UserOptionComponent.java +++ b/src/main/java/io/pakland/mdas/githubstats/infrastructure/shell/components/UserOptionComponent.java @@ -1,7 +1,7 @@ package io.pakland.mdas.githubstats.infrastructure.shell.components; -import io.pakland.mdas.githubstats.infrastructure.controller.UserOptionController; +import io.pakland.mdas.githubstats.infrastructure.github.controller.GitHubUserOptionController; import io.pakland.mdas.githubstats.infrastructure.github.model.GitHubUserOptionRequest; import io.pakland.mdas.githubstats.infrastructure.shell.model.UserOptionRequest; import io.pakland.mdas.githubstats.infrastructure.shell.validation.DateValidator; @@ -45,7 +45,7 @@ private boolean user( throw new RuntimeException(e); } - UserOptionController userControllerFromGithub = new UserOptionController( + GitHubUserOptionController userControllerFromGithub = new GitHubUserOptionController( GitHubUserOptionRequest.builder().userName( userOptionRequest.getUserName()).apiKey(userOptionRequest.getApiKey()) .from(userOptionRequest.getFrom()).to(userOptionRequest.getTo()).build());