|
5 | 5 |
|
6 | 6 | package com.magento.idea.magento2uct.packages;
|
7 | 7 |
|
| 8 | +import java.io.BufferedReader; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStreamReader; |
| 11 | +import java.net.HttpURLConnection; |
| 12 | +import java.net.URL; |
8 | 13 | import java.util.ArrayList;
|
9 |
| -import java.util.LinkedList; |
10 | 14 | import java.util.List;
|
11 | 15 | import org.jetbrains.annotations.NotNull;
|
12 | 16 | import org.jetbrains.annotations.Nullable;
|
| 17 | +import org.json.JSONArray; |
| 18 | +import org.json.JSONObject; |
13 | 19 |
|
14 | 20 | public enum SupportedVersion {
|
15 |
| - |
16 |
| - V230("2.3.0"), |
17 |
| - V231("2.3.1"), |
18 |
| - V232("2.3.2"), |
19 |
| - V2322("2.3.2-p2"), |
20 |
| - V233("2.3.3"), |
21 |
| - V2331("2.3.3-p1"), |
22 |
| - V234("2.3.4"), |
23 |
| - V2341("2.3.4-p1"), |
24 |
| - V2342("2.3.4-p2"), |
25 |
| - V235("2.3.5"), |
26 |
| - V2351("2.3.5-p1"), |
27 |
| - V2352("2.3.5-p2"), |
28 |
| - V236("2.3.6"), |
29 |
| - V2361("2.3.6-p1"), |
30 |
| - V237("2.3.7"), |
31 |
| - V2371("2.3.7-p1"), |
32 |
| - V240("2.4.0"), |
33 |
| - V2401("2.4.0-p1"), |
34 |
| - V241("2.4.1"), |
35 |
| - V2411("2.4.1-p1"), |
36 |
| - V242("2.4.2"), |
37 |
| - V2421("2.4.2-p1"), |
38 |
| - V2422("2.4.2-p2"), |
39 |
| - V243("2.4.3"), |
40 |
| - V2431("2.4.3-p1"), |
41 |
| - V2444("2.4.4-beta4"); |
42 |
| - |
| 21 | + ; |
43 | 22 | private final String version;
|
| 23 | + private static final Integer SUCCESS_CODE = 200; |
44 | 24 |
|
45 | 25 | SupportedVersion(final String version) {
|
46 | 26 | this.version = version;
|
@@ -77,10 +57,75 @@ public String getVersion() {
|
77 | 57 | * @return List[String]
|
78 | 58 | */
|
79 | 59 | public static List<String> getSupportedVersions() {
|
80 |
| - final List<String> versions = new LinkedList<>(); |
| 60 | + try { |
| 61 | + return fetchSupportedVersions(); |
| 62 | + } catch (Exception e) { //NOPMD - suppressed AvoidCatchingGenericException |
| 63 | + // Return an empty list or log the exception |
| 64 | + return List.of(); |
| 65 | + } |
| 66 | + } |
81 | 67 |
|
82 |
| - for (final SupportedVersion version : SupportedVersion.values()) { |
83 |
| - versions.add(version.getVersion()); |
| 68 | + /** |
| 69 | + * Fetch supported versions dynamically from Packagist |
| 70 | + * This method performs an HTTP GET request to fetch version data in JSON format |
| 71 | + * from a predefined URL and parses it into a list of version strings. |
| 72 | + * |
| 73 | + * @return List[String] containing supported version strings |
| 74 | + * @throws IOException if an error occurs during HTTP connection or JSON parsing |
| 75 | + */ |
| 76 | + public static List<String> fetchSupportedVersions() throws IOException { |
| 77 | + final String url = "https://repo.packagist.org/p2/magento/community-edition.json"; |
| 78 | + final List<String> versions = new ArrayList<>(); |
| 79 | + |
| 80 | + HttpURLConnection connection = null; |
| 81 | + try { |
| 82 | + // Establish HTTP connection |
| 83 | + connection = (HttpURLConnection) new URL(url).openConnection(); |
| 84 | + connection.setRequestMethod("GET"); |
| 85 | + connection.setRequestProperty("Accept", "application/json"); |
| 86 | + |
| 87 | + if (connection.getResponseCode() != SUCCESS_CODE) { |
| 88 | + throw new IOException(//NOPMD - suppressed AvoidThrowingRawExceptionTypes |
| 89 | + "Failed to fetch data, HTTP response code: " + connection.getResponseCode() |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + // Read JSON response |
| 94 | + try (BufferedReader reader = new BufferedReader( |
| 95 | + new InputStreamReader(connection.getInputStream())) |
| 96 | + ) { |
| 97 | + final StringBuilder response = new StringBuilder(); |
| 98 | + String line; |
| 99 | + while (true) { |
| 100 | + line = reader.readLine(); |
| 101 | + if (line == null) { |
| 102 | + break; |
| 103 | + } |
| 104 | + response.append(line); |
| 105 | + } |
| 106 | + |
| 107 | + // Parse JSON for version data |
| 108 | + final JSONObject jsonResponse = new JSONObject(response.toString()); |
| 109 | + final JSONArray packageObject = jsonResponse |
| 110 | + .getJSONObject("packages") |
| 111 | + .getJSONArray("magento/community-edition"); |
| 112 | + |
| 113 | + for (final Object o : packageObject) { |
| 114 | + final JSONObject version = (JSONObject) o; |
| 115 | + if (version == null) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + final String versionstring = version.getString("version"); |
| 119 | + if (versionstring == null) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + versions.add(versionstring); |
| 123 | + } |
| 124 | + } |
| 125 | + } finally { |
| 126 | + if (connection != null) { |
| 127 | + connection.disconnect(); |
| 128 | + } |
84 | 129 | }
|
85 | 130 |
|
86 | 131 | return versions;
|
|
0 commit comments