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

Improvements: little fixes and new 'custom_segmentation' field #389

Merged
merged 5 commits into from
Sep 17, 2021
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 build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ dependencies {

implementation 'info.picocli:picocli:4.6.1'

implementation 'com.github.crowdin:crowdin-api-client-java:1.3.10'
implementation 'com.github.crowdin:crowdin-api-client-java:1.3.14'

testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'
testImplementation 'org.hamcrest:hamcrest:2.2'
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/crowdin/cli/client/CrowdinClientCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ protected static <T, R extends Exception> T executeRequest(Map<BiPredicate<Strin
.collect(Collectors.joining("\n"));
throw new RuntimeException(errorMessage);
} catch (HttpException e) {
String code = (e.getError().code != null) ? e.getError().code : "<empty_code>";
String message = (e.getError().message != null) ? e.getError().message : "<empty_message>";
String code = (e.getError() != null && e.getError().code != null) ? e.getError().code : "<empty_code>";
String message = (e.getError() != null && e.getError().message != null) ? e.getError().message : "<empty_message>";
searchErrorHandler(errorHandlers, code, message);
throw new RuntimeException(String.format("Error from server: <Code: %s, Message: %s>", code, message));
} catch (Exception e) {
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/com/crowdin/cli/client/CrowdinProjectClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private void populateProjectWithInfo(CrowdinProjectInfo project) {
if (projectModel instanceof ProjectSettings) {
project.setAccessLevel(CrowdinProjectInfo.Access.MANAGER);
ProjectSettings projectSettings = (ProjectSettings) projectModel;
if (projectSettings.isInContext()) {
if (projectSettings.getInContext() != null && projectSettings.getInContext()) {
project.setInContextLanguage(projectSettings.getInContextPseudoLanguage());
}
project.setLanguageMapping(LanguageMapping.fromServerLanguageMapping(projectSettings.getLanguageMapping()));
Expand Down Expand Up @@ -164,9 +164,13 @@ public void deleteDirectory(Long directoryId) {
}

@Override
public void updateSource(Long sourceId, UpdateFileRequest request) {
public void updateSource(Long sourceId, UpdateFileRequest request) throws ResponseException {
Map<BiPredicate<String, String>, ResponseException> errorHandlers = new LinkedHashMap<BiPredicate<String, String>, ResponseException>() {{
put((code, message) -> message.contains("File from storage with id #" + request.getStorageId() + " was not found"), new RepeatException());
put((code, message) -> StringUtils.contains(message, "Invalid SRX specified"), new ResponseException("Invalid SRX file specified"));
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
}};
executeRequestWithPossibleRetry(
(code, message) -> message.contains("File from storage with id #" + request.getStorageId() + " was not found"),
errorHandlers,
() -> this.client.getSourceFilesApi()
.updateOrRestoreFile(this.projectId, sourceId, request));
}
Expand All @@ -176,6 +180,7 @@ public void addSource(AddFileRequest request) throws ResponseException {
Map<BiPredicate<String, String>, ResponseException> errorHandlers = new LinkedHashMap<BiPredicate<String, String>, ResponseException>() {{
put((code, message) -> message.contains("File from storage with id #" + request.getStorageId() + " was not found"), new RepeatException());
put((code, message) -> StringUtils.contains(message, "Name must be unique"), new ExistsResponseException());
put((code, message) -> StringUtils.contains(message, "Invalid SRX specified"), new ResponseException("Invalid SRX file specified"));
}};
executeRequestWithPossibleRetry(
errorHandlers,
Expand Down Expand Up @@ -241,9 +246,9 @@ public SourceString addSourceString(AddSourceStringRequest request) {
}

@Override
public List<SourceString> listSourceString(Long fileId, String labelIds, String filter) {
public List<SourceString> listSourceString(Long fileId, Long branchId, String labelIds, String filter) {
return executeRequestFullList((limit, offset) -> this.client.getSourceStringsApi()
.listSourceStrings(this.projectId, fileId, null, labelIds, null, filter, null, limit, offset));
.listSourceStrings(this.projectId, fileId, null, branchId, labelIds, null, filter, null, limit, offset));
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/crowdin/cli/client/ProjectClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface ProjectClient extends Client {

void deleteDirectory(Long directoryId);

void updateSource(Long sourceId, UpdateFileRequest request);
void updateSource(Long sourceId, UpdateFileRequest request) throws ResponseException;

void addSource(AddFileRequest request) throws ResponseException;

Expand All @@ -65,7 +65,7 @@ public interface ProjectClient extends Client {

SourceString addSourceString(AddSourceStringRequest request);

List<SourceString> listSourceString(Long fileId, String labelIds, String filter);
List<SourceString> listSourceString(Long fileId, Long branchId, String labelIds, String filter);

void deleteSourceString(Long id);

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/crowdin/cli/client/ResponseException.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
package com.crowdin.cli.client;

public class ResponseException extends Exception {

public ResponseException() {

}

public ResponseException(String message) {
super(message);
}
}
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 @@ -57,7 +57,7 @@ NewAction<ProjectProperties, ProjectClient> stringEdit(
boolean noProgress, Long id, String identifier, String newText, String newContext, Integer newMaxLength, List<String> labelNames, Boolean isHidden);

NewAction<ProjectProperties, ProjectClient> stringList(
boolean noProgress, boolean isVerbose, String file, String filter);
boolean noProgress, boolean isVerbose, String file, String filter, String branchName);

NewAction<PropertiesWithFiles, ProjectClient> uploadSources(
String branchName, boolean deleteObsolete, boolean noProgress, boolean autoUpdate, boolean debug, boolean plainView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ public NewAction<ProjectProperties, ProjectClient> stringEdit(

@Override
public NewAction<ProjectProperties, ProjectClient> stringList(
boolean noProgress, boolean isVerbose, String file, String filter
boolean noProgress, boolean isVerbose, String file, String filter, String branchName
) {
return new StringListAction(noProgress, isVerbose, file, filter);
return new StringListAction(noProgress, isVerbose, file, filter, branchName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private void updateWithUserInputs(Outputter out, Asking asking, List<String> fil
String token;
try {
ConsoleSpinner.start(out, "Waiting for authorization to complete (Press <Ctrl>+C to exit)", false);
token = OAuthUtil.getToken(OAUTH_CLIENT_ID);
token = OAuthUtil.getToken(out, OAUTH_CLIENT_ID);
ConsoleSpinner.stop(OK, "Authorization finished successfully");
} catch (Exception e) {
ConsoleSpinner.stop(ERROR, e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void act(Outputter out, ProjectProperties pb, ProjectClient client) {
.stream()
.collect(Collectors.toMap((entry) -> entry.getValue().getId(), Map.Entry::getKey));

List<SourceString> sourceStrings = client.listSourceString(null, null, null)
List<SourceString> sourceStrings = client.listSourceString(null, null, null, null)
.stream()
.filter(sourceString -> (ids != null && ids.contains(sourceString.getId()))
|| (texts != null && sourceString.getText() instanceof String && texts.contains(sourceString.getText()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public StringEditAction(
@Override
public void act(Outputter out, ProjectProperties pb, ProjectClient client) {

List<SourceString> sourceStrings = client.listSourceString(null, null, null);
List<SourceString> sourceStrings = client.listSourceString(null, null, null, null);

List<Long> labelIds = (labelNames != null && !labelNames.isEmpty()) ? this.prepareLabelIds(client) : null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.crowdin.cli.properties.ProjectProperties;
import com.crowdin.cli.utils.console.ConsoleSpinner;
import com.crowdin.client.labels.model.Label;
import com.crowdin.client.sourcefiles.model.Branch;
import com.crowdin.client.sourcefiles.model.FileInfo;
import com.crowdin.client.sourcestrings.model.SourceString;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -17,6 +18,7 @@
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.crowdin.cli.BaseCli.RESOURCE_BUNDLE;
Expand All @@ -28,19 +30,27 @@ class StringListAction implements NewAction<ProjectProperties, ProjectClient> {
private final boolean isVerbose;
private final String file;
private final String filter;
private final String branchName;

public StringListAction(boolean noProgress, boolean isVerbose, String file, String filter) {
public StringListAction(boolean noProgress, boolean isVerbose, String file, String filter, String branchName) {
this.noProgress = noProgress;
this.isVerbose = isVerbose;
this.file = file;
this.filter = filter;
this.branchName = branchName;
}

@Override
public void act(Outputter out, ProjectProperties pb, ProjectClient client) {
CrowdinProjectFull project = ConsoleSpinner.execute(out, "message.spinner.fetching_project_info", "error.collect_project_info",
this.noProgress, false, client::downloadFullProject);

Long branchId = Optional.ofNullable(this.branchName)
.map(br -> project.findBranchByName(br)
.orElseThrow(() -> new RuntimeException(RESOURCE_BUNDLE.getString("error.not_found_branch"))))
.map(Branch::getId)
.orElse(null);

Map<Long, String> labels = client.listLabels().stream()
.collect(Collectors.toMap(Label::getId, Label::getTitle));

Expand All @@ -58,10 +68,10 @@ public void act(Outputter out, ProjectProperties pb, ProjectClient client) {

List<SourceString> sourceStrings;
if (StringUtils.isEmpty(file)) {
sourceStrings = client.listSourceString(null, null, encodedFilter);
sourceStrings = client.listSourceString(null, branchId, null, encodedFilter);
} else {
if (paths.containsKey(file)) {
sourceStrings = client.listSourceString(paths.get(file).getId(), null, encodedFilter);
sourceStrings = client.listSourceString(paths.get(file).getId(), branchId, null, encodedFilter);
} else {
throw new RuntimeException(String.format(RESOURCE_BUNDLE.getString("error.file_not_exists"), file));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
return;
}
}
Long customSegmentationFileId = null;
if (file.getCustomSegmentation() != null) {
File customSegmentation = new File(Utils.normalizePath(Utils.joinPaths(pb.getBasePath(), file.getCustomSegmentation())));
try (InputStream customSegmentationFileStream = new FileInputStream(customSegmentation)) {
customSegmentationFileId = client.uploadStorage(customSegmentation.getName(), customSegmentationFileStream);
} catch (Exception e) {
errorsPresented.set(true);
throw new RuntimeException(
String.format(RESOURCE_BUNDLE.getString("error.upload_to_storage"), customSegmentation.getAbsolutePath(), e));
}
}
Long srxStorageId = customSegmentationFileId;

List<Runnable> taskss = sources.stream()
.map(source -> {
final File sourceFile = new File(source);
Expand All @@ -175,7 +188,7 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
if (autoUpdate && projectFile != null) {
final UpdateFileRequest request = new UpdateFileRequest();
request.setExportOptions(buildExportOptions(sourceFile, file, pb.getBasePath()));
request.setImportOptions(buildImportOptions(sourceFile, file));
request.setImportOptions(buildImportOptions(sourceFile, file, srxStorageId));
PropertiesBeanUtils.getUpdateOption(file.getUpdateOption()).ifPresent(request::setUpdateOption);

if (file.getLabels() != null) {
Expand All @@ -188,10 +201,10 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
return (Runnable) () -> {
try (InputStream fileStream = new FileInputStream(sourceFile)) {
request.setStorageId(client.uploadStorage(source.substring(source.lastIndexOf(Utils.PATH_SEPARATOR) + 1), fileStream));
} catch (IOException e) {
} catch (Exception e) {
errorsPresented.set(true);
throw new RuntimeException(
String.format(RESOURCE_BUNDLE.getString("error.upload_to_storage"), sourceFile.getAbsolutePath()));
String.format(RESOURCE_BUNDLE.getString("error.upload_to_storage"), sourceFile.getAbsolutePath()), e);
}

try {
Expand All @@ -218,7 +231,7 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
final AddFileRequest request = new AddFileRequest();
request.setName(fileName);
request.setExportOptions(buildExportOptions(sourceFile, file, pb.getBasePath()));
request.setImportOptions(buildImportOptions(sourceFile, file));
request.setImportOptions(buildImportOptions(sourceFile, file, srxStorageId));
if (file.getExcludedTargetLanguages() != null && !file.getExcludedTargetLanguages().isEmpty()) {
request.setExcludedTargetLanguages(file.getExcludedTargetLanguages());
}
Expand Down Expand Up @@ -250,7 +263,7 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
} catch (IOException e) {
errorsPresented.set(true);
throw new RuntimeException(
String.format(RESOURCE_BUNDLE.getString("error.upload_to_storage"), sourceFile.getAbsolutePath()));
String.format(RESOURCE_BUNDLE.getString("error.upload_to_storage"), sourceFile.getAbsolutePath()), e);
}
try {
client.addSource(request);
Expand Down Expand Up @@ -290,7 +303,7 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
}
}

private ImportOptions buildImportOptions(java.io.File sourceFile, FileBean fileBean) {
private ImportOptions buildImportOptions(java.io.File sourceFile, FileBean fileBean, Long srxStorageId) {
if (isSpreadsheet(sourceFile, fileBean)) {
SpreadsheetFileImportOptions importOptions = new SpreadsheetFileImportOptions();
importOptions.setFirstLineContainsHeader(fileBean.getFirstLineContainsHeader());
Expand All @@ -302,10 +315,12 @@ private ImportOptions buildImportOptions(java.io.File sourceFile, FileBean fileB
importOptions.setTranslateAttributes(fileBean.getTranslateAttributes());
importOptions.setContentSegmentation(fileBean.getContentSegmentation());
importOptions.setTranslatableElements(fileBean.getTranslatableElements());
importOptions.setSrxStorageId(srxStorageId);
return importOptions;
} else {
OtherFileImportOptions importOptions = new OtherFileImportOptions();
importOptions.setContentSegmentation(fileBean.getContentSegmentation());
importOptions.setSrxStorageId(srxStorageId);
return importOptions;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public int execute(Actions actions, PropertiesBuilders propertiesBuilders, Strin

public boolean hasMatchedArg(String name) {
CommandLine.ParseResult parseResult = commandLine.getParseResult();
return parseResult != null
&& ((parseResult.hasSubcommand())
? parseResult.subcommand().hasMatchedOption(name)
: parseResult.hasMatchedOption(name));
while (parseResult.hasSubcommand()) {
parseResult = parseResult.subcommand();
}
return parseResult.hasMatchedOption(name);
}

private void init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class StringListSubcommand extends ActCommandProject {
@CommandLine.Option(names = {"--filter"}, paramLabel = "...")
protected String filter;

@CommandLine.Option(names = {"-b", "--branch"}, paramLabel = "...")
protected String branchName;

@Override
protected List<String> checkOptions() {
if (file != null) {
Expand All @@ -32,6 +35,6 @@ protected List<String> checkOptions() {

@Override
protected NewAction<ProjectProperties, ProjectClient> getAction(Actions actions) {
return actions.stringList(noProgress, isVerbose, file, filter);
return actions.stringList(noProgress, isVerbose, file, filter, branchName);
}
}
Loading