Skip to content

Commit db81c49

Browse files
authored
Set SDK version to the previous released version automatically for mi… (#5561)
* Set SDK version to the previous released version automatically for migration tool * Add logging * Fix tests * Update version
1 parent e893c59 commit db81c49

File tree

13 files changed

+547
-592
lines changed

13 files changed

+547
-592
lines changed

buildspecs/update-master-from-release.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ phases:
4040
mvn versions:set -DnewVersion=$NEW_VERSION_SNAPSHOT -DgenerateBackupPoms=false -DprocessAllModules=true || { echo "Failed to update POMs to next snapshot version"; exit 1; }
4141
sed -i -E "s/(<version>).+(<\/version>)/\1$RELEASE_VERSION\2/" README.md
4242
sed -i -E "s/(<awsjavasdk.previous.version>).+(<\/awsjavasdk.previous.version>)/\1$RELEASE_VERSION\2/" pom.xml
43-
43+
sed -i -E "s/(newVersion: ).+/\1$RELEASE_VERSION/" v2-migration/src/main/resources/META-INF/rewrite/upgrade-sdk-dependencies.yml
44+
sed -i -E "s/(version: ).+/\1$RELEASE_VERSION/" v2-migration/src/main/resources/META-INF/rewrite/upgrade-sdk-dependencies.yml
45+
4446
git commit -am "Update to next snapshot version: $NEW_VERSION_SNAPSHOT"
4547
fi
4648
-
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.testutils;
17+
18+
import java.io.IOException;
19+
import java.net.HttpURLConnection;
20+
import java.net.Proxy;
21+
import java.net.URI;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.util.Optional;
25+
import software.amazon.awssdk.utils.IoUtils;
26+
27+
public final class SdkVersionUtils {
28+
29+
private SdkVersionUtils() {
30+
31+
}
32+
33+
public static String getSdkPreviousReleaseVersion(Path pomFile) throws IOException {
34+
Optional<String> versionString =
35+
Files.readAllLines(pomFile)
36+
.stream().filter(l -> l.contains("<awsjavasdk.previous.version>")).findFirst();
37+
38+
if (!versionString.isPresent()) {
39+
throw new AssertionError("No version is found");
40+
}
41+
42+
String string = versionString.get().trim();
43+
String substring = string.substring(29, string.indexOf('/') - 1);
44+
return substring;
45+
}
46+
47+
/**
48+
* Check if the provided v2 artifacts are available on Maven
49+
*/
50+
public static boolean checkVersionAvailability(String version, String... artifactIds) throws IOException {
51+
for (String artifactId : artifactIds) {
52+
HttpURLConnection connection = null;
53+
try {
54+
URI uri = URI.create(String.format("https://repo.maven.apache.org/maven2/software/amazon/awssdk/%s/%s",
55+
artifactId,
56+
version));
57+
connection = (HttpURLConnection) uri.toURL().openConnection(Proxy.NO_PROXY);
58+
connection.connect();
59+
int responseCode = connection.getResponseCode();
60+
if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
61+
return false;
62+
}
63+
} finally {
64+
IoUtils.closeQuietly(connection.getInputStream(), null);
65+
IoUtils.closeQuietly(connection.getErrorStream(), null);
66+
}
67+
}
68+
return true;
69+
}
70+
}

test/v2-migration-tests/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@
120120
<artifactId>assertj-core</artifactId>
121121
<scope>test</scope>
122122
</dependency>
123+
<dependency>
124+
<groupId>software.amazon.awssdk</groupId>
125+
<artifactId>test-utils</artifactId>
126+
<scope>test</scope>
127+
</dependency>
123128
</dependencies>
124129

125130
<build>

test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/GradleProjectTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import org.apache.commons.io.FileUtils;
3636
import org.junit.jupiter.api.BeforeAll;
3737
import org.junit.jupiter.api.Test;
38+
import org.junit.jupiter.api.condition.EnabledIf;
39+
import software.amazon.awssdk.testutils.SdkVersionUtils;
3840
import software.amazon.awssdk.utils.Logger;
3941

