You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ma...@apache.org on 2024/03/05 16:09:22 UTC

(camel-karavan) branch main updated: Ephemeral git

This is an automated email from the ASF dual-hosted git repository.

marat pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-karavan.git


The following commit(s) were added to refs/heads/main by this push:
     new 85bfa675 Ephemeral git
85bfa675 is described below

commit 85bfa675bbec15b2e046c6171081cc06b555d75a
Author: Marat Gubaidullin <ma...@talismancloud.io>
AuthorDate: Tue Mar 5 11:09:13 2024 -0500

    Ephemeral git
---
 .../apache/camel/karavan/service/GitService.java   | 78 ++++++++++++++--------
 1 file changed, 50 insertions(+), 28 deletions(-)

diff --git a/karavan-app/src/main/java/org/apache/camel/karavan/service/GitService.java b/karavan-app/src/main/java/org/apache/camel/karavan/service/GitService.java
index 7a4ce07e..db176e71 100644
--- a/karavan-app/src/main/java/org/apache/camel/karavan/service/GitService.java
+++ b/karavan-app/src/main/java/org/apache/camel/karavan/service/GitService.java
@@ -19,10 +19,12 @@ package org.apache.camel.karavan.service;
 import com.jcraft.jsch.JSch;
 import com.jcraft.jsch.JSchException;
 import io.quarkus.oidc.UserInfo;
+import io.quarkus.runtime.StartupEvent;
 import io.quarkus.security.identity.SecurityIdentity;
 import io.smallrye.mutiny.tuples.Tuple2;
 import io.vertx.core.Vertx;
 import jakarta.enterprise.context.ApplicationScoped;
+import jakarta.enterprise.event.Observes;
 import jakarta.inject.Inject;
 import org.apache.camel.karavan.model.*;
 import org.eclipse.jgit.api.*;
