An error occurred while loading the file. Please try again.
-
mamunozgil authored5e2566f4
package de.hftstuttgart.dtabackend.utils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.springframework.stereotype.Component;
import org.springframework.util.FileSystemUtils;
import org.tmatesoft.svn.core.*;
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.wc2.SvnCheckout;
import org.tmatesoft.svn.core.wc2.SvnOperationFactory;
import org.tmatesoft.svn.core.wc2.SvnTarget;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
@Component
public class RepoUtil {
private static final Logger LOG = LogManager.getLogger(RepoUtil.class);
public RepoUtil() {}
public void cloneRepository(Matcher config, String targetPath, String subDir) {
if (!config.matches()) {
LOG.error("Invalid repository URL format.");
return;
}
String repoUrl = config.group(1);
String username = config.group(2);
String password = config.group(3);
LOG.debug("Cloning repository: {}", repoUrl);
File targetDirectory = new File(targetPath);
if (targetDirectory.exists()) {
LOG.debug("Target directory exists, deleting it.");
FileSystemUtils.deleteRecursively(targetDirectory);
}
File checkoutDirectory = targetDirectory;
if (!subDir.isEmpty()) {
checkoutDirectory = new File(targetPath + "_checkout");
if (checkoutDirectory.exists()) {
LOG.debug("Checkout directory exists, deleting it.");
FileSystemUtils.deleteRecursively(checkoutDirectory);
}
}
try {
LOG.debug("Preparing clone...");
if (repoUrl.endsWith(".git")) {
LOG.debug("Cloning Git repository...");
CloneCommand cloneCommand = Git.cloneRepository()
.setDirectory(checkoutDirectory)
.setURI(repoUrl);
if (!"none".equals(username) && !"none".equals(password)) {
LOG.debug("Setting Git credentials.");
cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
}
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
cloneCommand.call().close();
LOG.info("Git repository successfully cloned to {}", checkoutDirectory.getAbsolutePath());
} else {
SvnOperationFactory operationFactory = new SvnOperationFactory();
try {
SVNURL svnUrl = SVNURL.parseURIEncoded(repoUrl);
SvnCheckout checkout = operationFactory.createCheckout();
checkout.setSingleTarget(SvnTarget.fromFile(checkoutDirectory));
checkout.setSource(SvnTarget.fromURL(svnUrl));
checkout.setDepth(SVNDepth.INFINITY);
if (repoUrl.startsWith("svn+ssh://")) {
LOG.debug("Using SSH authentication for SVN...");
File privateKeyFile = new File("/home/appuser/.ssh/id_rsa");
if (!privateKeyFile.exists()) {
LOG.error("SSH private key not found at: {}", privateKeyFile.getAbsolutePath());
throw new SVNException(SVNErrorMessage.create(SVNErrorCode.AUTHN_CREDS_UNAVAILABLE, "SSH key not found"));
}
ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(
privateKeyFile, username, new char[0], null, false);
DefaultSVNOptions options = SVNWCUtil.createDefaultOptions(true);
SVNClientManager clientManager = SVNClientManager.newInstance(options, authManager);
operationFactory.setAuthenticationManager(authManager);
}
LOG.debug("Performing SVN checkout...");
checkout.run();
LOG.info("SVN repository successfully cloned to {}", checkoutDirectory.getAbsolutePath());
} finally {
operationFactory.dispose();
}
}
if (!subDir.isEmpty()) {
File sourceSubDir = new File(checkoutDirectory, subDir);
if (sourceSubDir.exists()) {
FileSystemUtils.copyRecursively(sourceSubDir, targetDirectory);
} else {
LOG.error("Specified subdirectory does not exist.");
}
}
LOG.debug("Repository successfully cloned from {} to {}", repoUrl, targetDirectory);
} catch (IOException | GitAPIException | SVNException e) {
LOG.error("Error while cloning repository: " + repoUrl, e);
}
}
}