Skip to content

Commit

Permalink
Merge pull request #6 from chains-project/download_jars
Browse files Browse the repository at this point in the history
Adding download jar feature
  • Loading branch information
frankreyesgarcia authored Mar 1, 2024
2 parents 8c16af1 + 396bae8 commit e73a79a
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,10 @@ build/
.DS_Store
/projects/
/examples/

/src/main/java/se/kth/breaking_changes/Main.java
/src/main/java/se/kth/core/Main.java
/src/main/java/se/kth/data
/src/main/java/se/kth/explaining/Main.java
/src/main/java/se/kth/log_Analyzer/Main.java
/src/main/java/se/kth/spoon_compare/Main.java
71 changes: 71 additions & 0 deletions src/main/java/se/kth/breaking_changes/ApiMetadata.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,92 @@
package se.kth.breaking_changes;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@lombok.Setter
public class ApiMetadata {

private static final Logger log = LoggerFactory.getLogger(ApiMetadata.class);

String name;

Path file;

private static final Map<String, String> repositoryUrls = Map.of(
"mavenCentral", "https://repo1.maven.org/maven2/",
"jenkins","https://repo.jenkins-ci.org/artifactory/releases/"
);


public ApiMetadata(String name, Path file) {
this.name = name;
this.file = file;

}

public String getName() {
return name.contains(".jar") ? name.substring(0, name.indexOf(".jar")) : name;
}

/**
* Downloads the version of the dependency from the maven repository
*
* @param artifactId the name of the dependency
* @param groupId the group id of the dependency
* @param version the version of the dependency
* @return true if the download was successful, false otherwise
*/
public static boolean download(String groupId, String artifactId, String version) {



/*
* Maven repository link for the previous version of the dependency
*/
final OkHttpClient httpConnector
= new OkHttpClient.Builder().connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(120, TimeUnit.SECONDS).readTimeout(60, TimeUnit.SECONDS).build();

String jarName = artifactId + "-" + version + ".jar";

for (String repository : repositoryUrls.values()) {
String artifactUrl = getArtifactUrl(groupId, artifactId, version, repository);
try (Response response = httpConnector.newCall(new Request.Builder().url(artifactUrl).build()).execute()) {
if (response.code() == 200) {
downloadAndConvert(response, "jars" + File.separator + jarName);
return true;
}
} catch (IOException e) {
log.error("Maven source links could not be found for the updated dependency {}.", artifactId, e);
return false;
}
}

return false;
}

private static String getArtifactUrl(String groupId, String artifactId, String version, String repositoryUrl) {
groupId = groupId.replace('.', '/');
return repositoryUrl + groupId + "/" + artifactId + "/" + version + "/";
}

private static void downloadAndConvert(Response response, String fileName) throws IOException {
assert response.body() != null;
byte[] bytes = response.body().bytes();
try (FileOutputStream fos = new FileOutputStream(fileName)) {
fos.write(bytes);
}
}

}
50 changes: 42 additions & 8 deletions src/main/java/se/kth/breaking_changes/Main.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,57 @@
package se.kth.breaking_changes;

import se.kth.data.BreakingUpdateMetadata;

import java.nio.file.Path;
import java.util.Set;
import java.util.List;

import static se.kth.data.Main.getBreakingCommit;

public class Main {

public static void main(String[] args) {

JApiCmpAnalyze jApiCmpAnalyze = new JApiCmpAnalyze(
Path.of("/Users/frank/Documents/Work/PHD/Tools/bump_experiments/jars/0abf7148300f40a1da0538ab060552bca4a2f1d8/jasperreports-6.18.1.jar"),
Path.of("/Users/frank/Documents/Work/PHD/Tools/bump_experiments/jars/0abf7148300f40a1da0538ab060552bca4a2f1d8/jasperreports-6.19.1.jar")
);
// JApiCmpAnalyze jApiCmpAnalyze = new JApiCmpAnalyze(
// Path.of("/Users/frank/Documents/Work/PHD/Tools/bump_experiments/jars/0abf7148300f40a1da0538ab060552bca4a2f1d8/jasperreports-6.18.1.jar"),
// Path.of("/Users/frank/Documents/Work/PHD/Tools/bump_experiments/jars/0abf7148300f40a1da0538ab060552bca4a2f1d8/jasperreports-6.19.1.jar")
// );
//
// Set<ApiChange> apiChanges = jApiCmpAnalyze.useJApiCmp();
//
// for (ApiChange apiChange : apiChanges) {
// System.out.println(apiChange);
// }


List<BreakingUpdateMetadata> list =
getBreakingCommit(Path.of("/Users/frank/Documents/Work/PHD/chains-project/paper/bump/data/benchmark"));


Set<ApiChange> apiChanges = jApiCmpAnalyze.useJApiCmp();
for (BreakingUpdateMetadata breakingUpdate : list) {
boolean result = ApiMetadata.download(breakingUpdate.updatedDependency().dependencyGroupID(),
breakingUpdate.updatedDependency().dependencyArtifactID(),
breakingUpdate.updatedDependency().newVersion());

if (result) {
System.out.println("Downloaded " + breakingUpdate.updatedDependency().dependencyArtifactID() + "-" + breakingUpdate.updatedDependency().newVersion());
} else {
System.out.println("Failed to download " + breakingUpdate.updatedDependency().dependencyArtifactID() + "-" + breakingUpdate.updatedDependency().newVersion());
}

result = ApiMetadata.download(breakingUpdate.updatedDependency().dependencyGroupID(),
breakingUpdate.updatedDependency().dependencyArtifactID(),
breakingUpdate.updatedDependency().previousVersion());

if (result) {
System.out.println("Downloaded " + breakingUpdate.updatedDependency().dependencyArtifactID() + "-" + breakingUpdate.updatedDependency().newVersion());
} else {
System.out.println("Failed to download " + breakingUpdate.updatedDependency().dependencyArtifactID() + "-" + breakingUpdate.updatedDependency().newVersion());
}

for (ApiChange apiChange : apiChanges) {
System.out.println(apiChange);
}


}


}

0 comments on commit e73a79a

Please sign in to comment.