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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package io.pakland.mdas.githubstats.infrastructure.github.controller;

import io.pakland.mdas.githubstats.application.exceptions.HttpException;
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 java.util.List;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@NoArgsConstructor
public class GitHubUserOptionController {

Logger logger = LoggerFactory.getLogger(GitHubUserOptionController.class);
private OrganizationExternalRepository organizationExternalRepository;
private TeamExternalRepository teamExternalRepository;
private UserExternalRepository userExternalRepository;
private RepositoryExternalRepository repositoryExternalRepository;
private PullRequestExternalRepository pullRequestExternalRepository;
private CommitExternalRepository commitExternalRepository;

public GitHubUserOptionController(GitHubUserOptionRequest userOptionRequest) {
WebClientConfiguration webClientConfiguration = new 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.commitExternalRepository = new CommitGitHubRepository(webClientConfiguration);
}

public void execute() {
try {
// TODO: If the execution succeeds, we should make an entry to the historic_queries table.
// Fetch the API key's available organizations.
List<Organization> organizationList = new FetchAvailableOrganizations(
this.organizationExternalRepository)
.execute();
organizationList.forEach(this::fetchTeamsFromOrganization);
} catch (HttpException e) {
throw new RuntimeException(e);
}
}

private void fetchTeamsFromOrganization(Organization organization) {
try {
List<Team> 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<Repository> 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);
}
}

private void fetchUsersFromTeam(Organization organization, Team team) {
try {
// Fetch the members of each team.
List<User> userList = new FetchUsersFromTeam(userExternalRepository)
.execute(organization.getLogin(), team.getSlug());

team.setUsers(userList);
} catch (HttpException e) {
throw new RuntimeException(e);
}
}

private void fetchPullRequestsFromRepository(Team team, Repository repository) {
team.addRepository(repository);
try {
// Fetch pull requests from each team.
List<PullRequest> pullRequestList = new FetchPullRequestsFromRepository(
pullRequestExternalRepository)
.execute(repository.getOwnerLogin(), repository.getName());

pullRequestList.forEach(pullRequest -> this.fetchCommitsFromPullRequest(repository, pullRequest));
repository.setPullRequests(pullRequestList);
} catch (HttpException e) {
throw new RuntimeException(e);
}
}


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<Commit> commitList = new FetchCommitsFromPullRequest(
commitExternalRepository)
.execute(repository.getOwnerLogin(), repository.getName(),
pullRequest.getNumber());
for (Commit commit : commitList) {
// TODO: Fetch PR reviews.

}
} catch (HttpException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
package io.pakland.mdas.githubstats.infrastructure.shell.components;

import io.pakland.mdas.githubstats.infrastructure.controller.UserControllerFromGithub;

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;
import io.pakland.mdas.githubstats.infrastructure.shell.validation.UserNameValidator;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellOption;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellOption;

@ShellComponent
public class UserOptionComponent {

private UserOptionRequest userOptionRequest;

private boolean user(
@ShellOption(value = {"n"}) String userName,
@ShellOption(value = {"key"}) String apiKey,
@ShellOption(value = {"from"}) String fromDate,
@ShellOption(value = {"to"}) String toDate
@ShellOption(value = {"n"}) String userName,
@ShellOption(value = {"key"}) String apiKey,
@ShellOption(value = {"from"}) String fromDate,
@ShellOption(value = {"to"}) String toDate
) {
DateValidator dateValidator = new DateValidator();
UserNameValidator userNameValidator = new UserNameValidator();

boolean isInputValid = dateValidator.validate(fromDate)
&& dateValidator.validate(toDate)
&& userNameValidator.validate(userName);
&& dateValidator.validate(toDate)
&& userNameValidator.validate(userName);

if (!isInputValid) {
// Alert user, and halt command
Expand All @@ -34,16 +36,19 @@ private boolean user(

try {
this.userOptionRequest = UserOptionRequest.builder()
.userName(userName)
.apiKey(apiKey)
.from(new SimpleDateFormat("dd/MM/yy").parse("01/"+fromDate))
.to(new SimpleDateFormat("dd/MM/yy").parse("01/"+toDate))
.build();
.userName(userName)
.apiKey(apiKey)
.from(new SimpleDateFormat("dd/MM/yy").parse("01/" + fromDate))
.to(new SimpleDateFormat("dd/MM/yy").parse("01/" + toDate))
.build();
} catch (ParseException e) {
throw new RuntimeException(e);
}

UserControllerFromGithub userControllerFromGithub = new UserControllerFromGithub(this.userOptionRequest);
GitHubUserOptionController userControllerFromGithub = new GitHubUserOptionController(
GitHubUserOptionRequest.builder().userName(
userOptionRequest.getUserName()).apiKey(userOptionRequest.getApiKey())
.from(userOptionRequest.getFrom()).to(userOptionRequest.getTo()).build());
userControllerFromGithub.execute();
// ... Perform request ...

Expand Down