-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract gradle task for fetching newer versions
- Loading branch information
Showing
4 changed files
with
142 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
...src/main/groovy/org/graalvm/internal/tasks/FetchExistingLibrariesWithNewerVersions.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters