Merge pull request #12091 from thingsboard/fix/git-sync-gateway

Fix gateways dashboard sync
This commit is contained in:
Viacheslav Klimov 2024-11-21 12:06:55 +02:00 committed by GitHub
commit 392d2290cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 15 additions and 21 deletions

View File

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 2.7 KiB

View File

@ -73,23 +73,23 @@ public class DashboardSyncService {
List<RepoFile> resources = listFiles("resources"); List<RepoFile> resources = listFiles("resources");
for (RepoFile resourceFile : resources) { for (RepoFile resourceFile : resources) {
byte[] data = getFileContent(resourceFile.path()).getBytes(StandardCharsets.UTF_8); byte[] data = getFileContent(resourceFile.path());
resourceService.createOrUpdateSystemResource(ResourceType.JS_MODULE, resourceFile.name(), data); resourceService.createOrUpdateSystemResource(ResourceType.JS_MODULE, resourceFile.name(), data);
} }
List<RepoFile> images = listFiles("images"); List<RepoFile> images = listFiles("images");
for (RepoFile imageFile : images) { for (RepoFile imageFile : images) {
byte[] data = getFileContent(imageFile.path()).getBytes(StandardCharsets.UTF_8); byte[] data = getFileContent(imageFile.path());
imageService.createOrUpdateSystemImage(imageFile.name(), data); imageService.createOrUpdateSystemImage(imageFile.name(), data);
} }
Stream<String> widgetsBundles = listFiles("widget_bundles").stream() Stream<String> widgetsBundles = listFiles("widget_bundles").stream()
.map(widgetsBundleFile -> getFileContent(widgetsBundleFile.path())); .map(widgetsBundleFile -> new String(getFileContent(widgetsBundleFile.path()), StandardCharsets.UTF_8));
Stream<String> widgetTypes = listFiles("widget_types").stream() Stream<String> widgetTypes = listFiles("widget_types").stream()
.map(widgetTypeFile -> getFileContent(widgetTypeFile.path())); .map(widgetTypeFile -> new String(getFileContent(widgetTypeFile.path()), StandardCharsets.UTF_8));
widgetsBundleService.updateSystemWidgets(widgetsBundles, widgetTypes); widgetsBundleService.updateSystemWidgets(widgetsBundles, widgetTypes);
RepoFile dashboardFile = listFiles("dashboards").get(0); RepoFile dashboardFile = listFiles("dashboards").get(0);
resourceService.createOrUpdateSystemResource(ResourceType.DASHBOARD, GATEWAYS_DASHBOARD_KEY, getFileContent(dashboardFile.path()).getBytes(StandardCharsets.UTF_8)); resourceService.createOrUpdateSystemResource(ResourceType.DASHBOARD, GATEWAYS_DASHBOARD_KEY, getFileContent(dashboardFile.path()));
log.info("Gateways dashboard sync completed"); log.info("Gateways dashboard sync completed");
} }
@ -98,7 +98,7 @@ public class DashboardSyncService {
return gitSyncService.listFiles(REPO_KEY, path, 1, FileType.FILE); return gitSyncService.listFiles(REPO_KEY, path, 1, FileType.FILE);
} }
private String getFileContent(String path) { private byte[] getFileContent(String path) {
return gitSyncService.getFileContent(REPO_KEY, path); return gitSyncService.getFileContent(REPO_KEY, path);
} }

View File

