Skip to content

Commit

Permalink
fix upload strings action
Browse files Browse the repository at this point in the history
  • Loading branch information
katerina20 committed Jan 31, 2024
1 parent a6db450 commit 67862cb
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private DistributionRelease releaseDistributionFilesBased(Outputter out, ClientD
release = client.getDistributionRelease(hash);

if ("failed".equalsIgnoreCase(release.getStatus())) {
throw new RuntimeException(RESOURCE_BUNDLE.getString("message.spinner.build_has_failed"));
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.distribution_failed"));
}
}

Expand Down Expand Up @@ -92,7 +92,7 @@ private DistributionStringsBasedRelease releaseDistributionStringsBased(Outputte
release = client.getDistributionStringsBasedRelease(hash);

if ("failed".equalsIgnoreCase(release.getStatus())) {
throw new RuntimeException(RESOURCE_BUNDLE.getString("message.spinner.build_has_failed"));
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.distribution_failed"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,8 @@ public void act(Outputter out, PropertiesWithFiles properties, ProjectClient cli
duplicateTranslations, translateUntranslatedOnly, translateWithPerfectMatchOnly, labelIds);
this.applyPreTranslation(out, client, request);
} else if (Objects.equals(project.getType(), Type.STRINGS_BASED)) {
Branch branch = BranchUtils.getOrCreateBranch(out, branchName, client, project, false);
if (Objects.isNull(branch)) {
throw new RuntimeException(RESOURCE_BUNDLE.getString("error.branch_required_string_project"));
}
Branch branch = project.findBranchByName(branchName)
.orElseThrow(() -> new RuntimeException(RESOURCE_BUNDLE.getString("error.branch_required_string_project")));
ApplyPreTranslationStringsBasedRequest request = RequestBuilder.applyPreTranslationStringsBased(
languages, Collections.singletonList(branch.getId()), method, engineId, autoApproveOption,
duplicateTranslations, translateUntranslatedOnly, translateWithPerfectMatchOnly, labelIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,35 +130,14 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
String translation = TranslationsUtils.replaceDoubleAsterisk(file.getSource(), file.getTranslation(), fileSource);
translation = placeholderUtil.replaceFileDependentPlaceholders(translation, new java.io.File(source));
if (file.getScheme() != null && !PlaceholderUtil.containsLangPlaceholders(translation)) {
java.io.File transFile = new java.io.File(pb.getBasePath() + Utils.PATH_SEPARATOR + translation);
if (!transFile.exists()) {
if (!plainView) {
out.println(SKIPPED.withIcon(String.format(
RESOURCE_BUNDLE.getString("error.translation_not_exists"),
StringUtils.removeStart(transFile.getAbsolutePath(), pb.getBasePath()))));
}
return;
}
java.io.File transFile = getTranslationFile(out, pb, translation);
if (Objects.isNull(transFile)) return;
UploadTranslationsRequest request = RequestBuilder.uploadTranslations(fileId, importEqSuggestions, autoApproveImported, translateHidden);
preparedRequests.put(transFile, Pair.of(languages, request));
} else {
for (Language language : languages) {
LanguageMapping localLanguageMapping =
LanguageMapping.fromConfigFileLanguageMapping(file.getLanguagesMapping());
LanguageMapping languageMapping =
LanguageMapping.populate(localLanguageMapping, serverLanguageMapping);

String transFileName = placeholderUtil.replaceLanguageDependentPlaceholders(translation, languageMapping, language);
transFileName = PropertiesBeanUtils.useTranslationReplace(transFileName, file.getTranslationReplace());
java.io.File transFile = new java.io.File(pb.getBasePath() + Utils.PATH_SEPARATOR + transFileName);
if (!transFile.exists()) {
if (!plainView) {
out.println(SKIPPED.withIcon(String.format(
RESOURCE_BUNDLE.getString("error.translation_not_exists"),
StringUtils.removeStart(transFile.getAbsolutePath(), pb.getBasePath()))));
}
continue;
}
java.io.File transFile = getTranslationFileWithPlaceholders(out, pb, placeholderUtil, serverLanguageMapping, file, translation, language);
if (Objects.isNull(transFile)) continue;
UploadTranslationsRequest request = RequestBuilder.uploadTranslations(fileId, importEqSuggestions, autoApproveImported, translateHidden);
preparedRequests.put(transFile, Pair.of(Collections.singletonList(language), request));
}
Expand All @@ -171,16 +150,7 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
java.io.File translationFile = entry.getKey();
List<Language> langs = entry.getValue().getLeft();
UploadTranslationsRequest request = entry.getValue().getRight();
try (InputStream fileStream = new FileInputStream(translationFile)) {
Long storageId = client.uploadStorage(translationFile.getName(), fileStream);
request.setStorageId(storageId);
} catch (Exception e) {
containsErrors.set(true);
throw new RuntimeException(String.format(
RESOURCE_BUNDLE.getString("error.upload_translation_to_storage"),
StringUtils.removeStart(translationFile.getAbsolutePath(), pb.getBasePath())
), e);
}
request.setStorageId(uploadToStorage(pb, client, containsErrors, translationFile));
try {
for (Language lang : langs) {
try {
Expand Down Expand Up @@ -211,30 +181,32 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
Map<java.io.File, Pair<List<Language>, UploadTranslationsStringsRequest>> preparedRequests = new HashMap<>();
Branch branch = project.findBranchByName(branchName)
.orElseThrow(() -> new RuntimeException(RESOURCE_BUNDLE.getString("error.branch_required_string_project")));
for (Language language : languages) {
UploadTranslationsStringsRequest request = new UploadTranslationsStringsRequest();
request.setBranchId(branch.getId());
request.setTranslateHidden(translateHidden);
request.setAutoApproveImported(autoApproveImported);
request.setImportEqSuggestions(importEqSuggestions);
preparedRequests.put(new java.io.File(pb.getBasePath() + Utils.PATH_SEPARATOR + file.getTranslation()), Pair.of(Collections.singletonList(language), request));
}
fileSourcesWithoutIgnores.forEach(source -> {
String fileSource = StringUtils.removeStart(source, pb.getBasePath());
String translation = TranslationsUtils.replaceDoubleAsterisk(file.getSource(), file.getTranslation(), fileSource);
translation = placeholderUtil.replaceFileDependentPlaceholders(translation, new java.io.File(source));
if (file.getScheme() != null && !PlaceholderUtil.containsLangPlaceholders(translation)) {
java.io.File transFile = getTranslationFile(out, pb, translation);
if (Objects.isNull(transFile)) return;
UploadTranslationsStringsRequest request = RequestBuilder.uploadTranslationsStrings(branch.getId(), importEqSuggestions, autoApproveImported, translateHidden);
preparedRequests.put(transFile, Pair.of(languages, request));
} else {
for (Language language : languages) {
java.io.File transFile = getTranslationFileWithPlaceholders(out, pb, placeholderUtil, serverLanguageMapping, file, translation, language);
if (Objects.isNull(transFile)) continue;
UploadTranslationsStringsRequest request = RequestBuilder.uploadTranslationsStrings(branch.getId(), importEqSuggestions, autoApproveImported, translateHidden);
preparedRequests.put(transFile, Pair.of(Collections.singletonList(language), request));
}
}

});
tasks = preparedRequests.entrySet()
.stream()
.map(entry -> (Runnable) () -> {
java.io.File translationFile = entry.getKey();
List<Language> langs = entry.getValue().getLeft();
UploadTranslationsStringsRequest request = entry.getValue().getRight();
try (InputStream fileStream = new FileInputStream(translationFile)) {
Long storageId = client.uploadStorage(translationFile.getName(), fileStream);
request.setStorageId(storageId);
} catch (Exception e) {
containsErrors.set(true);
throw new RuntimeException(String.format(
RESOURCE_BUNDLE.getString("error.upload_translation_to_storage"),
StringUtils.removeStart(translationFile.getAbsolutePath(), pb.getBasePath())
), e);
}
request.setStorageId(uploadToStorage(pb, client, containsErrors, translationFile));
try {
for (Language lang : langs) {
client.uploadTranslationStringsBased(lang.getId(), request);
Expand Down Expand Up @@ -263,4 +235,41 @@ public void act(Outputter out, PropertiesWithFiles pb, ProjectClient client) {
}
}
}

private Long uploadToStorage(PropertiesWithFiles pb, ProjectClient client, AtomicBoolean containsErrors, java.io.File translationFile) {
try (InputStream fileStream = new FileInputStream(translationFile)) {
Long storageId = client.uploadStorage(translationFile.getName(), fileStream);
return storageId;
} catch (Exception e) {
containsErrors.set(true);
throw new RuntimeException(String.format(
RESOURCE_BUNDLE.getString("error.upload_translation_to_storage"),
StringUtils.removeStart(translationFile.getAbsolutePath(), pb.getBasePath())
), e);
}
}

private java.io.File getTranslationFileWithPlaceholders(Outputter out, PropertiesWithFiles pb, PlaceholderUtil placeholderUtil, LanguageMapping serverLanguageMapping, FileBean file, String translation, Language language) {
LanguageMapping localLanguageMapping =
LanguageMapping.fromConfigFileLanguageMapping(file.getLanguagesMapping());
LanguageMapping languageMapping =
LanguageMapping.populate(localLanguageMapping, serverLanguageMapping);

String transFileName = placeholderUtil.replaceLanguageDependentPlaceholders(translation, languageMapping, language);
transFileName = PropertiesBeanUtils.useTranslationReplace(transFileName, file.getTranslationReplace());
return getTranslationFile(out, pb, transFileName);
}

private java.io.File getTranslationFile(Outputter out, PropertiesWithFiles pb, String translation) {
java.io.File transFile = new java.io.File(pb.getBasePath() + Utils.PATH_SEPARATOR + translation);
if (!transFile.exists()) {
if (!plainView) {
out.println(SKIPPED.withIcon(String.format(
RESOURCE_BUNDLE.getString("error.translation_not_exists"),
StringUtils.removeStart(transFile.getAbsolutePath(), pb.getBasePath()))));
}
return null;
}
return transFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ public static UploadTranslationsRequest uploadTranslations(Long fileId, boolean
return request;
}

public static UploadTranslationsStringsRequest uploadTranslationsStrings(Long branchId, boolean importEqSuggestions, boolean autoApproveImported, boolean translateHidden) {
UploadTranslationsStringsRequest request = new UploadTranslationsStringsRequest();
request.setBranchId(branchId);
request.setImportEqSuggestions(importEqSuggestions);
request.setAutoApproveImported(autoApproveImported);
request.setTranslateHidden(translateHidden);
return request;
}

public static PatchRequest patch(Object value, PatchOperation op, String path) {
PatchRequest request = new PatchRequest();
request.setValue(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class StringAddSubcommand extends ActCommandProject {
@CommandLine.Option(names = {"--label"}, descriptionKey = "params.label", paramLabel = "...", order = -2)
protected List<String> labelNames;

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

@CommandLine.Option(names = {"--hidden"}, order = -2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void populateWithDefaultValues(PropertiesWithFiles props) {
public PropertiesBuilder.Messages checkProperties(PropertiesWithFiles props, CheckType checkType) {
PropertiesBuilder.Messages messages = new PropertiesBuilder.Messages();
if (props.getFiles() == null || props.getFiles().isEmpty()) {
messages.addError(RESOURCE_BUNDLE.getString("error.config.empty_or_missed_section_files"));
messages.addWarning(RESOURCE_BUNDLE.getString("message.warning.empty_or_missed_section_files"));
} else {
for (FileBean fileBean : props.getFiles()) {
messages.addAllErrors(FileBean.CONFIGURATOR.checkProperties(fileBean));
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/messages/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ error.comment_should_not_have_issue_type=Comment should not have the --issue-typ

error.distribution_is_not_added=Distribution was not added
error.distribution_is_not_released=Distribution was not released
error.distribution_failed=Distribution release failed
error.distribution.empty_file=The '--file' value can not be empty for the 'default' export mode
error.distribution.empty_bundle_ids=The '--bundle-id' value can not be empty for the 'bundle' export mode
error.distribution.incorrect_file_command_usage=The '--file' is used only for the 'default' export mode
Expand Down Expand Up @@ -747,6 +748,7 @@ message.no_manager_access=You need to have @|yellow manager access|@ in the proj
message.no_manager_access_in_upload_sources=You need to have @|yellow manager access|@ in the project to apply 'excluded-languages' or/and 'delete-obsolete' options
message.no_manager_access_in_upload_sources_dryrun=You need to have @|yellow manager access|@ in the project to apply 'delete-obsolete' options

message.warning.empty_or_missed_section_files=Required section 'files' is missing (or empty) in the configuration file. If you are working with a string-based project, you can ignore this message
message.warning.not_yml=File @|bold '%s'|@ is not a YAML or YML file
message.warning.browser_not_found=\nError opening a web browser. Please open the following link manually:\n@|bold %s|@
message.warning.file_not_uploaded_cause_of_language=Translation file @|yellow,bold '%s'|@ @|yellow hasn't been uploaded|@ since @|bold %s|@ is not enabled as a target language for the source file in your Crowdin project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,24 +213,24 @@ public void testUploadWithDest() throws ResponseException {
@Test
public void testUploadOneOfTwoTranslation_StringBasedProject() throws ResponseException {
project.addFile(Utils.normalizePath("first.po"), "Hello, World!");
project.addFile(Utils.normalizePath("translation.po"), "Hello, World!");
project.addFile(Utils.normalizePath("first.po-CR-uk-UA"), "Hello, World!");
NewPropertiesWithFilesUtilBuilder pbBuilder = NewPropertiesWithFilesUtilBuilder
.minimalBuiltPropertiesBean("*", "translation.po")
.minimalBuiltPropertiesBean("*", Utils.PATH_SEPARATOR + "%original_file_name%-CR-%locale%", Arrays.asList("*-CR-*"))
.setBasePath(project.getBasePath());
PropertiesWithFiles pb = pbBuilder.build();
ProjectClient client = mock(ProjectClient.class);
CrowdinProjectFull build = ProjectBuilder.emptyProject(Long.parseLong(pb.getProjectId())).addBranches(2L, "main").build();
build.setType(Type.STRINGS_BASED);
when(client.downloadFullProject("main"))
.thenReturn(build);
when(client.uploadStorage(eq("translation.po"), any()))
when(client.uploadStorage(eq("first.po-CR-uk-UA"), any()))
.thenReturn(1L);

NewAction<PropertiesWithFiles, ProjectClient> action = new UploadTranslationsAction(false, null, "main", false, false, false, false, false);
assertDoesNotThrow(() -> action.act(Outputter.getDefault(), pb, client));

verify(client).downloadFullProject("main");
verify(client).uploadStorage(eq("translation.po"), any());
verify(client).uploadStorage(eq("first.po-CR-uk-UA"), any());
UploadTranslationsStringsRequest uploadTranslationRequest = new UploadTranslationsStringsRequest() {{
setStorageId(1L);
setBranchId(2L);
Expand Down

0 comments on commit 67862cb

Please sign in to comment.