Skip to content

Commit

Permalink
I18N-1323 Update Mojito CLI to use OpenAPI spec for rest calls
Browse files Browse the repository at this point in the history
Refactor commands
  • Loading branch information
DarKhaos committed Dec 7, 2024
1 parent a407823 commit e47977a
Show file tree
Hide file tree
Showing 19 changed files with 122 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,14 @@ public AiChecksWsApi aiChecksWsApi() {
public AssetWsApi assetWsApi() {
return new AssetWsApi(this.apiClient);
}

@Bean
public DropWsApi dropWsApi() {
return new DropWsApi(this.apiClient);
}

@Bean
public CliWsApi cliWsApi() {
return new CliWsApi(this.apiClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static com.box.l10n.mojito.cli.command.param.Param.BRANCH_TRANSLATED_DESCRIPTION;
import static com.box.l10n.mojito.cli.command.param.Param.BRANCH_TRANSLATED_LONG;
import static com.box.l10n.mojito.cli.command.param.Param.BRANCH_TRANSLATED_SHORT;
import static java.util.Optional.ofNullable;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
Expand Down Expand Up @@ -124,7 +125,7 @@ public void execute() throws CommandException {
false,
translated,
includeNullBranch,
createdBeforeDateTime == null ? null : createdBeforeDateTime.toOffsetDateTime());
ofNullable(createdBeforeDateTime).map(ZonedDateTime::toOffsetDateTime).orElse(null));

for (BranchBranchSummary branch : branches) {
consoleWriter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static com.box.l10n.mojito.cli.command.param.Param.BRANCH_TRANSLATED_DESCRIPTION;
import static com.box.l10n.mojito.cli.command.param.Param.BRANCH_TRANSLATED_LONG;
import static com.box.l10n.mojito.cli.command.param.Param.BRANCH_TRANSLATED_SHORT;
import static java.util.Optional.ofNullable;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
Expand All @@ -17,6 +18,7 @@
import com.box.l10n.mojito.cli.console.ConsoleWriter;
import com.box.l10n.mojito.cli.model.BranchBranchSummary;
import com.box.l10n.mojito.cli.model.RepositoryRepository;
import java.time.ZonedDateTime;
import java.util.List;
import org.fusesource.jansi.Ansi;
import org.slf4j.Logger;
Expand Down Expand Up @@ -95,9 +97,9 @@ public void execute() throws CommandException {
deleted,
translated,
includeNullBranch,
commandHelper.getLastWeekDateIfTrue(beforeLastWeek) == null
? null
: commandHelper.getLastWeekDateIfTrue(beforeLastWeek).toOffsetDateTime());
ofNullable(commandHelper.getLastWeekDateIfTrue(beforeLastWeek))
.map(ZonedDateTime::toOffsetDateTime)
.orElse(null));

for (BranchBranchSummary branch : branches) {
consoleWriter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.box.l10n.mojito.cli.command;

import static java.util.Optional.ofNullable;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.box.l10n.mojito.JSR310Migration;
Expand Down Expand Up @@ -113,8 +115,11 @@ protected void execute() throws CommandException {
commitBody.setRepositoryId(repository.getId());
commitBody.setAuthorName(commitInfo.authorName);
commitBody.setAuthorEmail(commitInfo.authorEmail);
commitBody.setSourceCreationDate(
commitInfo.creationDate == null ? null : commitInfo.creationDate.toOffsetDateTime());
Long creationDateMilliSeconds =
ofNullable(commitInfo.creationDate)
.map(creationDate -> creationDate.toInstant().getEpochSecond() * 1_000)
.orElse(null);
commitBody.setSourceCreationDate(creationDateMilliSeconds);
CommitCommit commit;
try {
commit = commitClient.createCommit(commitBody);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class DeleteAIPromptCommand extends Command {

static Logger logger = LoggerFactory.getLogger(DeleteAIPromptCommand.class);

@Autowired AiPromptWsApi AIServiceClient;
@Autowired AiPromptWsApi aiServiceClient;

@Parameter(
names = {"--prompt-id", "-pi"},
Expand All @@ -44,7 +44,7 @@ protected void execute() throws CommandException {
private void deletePrompt() {
logger.debug("Received request to delete prompt {}", promptId);
try {
AIServiceClient.deletePrompt(promptId);
aiServiceClient.deletePrompt(promptId);
} catch (ApiException e) {
throw new CommandException(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.box.l10n.mojito.cli.GitInfo;
import com.box.l10n.mojito.cli.apiclient.ApiException;
import com.box.l10n.mojito.cli.apiclient.CliWsApi;
import com.box.l10n.mojito.cli.console.ConsoleWriter;
import com.box.l10n.mojito.rest.client.CliClient;
import org.fusesource.jansi.Ansi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -37,7 +38,7 @@ public class MainCommand extends Command {

@Autowired GitInfo gitInfo;

@Autowired CliClient cliClient;
@Autowired CliWsApi cliClient;

@Override
void showUsage() {
Expand All @@ -56,7 +57,12 @@ protected void execute() throws CommandException {
}

void checkServerVersion() throws CommandException {
String serverVersion = cliClient.getVersion();
String serverVersion;
try {
serverVersion = cliClient.getVersion();
} catch (ApiException e) {
throw new CommandException(e.getMessage(), e);
}
String cliVersion = getCliVersion();
if (!cliVersion.equals(serverVersion)) {
throw new CommandException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
import com.beust.jcommander.Parameters;
import com.box.l10n.mojito.cli.apiclient.ApiException;
import com.box.l10n.mojito.cli.apiclient.AssetWsApi;
import com.box.l10n.mojito.cli.apiclient.RepositoryWsApiHelper;
import com.box.l10n.mojito.cli.command.param.Param;
import com.box.l10n.mojito.cli.console.ConsoleWriter;
import com.box.l10n.mojito.cli.model.AssetAssetSummary;
import com.box.l10n.mojito.cli.model.RepositoryLocaleRepository;
import com.box.l10n.mojito.cli.model.RepositoryRepository;
import com.box.l10n.mojito.cli.model.XliffExportBody;
import com.box.l10n.mojito.rest.client.exception.PollableTaskException;
import com.box.l10n.mojito.rest.entity.Repository;
import com.box.l10n.mojito.rest.entity.RepositoryLocale;
import com.google.common.base.MoreObjects;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Set;
import org.fusesource.jansi.Ansi.Color;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -70,6 +70,8 @@ public class TMExportCommand extends Command {

@Autowired CommandHelper commandHelper;

@Autowired RepositoryWsApiHelper repositoryWsApiHelper;

@Autowired AssetWsApi assetClient;

CommandDirectories commandDirectories;
Expand All @@ -89,7 +91,8 @@ public void execute() throws CommandException {
logger.debug("Initialize targetBasename (use repository if no target bases name is specified)");
targetBasenameParam = MoreObjects.firstNonNull(targetBasenameParam, repositoryParam);

Repository repository = commandHelper.findRepositoryByName(repositoryParam);
RepositoryRepository repository =
this.repositoryWsApiHelper.findRepositoryByName(repositoryParam);

List<AssetAssetSummary> assets;
try {
Expand All @@ -98,7 +101,7 @@ public void execute() throws CommandException {
throw new CommandException(e.getMessage(), e);
}

Set<RepositoryLocale> repositoryLocales = repository.getRepositoryLocales();
List<RepositoryLocaleRepository> repositoryLocales = repository.getRepositoryLocales();

long assetNumber = 0;

Expand All @@ -107,7 +110,7 @@ public void execute() throws CommandException {

consoleWriter.newLine().a("Asset: ").fg(Color.CYAN).a(asset.getPath()).println();

for (RepositoryLocale repositoryLocale : repositoryLocales) {
for (RepositoryLocaleRepository repositoryLocale : repositoryLocales) {

String bcp47Tag = repositoryLocale.getLocale().getBcp47Tag();

Expand Down Expand Up @@ -154,7 +157,7 @@ public void execute() throws CommandException {
* @param assetNumber the asset number
* @return the export file
*/
private Path getExportFile(RepositoryLocale repositoryLocale, long assetNumber)
private Path getExportFile(RepositoryLocaleRepository repositoryLocale, long assetNumber)
throws CommandException {
String filename;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.box.l10n.mojito.cli.apiclient.ApiException;
import com.box.l10n.mojito.cli.apiclient.RepositoryWsApiHelper;
import com.box.l10n.mojito.cli.apiclient.ThirdPartyWsApi;
import com.box.l10n.mojito.cli.command.param.Param;
import com.box.l10n.mojito.cli.console.ConsoleWriter;
import com.box.l10n.mojito.cli.model.PollableTask;
import com.box.l10n.mojito.cli.model.RepositoryRepository;
import com.box.l10n.mojito.cli.model.ThirdPartySync;
import com.box.l10n.mojito.rest.entity.Repository;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -122,7 +123,9 @@ public class ThirdPartySyncCommand extends Command {

@Autowired CommandHelper commandHelper;

private ThirdPartySync getThirdPartySync(Repository repository) {
@Autowired RepositoryWsApiHelper repositoryWsApiHelper;

private ThirdPartySync getThirdPartySync(RepositoryRepository repository) {
ThirdPartySync thirdPartySyncBody = new ThirdPartySync();
thirdPartySyncBody.setRepositoryId(repository.getId());
thirdPartySyncBody.setProjectId(thirdPartyProjectId);
Expand Down Expand Up @@ -186,7 +189,8 @@ public void execute() throws CommandException {
.a(Objects.toString(options))
.println(2);

Repository repository = commandHelper.findRepositoryByName(repositoryParam);
RepositoryRepository repository =
this.repositoryWsApiHelper.findRepositoryByName(repositoryParam);

ThirdPartySync thirdPartySyncBody = getThirdPartySync(repository);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import com.box.l10n.mojito.cli.apiclient.ApiException;
import com.box.l10n.mojito.cli.apiclient.UserWsApi;
import com.box.l10n.mojito.cli.console.ConsoleWriter;
import com.box.l10n.mojito.cli.model.Authority;
import com.box.l10n.mojito.cli.model.PageUser;
import com.box.l10n.mojito.cli.model.Pageable;
import com.box.l10n.mojito.cli.model.User;
import com.box.l10n.mojito.rest.entity.Role;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

Expand All @@ -18,6 +21,19 @@ protected PageUser getPageUser(String username) throws ApiException {
pageable.setPage(0);
pageable.setSize(Integer.MAX_VALUE);
pageable.setSort(List.of());
return this.userClient.getUsers(pageable, username, null);
PageUser pageUser = this.userClient.getUsers(pageable, username, null);
if (pageUser.getContent().isEmpty()) {
throw new CommandException("User with username [" + username + "] is not found");
}
return pageUser;
}

protected void addAuthorities(User user, String roleName) {
Role role = Role.fromString(roleName);
if (role != null) {
Authority authority = new Authority();
authority.setAuthority(role.toString());
user.setAuthorities(List.of(authority));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import com.box.l10n.mojito.cli.apiclient.ApiException;
import com.box.l10n.mojito.cli.command.param.Param;
import com.box.l10n.mojito.cli.console.Console;
import com.box.l10n.mojito.cli.model.Authority;
import com.box.l10n.mojito.cli.model.User;
import com.box.l10n.mojito.rest.entity.Role;
import java.util.List;
import org.fusesource.jansi.Ansi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -77,18 +74,14 @@ protected void execute() throws CommandException {
consoleWriter.a("Enter new password for " + username + ":").println();
String password = console.readPassword();

Role role = Role.fromString(rolename);
User userBody = new User();
userBody.setUsername(username);
userBody.setPassword(password);
userBody.setSurname(surname);
userBody.setGivenName(givenName);
userBody.setCommonName(commonName);
if (role != null) {
Authority authority = new Authority();
authority.setAuthority(role.toString());
userBody.setAuthorities(List.of(authority));
}
this.addAuthorities(userBody, this.rolename);

User user = userClient.createUser(userBody);
consoleWriter
.newLine()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ protected void execute() throws CommandException {

try {
List<User> users = this.getPageUser(username).getContent();
if (users.isEmpty()) {
throw new CommandException("User with username [" + username + "] is not found");
}
this.userClient.deleteUserByUserId(users.getFirst().getId());
consoleWriter.newLine().a("deleted --> user: ").fg(Ansi.Color.MAGENTA).a(username).println();
} catch (ApiException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import com.box.l10n.mojito.cli.apiclient.ApiException;
import com.box.l10n.mojito.cli.command.param.Param;
import com.box.l10n.mojito.cli.console.Console;
import com.box.l10n.mojito.cli.model.Authority;
import com.box.l10n.mojito.cli.model.User;
import com.box.l10n.mojito.rest.entity.Role;
import java.util.ArrayList;
import java.util.List;
import org.fusesource.jansi.Ansi;
import org.slf4j.Logger;
Expand Down Expand Up @@ -81,23 +78,13 @@ protected void execute() throws CommandException {

try {
List<User> users = this.getPageUser(username).getContent();
if (users.isEmpty()) {
throw new CommandException("User with username [" + username + "] is not found");
}
User user = users.getFirst();
user.setPassword(getPassword());
user.setSurname(surname);
user.setGivenName(givenName);
user.setCommonName(commonName);

Role role = Role.fromString(rolename);
List<Authority> authorities = new ArrayList<>();
if (role != null) {
Authority authority = new Authority();
authority.setAuthority(role.toString());
authorities.add(authority);
}
user.setAuthorities(authorities);
this.addAuthorities(user, this.rolename);

userClient.updateUserByUserId(user, user.getId());
consoleWriter.newLine().a("updated --> user: ").fg(Ansi.Color.MAGENTA).a(username).println();
Expand Down
Loading

0 comments on commit e47977a

Please sign in to comment.