Skip to content

Commit

Permalink
feat: New command: distribution (#593)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrii Bodnar <[email protected]>
  • Loading branch information
yzerk and andrii-bodnar authored Aug 31, 2023
1 parent e6c1b08 commit 9312fe1
Show file tree
Hide file tree
Showing 31 changed files with 983 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/main/java/com/crowdin/cli/client/ClientDistribution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.crowdin.cli.client;

import com.crowdin.client.distributions.model.AddDistributionRequest;
import com.crowdin.client.distributions.model.Distribution;
import com.crowdin.client.distributions.model.DistributionRelease;

import java.util.List;

public interface ClientDistribution extends Client {

List<Distribution> listDistribution();

Distribution addDistribution(AddDistributionRequest request);

DistributionRelease release(String hash);

DistributionRelease getDistributionRelease(String hash);
}
5 changes: 5 additions & 0 deletions src/main/java/com/crowdin/cli/client/Clients.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public static ClientComment getClientComment(String apiToken, String baseUrl, St
return new CrowdinClientComment(client, projectId);
}

public static ClientDistribution getClientDistribution(String apiToken, String baseUrl, String projectId) {
com.crowdin.client.Client client = prepareClient(apiToken, baseUrl);
return new CrowdinClientDistribution(client, projectId);
}

public static ClientBundle getClientBundle(String apiToken, String baseUrl, String projectId) {
com.crowdin.client.Client client = prepareClient(apiToken, baseUrl);
return new CrowdinClientBundle(client, projectId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.crowdin.cli.client;

import com.crowdin.client.distributions.model.AddDistributionRequest;
import com.crowdin.client.distributions.model.Distribution;
import com.crowdin.client.distributions.model.DistributionRelease;

import java.util.List;

public class CrowdinClientDistribution extends CrowdinClientCore implements ClientDistribution {

private final com.crowdin.client.Client client;
private final String projectId;

public CrowdinClientDistribution(com.crowdin.client.Client client, String projectId) {
this.client = client;
this.projectId = projectId;
}

@Override
public List<Distribution> listDistribution() {
return executeRequestFullList((limit, offset) -> this.client.getDistributionsApi()
.listDistributions(Long.valueOf(projectId), limit, offset));
}

@Override
public Distribution addDistribution(AddDistributionRequest distributionRequest) {
return executeRequest(() -> this.client.getDistributionsApi()
.addDistribution(Long.valueOf(projectId), distributionRequest)
.getData());
}

@Override
public DistributionRelease release(String hash) {
return executeRequest(() -> this.client.getDistributionsApi()
.createDistributionRelease(Long.valueOf(projectId), hash)
.getData());
}

@Override
public DistributionRelease getDistributionRelease(String hash) {
return executeRequest(() -> this.client.getDistributionsApi()
.getDistributionRelease(Long.valueOf(projectId), hash)
.getData());
}

}
7 changes: 7 additions & 0 deletions src/main/java/com/crowdin/cli/commands/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.crowdin.cli.properties.PropertiesWithTargets;
import com.crowdin.cli.properties.PropertiesWithFiles;
import com.crowdin.client.core.model.Priority;
import com.crowdin.client.distributions.model.ExportMode;
import com.crowdin.client.glossaries.model.GlossariesFormat;
import com.crowdin.client.stringcomments.model.IssueStatus;
import com.crowdin.client.translationmemory.model.TranslationMemoryFormat;
Expand Down Expand Up @@ -88,6 +89,12 @@ NewAction<BaseProperties, ClientTm> tmDownload(

NewAction<ProjectProperties, ClientTask> taskAdd(String title, Integer type, String language, List<Long> fileId, Long workflowStep, String description, boolean skipAssignedStrings, boolean skipUntranslatedStrings, List<Long> labels);

NewAction<ProjectProperties, ClientDistribution> distributionList(boolean plainView);

NewAction<ProjectProperties, ClientDistribution> distributionAdd(boolean noProgress, boolean plainView, String name, ExportMode exportMode, List<String> files, List<Integer> bundleIds, String branch, ProjectClient projectClient);

NewAction<ProjectProperties, ClientDistribution> distributionRelease(boolean noProgress, boolean plainView, String hash);

NewAction<ProjectProperties, ClientComment> commentList(boolean plainView, boolean isVerbose, String stringId, com.crowdin.client.stringcomments.model.Type type, com.crowdin.client.issues.model.Type issueType, IssueStatus status);

NewAction<ProjectProperties, ClientComment> resolve(Long id);
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/crowdin/cli/commands/actions/CliActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.crowdin.cli.properties.PropertiesWithTargets;
import com.crowdin.cli.properties.PropertiesWithFiles;
import com.crowdin.client.core.model.Priority;
import com.crowdin.client.distributions.model.ExportMode;
import com.crowdin.client.glossaries.model.GlossariesFormat;
import com.crowdin.client.stringcomments.model.IssueStatus;
import com.crowdin.client.translationmemory.model.TranslationMemoryFormat;
Expand Down Expand Up @@ -172,6 +173,21 @@ public NewAction<ProjectProperties, ClientTask> taskAdd(String title, Integer ty
return new TaskAddAction(title, type, language, fileId, workflowStep, description, skipAssignedStrings, skipUntranslatedStrings, labels);
}

@Override
public NewAction<ProjectProperties, ClientDistribution> distributionList(boolean plainView) {
return new DistributionListAction(plainView);
}

@Override
public NewAction<ProjectProperties, ClientDistribution> distributionAdd(boolean noProgress, boolean plainView, String name, ExportMode exportMode, List<String> files, List<Integer> bundleIds, String branch, ProjectClient projectClient) {
return new DistributionAddAction(noProgress, plainView, name, exportMode, files, bundleIds, branch, projectClient);
}

@Override
public NewAction<ProjectProperties, ClientDistribution> distributionRelease(boolean noProgress, boolean plainView, String hash) {
return new DistributionReleaseAction(noProgress, plainView, hash);
}

@Override
public NewAction<ProjectProperties, ClientComment> commentList(boolean plainView, boolean isVerbose,String stringId,
com.crowdin.client.stringcomments.model.Type type, com.crowdin.client.issues.model.Type issueType,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.crowdin.cli.commands.actions;

import com.crowdin.cli.client.ClientDistribution;
import com.crowdin.cli.client.CrowdinProjectFull;
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.commands.functionality.RequestBuilder;
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.cli.utils.Utils;
import com.crowdin.cli.utils.console.ConsoleSpinner;
import com.crowdin.client.distributions.model.AddDistributionRequest;
import com.crowdin.client.distributions.model.Distribution;
import com.crowdin.client.distributions.model.ExportMode;
import com.crowdin.client.sourcefiles.model.Branch;
import com.crowdin.client.sourcefiles.model.FileInfo;
import lombok.AllArgsConstructor;

import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import static com.crowdin.cli.utils.console.ExecutionStatus.OK;

@AllArgsConstructor
class DistributionAddAction implements NewAction<ProjectProperties, ClientDistribution> {

private boolean noProgress;
private boolean plainView;
private String name;
private ExportMode exportMode;
private List<String> files;
private List<Integer> bundleIds;
private String branch;

private ProjectClient projectClient;

@Override
public void act(Outputter out, ProjectProperties pb, ClientDistribution client) {
CrowdinProjectFull project = ConsoleSpinner.execute(
out,
"message.spinner.fetching_project_info", "error.collect_project_info",
this.noProgress,
this.plainView,
() -> this.projectClient.downloadFullProject(this.branch)
);
List<Long> fileIds = null;
if (files != null) {
Map<String, Long> projectBranches = project.getBranches().values().stream()
.collect(Collectors.toMap(Branch::getName, Branch::getId));
List<String> projectFiles = project.getFiles().stream()
.filter(file -> branch == null || file.getBranchId().equals(projectBranches.get(branch)))
.map(FileInfo::getPath)
.collect(Collectors.toList());
List<String> notExistingFiles = files.stream()
.map(file -> branch == null ? file : Paths.get(branch, file).toString())
.map(Utils::sepAtStart)
.filter(file -> !projectFiles.contains(file))
.collect(Collectors.toList());
if (!notExistingFiles.isEmpty()) {
throw new RuntimeException(notExistingFiles.stream().map(Utils::noSepAtStart)
.map(file -> String.format(RESOURCE_BUNDLE.getString("error.file_not_found"), file))
.collect(Collectors.joining("\n\u274C ")));
}
files = branch != null ? files.stream().map(file -> Paths.get(branch, file).toString())
.collect(Collectors.toList()) : files;
files = files.stream().map(Utils::sepAtStart).collect(Collectors.toList());
fileIds = project
.getFiles()
.stream()
.filter(file -> files.contains(file.getPath()))
.map(FileInfo::getId)
.collect(Collectors.toList());
}

Distribution distribution;
AddDistributionRequest addDistributionRequest = RequestBuilder.addDistribution(name, exportMode, fileIds, bundleIds);
Optional.ofNullable(name).ifPresent(addDistributionRequest::setName);
Optional.ofNullable(exportMode).ifPresent(addDistributionRequest::setExportMode);
Optional.ofNullable(fileIds).ifPresent(addDistributionRequest::setFileIds);
Optional.ofNullable(bundleIds).ifPresent(addDistributionRequest::setBundleIds);

try {
distribution = client.addDistribution(addDistributionRequest);
} catch (Exception e) {
throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.distribution_is_not_added"), addDistributionRequest), e);
}

if (!plainView) {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.distribution.added"), distribution.getName())));
} else {
out.println(String.valueOf(distribution.getName()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.crowdin.cli.commands.actions;

import com.crowdin.cli.client.ClientDistribution;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.client.distributions.model.Distribution;

import java.util.List;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import static com.crowdin.cli.utils.console.ExecutionStatus.OK;

class DistributionListAction implements NewAction<ProjectProperties, ClientDistribution> {

private final boolean plainView;

public DistributionListAction(boolean plainView) {
this.plainView = plainView;
}

@Override
public void act(Outputter out, ProjectProperties pb, ClientDistribution client) {
List<Distribution> distributions = client.listDistribution();

for (Distribution distribution : distributions) {
if (!plainView) {
out.println(String.format(RESOURCE_BUNDLE.getString("message.distribution.list"), distribution.getHash(),
distribution.getName(),
distribution.getExportMode()));
} else {
out.println(distribution.getHash() + " " + distribution.getName());
}
}

if (distributions.isEmpty()) {
if (!plainView) {
out.println(OK.withIcon(RESOURCE_BUNDLE.getString("message.distribution.list_empty")));
} else {
out.println(RESOURCE_BUNDLE.getString("message.distribution.list_empty"));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.crowdin.cli.commands.actions;

import com.crowdin.cli.client.ClientDistribution;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.cli.utils.console.ConsoleSpinner;
import com.crowdin.client.distributions.model.DistributionRelease;
import lombok.AllArgsConstructor;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import static com.crowdin.cli.utils.console.ExecutionStatus.OK;

@AllArgsConstructor
class DistributionReleaseAction implements NewAction<ProjectProperties, ClientDistribution> {

private boolean noProgress;
private boolean plainView;
private String hash;

@Override
public void act(Outputter out, ProjectProperties pb, ClientDistribution client) {
this.releaseDistribution(out, client);
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.distribution.released"), hash)));
}

private DistributionRelease releaseDistribution(Outputter out, ClientDistribution client) {
return ConsoleSpinner.execute(
out,
"message.spinner.releasing_distribution",
"error.distribution_is_not_released",
this.noProgress,
false,
() -> {
DistributionRelease release = client.release(hash);

while (!"success".equalsIgnoreCase(release.getStatus())) {
ConsoleSpinner.update(
String.format(RESOURCE_BUNDLE.getString("message.spinner.releasing_distribution_percents"),
release.getProgress()));
Thread.sleep(1000);

release = client.getDistributionRelease(hash);

if ("failed".equalsIgnoreCase(release.getStatus())) {
throw new RuntimeException(RESOURCE_BUNDLE.getString("message.spinner.build_has_failed"));
}
}

ConsoleSpinner.update(String.format(RESOURCE_BUNDLE.getString("message.spinner.releasing_distribution_percents"), 100));

return release;
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.crowdin.client.core.model.PatchOperation;
import com.crowdin.client.core.model.PatchRequest;
import com.crowdin.client.core.model.Priority;
import com.crowdin.client.distributions.model.AddDistributionRequest;
import com.crowdin.client.distributions.model.ExportMode;
import com.crowdin.client.glossaries.model.AddGlossaryRequest;
import com.crowdin.client.glossaries.model.ExportGlossaryRequest;
import com.crowdin.client.glossaries.model.GlossariesFormat;
Expand Down Expand Up @@ -57,6 +59,15 @@ public static AddStringCommentRequest addComment(String text, String type, Strin
return request;
}

public static AddDistributionRequest addDistribution(String name, ExportMode exportMode, List<Long> fileIds, List<Integer> bundleIds) {
AddDistributionRequest request = new AddDistributionRequest();
Optional.ofNullable(name).ifPresent(request::setName);
Optional.ofNullable(exportMode).ifPresent(request::setExportMode);
Optional.ofNullable(fileIds).ifPresent(request::setFileIds);
Optional.ofNullable(bundleIds).ifPresent(request::setBundleIds);
return request;
}

public static CrowdinTaskCreateFormRequest addCrowdinTask(String title, Integer type, String languageId, List<Long> fileId, String description, boolean skipAssignedStrings, boolean skipUntranslatedStrings, List<Long> labelIds) {
CrowdinTaskCreateFormRequest request = new CrowdinTaskCreateFormRequest();
request.setTitle(title);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.crowdin.cli.commands.picocli;

import com.crowdin.cli.client.ClientDistribution;
import com.crowdin.cli.client.Clients;
import com.crowdin.cli.client.ProjectClient;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.properties.ProjectParams;
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.cli.properties.PropertiesBuilders;
import picocli.CommandLine;

public abstract class ActCommandDistribution extends GenericActCommand<ProjectProperties, ClientDistribution> {

@CommandLine.Mixin
private ConfigurationFilesProperties properties;

@CommandLine.ArgGroup(exclusive = false, headingKey = "params.heading")
private ProjectParams params;

@Override
protected ProjectProperties getProperties(PropertiesBuilders propertiesBuilders, Outputter out) {
return propertiesBuilders.buildProjectProperties(out, properties.getConfigFile(), properties.getIdentityFile(), params);
}

@Override
protected ClientDistribution getClient(ProjectProperties properties) {
return Clients.getClientDistribution(properties.getApiToken(), properties.getBaseUrl(), properties.getProjectId());
}

protected ProjectClient getProjectClient(ProjectProperties properties) {
return Clients.getProjectClient(properties.getApiToken(), properties.getBaseUrl(), Long.parseLong(properties.getProjectId()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ public final class CommandNames {
public static final String COMMENT = "comment";
public static final String COMMENT_LIST = "list";
public static final String COMMENT_RESOLVE = "resolve";

public static final String DISTRIBUTION = "distribution";
public static final String DISTRIBUTION_ADD = "add";
public static final String DISTRIBUTION_LIST = "list";
public static final String DISTRIBUTION_RELEASE = "release";
}
Loading

0 comments on commit 9312fe1

Please sign in to comment.