fix: Change js lib file name in git to avoid file name error in windows (#29656)

## Description
This PR stops writing custom js libs to a file with the same name as
url.

#### PR fixes following issue(s)
Fixes #29655
This commit is contained in:
Nayan 2023-12-21 16:21:08 +06:00 committed by GitHub
parent 4b6f826a9b
commit 9525bede2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 3 deletions

View File

@ -25,7 +25,7 @@ public class CommonConstants {
public static final String DELIMITER_PATH = "/";
public static final String EMPTY_STRING = "";
public static final String FILE_MIGRATION_MESSAGE =
"Some of the changes above are due to an improved file structure designed to reduce merge conflicts. You can safely commit them to your repository.";
"Some of the changes above are due to an improved file structure. You can safely commit them to your repository.";
public static final String TABS_WIDGET = "TABS_WIDGET";

View File

@ -47,8 +47,11 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Instant;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@ -304,7 +307,9 @@ public class FileUtilsImpl implements FileInterface {
Boolean isResourceUpdated = !CollectionUtils.isEmpty(updatedResources.get(CUSTOM_JS_LIB_LIST))
? updatedResources.get(CUSTOM_JS_LIB_LIST).contains(uidString)
: Boolean.FALSE;
String fileNameWithExtension = uidString.replaceAll("/", "_") + CommonConstants.JSON_EXTENSION;
String fileNameWithExtension = getJsLibFileName(uidString) + CommonConstants.JSON_EXTENSION;
Path jsLibSpecificFile = jsLibDirectory.resolve(fileNameWithExtension);
if (isResourceUpdated) {
saveResource(jsLibEntry.getValue(), jsLibSpecificFile, gson);
@ -1108,4 +1113,33 @@ public class FileUtilsImpl implements FileInterface {
return Mono.just(0L);
}
}
/**
* We use UID string for custom js lib. UID strings are in this format: {libname}_{url to the lib src}.
* This method converts this uid string into a valid file name so that there is no unsupported character in the
* file name for any OS.
* This method returns a string in the format: {libname}_{base64 encoded hash of uid string}
* @param uidString UID string value of a JS lib
* @return String
*/
public static String getJsLibFileName(String uidString) {
int firstUnderscoreIndex = uidString.indexOf('_'); // this finds the first occurrence of "_"
String prefix;
if (firstUnderscoreIndex != -1) {
prefix = uidString.substring(0, firstUnderscoreIndex); // we're getting the prefix from the uidString
} else {
prefix = "jslib";
}
StringBuilder stringBuilder = new StringBuilder(prefix);
stringBuilder.append("_");
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(uidString.getBytes(StandardCharsets.UTF_8));
stringBuilder.append(Base64.getUrlEncoder().withoutPadding().encodeToString(hash));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Failed to hash URL string", e);
}
return stringBuilder.toString();
}
}

View File

@ -60,6 +60,8 @@ import java.util.List;
import java.util.Optional;
import java.util.Set;
import static com.appsmith.git.constants.CommonConstants.FILE_MIGRATION_MESSAGE;
@RequiredArgsConstructor
@Component
@Slf4j
@ -552,6 +554,17 @@ public class GitExecutorImpl implements GitExecutor {
modifiedDatasources++;
} else if (x.contains(GitDirectories.JS_LIB_DIRECTORY + CommonConstants.DELIMITER_PATH)) {
modifiedJSLibs++;
// remove this code in future when all the older format js libs are migrated to new
// format
if (x.contains("js.json")) {
/*
As this updated filename has color(:), it means this is the older format js
lib file that we're going to rename with the format without colon.
Hence, we need to show a message to user saying this might be a system level change.
*/
response.setMigrationMessage(FILE_MIGRATION_MESSAGE);
}
}
}
response.setModified(modifiedAssets);

View File

@ -539,7 +539,11 @@ public class GitFileUtils {
List<CustomJSLib> customJSLibList =
getApplicationResource(applicationReference.getJsLibraries(), CustomJSLib.class);
applicationJson.setCustomJSLibList(customJSLibList);
// remove the duplicate js libraries if there is any
List<CustomJSLib> customJSLibListWithoutDuplicates = new ArrayList<>(new HashSet<>(customJSLibList));
applicationJson.setCustomJSLibList(customJSLibListWithoutDuplicates);
// Extract pages
List<NewPage> pages = getApplicationResource(applicationReference.getPages(), NewPage.class);