4042
public class GradleProjectTest {
@@ -82,11 +84,16 @@ private static void deleteTempDirectories() throws IOException {
8284
}
8385

8486
@Test
87+
@EnabledIf("versionAvailable")
8588
void gradleProject_shouldConvert() throws IOException {
8689
verifyTransformation();
8790
verifyCompilation();
8891
}
8992

93+
boolean versionAvailable() {
94+
return TestUtils.versionAvailable(sdkVersion);
95+
}
96+
9097
private static void verifyTransformation() throws IOException {
9198
List<String> rewriteArgs = new ArrayList<>();
9299
addAll(rewriteArgs, "./gradlew", "rewriteRun", "--init-script", "init.gradle",

test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/MavenProjectTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.apache.commons.io.FileUtils;
3131
import org.junit.jupiter.api.BeforeAll;
3232
import org.junit.jupiter.api.Test;
33+
import org.junit.jupiter.api.condition.EnabledIf;
34+
import software.amazon.awssdk.testutils.SdkVersionUtils;
3335
import software.amazon.awssdk.utils.Logger;
3436

3537
public class MavenProjectTest {
@@ -66,6 +68,7 @@ private static void deleteTempDirectories() throws IOException {
6668
}
6769

6870
@Test
71+
@EnabledIf("versionAvailable")
6972
void mavenProject_shouldConvert() throws IOException {
7073
verifyTransformation();
7174
verifyCompilation();
@@ -87,4 +90,8 @@ private static void verifyCompilation() {
8790
addAll(packageArgs, "mvn", "package");
8891
run(mavenActual, packageArgs.toArray(new String[0]));
8992
}
93+
94+
boolean versionAvailable() {
95+
return TestUtils.versionAvailable(sdkVersion);
96+
}
9097
}

test/v2-migration-tests/src/test/java/software/amazon/awssdk/v2migrationtests/TestUtils.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
import java.io.IOException;
2121
import java.io.UncheckedIOException;
22+
import java.net.HttpURLConnection;
23+
import java.net.Proxy;
24+
import java.net.URI;
2225
import java.nio.charset.StandardCharsets;
2326
import java.nio.file.Files;
2427
import java.nio.file.Path;
@@ -29,6 +32,7 @@
2932
import java.util.stream.Collectors;
3033
import java.util.stream.Stream;
3134
import org.opentest4j.AssertionFailedError;
35+
import software.amazon.awssdk.testutils.SdkVersionUtils;
3236
import software.amazon.awssdk.utils.IoUtils;
3337
import software.amazon.awssdk.utils.Logger;
3438
import software.amazon.awssdk.utils.StringUtils;
@@ -41,22 +45,25 @@ public static void assertTwoDirectoriesHaveSameStructure(Path a, Path b) {
4145
assertLeftHasRight(b, a);
4246
}
4347

48+
public static boolean versionAvailable(String sdkVersion) {
49+
try {
50+
return SdkVersionUtils.checkVersionAvailability(sdkVersion,
51+
"apache-client",
52+
"netty-nio-client",
53+
"dynamodb",
54+
"sqs",
55+
"s3",
56+
"http-auth-aws-eventstream",
57+
"utils");
58+
} catch (Exception exception) {
59+
log.warn(() -> "Exception occurred, ignoring", exception);
60+
return false;
61+
}
62+
}
63+
4464
public static String getVersion() throws IOException {
45-
// TODO: uncomment the following code to dynamically get version
46-
// once we update the version
47-
// Path root = Paths.get(".").normalize().toAbsolutePath();
48-
// Path pomFile = root.resolve("pom.xml");
49-
// Optional<String> versionString =
50-
// Files.readAllLines(pomFile)
51-
// .stream().filter(l -> l.contains("<version>")).findFirst();
52-
//
53-
// if (!versionString.isPresent()) {
54-
// throw new AssertionError("No version is found");
55-
// }
56-
//
57-
// String string = versionString.get().trim();
58-
// String substring = string.substring(9, string.indexOf('/') - 1);
59-
return "2.27.0";
65+
Path root = Paths.get(".").toAbsolutePath().getParent().getParent().getParent();
66+
return SdkVersionUtils.getSdkPreviousReleaseVersion(root.resolve("pom.xml"));
6067
}
6168

6269
public static String getMigrationToolVersion() throws IOException {

v2-migration/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,12 @@
231231
</exclusion>
232232
</exclusions>
233233
</dependency>
234+
<dependency>
235+
<groupId>software.amazon.awssdk</groupId>
236+
<artifactId>test-utils</artifactId>
237+
<version>${awsjavasdk.version}</version>
238+
<scope>test</scope>
239+
</dependency>
234240
</dependencies>
235241

236242
<build>

0 commit comments

Comments
 (0)