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 other commands
  • Loading branch information
DarKhaos committed Dec 9, 2024
1 parent 5282d54 commit 81ce4c1
Show file tree
Hide file tree
Showing 21 changed files with 147 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import com.box.l10n.mojito.cli.command.CommandException;
import com.box.l10n.mojito.cli.model.AssetAssetSummary;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

@Component
public class AssetWsApiHelper {
@Autowired AssetWsApi assetClient;
public class AssetClient extends AssetWsApi {

public AssetClient(ApiClient apiClient) {
super(apiClient);
}

public AssetAssetSummary getAssetByPathAndRepositoryId(String path, Long repositoryId)
throws CommandException {
Expand All @@ -18,7 +18,7 @@ public AssetAssetSummary getAssetByPathAndRepositoryId(String path, Long reposit

List<AssetAssetSummary> assets;
try {
assets = this.assetClient.getAssets(repositoryId, path, null, null, null);
assets = this.getAssets(repositoryId, path, null, null, null);
} catch (ApiException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import java.time.OffsetDateTime;
import java.util.List;
import java.util.regex.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RepositoryWsApiHelper {
@Autowired RepositoryWsApi repositoryClient;
public class RepositoryClient extends RepositoryWsApi {

public RepositoryClient(ApiClient apiClient) {
super(apiClient);
}

public List<BranchBranchSummary> getBranchesOfRepository(
Long repositoryId,
Expand All @@ -26,7 +26,7 @@ public List<BranchBranchSummary> getBranchesOfRepository(
List<BranchBranchSummary> branches;
try {
branches =
this.repositoryClient.getBranchesOfRepository(
this.getBranchesOfRepository(
repositoryId, branchName, deleted, translated, createdBefore);
} catch (ApiException e) {
throw new CommandException(e.getMessage(), e);
Expand All @@ -51,7 +51,7 @@ public List<BranchBranchSummary> getBranchesOfRepository(
public RepositoryRepository findRepositoryByName(String repositoryName) throws CommandException {
try {
Preconditions.checkNotNull(repositoryName, "Repository name can't be null");
List<RepositoryRepository> repositories = repositoryClient.getRepositories(repositoryName);
List<RepositoryRepository> repositories = this.getRepositories(repositoryName);
if (repositories.size() != 1) {
throw new CommandException("Repository with name [" + repositoryName + "] is not found");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ public RepositoryWsApi repositoryWsApi() {
return new RepositoryWsApi(this.apiClient);
}

@Bean
public CommitWsApi commitWsApi() {
return new CommitWsApi(this.apiClient);
}

@Bean
public ImageWsApi imageWsApi() {
return new ImageWsApi(this.apiClient);
}

@Bean
public AiPromptWsApi aiPromptWsApi() {
return new AiPromptWsApi(this.apiClient);
Expand All @@ -53,18 +43,13 @@ public AiChecksWsApi aiChecksWsApi() {
return new AiChecksWsApi(this.apiClient);
}

@Bean
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);
public ScreenshotWsApi screenshotWsApi() {
return new ScreenshotWsApi(this.apiClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.box.l10n.mojito.cli.apiclient.ApiClient;
import com.box.l10n.mojito.cli.apiclient.ApiException;
import com.box.l10n.mojito.cli.apiclient.RepositoryWsApi;
import com.box.l10n.mojito.cli.apiclient.RepositoryWsApiHelper;
import com.box.l10n.mojito.cli.apiclient.RepositoryClient;
import com.box.l10n.mojito.cli.command.param.Param;
import com.box.l10n.mojito.cli.console.ConsoleWriter;
import com.box.l10n.mojito.cli.model.BranchBranchSummary;
Expand All @@ -44,10 +44,6 @@ public class BranchDeleteCommand extends Command {

@Autowired ConsoleWriter consoleWriter;

@Autowired RepositoryWsApi repositoryClient;

@Autowired RepositoryWsApiHelper repositoryWsApiHelper;

@Autowired CommandHelper commandHelper;

@Parameter(
Expand Down Expand Up @@ -93,6 +89,8 @@ public class BranchDeleteCommand extends Command {
description = BRANCH_CREATED_BEFORE_DESCRIPTION)
String createdBefore = null;

@Autowired private ApiClient apiClient;

@Override
public void execute() throws CommandException {
consoleWriter
Expand All @@ -101,8 +99,8 @@ public void execute() throws CommandException {
.fg(Ansi.Color.CYAN)
.a(repositoryParam)
.println();
RepositoryRepository repository =
this.repositoryWsApiHelper.findRepositoryByName(this.repositoryParam);
RepositoryClient repositoryClient = new RepositoryClient(this.apiClient);
RepositoryRepository repository = repositoryClient.findRepositoryByName(this.repositoryParam);

ZonedDateTime createdBeforeDateTime =
this.commandHelper.getLastWeekDateIfTrue(this.beforeLastWeek);
Expand All @@ -118,7 +116,7 @@ public void execute() throws CommandException {
}

List<BranchBranchSummary> branches =
this.repositoryWsApiHelper.getBranchesOfRepository(
repositoryClient.getBranchesOfRepository(
repository.getId(),
branchName,
branchNameRegex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.box.l10n.mojito.cli.apiclient.RepositoryWsApiHelper;
import com.box.l10n.mojito.cli.apiclient.ApiClient;
import com.box.l10n.mojito.cli.apiclient.RepositoryClient;
import com.box.l10n.mojito.cli.command.param.Param;
import com.box.l10n.mojito.cli.console.ConsoleWriter;
import com.box.l10n.mojito.cli.model.BranchBranchSummary;
Expand All @@ -38,8 +39,6 @@ public class BranchViewCommand extends Command {

@Autowired ConsoleWriter consoleWriter;

@Autowired RepositoryWsApiHelper repositoryWsApiHelper;

@Autowired CommandHelper commandHelper;

@Parameter(
Expand Down Expand Up @@ -79,6 +78,8 @@ public class BranchViewCommand extends Command {
description = BRANCH_CREATED_BEFORE_LAST_WEEK_DESCRIPTION)
boolean beforeLastWeek;

@Autowired private ApiClient apiClient;

@Override
public void execute() throws CommandException {
consoleWriter
Expand All @@ -87,10 +88,11 @@ public void execute() throws CommandException {
.fg(Ansi.Color.CYAN)
.a(repositoryParam)
.println();
RepositoryRepository repository = repositoryWsApiHelper.findRepositoryByName(repositoryParam);
RepositoryClient repositoryClient = new RepositoryClient(this.apiClient);
RepositoryRepository repository = repositoryClient.findRepositoryByName(repositoryParam);

List<BranchBranchSummary> branches =
this.repositoryWsApiHelper.getBranchesOfRepository(
repositoryClient.getBranchesOfRepository(
repository.getId(),
null,
branchNameRegex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.box.l10n.mojito.JSR310Migration;
import com.box.l10n.mojito.cli.apiclient.ApiClient;
import com.box.l10n.mojito.cli.apiclient.ApiException;
import com.box.l10n.mojito.cli.apiclient.CommitWsApi;
import com.box.l10n.mojito.cli.apiclient.RepositoryWsApiHelper;
import com.box.l10n.mojito.cli.apiclient.RepositoryClient;
import com.box.l10n.mojito.cli.command.param.Param;
import com.box.l10n.mojito.cli.console.ConsoleWriter;
import com.box.l10n.mojito.cli.model.CommitBody;
Expand Down Expand Up @@ -46,10 +47,6 @@ public class CommitCreateCommand extends Command {

@Autowired CommandHelper commandHelper;

@Autowired CommitWsApi commitClient;

@Autowired RepositoryWsApiHelper repositoryWsApiHelper;

@Parameter(
names = {Param.COMMIT_HASH_LONG, Param.COMMIT_HASH_SHORT},
arity = 1,
Expand Down Expand Up @@ -90,6 +87,8 @@ public class CommitCreateCommand extends Command {
+ " the first commit from 'git log' is used.")
boolean readInfoFromGit = false;

@Autowired private ApiClient apiClient;

@Override
protected void execute() throws CommandException {
consoleWriter
Expand All @@ -98,9 +97,8 @@ protected void execute() throws CommandException {
.fg(Ansi.Color.CYAN)
.a(repositoryParam)
.println(2);

RepositoryRepository repository =
this.repositoryWsApiHelper.findRepositoryByName(repositoryParam);
new RepositoryClient(this.apiClient).findRepositoryByName(repositoryParam);

final CommitInfo commitInfo;

Expand All @@ -122,7 +120,7 @@ protected void execute() throws CommandException {
commitBody.setSourceCreationDate(creationDateMilliSeconds);
CommitCommit commit;
try {
commit = commitClient.createCommit(commitBody);
commit = new CommitWsApi(this.apiClient).createCommit(commitBody);
} catch (ApiException e) {
throw new CommandException(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.box.l10n.mojito.cli.apiclient.ApiClient;
import com.box.l10n.mojito.cli.apiclient.ApiException;
import com.box.l10n.mojito.cli.apiclient.CommitWsApi;
import com.box.l10n.mojito.cli.apiclient.RepositoryWsApiHelper;
import com.box.l10n.mojito.cli.apiclient.RepositoryClient;
import com.box.l10n.mojito.cli.command.param.Param;
import com.box.l10n.mojito.cli.console.ConsoleWriter;
import com.box.l10n.mojito.cli.model.CommitToPullRunBody;
Expand Down Expand Up @@ -60,18 +61,15 @@ public class CommitToPullRunCommand extends Command {

@Autowired ObjectMapper objectMapper;

@Autowired CommitWsApi commitClient;

@Autowired RepositoryWsApiHelper repositoryWsApiHelper;
@Autowired ApiClient apiClient;

RepositoryRepository repository;

@Override
public void execute() throws CommandException {

consoleWriter.newLine().a("Mapping commit: ").fg(Color.CYAN).a(commitHash).println(2);

repository = this.repositoryWsApiHelper.findRepositoryByName(repositoryParam);
repository = new RepositoryClient(this.apiClient).findRepositoryByName(repositoryParam);

String pullRunName = readPullRunNameFromFile();

Expand All @@ -96,7 +94,7 @@ void associateCommitToPullRun(
commitToPullRunBody.setRepositoryId(repository.getId());
commitToPullRunBody.setPullRunName(pullRunName);
try {
commitClient.associateCommitToPullRun(commitToPullRunBody);
new CommitWsApi(this.apiClient).associateCommitToPullRun(commitToPullRunBody);
} catch (ApiException e) {
throw new CommandException(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.box.l10n.mojito.cli.apiclient.ApiClient;
import com.box.l10n.mojito.cli.apiclient.ApiException;
import com.box.l10n.mojito.cli.apiclient.AssetWsApi;
import com.box.l10n.mojito.cli.command.param.Param;
import com.box.l10n.mojito.cli.console.ConsoleWriter;
import com.box.l10n.mojito.cli.model.LocalizedAssetBody;
import com.box.l10n.mojito.cli.model.SourceAsset;
import com.box.l10n.mojito.evolve.Course;
import com.box.l10n.mojito.evolve.Evolve;
import com.box.l10n.mojito.io.Files;
import com.box.l10n.mojito.rest.client.AssetClient;
import com.box.l10n.mojito.rest.client.PollableTaskClient;
import com.box.l10n.mojito.rest.client.RepositoryClient;
import com.box.l10n.mojito.rest.entity.Locale;
import com.box.l10n.mojito.rest.entity.LocalizedAssetBody;
import com.box.l10n.mojito.rest.entity.Repository;
import com.box.l10n.mojito.rest.entity.RepositoryLocale;
import com.box.l10n.mojito.rest.entity.SourceAsset;
import com.google.common.base.Preconditions;
import com.ibm.icu.util.ULocale;
import java.nio.file.Path;
Expand Down Expand Up @@ -79,21 +80,22 @@ public class EvolveCommand extends Command {
@Autowired(required = false)
Evolve evolve;

@Autowired RepositoryClient repositoryClient;

@Autowired AssetClient assetClient;
@Autowired ApiClient apiClient;

@Autowired PollableTaskClient pollableTaskClient;

@Autowired CommandHelper commandHelper;

private AssetWsApi assetClient;

@Override
public boolean shouldShowInCommandList() {
return false;
}

@Override
public void execute() throws CommandException {
this.assetClient = new AssetWsApi(apiClient);
checkEvolveConfiguration();

consoleWriter
Expand Down Expand Up @@ -173,24 +175,33 @@ SourceAsset sendSource(Repository repository, String courseId, String translatio
sourceAsset.setPath(courseId + ".json");
sourceAsset.setContent(translationsByCourseId);
sourceAsset.setFilterOptions(FILTER_OPTIONS);
sourceAsset = assetClient.sendSourceAsset(sourceAsset);
try {
sourceAsset = assetClient.importSourceAsset(sourceAsset);
} catch (ApiException e) {
throw new CommandException(e.getMessage(), e);
}
pollableTaskClient.waitForPollableTask(sourceAsset.getPollableTask().getId());
return sourceAsset;
}

String getLocalizedCourse(Long assetId, long localeId, String translationsByCourseId) {

LocalizedAssetBody localizedAssetForContent =
assetClient.getLocalizedAssetForContent(
assetId,
localeId,
translationsByCourseId,
null,
null,
FILTER_OPTIONS,
LocalizedAssetBody.Status.ALL,
LocalizedAssetBody.InheritanceMode.USE_PARENT,
null);
LocalizedAssetBody localizedAssetBody = new LocalizedAssetBody();
localizedAssetBody.setAssetId(assetId);
localizedAssetBody.setLocaleId(localeId);
localizedAssetBody.setContent(translationsByCourseId);
localizedAssetBody.setOutputBcp47tag(null);
localizedAssetBody.setFilterConfigIdOverride(null);
localizedAssetBody.setFilterOptions(FILTER_OPTIONS);
localizedAssetBody.setStatus(LocalizedAssetBody.StatusEnum.ALL);
localizedAssetBody.setInheritanceMode(LocalizedAssetBody.InheritanceModeEnum.USE_PARENT);
localizedAssetBody.setPullRunName(null);
LocalizedAssetBody localizedAssetForContent;
try {
localizedAssetForContent =
assetClient.getLocalizedAssetForContent(localizedAssetBody, assetId, localeId);
} catch (ApiException e) {
throw new CommandException(e.getMessage(), e);
}

return localizedAssetForContent.getContent();
}
Expand Down
Loading

0 comments on commit 81ce4c1

Please sign in to comment.