@@ -71,6 +73,9 @@ public class GitService {
     @ConfigProperty(name = "karavan.known-hosts-path")
     Optional<String> knownHostsPath;
 
+    @ConfigProperty(name = "karavan.git.ephemeral", defaultValue = "false")
+    boolean ephemeral;
+
     @Inject
     Vertx vertx;
 
@@ -95,6 +100,13 @@ public class GitService {
     }
 
     public GitConfig getGitConfig() {
+        if (ephemeral) {
+            repository = "http://karavan.git";
+            username = Optional.of("karavan");
+            password = Optional.of("karavan");
+            privateKeyPath = Optional.empty();
+            knownHostsPath = Optional.empty();
+        }
         return new GitConfig(repository, username.orElse(null), password.orElse(null), branch, privateKeyPath.orElse(null));
     }
 
@@ -121,16 +133,16 @@ public class GitService {
         String uuid = UUID.randomUUID().toString();
         String folder = vertx.fileSystem().createTempDirectoryBlocking(uuid);
         LOGGER.info("Temp folder created " + folder);
-        Git git = null;
-        try {
-            git = clone(folder, gitConfig.getUri(), gitConfig.getBranch());
-            checkout(git, false, null, null, gitConfig.getBranch());
-        } catch (RefNotFoundException | InvalidRemoteException | TransportException e) {
-            LOGGER.error("New repository");
-            git = init(folder, gitConfig.getUri(), gitConfig.getBranch());
-        } catch (Exception e) {
-            LOGGER.error("Error", e);
-        }
+        Git git = getGit(true, folder);
+//        try {
+//            git = clone(folder, gitConfig.getUri(), gitConfig.getBranch());
+//            checkout(git, false, null, null, gitConfig.getBranch());
+//        } catch (RefNotFoundException | InvalidRemoteException | TransportException e) {
+//            LOGGER.error("New repository");
+//            git = init(folder, gitConfig.getUri(), gitConfig.getBranch());
+//        } catch (Exception e) {
+//            LOGGER.error("Error", e);
+//        }
         writeProjectToFolder(folder, project, files);
         addDeletedFilesToIndex(git, folder, project, files);
         return commitAddedAndPush(git, gitConfig.getBranch(), message);
@@ -183,16 +195,21 @@ public class GitService {
         GitConfig gitConfig = getGitConfig();
         LOGGER.info("Temp folder created " + folder);
         Git git = null;
-        try {
-            git = clone(folder, gitConfig.getUri(), gitConfig.getBranch());
-            if (checkout) {
-                checkout(git, false, null, null, gitConfig.getBranch());
-            }
-        } catch (RefNotFoundException | InvalidRemoteException | TransportException e) {
-            LOGGER.error("New repository");
+        if (ephemeral) {
+            LOGGER.error("New ephemeral repository");
             git = init(folder, gitConfig.getUri(), gitConfig.getBranch());
-        } catch (Exception e) {
-            LOGGER.error("Error", e);
+        } else {
+            try {
+                git = clone(folder, gitConfig.getUri(), gitConfig.getBranch());
+                if (checkout) {
+                    checkout(git, false, null, null, gitConfig.getBranch());
+                }
+            } catch (RefNotFoundException | InvalidRemoteException | TransportException e) {
+                LOGGER.error("New repository");
+                git = init(folder, gitConfig.getUri(), gitConfig.getBranch());
+            } catch (Exception e) {
+                LOGGER.error("Error", e);
+            }
         }
         return git;
     }
@@ -270,11 +287,13 @@ public class GitService {
         LOGGER.info("Git add: " + git.add().addFilepattern(".").call());
         RevCommit commit = git.commit().setMessage(message).setAuthor(getPersonIdent()).call();
         LOGGER.info("Git commit: " + commit);
-        PushCommand pushCommand = git.push();
-        pushCommand.add(branch).setRemote("origin");
-        setCredentials(pushCommand);
-        Iterable<PushResult> result = pushCommand.call();
-        LOGGER.info("Git push: " + result);
+        if (!ephemeral) {
+            PushCommand pushCommand = git.push();
+            pushCommand.add(branch).setRemote("origin");
+            setCredentials(pushCommand);
+            Iterable<PushResult> result = pushCommand.call();
+            LOGGER.info("Git push: " + result);
+        }
         return commit;
     }
 
@@ -305,17 +324,17 @@ public class GitService {
         }
     }
 
-    public void deleteProject(String projectId, List<ProjectFile> files) {
+    public void deleteProject(String projectId, List<ProjectFile> files) throws GitAPIException, IOException, URISyntaxException {
         LOGGER.info("Delete and push project " + projectId);
         GitConfig gitConfig = getGitConfig();
         String uuid = UUID.randomUUID().toString();
         String folder = vertx.fileSystem().createTempDirectoryBlocking(uuid);
         String commitMessage = "Project " + projectId + " is deleted";
         LOGGER.infof("Temp folder %s is created for deletion of project %s", folder, projectId);
-        Git git = null;
         try {
-            git = clone(folder, gitConfig.getUri(), gitConfig.getBranch());
-            checkout(git, false, null, null, gitConfig.getBranch());
+            Git git = getGit(true, folder);
+//            git = clone(folder, gitConfig.getUri(), gitConfig.getBranch());
+//            checkout(git, false, null, null, gitConfig.getBranch());
             addDeletedFolderToIndex(git, folder, projectId, files);
             commitAddedAndPush(git, gitConfig.getBranch(), commitMessage);
             LOGGER.info("Delete Temp folder " + folder);
@@ -417,6 +436,9 @@ public class GitService {
 
     public boolean checkGit() throws Exception {
         LOGGER.info("Check git");
+        if (ephemeral) {
+            return true;
+        }
         GitConfig gitConfig = getGitConfig();
         String uuid = UUID.randomUUID().toString();
         String folder = vertx.fileSystem().createTempDirectoryBlocking(uuid);