From 32ea5f68c28c650cdbe459d0bcdb363b0dfe2490 Mon Sep 17 00:00:00 2001 From: Yevheniy Oliynyk Date: Mon, 14 Oct 2024 08:57:12 +0200 Subject: [PATCH] feat: upload screenshots improvements (#853) --- .../crowdin/cli/client/ClientScreenshot.java | 8 ++- .../cli/client/CrowdinClientScreenshot.java | 23 ++++++- .../actions/ScreenshotUploadAction.java | 67 ++++++++++++------- .../actions/ScreenshotUploadActionTest.java | 23 ++++--- versions.properties | 2 +- 5 files changed, 80 insertions(+), 43 deletions(-) diff --git a/src/main/java/com/crowdin/cli/client/ClientScreenshot.java b/src/main/java/com/crowdin/cli/client/ClientScreenshot.java index a61981ba..2934ddb7 100644 --- a/src/main/java/com/crowdin/cli/client/ClientScreenshot.java +++ b/src/main/java/com/crowdin/cli/client/ClientScreenshot.java @@ -1,8 +1,6 @@ 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; @@ -10,6 +8,8 @@ public interface ClientScreenshot extends Client { List listScreenshots(Long stringId); + List listScreenshotsByName(String fileName); + Screenshot getScreenshot(Long id); Screenshot uploadScreenshot(AddScreenshotRequest request) throws ResponseException; @@ -17,4 +17,6 @@ public interface ClientScreenshot extends Client { Screenshot updateScreenshot(Long screenshotId, UpdateScreenshotRequest request); void deleteScreenshot(Long id); + + void replaceTags(Long screenshotId, AutoTagReplaceTagsRequest request); } diff --git a/src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java b/src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java index 1a355b2b..c554fb4d 100644 --- a/src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java +++ b/src/main/java/com/crowdin/cli/client/CrowdinClientScreenshot.java @@ -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; @@ -25,6 +23,17 @@ public List listScreenshots(Long stringId) { .listScreenshots(parseLong(this.projectId), stringId, limit, offset)); } + @Override + public List 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() @@ -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; + }); + } } diff --git a/src/main/java/com/crowdin/cli/commands/actions/ScreenshotUploadAction.java b/src/main/java/com/crowdin/cli/commands/actions/ScreenshotUploadAction.java index 4b0ec359..4eadb125 100644 --- a/src/main/java/com/crowdin/cli/commands/actions/ScreenshotUploadAction.java +++ b/src/main/java/com/crowdin/cli/commands/actions/ScreenshotUploadAction.java @@ -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; @@ -49,57 +50,73 @@ class ScreenshotUploadAction implements NewAction screenshotList = client.listScreenshots(null); - Optional 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 screenshotList = client.listScreenshotsByName(file.getName()); + Optional 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); diff --git a/src/test/java/com/crowdin/cli/commands/actions/ScreenshotUploadActionTest.java b/src/test/java/com/crowdin/cli/commands/actions/ScreenshotUploadActionTest.java index 492fbde2..38c34a5f 100644 --- a/src/test/java/com/crowdin/cli/commands/actions/ScreenshotUploadActionTest.java +++ b/src/test/java/com/crowdin/cli/commands/actions/ScreenshotUploadActionTest.java @@ -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() {{ @@ -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); } @@ -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); @@ -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); } @@ -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); @@ -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); @@ -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); @@ -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() {{ @@ -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(); @@ -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() {{ @@ -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(); diff --git a/versions.properties b/versions.properties index 61f9cd69..5808f96e 100644 --- a/versions.properties +++ b/versions.properties @@ -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