Skip to content

Commit 32ea5f6

Browse files
authored
feat: upload screenshots improvements (#853)
1 parent af8bd46 commit 32ea5f6

File tree

5 files changed

+80
-43
lines changed

5 files changed

+80
-43
lines changed
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package com.crowdin.cli.client;
22

3-
import com.crowdin.client.screenshots.model.AddScreenshotRequest;
4-
import com.crowdin.client.screenshots.model.Screenshot;
5-
import com.crowdin.client.screenshots.model.UpdateScreenshotRequest;
3+
import com.crowdin.client.screenshots.model.*;
64

75
import java.util.List;
86

97
public interface ClientScreenshot extends Client {
108

119
List<Screenshot> listScreenshots(Long stringId);
1210

11+
List<Screenshot> listScreenshotsByName(String fileName);
12+
1313
Screenshot getScreenshot(Long id);
1414

1515
Screenshot uploadScreenshot(AddScreenshotRequest request) throws ResponseException;
1616

1717
Screenshot updateScreenshot(Long screenshotId, UpdateScreenshotRequest request);
1818

1919
void deleteScreenshot(Long id);
20+
21+
void replaceTags(Long screenshotId, AutoTagReplaceTagsRequest request);
2022
}

src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package com.crowdin.cli.client;
22

33
import com.crowdin.client.Client;
4-
import com.crowdin.client.screenshots.model.AddScreenshotRequest;
5-
import com.crowdin.client.screenshots.model.Screenshot;
6-
import com.crowdin.client.screenshots.model.UpdateScreenshotRequest;
4+
import com.crowdin.client.screenshots.model.*;
75
import lombok.AllArgsConstructor;
86

97
import java.util.LinkedHashMap;
@@ -25,6 +23,17 @@ public List<Screenshot> listScreenshots(Long stringId) {
2523
.listScreenshots(parseLong(this.projectId), stringId, limit, offset));
2624
}
2725

26+
@Override
27+
public List<Screenshot> listScreenshotsByName(String fileName) {
28+
return executeRequestFullList((limit, offset) -> {
29+
var params = new ListScreenshotsParams();
30+
params.setOffset(offset);
31+
params.setLimit(limit);
32+
params.setSearch(fileName);
33+
return this.client.getScreenshotsApi().listScreenshots(parseLong(this.projectId), params);
34+
});
35+
}
36+
2837
@Override
2938
public Screenshot getScreenshot(Long id) {
3039
return executeRequest(() -> this.client.getScreenshotsApi()
@@ -57,4 +66,12 @@ public void deleteScreenshot(Long id) {
5766
return null;
5867
});
5968
}
69+
70+
@Override
71+
public void replaceTags(Long screenshotId, AutoTagReplaceTagsRequest request) {
72+
executeRequest(() -> {
73+
this.client.getScreenshotsApi().replaceTags(parseLong(this.projectId), screenshotId, request);
74+
return null;
75+
});
76+
}
6077
}

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

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.crowdin.cli.utils.console.ConsoleSpinner;
1414
import com.crowdin.client.labels.model.Label;
1515
import com.crowdin.client.screenshots.model.AddScreenshotRequest;
16+
import com.crowdin.client.screenshots.model.AutoTagReplaceTagsRequest;
1617
import com.crowdin.client.screenshots.model.Screenshot;
1718
import com.crowdin.client.screenshots.model.UpdateScreenshotRequest;
1819
import com.crowdin.client.sourcefiles.model.Branch;
@@ -49,57 +50,73 @@ class ScreenshotUploadAction implements NewAction<ProjectProperties, ClientScree
4950

