Skip to content

Commit

Permalink
feat: context parameter for file upload (#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
katerina20 authored May 20, 2024
1 parent 206ca40 commit b60f05e
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/crowdin/cli/commands/Actions.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ NewAction<PropertiesWithFiles, ProjectClient> preTranslate(

NewAction<ProjectProperties, ClientLabel> labelDelete(String title);

NewAction<ProjectProperties, ProjectClient> fileUpload(File file, String branch, boolean autoUpdate, List<String> labels, String destination, String type, Integer parserVersion, List<String> excludedLanguages, boolean plainView, boolean cleanupMode, boolean updateString);
NewAction<ProjectProperties, ProjectClient> fileUpload(File file, String branch, boolean autoUpdate, List<String> labels, String destination, String context, String type, Integer parserVersion, List<String> excludedLanguages, boolean plainView, boolean cleanupMode, boolean updateString);

NewAction<ProjectProperties, ProjectClient> fileUploadTranslation(File file, String branch, String dest, String languageId, boolean plainView);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ public NewAction<ProjectProperties, ClientLabel> labelDelete(String title) {
}

@Override
public NewAction<ProjectProperties, ProjectClient> fileUpload(File file, String branch, boolean autoUpdate, List<String> labels, String destination, String type, Integer parserVersion, List<String> excludedLanguages, boolean plainView, boolean cleanupMode, boolean updateStrings) {
return new FileUploadAction(file, branch, autoUpdate, labels, destination, type, parserVersion, cleanupMode, updateStrings, excludedLanguages, plainView);
public NewAction<ProjectProperties, ProjectClient> fileUpload(File file, String branch, boolean autoUpdate, List<String> labels, String destination, String context, String type, Integer parserVersion, List<String> excludedLanguages, boolean plainView, boolean cleanupMode, boolean updateStrings) {
return new FileUploadAction(file, branch, autoUpdate, labels, destination, context, type, parserVersion, cleanupMode, updateStrings, excludedLanguages, plainView);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class FileUploadAction implements NewAction<ProjectProperties, ProjectClient> {
private final boolean autoUpdate;
private final List<String> labels;
private final String dest;
private final String context;
private final String type;
private final Integer parserVersion;
private final boolean cleanupMode;
Expand All @@ -52,6 +53,10 @@ public void act(Outputter out, ProjectProperties properties, ProjectClient clien
this.plainView, this.plainView, client::downloadFullProject);
boolean isStringsBasedProject = Objects.equals(project.getType(), Type.STRINGS_BASED);

if (isStringsBasedProject && nonNull(context)) {
throw new ExitCodeExceptionMapper.ValidationException(RESOURCE_BUNDLE.getString("error.file.context_file_based_only"));
}

if (!project.isManagerAccess()) {
if (!plainView) {
out.println(WARNING.withIcon(RESOURCE_BUNDLE.getString("message.no_manager_access")));
Expand Down Expand Up @@ -145,6 +150,9 @@ public void act(Outputter out, ProjectProperties properties, ProjectClient clien
if (nonNull(parserVersion)) {
request.setParserVersion(parserVersion);
}
if (nonNull(context)) {
request.setContext(context);
}

Optional<Long> directoryId = getOrCreateDirectoryId(out, client, project, properties, branch.orElse(null));
directoryId.ifPresent(request::setDirectoryId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class FileUploadSubcommand extends ActCommandProject {
@Option(names = {"-d", "--dest"}, paramLabel = "...", descriptionKey = "crowdin.file.upload.dest", order = -3)
protected String destination;

@Option(names = {"--context"}, descriptionKey = "crowdin.file.upload.context", paramLabel = "...", order = -2)
protected String context;

@Option(names = {"--type"}, descriptionKey = "crowdin.file.upload.type", paramLabel = "...", order = -2)
protected String type;

Expand Down Expand Up @@ -69,6 +72,6 @@ protected NewAction<ProjectProperties, ProjectClient> getAction(Actions actions)
if (Objects.nonNull(languageId)) {
return actions.fileUploadTranslation(file, branch, destination, languageId, plainView);
}
return actions.fileUpload(file, branch, autoUpdate, labels, destination, type, parserVersion, excludedLanguages, plainView, cleanupMode, updateStrings);
return actions.fileUpload(file, branch, autoUpdate, labels, destination, context, type, parserVersion, excludedLanguages, plainView, cleanupMode, updateStrings);
}
}
2 changes: 2 additions & 0 deletions src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ crowdin.file.upload.usage.description=Upload a file to a Crowdin project
crowdin.file.upload.usage.customSynopsis=@|fg(green) crowdin file upload|@ <file> [CONFIG OPTIONS] [OPTIONS]
crowdin.file.upload.file=Path to file to upload
crowdin.file.upload.dest=File destination in the Crowdin project
crowdin.file.upload.context=File context
crowdin.file.upload.type=File type
crowdin.file.upload.parser=Parser version. Must be used together with the 'type' option
crowdin.file.upload.auto-update=Specify whether to update the file in the Crowdin project if it already exists
Expand Down Expand Up @@ -572,6 +573,7 @@ error.label.not_found=Couldn't find label by the specified title

error.file.dest_required='dest' parameter required to specify source file path
error.file.type_required='--type' is required for '--parser-version' option
error.file.context_file_based_only='--context' parameter is only available for file-based projects

error.branch.clone=Failed to clone the branch

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void testLabelDelete() {

@Test
void testFileUpload() {
assertNotNull(actions.fileUpload(null, null, false, null, null, null, null, null, false, true, false));
assertNotNull(actions.fileUpload(null, null, false, null, null, null, null, null, null, false, true, false));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.crowdin.cli.client.ResponseException;
import com.crowdin.cli.commands.NewAction;
import com.crowdin.cli.commands.Outputter;
import com.crowdin.cli.commands.picocli.ExitCodeExceptionMapper;
import com.crowdin.cli.properties.NewPropertiesWithFilesUtilBuilder;
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.cli.properties.PropertiesWithFiles;
Expand All @@ -25,6 +26,7 @@
import java.util.Arrays;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -59,7 +61,7 @@ public void testUpload_FileBasedProject() throws ResponseException {
.thenReturn(1L);
when(client.addSource(any())).thenReturn(new FileInfo());

NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, null, false, null, null, "po", 3, false, false, null, false);
NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, null, false, null, null, "context", "po", 3, false, false, null, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
Expand All @@ -68,6 +70,7 @@ public void testUpload_FileBasedProject() throws ResponseException {
setName("first.po");
setStorageId(1L);
setType("po");
setContext("context");
setParserVersion(3);
}};
verify(client).addSource(eq(addFileRequest));
Expand Down Expand Up @@ -103,7 +106,7 @@ public void testUpload_StringBasedProject() throws ResponseException {
when(client.addSourceStringsBased(any())).thenReturn(progress);
when(client.getUploadStringsStatus(any())).thenReturn(progressFinished);

NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, "branch", false, null, null, null, null, false, false, null, false);
NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, "branch", false, null, null, null, null, null, false, false, null, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
Expand Down Expand Up @@ -137,7 +140,7 @@ public void testUploadUpdate_FileBasedProject() throws ResponseException {
when(client.uploadStorage(eq("first.po"), any()))
.thenReturn(1L);

NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, null, true, null, null, null, null, false, false, null, false);
NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, null, true, null, null, null, null, null, false, false, null, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
Expand All @@ -164,7 +167,7 @@ public void testUploadNoAutoUpdate_FileBasedProject() throws ResponseException {
when(client.downloadFullProject())
.thenReturn(build);

NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, null, false, null, null, null, null, false, false, null, false);
NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, null, false, null, null, null, null, null, false, false, null, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
Expand Down Expand Up @@ -193,7 +196,7 @@ public void testUploadWhenBranchMissed_FileBasedProject() throws ResponseExcepti
when(client.addBranch(any())).thenReturn(branch);
when(client.addSource(any())).thenReturn(new FileInfo());

NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, "main", false, null, null, null, null, false, false, null, false);
NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, "main", false, null, null, null, null, null, false, false, null, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
Expand Down Expand Up @@ -231,7 +234,7 @@ public void testUploadLabel_FileBasedProject() throws ResponseException {
when(client.addLabel(any())).thenReturn(label);
when(client.addSource(any())).thenReturn(new FileInfo());

NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, null, false, Collections.singletonList("main_label"), null, null, null, false, false, null, false);
NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, null, false, Collections.singletonList("main_label"), null, null, null, null, false, false, null, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
Expand Down Expand Up @@ -265,7 +268,7 @@ public void testUploadExcludedLangs_FileBasedProject() throws ResponseException
when(client.listLabels()).thenReturn(Collections.emptyList());
when(client.addSource(any())).thenReturn(new FileInfo());

NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, null, false, null, null, null, null, false, false, Arrays.asList("ua", "fr"), false);
NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, null, false, null, null, null, null, null, false, false, Arrays.asList("ua", "fr"), false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
Expand Down Expand Up @@ -300,7 +303,7 @@ public void testUploadWithDest_FileBasedProject() throws ResponseException {
when(client.uploadStorage(eq("save.po"), any()))
.thenReturn(1L);
when(client.addSource(any())).thenReturn(new FileInfo());
NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, null, false, null, "path/to/save.po", null, null, false, false, null, false);
NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, null, false, null, "path/to/save.po", null, null, null, false, false, null, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
Expand Down Expand Up @@ -344,7 +347,7 @@ public void testUploadCleanUpModeAndUpdateStrings_StringBasedProject() throws Re
when(client.addSourceStringsBased(any())).thenReturn(progress);
when(client.getUploadStringsStatus(any())).thenReturn(progressFinished);

NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, "branch", false, null, null, null, null, true, true, null, false);
NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, "branch", false, null, null, null, null, null, true, true, null, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
Expand All @@ -360,4 +363,23 @@ public void testUploadCleanUpModeAndUpdateStrings_StringBasedProject() throws Re
verify(client).addSourceStringsBased(eq(addFileRequest));
verifyNoMoreInteractions(client);
}

@Test
public void testThrowsUploadWithContext_StringBasedProject() {
File fileToUpload = new File(project.getBasePath() + "first.po");
project.addFile(Utils.normalizePath("first.po"), "Hello, World!");
NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder
.minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%")
.setBasePath(project.getBasePath());
PropertiesWithFiles pb = pbBuilder.build();
ProjectClient client = mock(ProjectClient.class);
CrowdinProjectFull build = ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId())).build();
build.setType(Type.STRINGS_BASED);
when(client.downloadFullProject()).thenReturn(build);

NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, "branch", false, null, null, "context", null, null, true, true, null, false);
assertThrows(ExitCodeExceptionMapper.ValidationException.class, () -> action.act(Outputter.getDefault(), pb, client));
verify(client).downloadFullProject();
verifyNoMoreInteractions(client);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class FileUploadSubcommandTest extends PicocliTestUtils {
@Test
public void testFileUpload() {
this.execute(CommandNames.FILE, CommandNames.FILE_UPLOAD, "file.txt");
verify(actionsMock).fileUpload(any(), any(), anyBoolean(), any(), any(), any(), any(), any(), anyBoolean(), anyBoolean(), anyBoolean());
verify(actionsMock).fileUpload(any(), any(), anyBoolean(), any(), any(), any(), any(), any(), any(), anyBoolean(), anyBoolean(), anyBoolean());
this.check(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ void mockActions() {
.thenReturn(actionMock);
when(actionsMock.labelDelete(any()))
.thenReturn(actionMock);
when(actionsMock.fileUpload(any(), any(), anyBoolean(), any(), any(), any(), any(), any(), anyBoolean(), anyBoolean(), anyBoolean()))
when(actionsMock.fileUpload(any(), any(), anyBoolean(), any(), any(), any(), any(), any(), any(), anyBoolean(), anyBoolean(), anyBoolean()))
.thenReturn(actionMock);
when(actionsMock.fileUploadTranslation(any(), any(), any(), any(), anyBoolean()))
.thenReturn(actionMock);
Expand Down

0 comments on commit b60f05e

Please sign in to comment.