Skip to content

Better container/Elasticsearch tags and add JDK tag #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public final class BuildScanMetadata {
private static final Pattern DOCKERFILE_FROM_PATTERN = Pattern.compile( "FROM (.+)" );
private static final Pattern CONTAINER_IMAGE_SHORT_PATTERN = Pattern.compile(
"^(?:.*/)?([^/]+:[^-.]+(?:[-.][^-.]+)?).*$" );
private static final Pattern JDK_VERSION_MAJOR_PATTERN = Pattern.compile(
"^.*version \"(\\d+).*$", Pattern.DOTALL );
private static final Pattern JDK_VERSION_MAJOR_FALLBACK_PATTERN = Pattern.compile( "(\\d+)\\." );

private BuildScanMetadata() {
}
Expand All @@ -40,12 +43,12 @@ public static void addMetadataToBuildScan(BuildScanApi buildScanApi, MavenSessio
tagIntegrations( buildScanApi, mavenSession, project );
}

recordExecutableVersion(
buildScanApi, mavenSession, "java-version.main.compiler", JavaVersions::forJavacExecutable );
recordExecutableVersion(
buildScanApi, mavenSession, "java-version.test.compiler", JavaVersions::forJavacExecutable );
recordExecutableVersion(
buildScanApi, mavenSession, "java-version.test.launcher", JavaVersions::forJavaExecutable );
recordExecutableVersion( buildScanApi, mavenSession, "java-version.main.compiler", false,
JavaVersions::forJavacExecutable );
recordExecutableVersion( buildScanApi, mavenSession, "java-version.test.compiler", false,
JavaVersions::forJavacExecutable );
recordExecutableVersion( buildScanApi, mavenSession, "java-version.test.launcher", true,
JavaVersions::forJavaExecutable );
}

private static void tagIntegrations(BuildScanApi buildScanApi, MavenSession mavenSession, MavenProject project) {
Expand All @@ -64,8 +67,7 @@ private static void tagIntegrations(BuildScanApi buildScanApi, MavenSession mave
if ( !getBooleanProperty( project, "test.lucene.skip" ) ) {
buildScanApi.tag( "lucene" );
}
if ( !getBooleanProperty( project, "test.elasticsearch.skip" )
&& getBooleanProperty( project, "test.elasticsearch.run.image.pull" ) ) {
if ( !getBooleanProperty( project, "test.elasticsearch.skip" ) ) {
var distribution = getStringProperty( mavenSession, "test.elasticsearch.distribution" );
tagDockerfileShortImageRef( buildScanApi, mavenSession,
"search-backend/%s.Dockerfile".formatted( distribution ),
Expand Down Expand Up @@ -97,7 +99,7 @@ private static void tagDockerfileShortImageRef(BuildScanApi buildScanApi, MavenS
ref = ref.substring( 0, ref.lastIndexOf( ':' ) + 1 ) + versionOverride;
}
String shortImageRef = toShortImageRef( ref );
buildScanApi.tag( shortImageRef );
buildScanApi.tag( shortImageRef.replace( ':', '-' ) );
buildScanApi.value(
shortImageRef.substring( 0, shortImageRef.lastIndexOf( ':' ) ),
ref.substring( ref.lastIndexOf( ':' ) + 1 )
Expand All @@ -119,12 +121,29 @@ static String toShortImageRef(String ref) {
}

private static void recordExecutableVersion(BuildScanApi buildScanApi, MavenSession mavenSession,
String propertyName, Function<String, String> executableToVersion) {
String propertyName, boolean tag, Function<String, String> executableToVersion) {
String javaExecutable = getStringProperty( mavenSession, propertyName );
String javaVersion = executableToVersion.apply( javaExecutable );
if ( tag ) {
buildScanApi.tag( "jdk-%s".formatted( toJdkMajor( javaVersion ) ) );
}
buildScanApi.value( propertyName, "Path: %s\nResolved version: %s".formatted( javaExecutable, javaVersion ) );
}

static Object toJdkMajor(String fullVersionText) {
var matcher = JDK_VERSION_MAJOR_PATTERN.matcher( fullVersionText );
if ( matcher.matches() ) {
return matcher.group( 1 );
}
// As a fallback, try to match a simple version string
// such as the one coming from Runtime.version().toString()
matcher = JDK_VERSION_MAJOR_FALLBACK_PATTERN.matcher( fullVersionText );
if ( matcher.find() ) {
return matcher.group( 1 );
}
return "unknown";
}

private static String getStringProperty(MavenSession mavenSession, String key) {
return getStringProperty( mavenSession.getResult().getProject(), key );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,28 @@ void toShortImageRef(String expected, String ref) {
assertEquals( expected, BuildScanMetadata.toShortImageRef( ref ) );
}

@ParameterizedTest
@CsvSource(textBlock = """
17,'javac 17.0.7
openjdk version "17.0.7" 2023-04-18
OpenJDK Runtime Environment Temurin-17.0.7+7 (build 17.0.7+7)
OpenJDK 64-Bit Server VM Temurin-17.0.7+7 (build 17.0.7+7, mixed mode, sharing)'
11,'javac 17.0.7
openjdk version "11.0.7" 2023-04-18
OpenJDK Runtime Environment Temurin-17.0.7+7 (build 17.0.7+7)
OpenJDK 64-Bit Server VM Temurin-17.0.7+7 (build 17.0.7+7, mixed mode, sharing)'
17,'openjdk version "17.0.7" 2023-04-18
OpenJDK Runtime Environment Temurin-17.0.7+7 (build 17.0.7+7)
OpenJDK 64-Bit Server VM Temurin-17.0.7+7 (build 17.0.7+7, mixed mode, sharing)'
11,'openjdk version "11.0.7" 2023-04-18
OpenJDK Runtime Environment Temurin-17.0.7+7 (build 17.0.7+7)
OpenJDK 64-Bit Server VM Temurin-17.0.7+7 (build 17.0.7+7, mixed mode, sharing)'
17,17.0.7
11,11.0.7
17,foobar 17.0 something
""")
void toJdkMajor(String expected, String fullVersionText) {
assertEquals( expected, BuildScanMetadata.toJdkMajor( fullVersionText ) );
}

}