Commit 5e2566f4 authored by mamunozgil's avatar mamunozgil
Browse files

repoUtil update

No related merge requests found
Pipeline #10998 passed with stage
in 15 seconds
Showing with 13 additions and 23 deletions
+13 -23
...@@ -10,6 +10,8 @@ import org.springframework.stereotype.Component; ...@@ -10,6 +10,8 @@ import org.springframework.stereotype.Component;
import org.springframework.util.FileSystemUtils; import org.springframework.util.FileSystemUtils;
import org.tmatesoft.svn.core.*; import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager; import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNWCUtil; import org.tmatesoft.svn.core.wc.SVNWCUtil;
import org.tmatesoft.svn.core.wc2.SvnCheckout; import org.tmatesoft.svn.core.wc2.SvnCheckout;
import org.tmatesoft.svn.core.wc2.SvnOperationFactory; import org.tmatesoft.svn.core.wc2.SvnOperationFactory;
...@@ -34,7 +36,7 @@ public class RepoUtil { ...@@ -34,7 +36,7 @@ public class RepoUtil {
String repoUrl = config.group(1); String repoUrl = config.group(1);
String username = config.group(2); String username = config.group(2);
String password = config.group(3); // Not used for SSH authentication String password = config.group(3);
LOG.debug("Cloning repository: {}", repoUrl); LOG.debug("Cloning repository: {}", repoUrl);
...@@ -57,7 +59,7 @@ public class RepoUtil { ...@@ -57,7 +59,7 @@ public class RepoUtil {
LOG.debug("Preparing clone..."); LOG.debug("Preparing clone...");
if (repoUrl.endsWith(".git")) { if (repoUrl.endsWith(".git")) {
// GIT Repository Clone LOG.debug("Cloning Git repository...");
CloneCommand cloneCommand = Git.cloneRepository() CloneCommand cloneCommand = Git.cloneRepository()
.setDirectory(checkoutDirectory) .setDirectory(checkoutDirectory)
.setURI(repoUrl); .setURI(repoUrl);
...@@ -67,11 +69,9 @@ public class RepoUtil { ...@@ -67,11 +69,9 @@ public class RepoUtil {
cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password)); cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
} }
LOG.debug("Cloning Git repository...");
cloneCommand.call().close(); cloneCommand.call().close();
LOG.info("Git repository successfully cloned to {}", checkoutDirectory.getAbsolutePath());
} else { } else {
// SVN Repository Clone (including svn+ssh support)
SvnOperationFactory operationFactory = new SvnOperationFactory(); SvnOperationFactory operationFactory = new SvnOperationFactory();
try { try {
SVNURL svnUrl = SVNURL.parseURIEncoded(repoUrl); SVNURL svnUrl = SVNURL.parseURIEncoded(repoUrl);
...@@ -80,37 +80,29 @@ public class RepoUtil { ...@@ -80,37 +80,29 @@ public class RepoUtil {
checkout.setSource(SvnTarget.fromURL(svnUrl)); checkout.setSource(SvnTarget.fromURL(svnUrl));
checkout.setDepth(SVNDepth.INFINITY); checkout.setDepth(SVNDepth.INFINITY);
// **Use SSH authentication if URL starts with svn+ssh://**
if (repoUrl.startsWith("svn+ssh://")) { if (repoUrl.startsWith("svn+ssh://")) {
LOG.debug("Setting up SSH authentication for SVN..."); LOG.debug("Using SSH authentication for SVN...");
File privateKeyFile = new File("/home/appuser/.ssh/id_rsa");
String sshPrivateKeyPath = "/home/appuser/.ssh/id_rsa";
File privateKeyFile = new File(sshPrivateKeyPath);
if (!privateKeyFile.exists()) { if (!privateKeyFile.exists()) {
LOG.error("SSH private key not found at: {}", sshPrivateKeyPath); LOG.error("SSH private key not found at: {}", privateKeyFile.getAbsolutePath());
throw new SVNException(SVNErrorMessage.create(SVNErrorCode.AUTHN_CREDS_UNAVAILABLE, "SSH key not found")); throw new SVNException(SVNErrorMessage.create(SVNErrorCode.AUTHN_CREDS_UNAVAILABLE, "SSH key not found"));
} }
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager( ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(
new File("/home/appuser/.ssh/id_rsa"), // Explicit SSH Key privateKeyFile, username, new char[0], null, false);
username, DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
null, SVNClientManager clientManager = SVNClientManager.newInstance(options, authManager);
new File("/home/appuser/.ssh/known_hosts"), // Explicit Known Hosts
false
);
operationFactory.setAuthenticationManager(authManager); operationFactory.setAuthenticationManager(authManager);
} }
LOG.debug("Performing SVN checkout..."); LOG.debug("Performing SVN checkout...");
checkout.run(); checkout.run();
LOG.info("SVN repository successfully cloned to {}", checkoutDirectory.getAbsolutePath());
} finally { } finally {
operationFactory.dispose(); operationFactory.dispose();
} }
} }
// Handle subdirectory case
if (!subDir.isEmpty()) { if (!subDir.isEmpty()) {
File sourceSubDir = new File(checkoutDirectory, subDir); File sourceSubDir = new File(checkoutDirectory, subDir);
if (sourceSubDir.exists()) { if (sourceSubDir.exists()) {
...@@ -121,10 +113,8 @@ public class RepoUtil { ...@@ -121,10 +113,8 @@ public class RepoUtil {
} }
LOG.debug("Repository successfully cloned from {} to {}", repoUrl, targetDirectory); LOG.debug("Repository successfully cloned from {} to {}", repoUrl, targetDirectory);
} catch (IOException | GitAPIException | SVNException e) { } catch (IOException | GitAPIException | SVNException e) {
LOG.error("Error while cloning repository: " + repoUrl, e); LOG.error("Error while cloning repository: " + repoUrl, e);
} }
} }
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment