Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: type and parser version options for file upload #765

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -129,7 +129,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, 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 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 @@ -270,8 +270,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, List<String> excludedLanguages, boolean plainView, boolean cleanupMode, boolean updateStrings) {
return new FileUploadAction(file, branch, autoUpdate, labels, destination, cleanupMode, updateStrings, excludedLanguages, plainView);
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);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class FileUploadAction implements NewAction<ProjectProperties, ProjectClient> {
private final boolean autoUpdate;
private final List<String> labels;
private final String dest;
private final String type;
private final Integer parserVersion;
private final boolean cleanupMode;
private final boolean updateStrings;
private final List<String> excludedLanguages;
Expand Down Expand Up @@ -137,6 +139,12 @@ public void act(Outputter out, ProjectProperties properties, ProjectClient clien
AddFileRequest request = new AddFileRequest();
request.setName(fileDestName);
request.setStorageId(storageId);
if (nonNull(type)) {
request.setType(type);
}
if (nonNull(parserVersion)) {
request.setParserVersion(parserVersion);
}

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 @@ -9,6 +9,7 @@
import picocli.CommandLine.Parameters;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

Expand All @@ -31,7 +32,13 @@ class FileUploadSubcommand extends ActCommandProject {
protected List<String> labels;

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

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

@Option(names = {"--parser-version"}, descriptionKey = "crowdin.file.upload.parser", paramLabel = "...", order = -2)
protected Integer parserVersion;

@Option(names = {"--excluded-language"}, descriptionKey = "params.excluded-languages", paramLabel = "...", order = -2)
protected List<String> excludedLanguages;
Expand All @@ -48,11 +55,20 @@ class FileUploadSubcommand extends ActCommandProject {
@Option(names = {"--plain"}, descriptionKey = "crowdin.list.usage.plain")
protected boolean plainView;

@Override
protected List<String> checkOptions() {
List<String> errors = new ArrayList<>();
if (parserVersion != null && type == null) {
errors.add(RESOURCE_BUNDLE.getString("error.file.type_required"));
}
return errors;
}

@Override
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, excludedLanguages, plainView, cleanupMode, updateStrings);
return actions.fileUpload(file, branch, autoUpdate, labels, destination, type, parserVersion, excludedLanguages, plainView, cleanupMode, updateStrings);
}
}
3 changes: 3 additions & 0 deletions src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ 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.type=File type
crowdin.file.upload.parser=Parser version
crowdin.file.upload.auto-update=Specify whether to update the file in the Crowdin project if it already exists
crowdin.file.upload.cleanup-mode=Specify whether to delete all strings with a system label that do not exist in the file
crowdin.file.upload.update-strings=Specify whether to update strings that have the same keys
Expand Down Expand Up @@ -552,6 +554,7 @@ error.screenshot.not_updated=Screenshot was not updated
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.response.401=Couldn't authorize. Check your 'api_token'
error.response.403=You do not have permission to view/edit project with provided id
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,false, true, false));
assertNotNull(actions.fileUpload(null, null, false, null, null, null, null, null, false, true, false));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void testProjectOneFittingFile() throws IOException, ResponseException {
}));

NewAction<PropertiesWithFiles, ProjectClient> action =
new DownloadAction(files, false, null, null,false, null, false, false, false, false, false);
new DownloadAction(files, false, null, null, false, null, false, false, false, false, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject(null);
Expand Down Expand Up @@ -552,7 +552,7 @@ public void testProjectOneFittingFile_UploadedWithoutHierarchy() throws IOExcept
}));

NewAction<PropertiesWithFiles, ProjectClient> action =
new DownloadAction(files, false, null, null,false, null, false, false, false, false, false);
new DownloadAction(files, false, null, null, false, null, false, false, false, false, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,16 @@ public void testUpload_FileBasedProject() throws ResponseException {
when(client.uploadStorage(eq("first.po"), any()))
.thenReturn(1L);

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

verify(client).downloadFullProject();
verify(client).uploadStorage(eq("first.po"), any());
AddFileRequest addFileRequest = new AddFileRequest() {{
setName("first.po");
setStorageId(1L);
setType("po");
setParserVersion(3);
}};
verify(client).addSource(eq(addFileRequest));
verifyNoMoreInteractions(client);
Expand Down Expand Up @@ -103,7 +105,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, false, false, null, false);
NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, "branch", false, null, null, null, null, false, false, null, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
Expand Down Expand Up @@ -137,7 +139,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, false, false, null, false);
NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, null, true, null, null, null, null, false, false, null, false);
action.act(Outputter.getDefault(), pb, client);

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

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

verify(client).downloadFullProject();
Expand Down Expand Up @@ -192,7 +194,7 @@ public void testUploadWhenBranchMissed_FileBasedProject() throws ResponseExcepti
.thenReturn(1L);
when(client.addBranch(any())).thenReturn(branch);

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

verify(client).downloadFullProject();
Expand Down Expand Up @@ -229,7 +231,7 @@ public void testUploadLabel_FileBasedProject() throws ResponseException {
when(client.listLabels()).thenReturn(Collections.emptyList());
when(client.addLabel(any())).thenReturn(label);

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

verify(client).downloadFullProject();
Expand Down Expand Up @@ -262,7 +264,7 @@ public void testUploadExcludedLangs_FileBasedProject() throws ResponseException
.thenReturn(1L);
when(client.listLabels()).thenReturn(Collections.emptyList());

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

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

verify(client).downloadFullProject();
Expand Down Expand Up @@ -340,7 +342,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, true, true, null, false);
NewAction<ProjectProperties, ProjectClient> action = new FileUploadAction(fileToUpload, "branch", false, null, null, null, null, true, true, null, false);
action.act(Outputter.getDefault(), pb, client);

verify(client).downloadFullProject();
Expand Down
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(), anyBoolean(), anyBoolean(), anyBoolean());
verify(actionsMock).fileUpload(any(), any(), anyBoolean(), 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 @@ -119,7 +119,7 @@ void mockActions() {
.thenReturn(actionMock);
when(actionsMock.labelDelete(any()))
.thenReturn(actionMock);
when(actionsMock.fileUpload(any(), any(), anyBoolean(), any(), any(), any(), anyBoolean(), anyBoolean(), anyBoolean()))
when(actionsMock.fileUpload(any(), any(), anyBoolean(), any(), any(), any(), any(), any(), anyBoolean(), anyBoolean(), anyBoolean()))
.thenReturn(actionMock);
when(actionsMock.fileUploadTranslation(any(), any(), any(), any(), anyBoolean()))
.thenReturn(actionMock);
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.14.0

version.commons-cli..commons-cli=1.5.0

version.com.github.crowdin..crowdin-api-client-java=1.15.2
version.com.github.crowdin..crowdin-api-client-java=1.15.4

plugin.org.asciidoctor.jvm.convert=3.3.2

Expand Down
Loading