5051
@Override
5152
public void act(Outputter out, ProjectProperties properties, ClientScreenshot client) {
52-
List<Screenshot> screenshotList = client.listScreenshots(null);
53-
Optional<Screenshot> existingScreenshot = screenshotList.stream()
54-
.filter((s) -> file.getName().equals(s.getName())).findFirst();
55-
if (existingScreenshot.isPresent()) {
56-
UpdateScreenshotRequest request = new UpdateScreenshotRequest();
57-
request.setStorageId(uploadToStorage(file));
58-
request.setName(file.getName());
59-
try {
60-
Screenshot screenshot = client.updateScreenshot(existingScreenshot.get().getId(), request);
61-
if (!plainView) {
62-
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.list"),
63-
screenshot.getId(), screenshot.getTagsCount(), screenshot.getName())));
64-
} else {
65-
out.println(file.getName());
66-
}
67-
} catch (Exception e) {
68-
throw ExitCodeExceptionMapper.remap(e, String.format(RESOURCE_BUNDLE.getString("error.screenshot.not_updated"), request));
69-
}
70-
return;
71-
}
72-
73-
AddScreenshotRequest request = new AddScreenshotRequest();
7453
CrowdinProjectFull project = ConsoleSpinner.execute(
7554
out,
7655
"message.spinner.fetching_project_info", "error.collect_project_info",
7756
this.noProgress,
7857
this.plainView,
7958
() -> this.projectClient.downloadFullProject());
59+
60+
Long branchId = null;
8061
if (nonNull(branchName)) {
8162
Branch branch = project.findBranchByName(branchName)
8263
.orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.branch_not_exists"), branchName)));
83-
request.setBranchId(branch.getId());
64+
branchId = branch.getId();
8465
}
66+
Long fileId = null;
8567
if (nonNull(pathToSourceFile)) {
8668
final String normalizedPath = Utils.toUnixPath(Utils.sepAtStart(pathToSourceFile));
8769
FileInfo fileInfo = project.getFileInfos().stream()
8870
.filter(f -> normalizedPath.equals(f.getPath())).findFirst()
8971
.orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.file_not_exists"), pathToSourceFile)));
90-
request.setFileId(fileInfo.getId());
72+
fileId = fileInfo.getId();
9173
}
74+
Long directoryId = null;
9275
if (nonNull(directoryPath)) {
9376
final String normalizedPath = Utils.toUnixPath(Utils.sepAtStart(directoryPath));
9477
Directory directory = project.getDirectories().values().stream()
9578
.filter(d -> normalizedPath.equals(d.getPath())).findFirst()
9679
.orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.dir_not_exists"), directoryPath)));
97-
request.setDirectoryId(directory.getId());
80+
directoryId = directory.getId();
9881
}
82+
83+
List<Screenshot> screenshotList = client.listScreenshotsByName(file.getName());
84+
Optional<Screenshot> existingScreenshot = screenshotList.stream().findFirst();
85+
if (existingScreenshot.isPresent()) {
86+
UpdateScreenshotRequest request = new UpdateScreenshotRequest();
87+
request.setStorageId(uploadToStorage(file));
88+
request.setName(file.getName());
89+
request.setUsePreviousTags(!autoTag);
90+
try {
91+
Screenshot screenshot = client.updateScreenshot(existingScreenshot.get().getId(), request);
92+
if (autoTag) {
93+
AutoTagReplaceTagsRequest autoTagReplaceTagsRequest = new AutoTagReplaceTagsRequest();
94+
autoTagReplaceTagsRequest.setAutoTag(true);
95+
autoTagReplaceTagsRequest.setBranchId(branchId);
96+
autoTagReplaceTagsRequest.setDirectoryId(directoryId);
97+
autoTagReplaceTagsRequest.setFileId(fileId);
98+
client.replaceTags(existingScreenshot.get().getId(), autoTagReplaceTagsRequest);
99+
}
100+
if (!plainView) {
101+
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.list"),
102+
screenshot.getId(), screenshot.getTagsCount(), screenshot.getName())));
103+
} else {
104+
out.println(file.getName());
105+
}
106+
} catch (Exception e) {
107+
throw ExitCodeExceptionMapper.remap(e, String.format(RESOURCE_BUNDLE.getString("error.screenshot.not_updated"), request));
108+
}
109+
return;
110+
}
111+
112+
AddScreenshotRequest request = new AddScreenshotRequest();
99113
if (nonNull(labelNames) && !labelNames.isEmpty()) {
100114
request.setLabelIds(prepareLabelIds());
101115
}
102116

