Skip to content

Commit 369ef8d

Browse files
authored
feat: label option for string list command (#722)
1 parent bb57d11 commit 369ef8d

File tree

8 files changed

+84
-27
lines changed

8 files changed

+84
-27
lines changed

src/main/java/com/crowdin/cli/commands/Actions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ NewAction<ProjectProperties, ProjectClient> stringEdit(
6060
boolean noProgress, Long id, String identifier, String newText, String newContext, Integer newMaxLength, List<String> labelNames, Boolean isHidden);
6161

6262
NewAction<ProjectProperties, ProjectClient> stringList(
63-
boolean noProgress, boolean isVerbose, String file, String filter, String branchName, String croql);
63+
boolean noProgress, boolean isVerbose, String file, String filter, String branchName, List<String> labelNames, String croql);
6464

6565
NewAction<PropertiesWithFiles, ProjectClient> uploadSources(
6666
String branchName, boolean deleteObsolete, boolean noProgress, boolean autoUpdate, boolean debug, boolean plainView);

src/main/java/com/crowdin/cli/commands/actions/CliActions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ public NewAction<ProjectProperties, ProjectClient> stringEdit(
106106

107107
@Override
108108
public NewAction<ProjectProperties, ProjectClient> stringList(
109-
boolean noProgress, boolean isVerbose, String file, String filter, String branchName, String croql
109+
boolean noProgress, boolean isVerbose, String file, String filter, String branchName, List<String> labelNames, String croql
110110
) {
111-
return new StringListAction(noProgress, isVerbose, file, filter, branchName, croql);
111+
return new StringListAction(noProgress, isVerbose, file, filter, branchName, labelNames, croql);
112112
}
113113

114114
@Override

src/main/java/com/crowdin/cli/commands/actions/StringListAction.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
2525
import static com.crowdin.cli.utils.console.ExecutionStatus.WARNING;
26+
import static java.util.Objects.nonNull;
2627

2728
class StringListAction implements NewAction<ProjectProperties, ProjectClient> {
2829

@@ -31,14 +32,16 @@ class StringListAction implements NewAction<ProjectProperties, ProjectClient> {
3132
private final String file;
3233
private final String filter;
3334
private final String branchName;
35+
private final List<String> labelNames;
3436
private final String croql;
3537

36-
public StringListAction(boolean noProgress, boolean isVerbose, String file, String filter, String branchName, String croql) {
38+
public StringListAction(boolean noProgress, boolean isVerbose, String file, String filter, String branchName, List<String> labelNames, String croql) {
3739
this.noProgress = noProgress;
3840
this.isVerbose = isVerbose;
3941
this.file = file;
4042
this.filter = filter;
4143
this.branchName = branchName;
44+
this.labelNames = labelNames;
4245
this.croql = croql;
4346
}
4447

@@ -52,7 +55,8 @@ public void act(Outputter out, ProjectProperties pb, ProjectClient client) {
5255
.map(Branch::getId)
5356
.orElse(null);
5457

55-
Map<Long, String> labels = client.listLabels().stream()
58+
List<Label> labels = client.listLabels();
59+
Map<Long, String> labelsMap = labels.stream()
5660
.collect(Collectors.toMap(Label::getId, Label::getTitle));
5761

5862
Map<String, FileInfo> paths = null;
@@ -65,28 +69,30 @@ public void act(Outputter out, ProjectProperties pb, ProjectClient client) {
6569
}
6670
Map<Long, String> finalReversePaths = reversePaths;
6771

68-
String encodedFilter = filter != null ? Utils.encodeURL(filter) : null;
69-
String encodedCroql = croql != null ? Utils.encodeURL(croql) : null;
72+
String encodedFilter = nonNull(filter) ? Utils.encodeURL(filter) : null;
73+
String encodedCroql = nonNull(croql) ? Utils.encodeURL(croql) : null;
74+
String labelIds = nonNull(labelNames) ? prepareLabelIds(labels) : null;
75+
String fullPath = nonNull(branchName) ? (branchName + Utils.PATH_SEPARATOR + file) : file;
7076

7177
List<SourceString> sourceStrings;
7278
if (StringUtils.isEmpty(file)) {
73-
sourceStrings = client.listSourceString(null, branchId, null, encodedFilter, encodedCroql);
79+
sourceStrings = client.listSourceString(null, branchId, labelIds, encodedFilter, encodedCroql);
7480
} else {
7581
if (isStringsBasedProject) {
7682
throw new RuntimeException(RESOURCE_BUNDLE.getString("message.no_file_string_project"));
7783
}
78-
if (paths.containsKey(file)) {
79-
sourceStrings = client.listSourceString(paths.get(file).getId(), branchId, null, encodedFilter, encodedCroql);
84+
if (paths.containsKey(fullPath)) {
85+
sourceStrings = client.listSourceString(paths.get(fullPath).getId(), branchId, labelIds, encodedFilter, encodedCroql);
8086
} else {
81-
throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.file_not_exists"), file));
87+
throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.file_not_exists"), fullPath));
8288
}
8389
}
8490
if (sourceStrings.isEmpty()) {
8591
out.println(WARNING.withIcon(RESOURCE_BUNDLE.getString("message.source_string_list_not_found")));
8692
}
8793
sourceStrings.forEach(ss -> {
8894
String labelsString = (ss.getLabelIds() != null)
89-
? ss.getLabelIds().stream().map(labels::get).map(s -> String.format("[@|cyan %s|@]", s)).collect(Collectors.joining(" "))
95+
? ss.getLabelIds().stream().map(labelsMap::get).map(s -> String.format("[@|cyan %s|@]", s)).collect(Collectors.joining(" "))
9096
: "";
9197
out.println(String.format(RESOURCE_BUNDLE.getString("message.source_string_list_text"), ss.getId(), ss.getText(), labelsString));
9298
if (isVerbose) {
@@ -106,4 +112,14 @@ public void act(Outputter out, ProjectProperties pb, ProjectClient client) {
106112
}
107113
});
108114
}
115+
116+
private String prepareLabelIds(List<Label> labels) {
117+
Map<String, Long> labelsMap = labels.stream()
118+
.collect(Collectors.toMap(Label::getTitle, Label::getId));
119+
120+
return labelNames.stream()
121+
.map(labelsMap::get)
122+
.map(String::valueOf)
123+
.collect(Collectors.joining(","));
124+
}
109125
}

src/main/java/com/crowdin/cli/commands/picocli/StringListSubcommand.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class StringListSubcommand extends ActCommandProject {
2626
@CommandLine.Option(names = {"-b", "--branch"}, paramLabel = "...", order = -2)
2727
protected String branchName;
2828

29+
@CommandLine.Option(names = {"--label"}, paramLabel = "...", descriptionKey = "params.label", order = -2)
30+
protected List<String> labelNames;
31+
2932
@CommandLine.Option(names = {"--croql"}, paramLabel = "...", order = -2)
3033
protected String croql;
3134

@@ -39,6 +42,6 @@ protected List<String> checkOptions() {
3942

4043
@Override
4144
protected NewAction<ProjectProperties, ProjectClient> getAction(Actions actions) {
42-
return actions.stringList(noProgress, isVerbose, file, filter, branchName, croql);
45+
return actions.stringList(noProgress, isVerbose, file, filter, branchName, labelNames, croql);
4346
}
4447
}

src/test/java/com/crowdin/cli/commands/actions/CliActionsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void testStringEdit() {
6464

6565
@Test
6666
public void testStringList() {
67-
assertNotNull(actions.stringList(false, false, null, null, null, null));
67+
assertNotNull(actions.stringList(false, false, null, null, null, null, null));
6868
}
6969

7070
@Test

src/test/java/com/crowdin/cli/commands/actions/StringListActionTest.java

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.crowdin.cli.properties.PropertiesWithFiles;
1212
import com.crowdin.cli.properties.NewPropertiesWithFilesUtilBuilder;
1313
import com.crowdin.cli.utils.Utils;
14+
import com.crowdin.client.labels.model.Label;
1415
import com.crowdin.client.projectsgroups.model.Type;
1516
import org.junit.jupiter.api.Test;
1617
import org.junit.jupiter.params.ParameterizedTest;
@@ -22,6 +23,7 @@
2223

2324
import static org.junit.jupiter.api.Assertions.assertThrows;
2425
import static org.junit.jupiter.params.provider.Arguments.arguments;
26+
import static org.mockito.ArgumentMatchers.eq;
2527
import static org.mockito.Mockito.mock;
2628
import static org.mockito.Mockito.verify;
2729
import static org.mockito.Mockito.verifyNoMoreInteractions;
@@ -35,25 +37,25 @@ public class StringListActionTest {
3537

3638
@ParameterizedTest
3739
@MethodSource
38-
public void testStringList(String file, String filter) throws ResponseException {
40+
public void testStringList(String file, String filter) {
3941
NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder
4042
.minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%")
4143
.setBasePath(Utils.PATH_SEPARATOR);
4244
pb = pbBuilder.build();
43-
CrowdinProjectFull projectFull = ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId()))
44-
.addFile("first.csv", "csv", 101L, null, null).build();
45+
CrowdinProjectFull projectFull = ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId())).addBranches(3L, "main")
46+
.addFile("first.csv", "csv", 101L, null, 3L).build();
4547
projectFull.setType(Type.FILES_BASED);
46-
when(client.downloadFullProject(null))
48+
when(client.downloadFullProject("main"))
4749
.thenReturn(projectFull);
4850
when(client.listSourceString(101L, null, null, filter, null))
4951
.thenReturn(Arrays.asList(SourceStringBuilder.standard()
5052
.setProjectId(Long.parseLong(pb.getProjectId()))
5153
.setIdentifiers(701L, "7-0-1", "seven-o-one", "7.0.1", 101L).build()));
5254

53-
action = new StringListAction(true, true, file, filter, null, null);
55+
action = new StringListAction(true, true, file, filter, "main", null, null);
5456
action.act(Outputter.getDefault(), pb, client);
5557

56-
verify(client).downloadFullProject(null);
58+
verify(client).downloadFullProject(eq("main"));
5759
verify(client).listLabels();
5860
if (file != null) {
5961
verify(client).listSourceString(101L, null, null, filter, null);
@@ -80,7 +82,7 @@ public void testGetProjectThrows() throws ResponseException {
8082
when(client.downloadFullProject(null))
8183
.thenThrow(new RuntimeException("Whoops"));
8284

83-
action = new StringListAction(true, true, null, null, null, null);
85+
action = new StringListAction(true, true, null, null, null, null, null);
8486
assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client));
8587

8688
verify(client).downloadFullProject(null);
@@ -98,7 +100,7 @@ public void testFileNotExistThrows() throws ResponseException {
98100
.thenReturn(ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId()))
99101
.addFile("first.csv", "csv", 101L, null, null).build());
100102

101-
action = new StringListAction(true, true, "nonexistent.csv", null, null, null);
103+
action = new StringListAction(true, true, "nonexistent.csv", null, null, null, null);
102104
assertThrows(RuntimeException.class, () -> action.act(Outputter.getDefault(), pb, client));
103105

104106
verify(client).downloadFullProject(null);
@@ -122,12 +124,48 @@ public void testStringList_StringsBasedProject() {
122124
.setProjectId(Long.parseLong(pb.getProjectId()))
123125
.setIdentifiers(701L, "7-0-1", "seven-o-one", "7.0.1", 101L).build()));
124126

125-
action = new StringListAction(true, true, null, null, null, null);
127+
action = new StringListAction(true, true, null, null, null, null, null);
126128
action.act(Outputter.getDefault(), pb, client);
127129

128130
verify(client).downloadFullProject(null);
129131
verify(client).listLabels();
130132
verify(client).listSourceString(null, null, null, null, null);
131133
verifyNoMoreInteractions(client);
132134
}
135+
136+
@Test
137+
public void testStringListLabels() {
138+
NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder
139+
.minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%")
140+
.setBasePath(Utils.PATH_SEPARATOR);
141+
pb = pbBuilder.build();
142+
CrowdinProjectFull projectFull = ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId())).addBranches(3L, "main")
143+
.addFile("first.csv", "csv", 101L, null, null).build();
144+
projectFull.setType(Type.FILES_BASED);
145+
Label label1 = new Label() {{
146+
setId(4L);
147+
setTitle("l1");
148+
}};
149+
Label label2 = new Label() {{
150+
setId(5L);
151+
setTitle("l2");
152+
}};
153+
154+
when(client.downloadFullProject(null))
155+
.thenReturn(projectFull);
156+
when(client.listSourceString(101L, null, null, null, null))
157+
.thenReturn(Arrays.asList(SourceStringBuilder.standard()
158+
.setProjectId(Long.parseLong(pb.getProjectId()))
159+
.setIdentifiers(701L, "7-0-1", "seven-o-one", "7.0.1", 101L).build()));
160+
when(client.listLabels()).thenReturn(Arrays.asList(label1, label2));
161+
162+
action = new StringListAction(
163+
true, true, "first.csv", null, null, Arrays.asList("l1", "l2"), null);
164+
action.act(Outputter.getDefault(), pb, client);
165+
166+
verify(client).downloadFullProject(null);
167+
verify(client).listLabels();
168+
verify(client).listSourceString(101L, null, "4,5", null, null);
169+
verifyNoMoreInteractions(client);
170+
}
133171
}

