-
Notifications
You must be signed in to change notification settings - Fork 0
feat: command configuration #34
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9ecbbd0
feat: introduce user input validation
sdomingobasora 5e468eb
feat: components for the 'team' and 'user' shell commands
sdomingobasora fdccd4a
feat: bean configuration for the 'team' and 'user' shell commands
sdomingobasora fd3b161
chore: remove old commands
sdomingobasora ee76fbb
refactor: extract function
sdomingobasora 3391cde
test: add tests for validators
sdomingobasora 360072f
fix: refine validations to pass tests
sdomingobasora 46e2a02
feat: add login command
sdomingobasora 3988741
docs: update README with available commands
sdomingobasora 814293c
test: separate truthy/falsy tests
mikededo 5ba4f00
Merge branch 'main' into feat/command-configuration
plozanol d3ed191
feat: add longname to name option
mikededo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 0 additions & 32 deletions
32
...ain/java/io/pakland/mdas/githubstats/infrastructure/shell/components/CommitComponent.java
This file was deleted.
Oops, something went wrong.
31 changes: 0 additions & 31 deletions
31
...ava/io/pakland/mdas/githubstats/infrastructure/shell/components/LinesOfCodeComponent.java
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
...main/java/io/pakland/mdas/githubstats/infrastructure/shell/components/LoginComponent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package io.pakland.mdas.githubstats.infrastructure.shell.components; | ||
|
|
||
| import org.springframework.shell.standard.ShellComponent; | ||
| import org.springframework.shell.standard.ShellOption; | ||
|
|
||
| @ShellComponent | ||
| public class LoginComponent { | ||
|
|
||
| private boolean login(@ShellOption(value = {"token"}) String token) { | ||
| return token != null; | ||
| } | ||
|
|
||
| } |
41 changes: 0 additions & 41 deletions
41
...ava/io/pakland/mdas/githubstats/infrastructure/shell/components/PullRequestComponent.java
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
src/main/java/io/pakland/mdas/githubstats/infrastructure/shell/components/TeamComponent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package io.pakland.mdas.githubstats.infrastructure.shell.components; | ||
|
|
||
| import io.pakland.mdas.githubstats.infrastructure.shell.validation.DateValidator; | ||
| import io.pakland.mdas.githubstats.infrastructure.shell.validation.TeamNameValidator; | ||
| import org.springframework.shell.standard.ShellComponent; | ||
| import org.springframework.shell.standard.ShellOption; | ||
|
|
||
| @ShellComponent | ||
| public class TeamComponent { | ||
|
|
||
| private boolean team( | ||
| @ShellOption(value = {"n"}) String teamName, | ||
| @ShellOption(value = {"from"}) String fromDate, | ||
| @ShellOption(value = {"to"}) String toDate | ||
| ) { | ||
| DateValidator dateValidator = new DateValidator(); | ||
| TeamNameValidator teamNameValidator = new TeamNameValidator(); | ||
|
|
||
| boolean isInputValid = dateValidator.validate(fromDate) | ||
| && dateValidator.validate(toDate) | ||
| && teamNameValidator.validate(teamName); | ||
|
|
||
| if (!isInputValid) { | ||
| // Alert user, and halt command | ||
| return false; | ||
| } | ||
|
|
||
| // ... Perform request ... | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| } |
33 changes: 33 additions & 0 deletions
33
src/main/java/io/pakland/mdas/githubstats/infrastructure/shell/components/UserComponent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package io.pakland.mdas.githubstats.infrastructure.shell.components; | ||
|
|
||
| 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; | ||
|
|
||
| @ShellComponent | ||
| public class UserComponent { | ||
|
|
||
| private boolean user( | ||
| @ShellOption(value = {"n"}) String userName, | ||
| @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); | ||
|
|
||
| if (!isInputValid) { | ||
| // Alert user, and halt command | ||
| return false; | ||
| } | ||
|
|
||
| // ... Perform request ... | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| } | ||
102 changes: 102 additions & 0 deletions
102
.../io/pakland/mdas/githubstats/infrastructure/shell/configuration/CommandConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package io.pakland.mdas.githubstats.infrastructure.shell.configuration; | ||
|
|
||
| import io.pakland.mdas.githubstats.infrastructure.shell.components.LoginComponent; | ||
| import io.pakland.mdas.githubstats.infrastructure.shell.components.TeamComponent; | ||
| import io.pakland.mdas.githubstats.infrastructure.shell.components.UserComponent; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.shell.command.CommandRegistration; | ||
|
|
||
| @Configuration | ||
| public class CommandConfiguration { | ||
|
|
||
| @Bean | ||
| public CommandRegistration buildUserCommand() { | ||
| UserComponent userComponent = new UserComponent(); | ||
|
|
||
| return CommandRegistration.builder() | ||
| .command("user") | ||
| .description("Get data from a specified user.") | ||
| .withTarget() | ||
| .method(userComponent, "user") | ||
| .and() | ||
| .withOption() | ||
| .shortNames('n') | ||
| .longNames("name") | ||
| .label("USER_NAME") | ||
| .arity(CommandRegistration.OptionArity.EXACTLY_ONE) | ||
| .type(String.class) | ||
| .required() | ||
| .and() | ||
| .withOption() | ||
| .longNames("from") | ||
| .label("FROM_DATE") | ||
| .arity(CommandRegistration.OptionArity.EXACTLY_ONE) | ||
| .type(String.class) | ||
| .required() | ||
| .and() | ||
| .withOption() | ||
| .longNames("to") | ||
| .label("TO_DATE") | ||
| .arity(CommandRegistration.OptionArity.EXACTLY_ONE) | ||
| .type(String.class) | ||
| .required() | ||
| .and() | ||
| .build(); | ||
| } | ||
|
|
||
| @Bean | ||
| public CommandRegistration buildTeamCommand() { | ||
| TeamComponent teamComponent = new TeamComponent(); | ||
|
|
||
| return CommandRegistration.builder() | ||
| .command("team") | ||
| .description("Get data from a specified team and its sub teams.") | ||
| .withTarget() | ||
| .method(teamComponent, "team") | ||
| .and() | ||
| .withOption() | ||
| .shortNames('n') | ||
| .longNames("name") | ||
| .label("TEAM_NAME") | ||
| .arity(CommandRegistration.OptionArity.EXACTLY_ONE) | ||
| .type(String.class) | ||
| .required() | ||
| .and() | ||
| .withOption() | ||
| .longNames("from") | ||
| .label("FROM_DATE") | ||
| .arity(CommandRegistration.OptionArity.EXACTLY_ONE) | ||
| .type(String.class) | ||
| .required() | ||
| .and() | ||
| .withOption() | ||
| .longNames("to") | ||
| .label("TO_DATE") | ||
| .arity(CommandRegistration.OptionArity.EXACTLY_ONE) | ||
| .type(String.class) | ||
| .required() | ||
| .and() | ||
| .build(); | ||
| } | ||
|
|
||
| @Bean | ||
| public CommandRegistration buildLoginCommand() { | ||
| LoginComponent loginComponent = new LoginComponent(); | ||
|
|
||
| return CommandRegistration.builder() | ||
| .command("login") | ||
| .description("Log in to the Github API before making any requests.") | ||
| .withTarget() | ||
| .method(loginComponent, "login") | ||
| .and() | ||
| .withOption() | ||
| .longNames("token") | ||
| .label("TOKEN") | ||
| .type(String.class) | ||
| .required() | ||
| .and() | ||
| .build(); | ||
| } | ||
|
|
||
| } |
37 changes: 37 additions & 0 deletions
37
src/main/java/io/pakland/mdas/githubstats/infrastructure/shell/validation/DateValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package io.pakland.mdas.githubstats.infrastructure.shell.validation; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.time.format.DateTimeFormatter; | ||
| import java.time.format.DateTimeParseException; | ||
|
|
||
| public class DateValidator implements InputValidator<String> { | ||
|
|
||
| private final DateTimeFormatter formatter; | ||
|
|
||
| public DateValidator() { | ||
| String datePattern = "dd/MM/yy HH:mm"; | ||
| formatter = DateTimeFormatter.ofPattern(datePattern); | ||
| } | ||
|
|
||
| /** | ||
| * @param input Must be MM/yy, and prior to the current month. Starting year: 2000 (01/01/99 parses to 01/01/2099) | ||
| */ | ||
| @Override | ||
| public boolean validate(String input) { | ||
| if (input == null || input.isBlank()) return false; | ||
|
|
||
| try { | ||
| LocalDateTime date = LocalDateTime.parse(getInitialDateFromMMYY(input), formatter); | ||
| LocalDateTime now = LocalDateTime.now(); | ||
| return date.isBefore(now.minusMonths(1)); | ||
| } catch (DateTimeParseException e) { | ||
| // Input was not properly formatted | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private String getInitialDateFromMMYY(String input) { | ||
| return "01/" + input + " 00:00"; | ||
| } | ||
|
|
||
| } |
7 changes: 7 additions & 0 deletions
7
...main/java/io/pakland/mdas/githubstats/infrastructure/shell/validation/InputValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package io.pakland.mdas.githubstats.infrastructure.shell.validation; | ||
|
|
||
| public interface InputValidator<T> { | ||
|
|
||
| boolean validate(T input); | ||
|
|
||
| } |
23 changes: 23 additions & 0 deletions
23
...n/java/io/pakland/mdas/githubstats/infrastructure/shell/validation/TeamNameValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package io.pakland.mdas.githubstats.infrastructure.shell.validation; | ||
|
|
||
| public class TeamNameValidator implements InputValidator<String> { | ||
|
|
||
| public TeamNameValidator() {} | ||
|
|
||
| /** | ||
| * @param input Must pass the Github team name creation constraints: | ||
| * - Team name may only contain alphanumeric characters or hyphens. | ||
| * - Team name cannot have multiple consecutive hyphens. | ||
| * - Team name cannot begin or end with a hyphen. | ||
| * - Maximum is 39 characters. | ||
| */ | ||
| @Override | ||
| public boolean validate(String input) { | ||
| if (input == null || input.isBlank()) return false; | ||
| return !input.matches("^-.*") | ||
| && !input.matches(".*-$") | ||
| && input.matches("[a-zA-Z0-9\\-]{0,39}") | ||
| && !input.matches(".*--.*"); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.