Skip to content

Commit

Permalink
Extract gradle task for fetching newer versions
Browse files Browse the repository at this point in the history
  • Loading branch information
dnestoro committed Nov 8, 2024
1 parent fbba242 commit d76a418
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 116 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check-new-library-versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- name: "🕸️ Populate matrix"
id: set-matrix
run: |
./gradlew getExistingLibrariesWithNewVersions -PlibraryMatrixLimit=200
./gradlew fetchExistingLibrariesWithNewerVersions --matrixLimit=200
- name: "🔨 Create branch"
run: |
git config --local user.email "[email protected]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ plugins {
}

import groovy.json.JsonOutput
import org.graalvm.internal.tasks.FetchExistingLibrariesWithNewerVersions
import org.graalvm.internal.tck.DockerTask
import org.graalvm.internal.tck.ConfigFilesChecker
import org.graalvm.internal.tck.ScaffoldTask
Expand Down Expand Up @@ -103,60 +104,6 @@ def matrixDefault = [

final String METADATA_GROUP = "Metadata"

List<String> getNewerVersionsFor(String library, String startingVersion) {
def baseUrl = "https://repo1.maven.org/maven2"
String[] libraryParts = library.split(":")
String group = libraryParts[0].replace(".", "/")
String artifact = libraryParts[1]
def data = new URL(baseUrl + "/" + group + "/" + artifact + "/" + "maven-metadata.xml").getText()

return tck.getNewerVersionsFromLibraryIndex(data, startingVersion, library)
}

// gradle getExistingLibrariesWithNewVersions
Provider<Task> getExistingLibrariesWithNewVersions = tasks.register("getExistingLibrariesWithNewVersions", DefaultTask) { task ->
task.setDescription("Returns list of all libraries coordinates")
task.setGroup(METADATA_GROUP)
task.doFirst {
if (!project.hasProperty("libraryMatrixLimit")) {
throw new IllegalArgumentException("Please provide the maximum number of libraries versions!")
}

// get all existing libraries
Set<String> libraries = []
matchingCoordinates.forEach {
libraries.add(it.substring(0, it.lastIndexOf(":")))
}

// foreach existing library find newer versions than the latest one tested except for infrastructure tests
List<String> infrastructureTests = List.of("samples", "org.example")
List<String> newerVersions = new ArrayList<>()
libraries.forEach {
String libraryName = it
if (infrastructureTests.stream().noneMatch(testName -> libraryName.startsWith(testName))) {
List<String> versions = getNewerVersionsFor(libraryName, tck.getLatestLibraryVersion(libraryName))
versions.forEach {
newerVersions.add(libraryName.concat(":").concat(it))
}
}
}

// number of discovered versions is potentially huge and we want to limit it
int limit = Integer.parseInt((String) project.findProperty("libraryMatrixLimit"))
if (newerVersions.size() > limit) {
newerVersions = newerVersions.subList(0, limit)
}

def matrix = [
"coordinates" : newerVersions,
"version" : ["17"],
"os" : ["ubuntu-latest"]
]

println "::set-output name=matrix::${JsonOutput.toJson(matrix)}"
}
}

// gradle generateMatrixMatchingCoordinates -Pcoordinates=<maven-coordinates>
Provider<Task> generateMatrixMatchingCoordinates = tasks.register("generateMatrixMatchingCoordinates", DefaultTask) { task ->
task.setDescription("Returns matrix definition populated with all matching coordinates")
Expand Down Expand Up @@ -216,6 +163,14 @@ Provider<Task> generateMatrixDiffCoordinates = tasks.register("generateMatrixDif
}
}

// groovy tasks
tasks.register("fetchExistingLibrariesWithNewerVersions", FetchExistingLibrariesWithNewerVersions.class) { task ->
task.setGroup(METADATA_GROUP)
task.setDescription("Returns list of all libraries coordinates")
task.setAllLibraryCoordinates(matchingCoordinates)
}

// java tasks
tasks.register("checkAllowedDockerImages", GrypeTask.class) { task ->
task.setDescription("Returns list of allowed docker images")
task.setGroup(METADATA_GROUP)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package org.graalvm.internal.tasks

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import groovy.json.JsonOutput
import org.graalvm.internal.tck.model.MetadataVersionsIndexEntry
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.options.Option
import org.gradle.util.internal.VersionNumber

import java.util.regex.Matcher
import java.util.regex.Pattern


abstract class FetchExistingLibrariesWithNewerVersions extends DefaultTask {

@Input
List<String> allLibraryCoordinates

@Input
@Option(option = "matrixLimit", description = "Sets the maximum number of coordinates in the final matrix")
Integer matrixLimit

private static final List<String> INFRASTRUCTURE_TESTS = List.of("samples", "org.example")

@TaskAction
void action() {
// get all existing libraries
Set<String> libraries = []
allLibraryCoordinates.forEach {
libraries.add(it.substring(0, it.lastIndexOf(":")))
}

// foreach existing library find newer versions than the latest one tested except for infrastructure tests
List<String> newerVersions = new ArrayList<>()
libraries.forEach {
String libraryName = it
if (INFRASTRUCTURE_TESTS.stream().noneMatch(testName -> libraryName.startsWith(testName))) {
List<String> versions = getNewerVersionsFor(libraryName, getLatestLibraryVersion(libraryName))
versions.forEach {
newerVersions.add(libraryName.concat(":").concat(it))
}
}
}

if (newerVersions.size() > getMatrixLimit()) {
newerVersions = newerVersions.subList(0, getMatrixLimit())
}

def matrix = [
"coordinates": newerVersions,
"version" : ["17"],
"os" : ["ubuntu-latest"]
]

println "::set-output name=matrix::${JsonOutput.toJson(matrix)}"
}

static List<String> getNewerVersionsFor(String library, String startingVersion) {
def baseUrl = "https://repo1.maven.org/maven2"
String[] libraryParts = library.split(":")
String group = libraryParts[0].replace(".", "/")
String artifact = libraryParts[1]
def data = new URL(baseUrl + "/" + group + "/" + artifact + "/" + "maven-metadata.xml").getText()

return getNewerVersionsFromLibraryIndex(data, startingVersion, library)
}

static List<String> getNewerVersionsFromLibraryIndex(String index, String startingVersion, String libraryName) {
Pattern pattern = Pattern.compile("<version>(.*)</version>");
Matcher matcher = pattern.matcher(index);
List<String> allVersions = new ArrayList<>();

if (matcher.groupCount() < 1) {
throw new RuntimeException("Cannot find versions in the given index file: " + libraryName);
}

while (matcher.find()) {
allVersions.add(matcher.group(1));
}

int indexOfStartingVersion = allVersions.indexOf(startingVersion);
if (indexOfStartingVersion < 0) {
System.out.println("Cannot find starting version in index file: " + libraryName + " for version " + startingVersion);
return new ArrayList<>();
}

allVersions = allVersions.subList(indexOfStartingVersion, allVersions.size());
if (allVersions.size() <= 1) {
System.out.println("Cannot find newer versions for " + libraryName + " after the version " + startingVersion);
}

return allVersions.subList(1, allVersions.size());
}

static String getLatestLibraryVersion(String libraryModule) {
try {
String[] coordinates = libraryModule.split(":");
String group = coordinates[0];
String artifact = coordinates[1];

File coordinatesMetadataIndex = new File("metadata/" + group + "/" + artifact +"/index.json");
ObjectMapper objectMapper = new ObjectMapper()
.enable(SerializationFeature.INDENT_OUTPUT)
.setSerializationInclusion(JsonInclude.Include.NON_NULL);

List<MetadataVersionsIndexEntry> entries = objectMapper.readValue(coordinatesMetadataIndex, new TypeReference<List<MetadataVersionsIndexEntry>>() {
});

List<String> allTested = new ArrayList<>();
for (MetadataVersionsIndexEntry entry : entries) {
allTested.addAll(entry.testedVersions());
}

if (allTested.isEmpty()) {
throw new IllegalStateException("Cannot find any tested version for: " + libraryModule);
}

allTested.sort(Comparator.comparing(VersionNumber::parse));
return allTested.get(allTested.size() - 1);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,7 @@ List<String> getMatchingCoordinates(String coordinateFilter) {
String artifactId = strings.get(1);
String version = strings.get(2);


Set<String> matchingCoordinates = new HashSet<>();

for (String directory : getMatchingMetadataDirs(groupId, artifactId)) {
Path index = metadataRoot().resolve(directory).resolve("index.json");
List<Map<String, ?>> metadataIndex = (List<Map<String, ?>>) extractJsonFile(index);
Expand All @@ -324,7 +322,8 @@ List<String> getMatchingCoordinates(String coordinateFilter) {
}
}
}
return matchingCoordinates.stream().collect(Collectors.toList());

return new ArrayList<>(matchingCoordinates);
}

/**
Expand Down Expand Up @@ -353,62 +352,4 @@ List<String> getMetadataFileList(Path directory) throws IOException {
return foundFiles;
}
}

String getLatestLibraryVersion(String libraryModule) {
try {
String[] coordinates = libraryModule.split(":");
String group = coordinates[0];
String artifact = coordinates[1];

File coordinatesMetadataIndex = new File("metadata/" + group + "/" + artifact +"/index.json");
ObjectMapper objectMapper = new ObjectMapper()
.enable(SerializationFeature.INDENT_OUTPUT)
.setSerializationInclusion(JsonInclude.Include.NON_NULL);

List<MetadataVersionsIndexEntry> entries = objectMapper.readValue(coordinatesMetadataIndex, new TypeReference<>() {
});

List<String> allTested = new ArrayList<>();
for (MetadataVersionsIndexEntry entry : entries) {
allTested.addAll(entry.testedVersions());
}

if (allTested.isEmpty()) {
throw new IllegalStateException("Cannot find any tested version for: " + libraryModule);
}

allTested.sort(Comparator.comparing(VersionNumber::parse));
return allTested.get(allTested.size() - 1);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

List<String> getNewerVersionsFromLibraryIndex(String index, String startingVersion, String libraryName) {
Pattern pattern = Pattern.compile("<version>(.*)</version>");
Matcher matcher = pattern.matcher(index);
List<String> allVersions = new ArrayList<>();

if (matcher.groupCount() < 1) {
throw new RuntimeException("Cannot find versions in the given index file: " + libraryName);
}

while (matcher.find()) {
allVersions.add(matcher.group(1));
}

int indexOfStartingVersion = allVersions.indexOf(startingVersion);
if (indexOfStartingVersion < 0) {
System.out.println("Cannot find starting version in index file: " + libraryName + " for version " + startingVersion);
return new ArrayList<>();
}

allVersions = allVersions.subList(indexOfStartingVersion, allVersions.size());
if (allVersions.size() <= 1) {
System.out.println("Cannot find newer versions for " + libraryName + " after the version " + startingVersion);
}

return allVersions.subList(1, allVersions.size());
}

}

0 comments on commit d76a418

Please sign in to comment.