src/test/java/com/crowdin/cli/commands/picocli/PicocliTestUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void mockActions() {
8080
.thenReturn(actionMock);
8181
when(actionsMock.stringEdit(anyBoolean(), any(), any(), any(), any(), any(), any(), any()))
8282
.thenReturn(actionMock);
83-
when(actionsMock.stringList(anyBoolean(), anyBoolean(), any(), any(), any(), any()))
83+
when(actionsMock.stringList(anyBoolean(), anyBoolean(), any(), any(), any(), any(), any()))
8484
.thenReturn(actionMock);
8585
when(actionsMock.uploadSources(any(), anyBoolean(), anyBoolean(), anyBoolean(), anyBoolean(), anyBoolean()))
8686
.thenReturn(actionMock);

src/test/java/com/crowdin/cli/commands/picocli/StringListSubcommandTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ public class StringListSubcommandTest extends PicocliTestUtils {
1212
public void testStringList() {
1313
this.execute(CommandNames.STRING, CommandNames.STRING_LIST);
1414
verify(actionsMock)
15-
.stringList(anyBoolean(), anyBoolean(), any(), any(), any(), any());
15+
.stringList(anyBoolean(), anyBoolean(), any(), any(), any(), any(), any());
1616
this.check(true);
1717
}
1818

1919
@Test
2020
public void testStringList2() {
2121
this.execute(CommandNames.STRING, CommandNames.STRING_LIST, "--file", "some/path/to/file.txt");
2222
verify(actionsMock)
23-
.stringList(anyBoolean(), anyBoolean(), any(), any(), any(), any());
23+
.stringList(anyBoolean(), anyBoolean(), any(), any(), any(), any(), any());
2424
this.check(true);
2525
}
26-
}
26+
}

0 commit comments

Comments
 (0)