Skip to content

Commit 41edc6f

Browse files
feat: Added DependencyVersionHelper for finding the latest release version for a given dependency (#780)
1 parent 5f41c2a commit 41edc6f

File tree

2 files changed

+122
-6
lines changed

2 files changed

+122
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2021 - 2022 the original author or authors.
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+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.sbm.helpers;
17+
18+
import net.minidev.json.JSONArray;
19+
import net.minidev.json.JSONObject;
20+
import net.minidev.json.parser.JSONParser;
21+
import net.minidev.json.parser.ParseException;
22+
23+
import java.io.IOException;
24+
import java.net.URI;
25+
import java.net.URISyntaxException;
26+
import java.net.URLEncoder;
27+
import java.net.http.HttpClient;
28+
import java.net.http.HttpRequest;
29+
import java.net.http.HttpResponse;
30+
import java.nio.charset.StandardCharsets;
31+
import java.text.MessageFormat;
32+
import java.util.Optional;
33+
34+
35+
/**
36+
* Test helper that provides various information about the dependencies.
37+
*
38+
* @author Szymon Sadowski
39+
*/
40+
public class DependencyVersionHelper {
41+
public static final String MAVEN_DEPENDENCY_SEARCH_URL_TEMPLATE =
42+
"https://search.maven.org/solrsearch/select?q={0}&rows=1&wt=json";
43+
public static final String MAVEN_DEPENDENCY_QUERY_TEMPLATE = "g:\"{0}\" AND a:\"{1}\"";
44+
public static final String RESPONSE_JSON_KEY = "response";
45+
public static final String DOCS_JSON_KEY = "docs";
46+
public static final String LATEST_VERSION_JSON_KEY = "latestVersion";
47+
48+
/**
49+
* Finds the latest release version for a given dependency.
50+
* @param groupId GroupId of a sought dependency.
51+
* @param artifactId ArtifactId of a sought dependency.
52+
* @return Optional string with the version number of the latest release for a given dependency
53+
* if the search was successful. Empty optional if the search was not successful.
54+
*/
55+
public static Optional<String> getLatestReleaseVersion(String groupId, String artifactId) {
56+
String url = MessageFormat.format(
57+
MAVEN_DEPENDENCY_SEARCH_URL_TEMPLATE,
58+
URLEncoder.encode(
59+
MessageFormat.format(MAVEN_DEPENDENCY_QUERY_TEMPLATE, groupId, artifactId),
60+
StandardCharsets.UTF_8
61+
)
62+
);
63+
64+
final HttpResponse<String> response;
65+
66+
try {
67+
HttpClient client = HttpClient.newHttpClient();
68+
69+
HttpRequest request2 = HttpRequest.newBuilder()
70+
.uri(new URI(url))
71+
.GET()
72+
.build();
73+
74+
response = client.send(request2, HttpResponse.BodyHandlers.ofString());
75+
} catch (URISyntaxException | IOException | InterruptedException e) {
76+
return Optional.empty();
77+
}
78+
79+
80+
JSONObject json;
81+
82+
try {
83+
json = new JSONParser(JSONParser.MODE_PERMISSIVE).parse(response.body(), JSONObject.class);
84+
} catch(ParseException jsonException) {
85+
return Optional.empty();
86+
}
87+
88+
89+
if (!json.containsKey(RESPONSE_JSON_KEY)) {
90+
return Optional.empty();
91+
}
92+
93+
JSONObject responseSection = (JSONObject) json.get(RESPONSE_JSON_KEY);
94+
95+
if(!responseSection.containsKey(DOCS_JSON_KEY)) {
96+
return Optional.empty();
97+
}
98+
99+
JSONArray docs = (JSONArray) responseSection.get(DOCS_JSON_KEY);
100+
101+
if (docs.size() == 0) {
102+
return Optional.empty();
103+
}
104+
105+
JSONObject docEntry = (JSONObject) docs.get(0);
106+
107+
if(!docEntry.containsKey(LATEST_VERSION_JSON_KEY)) {
108+
return Optional.empty();
109+
}
110+
111+
return Optional.of(docEntry.getAsString(LATEST_VERSION_JSON_KEY));
112+
}
113+
}

components/sbm-openrewrite/src/test/java/org/springframework/sbm/support/openrewrite/api/UpgradeDependencyVersionTest.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import org.openrewrite.maven.MavenParser;
2424
import org.openrewrite.maven.UpgradeDependencyVersion;
2525
import org.openrewrite.xml.tree.Xml;
26+
import org.springframework.sbm.helpers.DependencyVersionHelper;
2627

2728
import java.util.List;
29+
import java.util.Optional;
2830
import java.util.concurrent.atomic.AtomicBoolean;
2931

3032
import static org.assertj.core.api.Assertions.assertThat;
@@ -157,8 +159,11 @@ void testUpgradeDependency_trustParent() {
157159

158160
@Test
159161
void testUpgradeDependency_latestReleaseVersion() {
162+
String groupId = "org.springframework.boot";
163+
String artifactId = "spring-boot-starter-test";
160164

161-
String springBootVersion = getLatestBootReleaseVersion();
165+
Optional<String> optionalSpringBootVersion = getLatestBootReleaseVersion(groupId, artifactId);
166+
assertThat(optionalSpringBootVersion).isPresent();
162167

163168
@Language("xml")
164169
String expectedPomXml =
@@ -189,10 +194,8 @@ void testUpgradeDependency_latestReleaseVersion() {
189194
</dependency>
190195
</dependencies>
191196
</project>
192-
""".formatted(springBootVersion);
197+
""".formatted(optionalSpringBootVersion.get());
193198

194-
String groupId = "org.springframework.boot";
195-
String artifactId = "spring-boot-starter-test";
196199
String version = "latest.release";
197200
UpgradeDependencyVersion sut = new UpgradeDependencyVersion(groupId, artifactId, version, null, false, List.of());
198201

@@ -201,8 +204,8 @@ void testUpgradeDependency_latestReleaseVersion() {
201204
assertThat(results.getResults().get(0).getAfter().printAll()).isEqualTo(expectedPomXml);
202205
}
203206

204-
private String getLatestBootReleaseVersion() {
205-
return "3.0.5";
207+
private Optional<String> getLatestBootReleaseVersion(String groupId, String artifactId) {
208+
return DependencyVersionHelper.getLatestReleaseVersion(groupId, artifactId);
206209
}
207210

208211
@Test

0 commit comments

Comments
 (0)