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 0014c86c author for commit
0014c86c is described below

commit 0014c86c3d87a6a5b6d3f2812c8c3f214762e948
Author: Marat Gubaidullin <ma...@talismancloud.io>
AuthorDate: Mon Aug 12 13:55:14 2024 -0400

    author for commit
---
 .../camel/karavan/api/AbstractApiResource.java     | 47 ++++++++++++++++++++++
 .../camel/karavan/api/ProjectGitResource.java      | 12 +++++-
 .../apache/camel/karavan/api/ProjectResource.java  |  5 ++-
 .../camel/karavan/listener/CommitListener.java     |  4 +-
 .../apache/camel/karavan/service/GitService.java   | 28 +++----------
 .../camel/karavan/service/ProjectService.java      | 12 ++++--
 6 files changed, 78 insertions(+), 30 deletions(-)

diff --git 
a/karavan-app/src/main/java/org/apache/camel/karavan/api/AbstractApiResource.java
 
b/karavan-app/src/main/java/org/apache/camel/karavan/api/AbstractApiResource.java
new file mode 100644
index 00000000..a32124e6
--- /dev/null
+++ 
b/karavan-app/src/main/java/org/apache/camel/karavan/api/AbstractApiResource.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.karavan.api;
+
+import io.quarkus.oidc.UserInfo;
+import io.quarkus.security.identity.SecurityIdentity;
+import jakarta.inject.Inject;
+
+import java.util.HashMap;
+import java.util.Objects;
+
+
+public class AbstractApiResource {
+
+    @Inject
+    SecurityIdentity securityIdentity;
+
+    public HashMap<String, String> getIdentity() {
+        var identity = new HashMap<String, String>();
+
+        if (securityIdentity != null && securityIdentity.getPrincipal() != 
null) {
+            identity.put("name", securityIdentity.getPrincipal().getName());
+        }
+        if (securityIdentity != null && 
securityIdentity.getAttributes().get("email") != null && 
!securityIdentity.getAttributes().get("email").toString().isBlank()) {
+            identity.put("email", 
securityIdentity.getAttributes().get("email").toString());
+        } else if (securityIdentity != null && 
securityIdentity.getAttributes().get("userinfo") != null) {
+            UserInfo userInfo = (UserInfo) 
securityIdentity.getAttributes().get("userinfo");
+            String email = Objects.isNull(userInfo.getEmail()) || 
userInfo.getEmail().isBlank() ? "kara...@test.org" : userInfo.getEmail();
+            identity.put("email", email);
+        }
+        return identity;
+    }
+}
\ No newline at end of file
diff --git 
a/karavan-app/src/main/java/org/apache/camel/karavan/api/ProjectGitResource.java
 
b/karavan-app/src/main/java/org/apache/camel/karavan/api/ProjectGitResource.java
index a43d751a..57c7ce1d 100644
--- 
a/karavan-app/src/main/java/org/apache/camel/karavan/api/ProjectGitResource.java
+++ 
b/karavan-app/src/main/java/org/apache/camel/karavan/api/ProjectGitResource.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.karavan.api;
 
+import io.quarkus.security.identity.SecurityIdentity;
 import io.vertx.core.json.JsonObject;
 import io.vertx.mutiny.core.eventbus.EventBus;
 import jakarta.inject.Inject;
@@ -30,7 +31,7 @@ import java.util.HashMap;
 import static org.apache.camel.karavan.KaravanEvents.CMD_PUSH_PROJECT;
 
 @Path("/ui/git")
