Skip to content

Commit

Permalink
Set SDK version to the previous released version automatically for mi… (
Browse files Browse the repository at this point in the history
#5561)

* Set SDK version to the previous released version automatically for migration tool

* Add logging

* Fix tests

* Update version
  • Loading branch information
zoewangg committed Sep 19, 2024
1 parent e893c59 commit db81c49
Show file tree
Hide file tree
Showing 13 changed files with 547 additions and 592 deletions.
4 changes: 3 additions & 1 deletion buildspecs/update-master-from-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ phases:
mvn versions:set -DnewVersion=$NEW_VERSION_SNAPSHOT -DgenerateBackupPoms=false -DprocessAllModules=true || { echo "Failed to update POMs to next snapshot version"; exit 1; }
sed -i -E "s/(<version>).+(<\/version>)/\1$RELEASE_VERSION\2/" README.md
sed -i -E "s/(<awsjavasdk.previous.version>).+(<\/awsjavasdk.previous.version>)/\1$RELEASE_VERSION\2/" pom.xml
sed -i -E "s/(newVersion: ).+/\1$RELEASE_VERSION/" v2-migration/src/main/resources/META-INF/rewrite/upgrade-sdk-dependencies.yml
sed -i -E "s/(version: ).+/\1$RELEASE_VERSION/" v2-migration/src/main/resources/META-INF/rewrite/upgrade-sdk-dependencies.yml
git commit -am "Update to next snapshot version: $NEW_VERSION_SNAPSHOT"
fi
-
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.testutils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import software.amazon.awssdk.utils.IoUtils;

public final class SdkVersionUtils {

private SdkVersionUtils() {

}

public static String getSdkPreviousReleaseVersion(Path pomFile) throws IOException {
Optional<String> versionString =
Files.readAllLines(pomFile)
.stream().filter(l -> l.contains("<awsjavasdk.previous.version>")).findFirst();

if (!versionString.isPresent()) {
throw new AssertionError("No version is found");
}

String string = versionString.get().trim();
String substring = string.substring(29, string.indexOf('/') - 1);
return substring;
}

/**
* Check if the provided v2 artifacts are available on Maven
*/
public static boolean checkVersionAvailability(String version, String... artifactIds) throws IOException {
for (String artifactId : artifactIds) {
HttpURLConnection connection = null;
try {
URI uri = URI.create(String.format("https://repo.maven.apache.org/maven2/software/amazon/awssdk/%s/%s",
artifactId,
version));
connection = (HttpURLConnection) uri.toURL().openConnection(Proxy.NO_PROXY);
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
return false;
}
} finally {
IoUtils.closeQuietly(connection.getInputStream(), null);
IoUtils.closeQuietly(connection.getErrorStream(), null);
}
}
return true;
}
}
5 changes: 5 additions & 0 deletions test/v2-migration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>test-utils</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import software.amazon.awssdk.testutils.SdkVersionUtils;
import software.amazon.awssdk.utils.Logger;

public class GradleProjectTest {
Expand Down Expand Up @@ -82,11 +84,16 @@ private static void deleteTempDirectories() throws IOException {
}

@Test
@EnabledIf("versionAvailable")
void gradleProject_shouldConvert() throws IOException {
verifyTransformation();
verifyCompilation();
}

boolean versionAvailable() {
return TestUtils.versionAvailable(sdkVersion);
}

private static void verifyTransformation() throws IOException {
List<String> rewriteArgs = new ArrayList<>();
addAll(rewriteArgs, "./gradlew", "rewriteRun", "--init-script", "init.gradle",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import software.amazon.awssdk.testutils.SdkVersionUtils;
import software.amazon.awssdk.utils.Logger;

public class MavenProjectTest {
Expand Down Expand Up @@ -66,6 +68,7 @@ private static void deleteTempDirectories() throws IOException {
}

@Test
@EnabledIf("versionAvailable")
void mavenProject_shouldConvert() throws IOException {
verifyTransformation();
verifyCompilation();
Expand All @@ -87,4 +90,8 @@ private static void verifyCompilation() {
addAll(packageArgs, "mvn", "package");
run(mavenActual, packageArgs.toArray(new String[0]));
}

boolean versionAvailable() {
return TestUtils.versionAvailable(sdkVersion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -29,6 +32,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.opentest4j.AssertionFailedError;
import software.amazon.awssdk.testutils.SdkVersionUtils;
import software.amazon.awssdk.utils.IoUtils;
import software.amazon.awssdk.utils.Logger;
import software.amazon.awssdk.utils.StringUtils;
Expand All @@ -41,22 +45,25 @@ public static void assertTwoDirectoriesHaveSameStructure(Path a, Path b) {
assertLeftHasRight(b, a);
}

public static boolean versionAvailable(String sdkVersion) {
try {
return SdkVersionUtils.checkVersionAvailability(sdkVersion,
"apache-client",
"netty-nio-client",
"dynamodb",
"sqs",
"s3",
"http-auth-aws-eventstream",
"utils");
} catch (Exception exception) {
log.warn(() -> "Exception occurred, ignoring", exception);
return false;
}
}

public static String getVersion() throws IOException {
// TODO: uncomment the following code to dynamically get version
// once we update the version
// Path root = Paths.get(".").normalize().toAbsolutePath();
// Path pomFile = root.resolve("pom.xml");
// Optional<String> versionString =
// Files.readAllLines(pomFile)
// .stream().filter(l -> l.contains("<version>")).findFirst();
//
// if (!versionString.isPresent()) {
// throw new AssertionError("No version is found");
// }
//
// String string = versionString.get().trim();
// String substring = string.substring(9, string.indexOf('/') - 1);
return "2.27.0";
Path root = Paths.get(".").toAbsolutePath().getParent().getParent().getParent();
return SdkVersionUtils.getSdkPreviousReleaseVersion(root.resolve("pom.xml"));
}

public static String getMigrationToolVersion() throws IOException {
Expand Down
6 changes: 6 additions & 0 deletions v2-migration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>test-utils</artifactId>
<version>${awsjavasdk.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Loading

0 comments on commit db81c49

Please sign in to comment.