Skip to content

Commit f1e66e6

Browse files
author
Alex Johnson
committed
BUGFIX - Change SupportedVersion from enum to class to resolve broken SupportedVersion.values() calls in UCT
1 parent 99322f2 commit f1e66e6

File tree

13 files changed

+87
-91
lines changed

13 files changed

+87
-91
lines changed

src/main/java/com/magento/idea/magento2uct/execution/configurations/UctSettingsEditor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ private String getStoredUctExecutablePath(
320320
private void initializeComboboxSources() {
321321
comingVersion.setToolTipText("Choose a target version");
322322

323-
for (final String version : SupportedVersion.getSupportedVersions()) {
324-
comingVersion.addItem(new ComboBoxItemData(version, version));
323+
for (final SupportedVersion version : SupportedVersion.getSupportedVersions()) {
324+
comingVersion.addItem(new ComboBoxItemData(version.getVersion(), version.getVersion()));
325325
}
326326

327327
for (final IssueSeverityLevel level : IssueSeverityLevel.getSeverityLabels()) {

src/main/java/com/magento/idea/magento2uct/packages/IndexRegistry.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ public enum IndexRegistry {
2222
DEPRECATION(
2323
DeprecationStateIndex.class,
2424
new DeprecationIndexProcessor(),
25-
SupportedVersion.getSupportedVersions().toArray(new String[0])
25+
SupportedVersion.getSupportedVersionStrings()
2626
),
2727
EXISTENCE(
2828
ExistenceStateIndex.class,
2929
new ExistenceIndexProcessor(),
30-
SupportedVersion.getSupportedVersions().toArray(new String[0])
30+
SupportedVersion.getSupportedVersionStrings()
3131
),
3232
API_COVERAGE(
3333
ApiCoverageStateIndex.class,
3434
new ApiCoverageIndexProcessor(),
35-
SupportedVersion.getSupportedVersions().toArray(new String[0])
35+
SupportedVersion.getSupportedVersionStrings()
3636
);
3737

3838
private final String key;

src/main/java/com/magento/idea/magento2uct/packages/SupportedVersion.java

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,79 @@
55

66
package com.magento.idea.magento2uct.packages;
77

8+
import org.jetbrains.annotations.NotNull;
9+
import org.jetbrains.annotations.Nullable;
10+
import org.json.JSONArray;
11+
import org.json.JSONObject;
812
import java.io.BufferedReader;
913
import java.io.IOException;
1014
import java.io.InputStreamReader;
1115
import java.net.HttpURLConnection;
1216
import java.net.URL;
1317
import java.util.ArrayList;
1418
import java.util.List;
15-
import org.jetbrains.annotations.NotNull;
16-
import org.jetbrains.annotations.Nullable;
17-
import org.json.JSONArray;
18-
import org.json.JSONObject;
19+
import java.util.concurrent.atomic.AtomicReference;
20+
21+
public final class SupportedVersion {
1922

20-
public enum SupportedVersion {
21-
;
2223
private final String version;
23-
private static final Integer SUCCESS_CODE = 200;
2424

25-
SupportedVersion(final String version) {
25+
private SupportedVersion(final String version) {
2626
this.version = version;
2727
}
2828

29-
/**
30-
* Get version.
31-
*
32-
* @return String
33-
*/
34-
public String getVersion() {
29+
public @NotNull String getVersion() {
3530
return version;
3631
}
3732

38-
/**
39-
* Get version ENUM by version code.
40-
*
41-
* @param versionCandidate String
42-
*
43-
* @return SupportedVersion
44-
*/
45-
public @Nullable static SupportedVersion getVersion(final @NotNull String versionCandidate) {
46-
for (final SupportedVersion version : SupportedVersion.values()) {
33+
private static final AtomicReference<List<SupportedVersion>> cachedVersions = new AtomicReference<>();
34+
35+
public static @NotNull List<SupportedVersion> getSupportedVersions() {
36+
List<SupportedVersion> versions = cachedVersions.get();
37+
38+
if (versions == null) {
39+
versions = new ArrayList<>();
40+
41+
try {
42+
for (final String versionStr : fetchRemoteSupportedVersions()) {
43+
versions.add(new SupportedVersion(versionStr));
44+
}
45+
} catch (Exception ignored) {
46+
}
47+
48+
cachedVersions.set(versions);
49+
}
50+
51+
return versions;
52+
}
53+
54+
public static String[] getSupportedVersionStrings() {
55+
return getSupportedVersions()
56+
.stream()
57+
.map(SupportedVersion::toString)
58+
.toArray(String[]::new);
59+
}
60+
61+
public static @Nullable SupportedVersion getVersion(final @NotNull String versionCandidate) {
62+
for (final SupportedVersion version : getSupportedVersions()) {
4763
if (version.getVersion().equals(versionCandidate)) {
4864
return version;
4965
}
5066
}
67+
5168
return null;
5269
}
5370

54-
/**
55-
* Get supported versions.
56-
*
57-
* @return List[String]
58-
*/
59-
public static List<String> getSupportedVersions() {
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();
71+
public static List<SupportedVersion> getPriorVersions(final SupportedVersion version) {
72+
final List<SupportedVersion> previousVersions = new ArrayList<>();
73+
74+
for (final SupportedVersion supportedVersion : getSupportedVersions()) {
75+
if (supportedVersion.getVersion().compareTo(version.toString()) < 0) {
76+
previousVersions.add(supportedVersion);
77+
}
6578
}
79+
80+
return previousVersions;
6681
}
6782

6883
/**
@@ -71,22 +86,22 @@ public static List<String> getSupportedVersions() {
7186
* from a predefined URL and parses it into a list of version strings.
7287
*
7388
* @return List[String] containing supported version strings
74-
* @throws IOException if an error occurs during HTTP connection or JSON parsing
7589
*/
76-
public static List<String> fetchSupportedVersions() throws IOException {
90+
private static List<String> fetchRemoteSupportedVersions() {
7791
final String url = "https://repo.packagist.org/p2/magento/community-edition.json";
7892
final List<String> versions = new ArrayList<>();
7993

8094
HttpURLConnection connection = null;
95+
8196
try {
8297
// Establish HTTP connection
8398
connection = (HttpURLConnection) new URL(url).openConnection();
8499
connection.setRequestMethod("GET");
85100
connection.setRequestProperty("Accept", "application/json");
86101

87-
if (connection.getResponseCode() != SUCCESS_CODE) {
102+
if (connection.getResponseCode() != 200) {
88103
throw new IOException(//NOPMD - suppressed AvoidThrowingRawExceptionTypes
89-
"Failed to fetch data, HTTP response code: " + connection.getResponseCode()
104+
"Failed to fetch data, HTTP response code: " + connection.getResponseCode()
90105
);
91106
}
92107

@@ -122,6 +137,7 @@ public static List<String> fetchSupportedVersions() throws IOException {
122137
versions.add(versionstring);
123138
}
124139
}
140+
} catch (IOException ignored) {
125141
} finally {
126142
if (connection != null) {
127143
connection.disconnect();
@@ -131,22 +147,4 @@ public static List<String> fetchSupportedVersions() throws IOException {
131147
return versions;
132148
}
133149

134-
/**
135-
* Get previous versions.
136-
*
137-
* @param version SupportedVersion
138-
*
139-
* @return List[SupportedVersion]
140-
*/
141-
public static List<SupportedVersion> getPriorVersions(final SupportedVersion version) {
142-
final List<SupportedVersion> previousVersions = new ArrayList<>();
143-
144-
for (final SupportedVersion supportedVersion : SupportedVersion.values()) {
145-
if (supportedVersion.compareTo(version) < 0) {
146-
previousVersions.add(supportedVersion);
147-
}
148-
}
149-
150-
return previousVersions;
151-
}
152-
}
150+
}

src/main/java/com/magento/idea/magento2uct/settings/UctSettingsService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.magento.idea.magento2uct.packages.SupportedVersion;
1616
import org.jetbrains.annotations.NotNull;
1717
import org.jetbrains.annotations.Nullable;
18+
import java.util.List;
1819

1920
@State(name = "Magento2UctSettings", storages = @Storage(UctSettingsService.M2_UCT_SETTINGS_XML))
2021
public class UctSettingsService implements PersistentStateComponent<UctSettingsService> {
@@ -164,6 +165,7 @@ public void setCurrentVersion(final @Nullable SupportedVersion version) {
164165
if (currentVersion == null) {
165166
return null;
166167
}
168+
167169
return SupportedVersion.getVersion(currentVersion);
168170
}
169171

@@ -174,8 +176,9 @@ public void setCurrentVersion(final @Nullable SupportedVersion version) {
174176
*/
175177
public @NotNull SupportedVersion getCurrentVersionOrDefault() {
176178
final SupportedVersion currentVersion = getCurrentVersion();
179+
final @NotNull List<SupportedVersion> supportedVersions = SupportedVersion.getSupportedVersions();
177180

178-
return currentVersion == null ? SupportedVersion.valueOf("2.3.0") : currentVersion;
181+
return currentVersion == null ? supportedVersions.get(0) : currentVersion;
179182
}
180183

181184
/**
@@ -196,6 +199,7 @@ public void setTargetVersion(final @NotNull SupportedVersion version) {
196199
if (targetVersion == null) {
197200
return null;
198201
}
202+
199203
return SupportedVersion.getVersion(targetVersion);
200204
}
201205

src/main/java/com/magento/idea/magento2uct/ui/ConfigurationDialog.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,14 @@ private void setSelectedValueByItsKey(
267267
private void createUIComponents() {
268268
targetVersion = new ComboBox<>();
269269

270-
for (final String version : SupportedVersion.getSupportedVersions()) {
271-
targetVersion.addItem(new ComboBoxItemData(version, version));
270+
for (final SupportedVersion version : SupportedVersion.getSupportedVersions()) {
271+
targetVersion.addItem(new ComboBoxItemData(version.getVersion(), version.getVersion()));
272272
}
273273
currentVersion = new ComboBox<>();
274274
currentVersion.addItem(new ComboBoxItemData("", "Less than 2.3.0"));
275275

276-
for (final String version : SupportedVersion.getSupportedVersions()) {
277-
currentVersion.addItem(new ComboBoxItemData(version, version));
276+
for (final SupportedVersion version : SupportedVersion.getSupportedVersions()) {
277+
currentVersion.addItem(new ComboBoxItemData(version.getVersion(), version.getVersion()));
278278
}
279279
issueSeverityLevel = new ComboBox<>();
280280

src/main/java/com/magento/idea/magento2uct/ui/ReindexDialog.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ protected void onWriteActionOK() {
132132
private void createUIComponents() {
133133
targetVersion = new ComboBox<>();
134134

135-
for (final String version : SupportedVersion.getSupportedVersions()) {
136-
targetVersion.addItem(new ComboBoxItemData(version, version));
135+
for (final SupportedVersion version : SupportedVersion.getSupportedVersions()) {
136+
targetVersion.addItem(new ComboBoxItemData(version.getVersion(), version.getVersion()));
137137
}
138138
targetIndex = new ComboBox<>();
139139
targetIndex.addItem(new ComboBoxItemData("", " --- Choose Target Index --- "));

src/main/java/com/magento/idea/magento2uct/versioning/VersionStateManager.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,22 +148,16 @@ private VersionStateManager(final @NotNull Project project) {
148148
@SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel")
149149
private synchronized void correctSettings(final @NotNull Project project) {
150150
final UctSettingsService settingsService = UctSettingsService.getInstance(project);
151-
final List<String> allVersions = SupportedVersion.getSupportedVersions();
151+
final List<SupportedVersion> allVersions = SupportedVersion.getSupportedVersions();
152152

153-
if (currentVersion == null
154-
|| SupportedVersion.getVersion(currentVersion.getVersion()) == null) {
155-
final SupportedVersion correctCurrentVersion = SupportedVersion.getVersion(
156-
allVersions.get(0)
157-
);
153+
if (currentVersion == null) {
154+
final SupportedVersion correctCurrentVersion = allVersions.get(0);
158155
settingsService.setCurrentVersion(correctCurrentVersion);
159156
currentVersion = correctCurrentVersion;
160157
}
161158

162-
if (targetVersion == null
163-
|| SupportedVersion.getVersion(targetVersion.getVersion()) == null) {
164-
final SupportedVersion correctTargetVersion = SupportedVersion.getVersion(
165-
allVersions.get(allVersions.size() - 1)
166-
);
159+
if (targetVersion == null) {
160+
final SupportedVersion correctTargetVersion = allVersions.get(allVersions.size() - 1);
167161
settingsService.setTargetVersion(correctTargetVersion);
168162
targetVersion = correctTargetVersion;
169163
}
@@ -201,11 +195,11 @@ private void compute(final VersionStateIndex index) {
201195
}
202196

203197
if (versionsToLoad.isEmpty()) {
204-
for (final SupportedVersion version : SupportedVersion.values()) {
205-
if (version.compareTo(targetVersion) <= 0) {
198+
for (final SupportedVersion version : SupportedVersion.getSupportedVersions()) {
199+
if (version.getVersion().compareTo(targetVersion.getVersion()) <= 0) {
206200
if (isSetIgnoreFlag != null && isSetIgnoreFlag) {
207201
// If current version is NULL, it is less than minimum supported version.
208-
if (currentVersion == null || version.compareTo(currentVersion) > 0) {
202+
if (currentVersion == null || version.getVersion().compareTo(currentVersion.getVersion()) > 0) {
209203
versionsToLoad.add(version);
210204
}
211205
} else {

src/main/java/com/magento/idea/magento2uct/versioning/indexes/data/ApiCoverageStateIndex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ private void groupLoadedData() {
168168
VersioningDataOperationsUtil.unionVersionDataWithChangelog(
169169
versioningData,
170170
new ArrayList<>(Collections.singletonList(
171-
SupportedVersion.valueOf("2.3.0").getVersion()
171+
SupportedVersion.getSupportedVersions().get(0).getVersion()
172172
)),
173173
true
174174
);

src/main/java/com/magento/idea/magento2uct/versioning/indexes/data/DeprecationStateIndex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private void groupLoadedData() {
151151
VersioningDataOperationsUtil.unionVersionDataWithChangelog(
152152
versioningData,
153153
new ArrayList<>(Collections.singletonList(
154-
SupportedVersion.valueOf("2.3.0").getVersion()
154+
SupportedVersion.getSupportedVersions().get(0).getVersion()
155155
)),
156156
true
157157
);

src/main/java/com/magento/idea/magento2uct/versioning/indexes/data/ExistenceStateIndex.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private void groupLoadedData() {
196196
VersioningDataOperationsUtil.unionVersionDataWithChangelog(
197197
versioningData,
198198
new ArrayList<>(Collections.singletonList(
199-
SupportedVersion.valueOf("2.3.0").getVersion()
199+
SupportedVersion.getSupportedVersions().get(0).getVersion()
200200
)),
201201
false
202202
);

0 commit comments

Comments
 (0)