117+
request.setBranchId(branchId);
118+
request.setDirectoryId(directoryId);
119+
request.setFileId(fileId);
103120
request.setStorageId(uploadToStorage(file));
104121
request.setName(file.getName());
105122
request.setAutoTag(autoTag);

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void testUploadScreenshot(String fileName, String sourceFilePath, Long so
9595
when(projectFull.getDirectories()).thenReturn(nonNull(directoryId) ? directories : new HashMap<>());
9696

9797
when(projectClient.uploadStorage(eq(fileName), any())).thenReturn(1L);
98-
when(client.listScreenshots(null)).thenReturn(new ArrayList<>());
98+
when(client.listScreenshotsByName(eq(fileName))).thenReturn(new ArrayList<>());
9999

100100
when(client.uploadScreenshot(request))
101101
.thenReturn(new Screenshot() {{
@@ -106,7 +106,7 @@ public void testUploadScreenshot(String fileName, String sourceFilePath, Long so
106106
action = new ScreenshotUploadAction(fileToUpload, branchName, labelNames, sourceFilePath, directoryPath, autoTag, false, false, projectClient);
107107
action.act(Outputter.getDefault(), pb, client);
108108

109-
verify(client).listScreenshots(null);
109+
verify(client).listScreenshotsByName(eq(fileName));
110110
verify(client).uploadScreenshot(request);
111111
verifyNoMoreInteractions(client);
112112
}
@@ -134,11 +134,12 @@ public void testUploadScreenshotToUpdate() throws ResponseException {
134134

135135
when(screenshot.getName()).thenReturn(fileName);
136136
when(screenshot.getId()).thenReturn(123L);
137-
when(client.listScreenshots(null)).thenReturn(Arrays.asList(screenshot));
137+
when(client.listScreenshotsByName(eq(fileName))).thenReturn(Arrays.asList(screenshot));
138138

139139
UpdateScreenshotRequest request = new UpdateScreenshotRequest();
140140
request.setStorageId(1L);
141141
request.setName(fileName);
142+
request.setUsePreviousTags(true);
142143

143144
ProjectClient projectClient = mock(ProjectClient.class);
144145
when(projectClient.uploadStorage(eq(fileName), any())).thenReturn(1L);
@@ -152,7 +153,7 @@ public void testUploadScreenshotToUpdate() throws ResponseException {
152153
action = new ScreenshotUploadAction(fileToUpload, null, null, null, null, false, false, false, projectClient);
153154
action.act(Outputter.getDefault(), pb, client);
154155

155-
verify(client).listScreenshots(null);
156+
verify(client).listScreenshotsByName(eq(fileName));
156157
verify(client).updateScreenshot(123L, request);
157158
verifyNoMoreInteractions(client);
158159
}
@@ -165,7 +166,7 @@ public void testUploadScreenshotNotExistingBranch() {
165166
PropertiesWithFiles pb = pbBuilder.build();
166167

167168
ClientScreenshot client = mock(ClientScreenshot.class);
168-
when(client.listScreenshots(null)).thenReturn(new ArrayList<>());
169+
when(client.listScreenshotsByName(eq("screenshot.png"))).thenReturn(new ArrayList<>());
169170

170171
ProjectClient projectClient = mock(ProjectClient.class);
171172
CrowdinProjectFull projectFull = mock(CrowdinProjectFull.class);
@@ -185,7 +186,7 @@ public void testUploadScreenshotNotExistingSourceFile() {
185186
PropertiesWithFiles pb = pbBuilder.build();
186187

187188
ClientScreenshot client = mock(ClientScreenshot.class);
188-
when(client.listScreenshots(null)).thenReturn(new ArrayList<>());
189+
when(client.listScreenshotsByName("screenshot.png")).thenReturn(new ArrayList<>());
189190

190191
ProjectClient projectClient = mock(ProjectClient.class);
191192
CrowdinProjectFull projectFull = mock(CrowdinProjectFull.class);
@@ -205,7 +206,7 @@ public void testUploadScreenshotNotExistingDirectory() {
205206
PropertiesWithFiles pb = pbBuilder.build();
206207

207208
ClientScreenshot client = mock(ClientScreenshot.class);
208-
when(client.listScreenshots(null)).thenReturn(new ArrayList<>());
209+
when(client.listScreenshotsByName("screenshot.png")).thenReturn(new ArrayList<>());
209210

210211
ProjectClient projectClient = mock(ProjectClient.class);
211212
CrowdinProjectFull projectFull = mock(CrowdinProjectFull.class);
@@ -251,7 +252,7 @@ public void testUploadScreenshotWithLabels() throws ResponseException {
251252

252253
when(projectClient.uploadStorage(eq("screenshot.png"), any())).thenReturn(1L);
253254
when(projectClient.listLabels()).thenReturn(Arrays.asList(label1, label2));
254-
when(client.listScreenshots(null)).thenReturn(new ArrayList<>());
255+
when(client.listScreenshotsByName(eq(fileToUpload.getName()))).thenReturn(new ArrayList<>());
255256

256257
when(client.uploadScreenshot(request))
257258
.thenReturn(new Screenshot() {{
@@ -262,7 +263,7 @@ public void testUploadScreenshotWithLabels() throws ResponseException {
262263
action = new ScreenshotUploadAction(fileToUpload, null, Arrays.asList("label1", "label2"), null, null, false, false, false, projectClient);
263264
action.act(Outputter.getDefault(), pb, client);
264265

265-
verify(client).listScreenshots(null);
266+
verify(client).listScreenshotsByName(eq(fileToUpload.getName()));
266267
verify(client).uploadScreenshot(request);
267268
verify(projectClient).downloadFullProject();
268269
verify(projectClient).listLabels();
@@ -303,7 +304,7 @@ public void testUploadScreenshotNotExistingLabel() throws ResponseException {
303304
when(projectClient.uploadStorage(eq("screenshot.png"), any())).thenReturn(1L);
304305
when(projectClient.listLabels()).thenReturn(new ArrayList<>());
305306
when(projectClient.addLabel(any())).thenReturn(label1);
306-
when(client.listScreenshots(null)).thenReturn(new ArrayList<>());
307+
when(client.listScreenshotsByName(eq(fileToUpload.getName()))).thenReturn(new ArrayList<>());
307308

308309
when(client.uploadScreenshot(request))
309310
.thenReturn(new Screenshot() {{
@@ -314,7 +315,7 @@ public void testUploadScreenshotNotExistingLabel() throws ResponseException {
314315
action = new ScreenshotUploadAction(fileToUpload, null, Arrays.asList("label1"), null, null, false, false, false, projectClient);
315316
action.act(Outputter.getDefault(), pb, client);
316317

317-
verify(client).listScreenshots(null);
318+
verify(client).listScreenshotsByName(eq(fileToUpload.getName()));
318319
verify(client).uploadScreenshot(request);
319320
verify(projectClient).downloadFullProject();
320321
verify(projectClient).listLabels();

versions.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ version.commons-io..commons-io=2.16.1
4343

4444
version.commons-cli..commons-cli=1.7.0
4545

46-
version.com.github.crowdin..crowdin-api-client-java=1.17.1
46+
version.com.github.crowdin..crowdin-api-client-java=1.18.1
4747

4848
plugin.org.asciidoctor.jvm.convert=3.3.2
4949

0 commit comments

Comments
 (0)