Skip to content

Commit

Permalink
Refactor Deployer to take Configuration as parameter
Browse files Browse the repository at this point in the history
By passing Configuration in and storing as a class variable,
we can ensure that the params provided to Deployer dont
continue to expand. It simplifies the method signatures too.

Signed-off-by: Samuel Dacanay <sam.dacanay@chainguard.dev>
  • Loading branch information
dakaneye committed Oct 29, 2024
1 parent 2c7359b commit 7a2644e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class MavenRepositoryDeployer {

private DefaultRepositorySystemSession session;

private final Configuration config;

private final TreeSet<String> successfulDeploys = new TreeSet<String>();

private final TreeSet<String> failedDeploys = new TreeSet<String>();
Expand All @@ -59,17 +61,18 @@ public class MavenRepositoryDeployer {

private final TreeSet<String> potentialDeploys = new TreeSet<String>();

public MavenRepositoryDeployer(File repositoryPath, Boolean parallelDeploy, int deployThreads) {
public MavenRepositoryDeployer(File repositoryPath, Configuration configuration) {
this.repositoryPath = repositoryPath;
initialize(parallelDeploy, deployThreads);
this.config = configuration;
initialize();
}

private void initialize(Boolean parallelDeploy, int deployThreads) {
private void initialize() {
system = RepositoryHandler.getRepositorySystem();
session = RepositoryHandler.getRepositorySystemSession(system, repositoryPath);
if (parallelDeploy) {
if (config.getParallelDeploy()) {
session.setConfigProperty("aether.connector.basic.parallelPut", "true");
session.setConfigProperty("aether.connector.basic.threads", deployThreads);
session.setConfigProperty("aether.connector.basic.threads", config.getDeployThreads());
}
}

Expand Down Expand Up @@ -115,8 +118,7 @@ public static Collection<File> getPomFiles(File repoPath) {
return pomFiles;
}

public void deployToRemote(
String targetUrl, String username, String password, Boolean checkTarget, Boolean verifyOnly) {
public void deployToRemote() {
Collection<File> leafDirectories = getLeafDirectories(repositoryPath);

for (File leafDirectory : leafDirectories) {
Expand All @@ -128,8 +130,8 @@ public void deployToRemote(
Gav gav = GavUtil.getGavFromRepositoryPath(leafRepoPath);

boolean pomInTarget = false;
if (checkTarget) {
pomInTarget = checkIfPomInTarget(targetUrl, username, password, gav);
if (config.getCheckTarget()) {
pomInTarget = checkIfPomInTarget(gav);
}

if (pomInTarget) {
Expand All @@ -144,12 +146,13 @@ public void deployToRemote(
Collection<File> artifacts = FileUtils.listFiles(leafDirectory, fileFilter, null);

Authentication auth = new AuthenticationBuilder()
.addUsername(username)
.addPassword(password)
.addUsername(config.getUsername())
.addPassword(config.getPassword())
.build();

RemoteRepository distRepo = new RemoteRepository.Builder("repositoryIdentifier", "default", targetUrl)
.setProxy(ProxyHelper.getProxy(targetUrl))
RemoteRepository distRepo = new RemoteRepository.Builder("repositoryIdentifier", "default",
config.getTargetUrl())
.setProxy(ProxyHelper.getProxy(config.getTargetUrl()))
.setAuthentication(auth)
.build();

Expand Down Expand Up @@ -195,7 +198,7 @@ public void deployToRemote(
}

try {
if (verifyOnly) {
if (config.getVerifyOnly()) {
for (Artifact artifact : deployRequest.getArtifacts()) {
potentialDeploys.add(artifact.toString());
}
Expand All @@ -218,14 +221,13 @@ public void deployToRemote(
/**
* Check if POM file for provided gav can be found in target. Just does
* a HTTP get of the header and verifies http status OK 200.
* @param targetUrl url of the target repository
* @param gav group artifact version string
* @return {@code true} if the pom.xml already exists in the target repository
*/
private boolean checkIfPomInTarget(String targetUrl, String username, String password, Gav gav) {
private boolean checkIfPomInTarget(Gav gav) {
boolean alreadyInTarget = false;

String artifactUrl = targetUrl + gav.getRepositoryURLPath() + gav.getPomFilename();
String artifactUrl = config.getTargetUrl() + gav.getRepositoryURLPath() + gav.getPomFilename();
logger.debug("Headers for {}", artifactUrl);

HttpHead httphead;
Expand All @@ -236,8 +238,9 @@ private boolean checkIfPomInTarget(String targetUrl, String username, String pas
return true;
}

if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
String encoding = java.util.Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
if (!StringUtils.isEmpty(config.getUsername()) && !StringUtils.isEmpty(config.getUsername())) {
String encoding =
java.util.Base64.getEncoder().encodeToString((config.getUsername() + ":" + config.getPassword()).getBytes());
httphead.setHeader("Authorization", "Basic " + encoding);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,8 @@ private static ArtifactRetriever retrieveArtifacts() {

private static MavenRepositoryDeployer deployArtifacts() {
logger.info("Artifact deployment starting.");
MavenRepositoryDeployer helper = new MavenRepositoryDeployer(cacheDirectory, config.getParallelDeploy(),
config.getDeployThreads());
helper.deployToRemote(
config.getTargetUrl(),
config.getUsername(),
config.getPassword(),
config.getCheckTarget(),
config.getVerifyOnly());
MavenRepositoryDeployer helper = new MavenRepositoryDeployer(cacheDirectory, config);
helper.deployToRemote();
logger.info("Artifact deployment completed.");
return helper;
}
Expand Down

0 comments on commit 7a2644e

Please sign in to comment.