Skip to content

Commit

Permalink
Adding download jar functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
frankreyesgarcia committed Apr 1, 2024
1 parent 5b4b1e2 commit cf26d7b
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 60 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@
<artifactId>japicmp</artifactId>
<version>0.20.0</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.17.2</version>
</dependency>
</dependencies>

</project>
5 changes: 3 additions & 2 deletions src/main/java/se/kth/breaking_changes/ApiMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public String getName() {
* @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) {
public static boolean download(String groupId, String artifactId, String version, Path directory) {
/*
* Maven repository link for the previous version of the dependency
*/
Expand All @@ -61,7 +61,7 @@ public static boolean download(String groupId, String artifactId, String version
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);
downloadAndConvert(response, directory.toString() + File.separator + jarName);
return true;
}
} catch (IOException e) {
Expand All @@ -87,3 +87,4 @@ private static void downloadAndConvert(Response response, String fileName) throw
}

}

73 changes: 53 additions & 20 deletions src/main/java/se/kth/data/DockerImages.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import se.kth.breaking_changes.Download;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
Expand All @@ -25,7 +27,6 @@
import java.util.Map;
import java.util.Objects;


public class DockerImages {


Expand All @@ -39,7 +40,7 @@ public class DockerImages {
private static final List<String> containers = new ArrayList<>();


public void getProject(BreakingUpdateMetadata breakingUpdates) {
public void getProject(BreakingUpdateMetadata breakingUpdates) throws IOException, InterruptedException {

createDockerClient();

Expand All @@ -56,31 +57,55 @@ public void getProject(BreakingUpdateMetadata breakingUpdates) {
Path breaking = dir.resolve(breakingCommit);


if (Files.exists(breaking)) {
return;
}
String previousVersion = breakingUpdates.updatedDependency().dependencyArtifactID() + "-" + breakingUpdates.updatedDependency().previousVersion() + ".jar";
String newVersion = breakingUpdates.updatedDependency().dependencyArtifactID() + "-" + breakingUpdates.updatedDependency().newVersion() + ".jar";

Path projectDir_breaking;
try {
projectDir_breaking = Files.createDirectories(breaking);
} catch (IOException e) {
log.error("Could not create the project directory {}", project, e);
return;
}
boolean existsProject = Files.exists(breaking.resolve(project));
boolean existPreviousVersion = Files.exists(breaking.resolve(previousVersion));
boolean existNewVersion = Files.exists(breaking.resolve(newVersion));


Path projectDir = copyProject(Objects.requireNonNull(startContainer(breakingImage, project)).getKey(), project, projectDir_breaking);
if (projectDir == null) {
if (!existsProject) {
Path projectDir_breaking;
try {
projectDir_breaking = Files.createDirectories(breaking);
} catch (IOException e) {
log.error("Could not create the project directory {}", project, e);
return;
}
Path projectDir = copyProject(Objects.requireNonNull(startContainer(breakingImage, project)).getKey(), project, projectDir_breaking);
deleteContainers(breakingImage);
return;
if (projectDir == null) {
log.error("Could not copy the project {}", project);
return;
}
}

try {
if (!existPreviousVersion) {
Files.createDirectories(breaking);
File prev = Download.getJarFile(breakingUpdates.updatedDependency().dependencyGroupID(),
breakingUpdates.updatedDependency().dependencyArtifactID(),
breakingUpdates.updatedDependency().previousVersion(), breaking);
System.out.println((prev != null ? "Downloaded " : "Fail to download ") + breakingUpdates.updatedDependency().dependencyArtifactID() + "-" + breakingUpdates.updatedDependency().previousVersion());
}
if (!existNewVersion) {
File newV = Download.getJarFile(breakingUpdates.updatedDependency().dependencyGroupID(),
breakingUpdates.updatedDependency().dependencyArtifactID(),
breakingUpdates.updatedDependency().newVersion(), breaking);
System.out.println((newV != null ? "Downloaded " : "Fail to download ") + breakingUpdates.updatedDependency().dependencyArtifactID() + "-" + breakingUpdates.updatedDependency().newVersion());
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}

deleteContainers(breakingImage);

log.info("=====================================================================================================");
log.info("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");
log.info("=====================================================================================================");
log.info("");
// deleteContainers(breakingImage);

// log.info("=====================================================================================================");
// log.info("* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ");
// log.info("=====================================================================================================");
// log.info("");


}
Expand Down Expand Up @@ -201,6 +226,14 @@ private void createDockerClient() {
dockerClient = DockerClientImpl.getInstance(clientConfig, httpClient);
}


/**
* Copy old/new pair of dependency jar/pom files from the corresponding containers.
*
* @return the type of the updated dependency.
*/


public DockerImages() {
createDockerClient();
}
Expand Down
100 changes: 63 additions & 37 deletions src/main/java/se/kth/data/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -21,9 +22,9 @@ public class Main {


public static void main(String[] args) {
List<BreakingUpdateMetadata> list = getBreakingCommit(Path.of("/Users/frank/Documents/Work/PHD/chains-project/paper/bump/data/benchmark"));
//List<BreakingUpdateMetadata> list = getBreakingCommit(Path.of("examples/Benchmark"));

List<BreakingUpdateMetadata> list = getBreakingCommit(Path.of("/Users/frank/Documents/Work/PHD/Explaining/breaking-good/benchmark/data/benchmark"));
// List<BreakingUpdateMetadata> list = getBreakingCommit(Path.of("examples/Benchmark"));
//
List<BreakingUpdateMetadata> compilationErrors = list.stream().filter(b -> b.failureCategory().equals("COMPILATION_FAILURE")).toList();

generateTemplate(compilationErrors);
Expand Down Expand Up @@ -54,52 +55,77 @@ public static void generateTemplate(List<BreakingUpdateMetadata> breakingUpdateL
String githubURL = "https://github.com/knaufk/flink-faker/blob/1ef97ea6c5b6e34151fe6167001b69e003449f95/src/main/java/com/github/knaufk/flink/faker/DateTime.java#L44";

Path jars = Path.of("/Users/frank/Documents/Work/PHD/Tools/bump_experiments/jars");

DockerImages dockerImages = new DockerImages();

List<ExplanationStatistics> explanationStatistics = new ArrayList<>();

for (BreakingUpdateMetadata breakingUpdate : breakingUpdateList) {

Path jarsFile = Path.of("projects/");

System.out.println("Processing breaking update " + breakingUpdate.breakingCommit());
dockerImages.getProject(breakingUpdate);
try {
dockerImages.getProject(breakingUpdate);
} catch (IOException | InterruptedException e) {
System.out.println("Error downloading breaking update " + breakingUpdate.breakingCommit());
}

// processingBreakingUpdate(breakingUpdate, jarsFile, explanationStatistics);
}

try {
JApiCmpAnalyze jApiCmpAnalyze = new JApiCmpAnalyze(
jars.resolve("%s/%s-%s.jar".formatted(breakingUpdate.breakingCommit(), breakingUpdate.updatedDependency().dependencyArtifactID(), breakingUpdate.updatedDependency().previousVersion())),
jars.resolve("%s/%s-%s.jar".formatted(breakingUpdate.breakingCommit(), breakingUpdate.updatedDependency().dependencyArtifactID(), breakingUpdate.updatedDependency().newVersion()))
);
}

// System.out.println(jApiCmpAnalyze);
private static void processingBreakingUpdate(BreakingUpdateMetadata breakingUpdate, Path jarsFile, List<ExplanationStatistics> explanationStatistics) {
try {
JApiCmpAnalyze jApiCmpAnalyze = new JApiCmpAnalyze(
jarsFile.resolve("%s/%s-%s.jar".formatted(breakingUpdate.breakingCommit(), breakingUpdate.updatedDependency().dependencyArtifactID(), breakingUpdate.updatedDependency().previousVersion())),
jarsFile.resolve("%s/%s-%s.jar".formatted(breakingUpdate.breakingCommit(), breakingUpdate.updatedDependency().dependencyArtifactID(), breakingUpdate.updatedDependency().newVersion()))
);

Set<ApiChange> apiChanges = jApiCmpAnalyze.useJApiCmp();

CombineResults combineResults = new CombineResults(apiChanges);
//
combineResults.setDependencyGroupID(breakingUpdate.updatedDependency().dependencyGroupID());

combineResults.setProject("projects/%s".formatted(breakingUpdate.breakingCommit()));

combineResults.setMavenLog(new MavenLogAnalyzer(
new File("projects/%s/%s/%s.log".formatted(breakingUpdate.breakingCommit(), breakingUpdate.project(), breakingUpdate.breakingCommit()))));

try {
Changes changes = combineResults.analyze();

System.out.println("Project: " + breakingUpdate.project());
System.out.println("Breaking Commit: " + breakingUpdate.breakingCommit());
System.out.println("Changes: " + changes.changes().size());
System.out.println("**********************************************************");
System.out.println();
ExplanationTemplate explanationTemplate = new CompilationErrorTemplate(changes, "Explanations/" + breakingUpdate.breakingCommit() + ".md");
explanationTemplate.generateTemplate();
} catch (IOException e) {
throw new RuntimeException(e);
}

} catch (Exception e) {
System.out.println("Error processing breaking update " + breakingUpdate.breakingCommit());
System.out.println(e.getMessage());
Set<ApiChange> apiChanges = jApiCmpAnalyze.useJApiCmp();

CombineResults combineResults = new CombineResults(apiChanges);

combineResults.setDependencyGroupID(breakingUpdate.updatedDependency().dependencyGroupID());

combineResults.setProject("projects/%s".formatted(breakingUpdate.breakingCommit()));

combineResults.setMavenLog(new MavenLogAnalyzer(
new File("projects/%s/%s/%s.log".formatted(breakingUpdate.breakingCommit(), breakingUpdate.project(), breakingUpdate.breakingCommit()))));

try {
Changes changes = combineResults.analyze();

System.out.println("Project: " + breakingUpdate.project());
System.out.println("Breaking Commit: " + breakingUpdate.breakingCommit());
System.out.println("Changes: " + changes.changes().size());
explanationStatistics.add(new ExplanationStatistics(breakingUpdate.project(), breakingUpdate.breakingCommit(), changes.changes().size()));
ExplanationTemplate explanationTemplate = new CompilationErrorTemplate(changes, "Explanations/" + breakingUpdate.breakingCommit() + ".md");
explanationTemplate.generateTemplate();
System.out.println("**********************************************************");
System.out.println();
} catch (IOException e) {
System.out.println("Error analyzing breaking update " + breakingUpdate.breakingCommit());
System.out.println(e);
throw new RuntimeException(e);
}
try {
Path file = Path.of("explanationStatistics.json");
Files.deleteIfExists(file);
Files.createFile(file);
JsonUtils.writeToFile(file, explanationStatistics);
} catch (IOException e) {
throw new RuntimeException(e);
}

} catch (Exception e) {
System.out.println("Error processing breaking update " + breakingUpdate.breakingCommit());
System.out.println(e);
}
}

public record ExplanationStatistics(String project, String commit, int changes) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public CompilationErrorTemplate(Changes changes, String fileName) {
@Override
public String getHead() {

BreakingChange breakingChange = changes.changes().iterator().next();
BreakingChange breakingChange = !changes.changes().isEmpty() ?changes.changes().iterator().next() : null;


return "CI detected that the dependency upgrade from version **%s** to **%s** has failed. Here are details to help you understand and fix the problem:"
.formatted(breakingChange.getApiChanges().getOldVersion().getName(), breakingChange.getApiChanges().getNewVersion().getName());
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/se/kth/explaining/ExplanationTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public String translateCategory(String category) {

public void generateTemplate() {

if(changes.changes().isEmpty()){
return;
}

FileWriter markdownFile = null;
try {
Expand Down

0 comments on commit cf26d7b

Please sign in to comment.