-public class ProjectGitResource {
+public class ProjectGitResource extends AbstractApiResource {
 
     private static final Logger LOGGER = 
Logger.getLogger(ProjectGitResource.class.getName());
 
@@ -40,11 +41,18 @@ public class ProjectGitResource {
     @Inject
     EventBus eventBus;
 
+    @Inject
+    SecurityIdentity securityIdentity;
+
     @POST
     @Produces(MediaType.APPLICATION_JSON)
     @Consumes(MediaType.APPLICATION_JSON)
     public HashMap<String, String> push(HashMap<String, String> params) throws 
Exception {
-        eventBus.publish(CMD_PUSH_PROJECT, JsonObject.mapFrom(params));
+        var identity = getIdentity();
+        var data = JsonObject.mapFrom(params);
+        data.put("authorName", identity.get("name"));
+        data.put("authorEmail", identity.get("email"));
+        eventBus.publish(CMD_PUSH_PROJECT, data);
         return params;
     }
 
diff --git 
a/karavan-app/src/main/java/org/apache/camel/karavan/api/ProjectResource.java 
b/karavan-app/src/main/java/org/apache/camel/karavan/api/ProjectResource.java
index 581d5ec5..72361f4e 100644
--- 
a/karavan-app/src/main/java/org/apache/camel/karavan/api/ProjectResource.java
+++ 
b/karavan-app/src/main/java/org/apache/camel/karavan/api/ProjectResource.java
@@ -38,7 +38,7 @@ import java.util.List;
 import java.util.Objects;
 
 @Path("/ui/project")
-public class ProjectResource {
+public class ProjectResource extends AbstractApiResource {
     private static final Logger LOGGER = 
Logger.getLogger(ProjectResource.class.getName());
 
     @Inject
@@ -102,7 +102,8 @@ public class ProjectResource {
             LOGGER.info("Deleting deployments");
             Response res4 = infrastructureResource.deleteDeployment(null, 
projectId);
         }
-        gitService.deleteProject(projectId, 
karavanCache.getProjectFiles(projectId));
+        var identity = getIdentity();
+        gitService.deleteProject(projectId, 
karavanCache.getProjectFiles(projectId), identity.get("name"), 
identity.get("email"));
         karavanCache.getProjectFiles(projectId).forEach(file -> 
karavanCache.deleteProjectFile(projectId, file.getName()));
         karavanCache.deleteProject(projectId);
         LOGGER.info("Project deleted");
diff --git 
a/karavan-app/src/main/java/org/apache/camel/karavan/listener/CommitListener.java
 
b/karavan-app/src/main/java/org/apache/camel/karavan/listener/CommitListener.java
index 7a689bf1..3392ec2a 100644
--- 
a/karavan-app/src/main/java/org/apache/camel/karavan/listener/CommitListener.java
+++ 
b/karavan-app/src/main/java/org/apache/camel/karavan/listener/CommitListener.java
@@ -47,8 +47,10 @@ public class CommitListener {
         String message = event.getString("message");
         String userId = event.getString("userId");
         String eventId = event.getString("eventId");
+        String authorName = event.getString("authorName");
+        String authorEmail = event.getString("authorEmail");
         try {
-            Project p = projectService.commitAndPushProject(projectId, 
message);
+            Project p = projectService.commitAndPushProject(projectId, 
message, authorName, authorEmail);
             if (userId != null) {
                 eventBus.publish(COMMIT_HAPPENED, JsonObject.of("userId", 
userId, "eventId", eventId, "project", JsonObject.mapFrom(p)));
             }
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 2b47c1e5..533e523a 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
@@ -18,8 +18,6 @@ package org.apache.camel.karavan.service;
 
 import com.jcraft.jsch.JSch;
 import com.jcraft.jsch.JSchException;
-import io.quarkus.oidc.UserInfo;
-import io.quarkus.security.identity.SecurityIdentity;
 import io.smallrye.mutiny.tuples.Tuple2;
 import io.vertx.core.Vertx;
 import jakarta.enterprise.context.ApplicationScoped;
@@ -77,9 +75,6 @@ public class GitService {
     @Inject
     Vertx vertx;
 
-    @Inject
-    SecurityIdentity securityIdentity;
-
     SshSessionFactory sshSessionFactory;
 
     private Git gitForImport;
@@ -116,7 +111,7 @@ public class GitService {
         return new GitConfig(repository, username.orElse(null), 
password.orElse(null), branch, privateKeyPath.orElse(null));
     }
 
-    public RevCommit commitAndPushProject(Project project, List<ProjectFile> 
files, String message) throws GitAPIException, IOException, URISyntaxException {
+    public RevCommit commitAndPushProject(Project project, List<ProjectFile> 
files, String message, String authorName, String authorEmail) throws 
GitAPIException, IOException, URISyntaxException {
         LOGGER.info("Commit and push project " + project.getProjectId());
         GitConfig gitConfig = getGitConfig();
         String uuid = UUID.randomUUID().toString();
@@ -134,7 +129,7 @@ public class GitService {
 //        }
         writeProjectToFolder(folder, project, files);
         addDeletedFilesToIndex(git, folder, project, files);
-        return commitAddedAndPush(git, gitConfig.getBranch(), message);
+        return commitAddedAndPush(git, gitConfig.getBranch(), message, 
authorName, authorName);
     }
 
     public List<GitRepo> readProjectsToImport() {
@@ -275,10 +270,10 @@ public class GitService {
         });
     }
 
-    public RevCommit commitAddedAndPush(Git git, String branch, String 
message) throws GitAPIException {
+    public RevCommit commitAddedAndPush(Git git, String branch, String 
message, String authorName, String authorEmail) throws GitAPIException {
         LOGGER.info("Commit and push changes to the branch " + branch);
         LOGGER.info("Git add: " + git.add().addFilepattern(".").call());
-        RevCommit commit = 
git.commit().setMessage(message).setAuthor(getPersonIdent()).call();
+        RevCommit commit = git.commit().setMessage(message).setAuthor(new 
PersonIdent(authorName, authorEmail)).call();
         LOGGER.info("Git commit: " + commit);
         if (!ephemeral) {
             PushCommand pushCommand = git.push();
@@ -290,17 +285,6 @@ public class GitService {
         return commit;
     }
 
-    private PersonIdent getPersonIdent() {
-        String defaultEmailAddress = "kara...@test.org";
-
-        if (securityIdentity != null && 
securityIdentity.getAttributes().get("userinfo") != null) {
-            UserInfo userInfo = (UserInfo) 
securityIdentity.getAttributes().get("userinfo");
-            String email = Objects.isNull(userInfo.getEmail()) || 
userInfo.getEmail().isBlank() ? defaultEmailAddress : userInfo.getEmail();
-            return new PersonIdent(securityIdentity.getPrincipal().getName(), 
email);
-        }
-        return new PersonIdent("karavan", defaultEmailAddress);
-    }
-
     public Git init(String dir, String uri, String branch) throws 
GitAPIException, IOException, URISyntaxException {
         Git git = 
Git.init().setInitialBranch(branch).setDirectory(Path.of(dir).toFile()).call();
 //        git.branchCreate().setName(branch).call();
@@ -317,7 +301,7 @@ public class GitService {
         }
     }
 
-    public void deleteProject(String projectId, List<ProjectFile> files) 
throws GitAPIException, IOException, URISyntaxException {
+    public void deleteProject(String projectId, List<ProjectFile> files, 
String authorName, String authorEmail) {
         LOGGER.info("Delete and push project " + projectId);
         GitConfig gitConfig = getGitConfig();
         String uuid = UUID.randomUUID().toString();
@@ -327,7 +311,7 @@ public class GitService {
         try {
             Git git = getGit(true, folder);
             addDeletedFolderToIndex(git, folder, projectId, files);
-            commitAddedAndPush(git, gitConfig.getBranch(), commitMessage);
+            commitAddedAndPush(git, gitConfig.getBranch(), commitMessage, 
authorName, authorEmail);
             LOGGER.info("Delete Temp folder " + folder);
             vertx.fileSystem().deleteRecursiveBlocking(folder, true);
             LOGGER.infof("Project %s deleted from Git", projectId);
diff --git 
a/karavan-app/src/main/java/org/apache/camel/karavan/service/ProjectService.java
 
b/karavan-app/src/main/java/org/apache/camel/karavan/service/ProjectService.java
index f1ab9a25..e1340087 100644
--- 
a/karavan-app/src/main/java/org/apache/camel/karavan/service/ProjectService.java
+++ 
b/karavan-app/src/main/java/org/apache/camel/karavan/service/ProjectService.java
@@ -23,7 +23,6 @@ import jakarta.enterprise.context.ApplicationScoped;
 import jakarta.enterprise.inject.Default;
 import jakarta.inject.Inject;
 import org.apache.camel.karavan.KaravanCache;
-import org.apache.camel.karavan.KaravanConstants;
 import org.apache.camel.karavan.docker.DockerComposeConverter;
 import org.apache.camel.karavan.docker.DockerForKaravan;
 import org.apache.camel.karavan.kubernetes.KubernetesService;
@@ -47,8 +46,10 @@ import static org.apache.camel.karavan.service.CodeService.*;
 public class ProjectService {
 
     private static final Logger LOGGER = 
Logger.getLogger(ProjectService.class.getName());
+    private static final String DEFAULT_AUTHOR_NAME = "karavan";
+    private static final String DEFAULT_AUTHOR_EMAIL = "kara...@test.org";
 
-    @ConfigProperty(name = "karavan.environment", defaultValue = 
KaravanConstants.DEV)
+    @ConfigProperty(name = "karavan.environment", defaultValue = DEV)
     String environment;
 
     @Inject
@@ -69,12 +70,17 @@ public class ProjectService {
     @Inject
     EventBus eventBus;
 
+
     public Project commitAndPushProject(String projectId, String message) 
throws Exception {
+        return commitAndPushProject(projectId, message, DEFAULT_AUTHOR_NAME, 
DEFAULT_AUTHOR_EMAIL);
+    }
+
+    public Project commitAndPushProject(String projectId, String message, 
String authorName, String authorEmail) throws Exception {
         if (Objects.equals(environment, DEV)) {
             LOGGER.info("Commit project: " + projectId);
             Project p = karavanCache.getProject(projectId);
             List<ProjectFile> files = karavanCache.getProjectFiles(projectId);
-            RevCommit commit = gitService.commitAndPushProject(p, files, 
message);
+            RevCommit commit = gitService.commitAndPushProject(p, files, 
message, authorName, authorEmail);
             karavanCache.syncFilesCommited(projectId);
             String commitId = commit.getId().getName();
             Long lastUpdate = commit.getCommitTime() * 1000L;

Reply via email to