Skip to content

Commit

Permalink
Add '--label' param for String Add and String Edit actions
Browse files Browse the repository at this point in the history
  • Loading branch information
frombetelgeuse committed Jul 21, 2021
1 parent 2e1da0b commit f065944
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/crowdin/cli/commands/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ NewAction<PropertiesWithFiles, ProjectClient> status(
boolean noProgress, String branchName, String languageId, boolean isVerbose, boolean showTranslated, boolean showApproved);

NewAction<PropertiesWithFiles, ProjectClient> stringAdd(
boolean noProgress, String text, String identifier, Integer maxLength, String context, List<String> files, Boolean hidden);
boolean noProgress, String text, String identifier, Integer maxLength, String context, List<String> files, List<String> labelNames, Boolean hidden);

NewAction<PropertiesWithFiles, ProjectClient> stringDelete(
boolean noProgress, List<Long> ids, List<String> texts, List<String> identifiers);

NewAction<PropertiesWithFiles, ProjectClient> stringEdit(
boolean noProgress, Long id, String identifier, String newText, String newContext, Integer newMaxLength, Boolean isHidden);
boolean noProgress, Long id, String identifier, String newText, String newContext, Integer newMaxLength, List<String> labelNames, Boolean isHidden);

NewAction<PropertiesWithFiles, ProjectClient> stringList(
boolean noProgress, boolean isVerbose, String file, String filter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public NewAction<PropertiesWithFiles, ProjectClient> status(

@Override
public NewAction<PropertiesWithFiles, ProjectClient> stringAdd(
boolean noProgress, String text, String identifier, Integer maxLength, String context, List<String> files, Boolean hidden
boolean noProgress, String text, String identifier, Integer maxLength, String context, List<String> files, List<String> labelNames, Boolean hidden
) {
return new StringAddAction(noProgress, text, identifier, maxLength, context, files, hidden);
return new StringAddAction(noProgress, text, identifier, maxLength, context, files, labelNames, hidden);
}

@Override
Expand All @@ -91,9 +91,9 @@ public NewAction<PropertiesWithFiles, ProjectClient> stringDelete(

@Override
public NewAction<PropertiesWithFiles, ProjectClient> stringEdit(
boolean noProgress, Long id, String identifier, String newText, String newContext, Integer newMaxLength, Boolean isHidden
boolean noProgress, Long id, String identifier, String newText, String newContext, Integer newMaxLength, List<String> labelNames, Boolean isHidden
) {
return new StringEditAction(noProgress, id, identifier, newText, newContext, newMaxLength, isHidden);
return new StringEditAction(noProgress, id, identifier, newText, newContext, newMaxLength, labelNames, isHidden);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import com.crowdin.cli.commands.functionality.RequestBuilder;
import com.crowdin.cli.properties.PropertiesWithFiles;
import com.crowdin.cli.utils.console.ConsoleSpinner;
import com.crowdin.client.labels.model.Label;
import com.crowdin.client.sourcefiles.model.FileInfo;
import com.crowdin.client.sourcestrings.model.AddSourceStringRequest;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import static com.crowdin.cli.utils.console.ExecutionStatus.OK;
Expand All @@ -26,17 +28,19 @@ class StringAddAction implements NewAction<PropertiesWithFiles, ProjectClient> {
private final Integer maxLength;
private final String context;
private final List<String> files;
private final List<String> labelNames;
private final Boolean hidden;

public StringAddAction(
boolean noProgress, String text, String identifier, Integer maxLength, String context, List<String> files, Boolean hidden
boolean noProgress, String text, String identifier, Integer maxLength, String context, List<String> files, List<String> labelNames, Boolean hidden
) {
this.noProgress = noProgress;
this.text = text;
this.identifier = identifier;
this.maxLength = maxLength;
this.context = context;
this.files = files;
this.labelNames = labelNames;
this.hidden = hidden;
}

Expand All @@ -45,8 +49,10 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
CrowdinProjectFull project = ConsoleSpinner.execute(out, "message.spinner.fetching_project_info", "error.collect_project_info",
this.noProgress, false, client::downloadFullProject);

List<Long> labelIds = (labelNames != null && !labelNames.isEmpty()) ? this.prepareLabelIds(client) : null;

if (files == null || files.isEmpty()) {
AddSourceStringRequest request = RequestBuilder.addString(this.text, this.identifier, this.maxLength, this.context, null, this.hidden);
AddSourceStringRequest request = RequestBuilder.addString(this.text, this.identifier, this.maxLength, this.context, null, this.hidden, labelIds);
client.addSourceString(request);
out.println(OK.withIcon(RESOURCE_BUNDLE.getString("message.source_string_uploaded")));
} else {
Expand All @@ -65,7 +71,7 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
Long fileId = paths.get(file).getId();

AddSourceStringRequest request =
RequestBuilder.addString(this.text, this.identifier, this.maxLength, this.context, fileId, this.hidden);
RequestBuilder.addString(this.text, this.identifier, this.maxLength, this.context, fileId, this.hidden, labelIds);
client.addSourceString(request);
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.source_string_for_file_uploaded"), file)));
}
Expand All @@ -75,4 +81,15 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
}

}

private List<Long> prepareLabelIds(ProjectClient client) {
Map<String, Long> labels = client.listLabels().stream()
.collect(Collectors.toMap(Label::getTitle, Label::getId));
labelNames.stream()
.distinct()
.forEach(labelName -> labels.computeIfAbsent(labelName, (title) -> client.addLabel(RequestBuilder.addLabel(title)).getId()));
return labelNames.stream()
.map(labels::get)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import com.crowdin.cli.properties.PropertiesWithFiles;
import com.crowdin.client.core.model.PatchOperation;
import com.crowdin.client.core.model.PatchRequest;
import com.crowdin.client.labels.model.Label;
import com.crowdin.client.sourcestrings.model.SourceString;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
import static com.crowdin.cli.utils.console.ExecutionStatus.OK;
Expand All @@ -23,17 +26,19 @@ class StringEditAction implements NewAction<PropertiesWithFiles, ProjectClient>
private final String newText;
private final String newContext;
private final Integer newMaxLength;
private final List<String> labelNames;
private final Boolean isHidden;

public StringEditAction(
boolean noProgress, Long id, String identifier, String newText, String newContext, Integer newMaxLength, Boolean isHidden
boolean noProgress, Long id, String identifier, String newText, String newContext, Integer newMaxLength, List<String> labelNames, Boolean isHidden
) {
this.noProgress = noProgress;
this.id = id;
this.identifier = identifier;
this.newText = newText;
this.newContext = newContext;
this.newMaxLength = newMaxLength;
this.labelNames = labelNames;
this.isHidden = isHidden;
}

Expand All @@ -42,6 +47,8 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {

List<SourceString> sourceStrings = client.listSourceString(null, null, null);

List<Long> labelIds = (labelNames != null && !labelNames.isEmpty()) ? this.prepareLabelIds(client) : null;

Long foundStringId;
if (id != null) {
foundStringId = sourceStrings.stream()
Expand Down Expand Up @@ -76,8 +83,23 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
PatchRequest request = RequestBuilder.patch(isHidden, PatchOperation.REPLACE, "/isHidden");
requests.add(request);
}
if (labelIds != null) {
PatchRequest request = RequestBuilder.patch(labelIds, PatchOperation.REPLACE, "/labelIds");
requests.add(request);
}

client.editSourceString(foundStringId, requests);
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.source_string_updated"), foundStringId)));
}

private List<Long> prepareLabelIds(ProjectClient client) {
Map<String, Long> labels = client.listLabels().stream()
.collect(Collectors.toMap(Label::getTitle, Label::getId));
labelNames.stream()
.distinct()
.forEach(labelName -> labels.computeIfAbsent(labelName, (title) -> client.addLabel(RequestBuilder.addLabel(title)).getId()));
return labelNames.stream()
.map(labels::get)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@

public class RequestBuilder {

public static AddSourceStringRequest addString(String text, String identifier, Integer maxLength, String context, Long fileId, Boolean hidden) {
public static AddSourceStringRequest addString(String text, String identifier, Integer maxLength, String context, Long fileId, Boolean hidden, List<Long> labelIds) {
AddSourceStringRequest request = new AddSourceStringRequest();
request.setText(text);
request.setIdentifier(identifier);
request.setMaxLength(maxLength);
request.setContext(context);
request.setFileId(fileId);
request.setIsHidden(hidden);
request.setLabelIds(labelIds);
return request;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class StringAddSubcommand extends ActCommandWithFiles {
@CommandLine.Option(names = {"--file"}, paramLabel = "...")
protected List<String> files;

@CommandLine.Option(names = {"--label"}, descriptionKey = "params.label", paramLabel = "...")
protected List<String> labelNames;

@CommandLine.Option(names = {"--hidden"})
protected Boolean isHidden;

Expand All @@ -52,6 +55,6 @@ protected List<String> checkOptions() {

@Override
protected NewAction<PropertiesWithFiles, ProjectClient> getAction(Actions actions) {
return actions.stringAdd(noProgress, text, identifier, maxLength, context, files, isHidden);
return actions.stringAdd(noProgress, text, identifier, maxLength, context, files, labelNames, isHidden);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class StringEditSubcommand extends ActCommandWithFiles {
@CommandLine.Option(names = {"--max-length"}, paramLabel = "...")
protected Integer newMaxLength;

@CommandLine.Option(names = {"--label"}, descriptionKey = "params.label", paramLabel = "...")
protected List<String> labelNames;

@CommandLine.Option(names = {"--hidden"}, negatable = true)
protected Boolean newIsHidden;

Expand All @@ -40,15 +43,15 @@ protected List<String> checkOptions() {
} else if (id != null && identifier != null) {
errors.add("You can't use both identifiers");
}
if (newText == null && newContext == null && newMaxLength == null && newIsHidden == null) {
if (newText == null && newContext == null && newMaxLength == null && newIsHidden == null && (labelNames == null || labelNames.isEmpty())) {
errors.add(RESOURCE_BUNDLE.getString("error.source_string_no_edit"));
}
return errors;
}

@Override
protected NewAction<PropertiesWithFiles, ProjectClient> getAction(Actions actions) {
return actions.stringEdit(noProgress, id, identifier, newText, newContext, newMaxLength, newIsHidden);
return actions.stringEdit(noProgress, id, identifier, newText, newContext, newMaxLength, labelNames, newIsHidden);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void testStatus() {

@Test
public void testStringAdd() {
assertNotNull(actions.stringAdd(false, null, null, null, null, null, null));
assertNotNull(actions.stringAdd(false, null, null, null, null, null, null, null));
}

@Test
Expand All @@ -59,7 +59,7 @@ public void testStringDelete() {

@Test
public void testStringEdit() {
assertNotNull(actions.stringEdit(false, null, null, null, null, null, null));
assertNotNull(actions.stringEdit(false, null, null, null, null, null, null, null));
}

@Test
Expand Down
Loading

0 comments on commit f065944

Please sign in to comment.