@ -92,14 +92,9 @@ public class DefaultGitSyncService implements GitSyncService {
@Override @Override
public String getFileContent(String key, String path) { public byte[] getFileContent(String key, String path) {
GitRepository repository = getRepository(key); GitRepository repository = getRepository(key);
try { return repository.getFileContentAtCommit(path, getBranchRef(repository));
return repository.getFileContentAtCommit(path, getBranchRef(repository));
} catch (Exception e) {
log.warn("[{}] Failed to get file content for path {}: {}", key, path, e.getMessage());
return "{}";
}
} }
@Override @Override

View File

@ -26,7 +26,7 @@ public interface GitSyncService {
List<RepoFile> listFiles(String key, String path, int depth, FileType type); List<RepoFile> listFiles(String key, String path, int depth, FileType type);
String getFileContent(String key, String path); byte[] getFileContent(String key, String path);
String getGithubRawContentUrl(String key, String path); String getGithubRawContentUrl(String key, String path);

View File

@ -172,7 +172,7 @@ public class DefaultGitRepositoryService implements GitRepositoryService {
@Override @Override
public String getFileContentAtCommit(TenantId tenantId, String relativePath, String versionId) throws IOException { public String getFileContentAtCommit(TenantId tenantId, String relativePath, String versionId) throws IOException {
GitRepository repository = checkRepository(tenantId); GitRepository repository = checkRepository(tenantId);
return repository.getFileContentAtCommit(relativePath, versionId); return new String(repository.getFileContentAtCommit(relativePath, versionId), StandardCharsets.UTF_8);
} }
@Override @Override

View File

@ -284,8 +284,8 @@ public class GitRepository {
return files; return files;
} }
@SneakyThrows
public String getFileContentAtCommit(String file, String commitId) throws IOException { public byte[] getFileContentAtCommit(String file, String commitId) {
log.debug("Executing getFileContentAtCommit [{}][{}][{}]", settings.getRepositoryUri(), commitId, file); log.debug("Executing getFileContentAtCommit [{}][{}][{}]", settings.getRepositoryUri(), commitId, file);
RevCommit revCommit = resolveCommit(commitId); RevCommit revCommit = resolveCommit(commitId);
try (TreeWalk treeWalk = TreeWalk.forPath(git.getRepository(), file, revCommit.getTree())) { try (TreeWalk treeWalk = TreeWalk.forPath(git.getRepository(), file, revCommit.getTree())) {
@ -296,8 +296,7 @@ public class GitRepository {
try (ObjectReader objectReader = git.getRepository().newObjectReader()) { try (ObjectReader objectReader = git.getRepository().newObjectReader()) {
ObjectLoader objectLoader = objectReader.open(blobId); ObjectLoader objectLoader = objectReader.open(blobId);
try { try {
byte[] bytes = objectLoader.getBytes(); return objectLoader.getBytes();
return new String(bytes, StandardCharsets.UTF_8);
} catch (LargeObjectException e) { } catch (LargeObjectException e) {
throw new RuntimeException("File " + file + " is too big to load"); throw new RuntimeException("File " + file + " is too big to load");
} }
@ -397,11 +396,11 @@ public class GitRepository {
diff.setFilePath(diffEntry.getChangeType() != DiffEntry.ChangeType.DELETE ? diffEntry.getNewPath() : diffEntry.getOldPath()); diff.setFilePath(diffEntry.getChangeType() != DiffEntry.ChangeType.DELETE ? diffEntry.getNewPath() : diffEntry.getOldPath());
diff.setChangeType(diffEntry.getChangeType()); diff.setChangeType(diffEntry.getChangeType());
try { try {
diff.setFileContentAtCommit1(getFileContentAtCommit(diff.getFilePath(), commit1)); diff.setFileContentAtCommit1(new String(getFileContentAtCommit(diff.getFilePath(), commit1), StandardCharsets.UTF_8));
} catch (IllegalArgumentException ignored) { } catch (IllegalArgumentException ignored) {
} }
try { try {
diff.setFileContentAtCommit2(getFileContentAtCommit(diff.getFilePath(), commit2)); diff.setFileContentAtCommit2(new String(getFileContentAtCommit(diff.getFilePath(), commit2), StandardCharsets.UTF_8));
} catch (IllegalArgumentException ignored) { } catch (IllegalArgumentException ignored) {
} }
return diff; return diff;