Skip to content
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

feat: New command: distribution #593

Merged
merged 19 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
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());

Check warning on line 36 in src/main/java/com/crowdin/cli/client/CrowdinClientDistribution.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/client/CrowdinClientDistribution.java#L34-L36

Added lines #L34 - L36 were not covered by tests
}

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

Check warning on line 43 in src/main/java/com/crowdin/cli/client/CrowdinClientDistribution.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/client/CrowdinClientDistribution.java#L41-L43

Added lines #L41 - L43 were not covered by tests
}

}
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 ")));

Check warning on line 66 in src/main/java/com/crowdin/cli/commands/actions/DistributionAddAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/DistributionAddAction.java#L64-L66

Added lines #L64 - L66 were not covered by tests
}
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);

Check warning on line 89 in src/main/java/com/crowdin/cli/commands/actions/DistributionAddAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/DistributionAddAction.java#L88-L89

Added lines #L88 - L89 were not covered by tests
}

if (!plainView) {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.distribution.added"), distribution.getName())));

Check warning on line 93 in src/main/java/com/crowdin/cli/commands/actions/DistributionAddAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/DistributionAddAction.java#L93

Added line #L93 was not covered by tests
} 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)));
}

Check warning on line 25 in src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java#L23-L25

Added lines #L23 - L25 were not covered by tests

private DistributionRelease releaseDistribution(Outputter out, ClientDistribution client) {
return ConsoleSpinner.execute(

Check warning on line 28 in src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java#L28

Added line #L28 was not covered by tests
out,
"message.spinner.releasing_distribution",
"error.distribution_is_not_released",
this.noProgress,
false,
() -> {
DistributionRelease release = client.release(hash);

Check warning on line 35 in src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java#L35

Added line #L35 was not covered by tests

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

Check warning on line 41 in src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java#L38-L41

Added lines #L38 - L41 were not covered by tests

release = client.getDistributionRelease(hash);

Check warning on line 43 in src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java#L43

Added line #L43 was not covered by tests

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

Check warning on line 46 in src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java#L46

Added line #L46 was not covered by tests
}
}

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

Check warning on line 50 in src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java#L50

Added line #L50 was not covered by tests

return release;

Check warning on line 52 in src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/actions/DistributionReleaseAction.java#L52

Added line #L52 was not covered by tests
}
);
}
}
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()));

Check warning on line 31 in src/main/java/com/crowdin/cli/commands/picocli/ActCommandDistribution.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/com/crowdin/cli/commands/picocli/ActCommandDistribution.java#L31

Added line #L31 was not covered by tests
}
}
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