From 69b99bb93ef23269abe378d280d2f24131c1e841 Mon Sep 17 00:00:00 2001 From: Yevheniy Oliynyk Date: Fri, 1 Nov 2024 14:41:23 +0100 Subject: [PATCH] fix: bundles urls (#138) --- .../crowdin/ui/panel/download/DownloadWindow.java | 14 ++++++++------ src/main/java/com/crowdin/util/Util.java | 11 +++++++++++ src/main/resources/messages/messages.properties | 2 +- src/test/java/com/crowdin/util/UtilTest.java | 10 ++++++++++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/crowdin/ui/panel/download/DownloadWindow.java b/src/main/java/com/crowdin/ui/panel/download/DownloadWindow.java index 72f5aba..dabb8b8 100644 --- a/src/main/java/com/crowdin/ui/panel/download/DownloadWindow.java +++ b/src/main/java/com/crowdin/ui/panel/download/DownloadWindow.java @@ -26,6 +26,8 @@ import java.util.Optional; import static com.crowdin.Constants.*; +import static com.crowdin.util.Util.extractOrganization; +import static com.crowdin.util.Util.isEnterpriseUrl; public class DownloadWindow implements ContentTab { @@ -183,17 +185,17 @@ public String buildBundleUrl() { var bundle = this.getSelectedBundle(); if (bundle == null) { - if (this.baseUrl != null) { - String base = this.baseUrl.endsWith("/") ? this.baseUrl : this.baseUrl + "/"; - return base + "u/projects/" + project.getId() + "/download"; + if (this.baseUrl != null && isEnterpriseUrl(this.baseUrl)) { + String organization = extractOrganization(this.baseUrl); + return "https://" + organization + ".crowdin.com/u/projects/" + project.getId() + "/download"; } else { return "https://crowdin.com/project/" + project.getIdentifier() + "/download#bundles"; } } - if (this.baseUrl != null) { - String base = this.baseUrl.endsWith("/") ? this.baseUrl : this.baseUrl + "/"; - return base + "u/projects/" + project.getId() + "/translations/bundle/" + bundle.getId(); + if (this.baseUrl != null && isEnterpriseUrl(this.baseUrl)) { + String organization = extractOrganization(this.baseUrl); + return "https://" + organization + ".crowdin.com/u/projects/" + project.getId() + "/translations/bundle/" + bundle.getId(); } else { return "https://crowdin.com/project/" + project.getIdentifier() + "/download#bundles:" + bundle.getId(); } diff --git a/src/main/java/com/crowdin/util/Util.java b/src/main/java/com/crowdin/util/Util.java index 347b729..f2877e1 100644 --- a/src/main/java/com/crowdin/util/Util.java +++ b/src/main/java/com/crowdin/util/Util.java @@ -39,4 +39,15 @@ public static boolean isFileFormatNotAllowed(Exception e) { return e.getMessage().contains("files are not allowed to upload in string-based projects") || e.getMessage().contains("files are not allowed to upload in strings-based projects"); } + + public static String extractOrganization(String baseUrl) { + if (baseUrl.contains(".api.crowdin.com")) { + return baseUrl.split(".api.crowdin.com")[0].split("https://")[1]; + } + return baseUrl.split(".crowdin.com")[0].split("https://")[1]; + } + + public static boolean isEnterpriseUrl(String baseUrl) { + return !baseUrl.contains("https://api.crowdin.com"); + } } diff --git a/src/main/resources/messages/messages.properties b/src/main/resources/messages/messages.properties index 22c5bd7..00ccaab 100644 --- a/src/main/resources/messages/messages.properties +++ b/src/main/resources/messages/messages.properties @@ -15,7 +15,7 @@ errors.config.invalid_format=Property '%s' has wrong format errors.config.has_errors=Errors in the configuration file: errors.config.missing_config_file=File '%s' with Crowdin plugin configuration doesn't exist in the project root directory errors.config.missing_property=Required property '%s' is missing in the configuration file -errors.config.invalid_url_property=Base URL contains '%s' unexpected value. The expected format is 'https://crowdin.com' or 'https://{domain_name}.api.crowdin.com' +errors.config.invalid_url_property=Base URL contains '%s' unexpected value. The expected format is 'https://api.crowdin.com' or 'https://{domain_name}.api.crowdin.com' errors.config.invalid_url_env=Environment variable '%s' contains unexpected '%s' value. The expected format is 'https://crowdin.com' or 'https://{domain_name}.crowdin.com' errors.extract_file=Failed to extract the file '%s' diff --git a/src/test/java/com/crowdin/util/UtilTest.java b/src/test/java/com/crowdin/util/UtilTest.java index dca6db9..d3df0a1 100644 --- a/src/test/java/com/crowdin/util/UtilTest.java +++ b/src/test/java/com/crowdin/util/UtilTest.java @@ -1,6 +1,7 @@ package com.crowdin.util; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -27,4 +28,13 @@ public static Stream testPrepareListMessageText() { "") ); } + + @Test + public void extractOrganizationTest() { + Assertions.assertEquals("test", Util.extractOrganization("https://test.api.crowdin.com")); + Assertions.assertEquals("test342", Util.extractOrganization("https://test342.crowdin.com")); + Assertions.assertEquals("org-1", Util.extractOrganization("https://org-1.api.crowdin.com")); + Assertions.assertEquals("org-test", Util.extractOrganization("https://org-test.api.crowdin.com")); + Assertions.assertEquals("org-test", Util.extractOrganization("https://org-test.api.crowdin.com/")); + } }