Skip to content

Commit

Permalink
feat: upload screenshots improvements (#853)
Browse files Browse the repository at this point in the history
  • Loading branch information
yevheniyJ authored Oct 14, 2024
1 parent af8bd46 commit 32ea5f6
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 43 deletions.
8 changes: 5 additions & 3 deletions src/main/java/com/crowdin/cli/client/ClientScreenshot.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package com.crowdin.cli.client;

import com.crowdin.client.screenshots.model.AddScreenshotRequest;
import com.crowdin.client.screenshots.model.Screenshot;
import com.crowdin.client.screenshots.model.UpdateScreenshotRequest;
import com.crowdin.client.screenshots.model.*;

import java.util.List;

public interface ClientScreenshot extends Client {

List<Screenshot> listScreenshots(Long stringId);

List<Screenshot> listScreenshotsByName(String fileName);

Screenshot getScreenshot(Long id);

Screenshot uploadScreenshot(AddScreenshotRequest request) throws ResponseException;

Screenshot updateScreenshot(Long screenshotId, UpdateScreenshotRequest request);

void deleteScreenshot(Long id);

void replaceTags(Long screenshotId, AutoTagReplaceTagsRequest request);
}
23 changes: 20 additions & 3 deletions src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.crowdin.cli.client;

import com.crowdin.client.Client;
import com.crowdin.client.screenshots.model.AddScreenshotRequest;
import com.crowdin.client.screenshots.model.Screenshot;
import com.crowdin.client.screenshots.model.UpdateScreenshotRequest;
import com.crowdin.client.screenshots.model.*;
import lombok.AllArgsConstructor;

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

@Override
public List<Screenshot> listScreenshotsByName(String fileName) {
return executeRequestFullList((limit, offset) -> {
var params = new ListScreenshotsParams();
params.setOffset(offset);
params.setLimit(limit);
params.setSearch(fileName);
return this.client.getScreenshotsApi().listScreenshots(parseLong(this.projectId), params);
});
}

@Override
public Screenshot getScreenshot(Long id) {
return executeRequest(() -> this.client.getScreenshotsApi()
Expand Down Expand Up @@ -57,4 +66,12 @@ public void deleteScreenshot(Long id) {
return null;
});
}

@Override
public void replaceTags(Long screenshotId, AutoTagReplaceTagsRequest request) {
executeRequest(() -> {
this.client.getScreenshotsApi().replaceTags(parseLong(this.projectId), screenshotId, request);
return null;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.crowdin.cli.utils.console.ConsoleSpinner;
import com.crowdin.client.labels.model.Label;
import com.crowdin.client.screenshots.model.AddScreenshotRequest;
import com.crowdin.client.screenshots.model.AutoTagReplaceTagsRequest;
import com.crowdin.client.screenshots.model.Screenshot;
import com.crowdin.client.screenshots.model.UpdateScreenshotRequest;
import com.crowdin.client.sourcefiles.model.Branch;
Expand Down Expand Up @@ -49,57 +50,73 @@ class ScreenshotUploadAction implements NewAction<ProjectProperties, ClientScree

@Override
public void act(Outputter out, ProjectProperties properties, ClientScreenshot client) {
List<Screenshot> screenshotList = client.listScreenshots(null);
Optional<Screenshot> existingScreenshot = screenshotList.stream()
.filter((s) -> file.getName().equals(s.getName())).findFirst();
if (existingScreenshot.isPresent()) {
UpdateScreenshotRequest request = new UpdateScreenshotRequest();
request.setStorageId(uploadToStorage(file));
request.setName(file.getName());
try {
Screenshot screenshot = client.updateScreenshot(existingScreenshot.get().getId(), request);
if (!plainView) {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.list"),
screenshot.getId(), screenshot.getTagsCount(), screenshot.getName())));
} else {
out.println(file.getName());
}
} catch (Exception e) {
throw ExitCodeExceptionMapper.remap(e, String.format(RESOURCE_BUNDLE.getString("error.screenshot.not_updated"), request));
}
return;
}

AddScreenshotRequest request = new AddScreenshotRequest();
CrowdinProjectFull project = ConsoleSpinner.execute(
out,
"message.spinner.fetching_project_info", "error.collect_project_info",
this.noProgress,
this.plainView,
() -> this.projectClient.downloadFullProject());

Long branchId = null;
if (nonNull(branchName)) {
Branch branch = project.findBranchByName(branchName)
.orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.branch_not_exists"), branchName)));
request.setBranchId(branch.getId());
branchId = branch.getId();
}
Long fileId = null;
if (nonNull(pathToSourceFile)) {
final String normalizedPath = Utils.toUnixPath(Utils.sepAtStart(pathToSourceFile));
FileInfo fileInfo = project.getFileInfos().stream()
.filter(f -> normalizedPath.equals(f.getPath())).findFirst()
.orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.file_not_exists"), pathToSourceFile)));
request.setFileId(fileInfo.getId());
fileId = fileInfo.getId();
}
Long directoryId = null;
if (nonNull(directoryPath)) {
final String normalizedPath = Utils.toUnixPath(Utils.sepAtStart(directoryPath));
Directory directory = project.getDirectories().values().stream()
.filter(d -> normalizedPath.equals(d.getPath())).findFirst()
.orElseThrow(() -> new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.dir_not_exists"), directoryPath)));
request.setDirectoryId(directory.getId());
directoryId = directory.getId();
}

List<Screenshot> screenshotList = client.listScreenshotsByName(file.getName());
Optional<Screenshot> existingScreenshot = screenshotList.stream().findFirst();
if (existingScreenshot.isPresent()) {
UpdateScreenshotRequest request = new UpdateScreenshotRequest();
request.setStorageId(uploadToStorage(file));
request.setName(file.getName());
request.setUsePreviousTags(!autoTag);
try {
Screenshot screenshot = client.updateScreenshot(existingScreenshot.get().getId(), request);
if (autoTag) {
AutoTagReplaceTagsRequest autoTagReplaceTagsRequest = new AutoTagReplaceTagsRequest();
autoTagReplaceTagsRequest.setAutoTag(true);
autoTagReplaceTagsRequest.setBranchId(branchId);
autoTagReplaceTagsRequest.setDirectoryId(directoryId);
autoTagReplaceTagsRequest.setFileId(fileId);
client.replaceTags(existingScreenshot.get().getId(), autoTagReplaceTagsRequest);
}
if (!plainView) {
out.println(OK.withIcon(String.format(RESOURCE_BUNDLE.getString("message.screenshot.list"),
screenshot.getId(), screenshot.getTagsCount(), screenshot.getName())));
} else {
out.println(file.getName());
}
} catch (Exception e) {
throw ExitCodeExceptionMapper.remap(e, String.format(RESOURCE_BUNDLE.getString("error.screenshot.not_updated"), request));
}
return;
}

AddScreenshotRequest request = new AddScreenshotRequest();
if (nonNull(labelNames) && !labelNames.isEmpty()) {
request.setLabelIds(prepareLabelIds());
}

request.setBranchId(branchId);
request.setDirectoryId(directoryId);
request.setFileId(fileId);
request.setStorageId(uploadToStorage(file));
request.setName(file.getName());
request.setAutoTag(autoTag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void testUploadScreenshot(String fileName, String sourceFilePath, Long so
when(projectFull.getDirectories()).thenReturn(nonNull(directoryId) ? directories : new HashMap<>());

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

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

verify(client).listScreenshots(null);
verify(client).listScreenshotsByName(eq(fileName));
verify(client).uploadScreenshot(request);
verifyNoMoreInteractions(client);
}
Expand Down Expand Up @@ -134,11 +134,12 @@ public void testUploadScreenshotToUpdate() throws ResponseException {

when(screenshot.getName()).thenReturn(fileName);
when(screenshot.getId()).thenReturn(123L);
when(client.listScreenshots(null)).thenReturn(Arrays.asList(screenshot));
when(client.listScreenshotsByName(eq(fileName))).thenReturn(Arrays.asList(screenshot));

UpdateScreenshotRequest request = new UpdateScreenshotRequest();
request.setStorageId(1L);
request.setName(fileName);
request.setUsePreviousTags(true);

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

verify(client).listScreenshots(null);
verify(client).listScreenshotsByName(eq(fileName));
verify(client).updateScreenshot(123L, request);
verifyNoMoreInteractions(client);
}
Expand All @@ -165,7 +166,7 @@ public void testUploadScreenshotNotExistingBranch() {
PropertiesWithFiles pb = pbBuilder.build();

ClientScreenshot client = mock(ClientScreenshot.class);
when(client.listScreenshots(null)).thenReturn(new ArrayList<>());
when(client.listScreenshotsByName(eq("screenshot.png"))).thenReturn(new ArrayList<>());

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

ClientScreenshot client = mock(ClientScreenshot.class);
when(client.listScreenshots(null)).thenReturn(new ArrayList<>());
when(client.listScreenshotsByName("screenshot.png")).thenReturn(new ArrayList<>());

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

ClientScreenshot client = mock(ClientScreenshot.class);
when(client.listScreenshots(null)).thenReturn(new ArrayList<>());
when(client.listScreenshotsByName("screenshot.png")).thenReturn(new ArrayList<>());

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

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

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

verify(client).listScreenshots(null);
verify(client).listScreenshotsByName(eq(fileToUpload.getName()));
verify(client).uploadScreenshot(request);
verify(projectClient).downloadFullProject();
verify(projectClient).listLabels();
Expand Down Expand Up @@ -303,7 +304,7 @@ public void testUploadScreenshotNotExistingLabel() throws ResponseException {
when(projectClient.uploadStorage(eq("screenshot.png"), any())).thenReturn(1L);
when(projectClient.listLabels()).thenReturn(new ArrayList<>());
when(projectClient.addLabel(any())).thenReturn(label1);
when(client.listScreenshots(null)).thenReturn(new ArrayList<>());
when(client.listScreenshotsByName(eq(fileToUpload.getName()))).thenReturn(new ArrayList<>());

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

verify(client).listScreenshots(null);
verify(client).listScreenshotsByName(eq(fileToUpload.getName()));
verify(client).uploadScreenshot(request);
verify(projectClient).downloadFullProject();
verify(projectClient).listLabels();
Expand Down
2 changes: 1 addition & 1 deletion versions.properties
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ version.commons-io..commons-io=2.16.1

version.commons-cli..commons-cli=1.7.0

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

plugin.org.asciidoctor.jvm.convert=3.3.2

Expand Down

0 comments on commit 32ea5f6

Please sign in to comment.