Skip to content

Commit e1c26f5

Browse files
feat: Added DependencyVersionHelper for finding the latest release version for a given dependency
1 parent 8c0e880 commit e1c26f5

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
import org.springframework.web.client.RestClientException;
23+
import org.springframework.web.client.RestTemplate;
24+
25+
import java.text.MessageFormat;
26+
import java.util.Optional;
27+
28+
29+
/**
30+
* Test helper that provides various information about the dependencies.
31+
*/
32+
public class DependencyVersionHelper {
33+
public static final String MAVEN_DEPENDENCY_SEARCH_URL_TEMPLATE =
34+
"https://search.maven.org/solrsearch/select?q=g:\"{0}\"+AND+a:\"{1}\"&rows=1&wt=json";
35+
public static final String RESPONSE_JSON_KEY = "response";
36+
public static final String DOCS_JSON_KEY = "docs";
37+
public static final String LATEST_VERSION_JSON_KEY = "latestVersion";
38+
39+
/**
40+
* Finds the latest release version for a given dependency.
41+
* @param groupId GroupId of a sought dependency.
42+
* @param artifactId ArtifactId of a sought dependency.
43+
* @return Optional string with the version number of the latest release for a given dependency
44+
* if the search was successful. Empty optional if the search was not successful.
45+
*/
46+
public static Optional<String> getLatestReleaseVersion(String groupId, String artifactId) {
47+
final RestTemplate restTemplate = new RestTemplate();
48+
49+
String url = MessageFormat.format(
50+
MAVEN_DEPENDENCY_SEARCH_URL_TEMPLATE,
51+
groupId,
52+
artifactId
53+
);
54+
55+
final String response;
56+
57+
try {
58+
response = restTemplate.getForObject(url, String.class);
59+
} catch(RestClientException restClientException) {
60+
return Optional.empty();
61+
}
62+
63+
64+
JSONObject json;
65+
66+
try {
67+
json = new JSONParser(JSONParser.MODE_PERMISSIVE).parse(response, JSONObject.class);
68+
} catch(ParseException jsonException) {
69+
return Optional.empty();
70+
}
71+
72+
73+
if (!json.containsKey(RESPONSE_JSON_KEY)) {
74+
return Optional.empty();
75+
}
76+
77+
JSONObject responseSection = (JSONObject) json.get(RESPONSE_JSON_KEY);
78+
79+
if(!responseSection.containsKey(DOCS_JSON_KEY)) {
80+
return Optional.empty();
81+
}
82+
83+
JSONArray docs = (JSONArray) responseSection.get(DOCS_JSON_KEY);
84+
85+
if (docs.size() == 0) {
86+
return Optional.empty();
87+
}
88+
89+
JSONObject docEntry = (JSONObject) docs.get(0);
90+
91+
if(!docEntry.containsKey(LATEST_VERSION_JSON_KEY)) {
92+
return Optional.empty();
93+
}
94+
95+
return Optional.of(docEntry.getAsString(LATEST_VERSION_JSON_KEY));
96+
}
97+
}

0 commit comments

Comments
 (0)