Skip to content

Commit 60dfb16

Browse files
Allow user to save incomplete or invalid configurations.
1 parent cd3aaa6 commit 60dfb16

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

src/main/java/com/codedx/bambooplugin/CodeDxScanTask.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.net.MalformedURLException;
22+
import java.net.URL;
2123
import java.nio.file.Path;
2224
import java.nio.file.Paths;
2325
import java.util.ArrayList;
@@ -117,7 +119,12 @@ private static Boolean loadUserPreferences(ScanTaskState state) {
117119
state.analysisName = config.get("analysisName");
118120
state.includePaths = config.get("includePaths");
119121
state.excludePaths = config.get("excludePaths");
120-
state.projectId = Integer.parseInt(config.get("selectedProjectId"));
122+
try {
123+
state.projectId = Integer.parseInt(config.get("selectedProjectId"));
124+
} catch (NumberFormatException e) {
125+
logError(state, "No project selected. Please select a Code Dx project on the task configuration page.");
126+
return false;
127+
}
121128
state.toolOutputFiles = config.get("toolOutputFiles");
122129
state.reportArchiveName = config.get("reportArchiveName");
123130
state.waitForResults = config.getAsBoolean("waitForResults");
@@ -133,16 +140,44 @@ private static Boolean loadUserPreferences(ScanTaskState state) {
133140
state.apiUrl = ServerConfigManager.getUrl();
134141
state.apiKey = ServerConfigManager.getApiKey();
135142
state.fingerprint = ServerConfigManager.getFingerprint();
143+
144+
if (state.apiUrl == null || state.apiKey == null || state.apiUrl.isEmpty() || state.apiKey.isEmpty()) {
145+
logError(state, "Code Dx url and api key are not properly configured. Please configure them on the plug-in settings page.");
146+
return false;
147+
}
136148
} else {
137149
state.apiUrl = config.get("url");
138150
state.apiKey = config.get("apiKey");
139151
state.fingerprint = config.get("fingerprint");
152+
153+
if (state.apiUrl == null || state.apiUrl.isEmpty()) {
154+
logError(state, "Missing Code Dx url. Please configure it on the task configuration page.");
155+
return false;
156+
} else {
157+
try {
158+
new URL(state.apiUrl);
159+
} catch (MalformedURLException e) {
160+
logError(state, "Malformed Code Dx url. Please correct it on the task configuration page.");
161+
return false;
162+
}
163+
}
164+
165+
if (state.apiKey == null || state.apiKey.isEmpty()) {
166+
logError(state, "Missing Code Dx api key. Please configure it on the task configuration page.");
167+
return false;
168+
}
140169
}
141170

142-
if (state.apiUrl == null || state.apiKey == null || state.apiUrl.isEmpty() || state.apiKey.isEmpty()) {
143-
logError(state, "Code Dx url and api key are not properly configured. Please configure them on the plug-in settings page.");
171+
if (state.analysisName == null || state.analysisName.isEmpty()) {
172+
logError(state, "Missing Code Dx Analysis Name. Please configure it on the task configuration page.");
144173
return false;
145174
}
175+
176+
if (state.includePaths == null || state.includePaths.isEmpty()) {
177+
logError(state, "Missing source and binary files. Please configure the field on the task configuration page.");
178+
return false;
179+
}
180+
146181
log(state, "Code Dx url set to %s", state.apiUrl);
147182

148183
return true;

src/main/java/com/codedx/bambooplugin/CodeDxScanTaskConfigurator.java

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

2020
public class CodeDxScanTaskConfigurator extends AbstractTaskConfigurator {
2121

22+
// Since much work was done surrounding saving invalid configs, lets keep the code and toggle it with a flag.
23+
private static final boolean ALLOW_SAVE_INVALID_CONFIG = true;
24+
2225
private static final Severity[] severities = Severity.all;
2326

2427
// Tracks if we are going into an edit after a failed save attempt and remembers the credentials the user put in
@@ -84,6 +87,10 @@ public void validate(final ActionParametersMap params, final ErrorCollection err
8487

8588
super.validate(params, errorCollection);
8689

90+
if (ALLOW_SAVE_INVALID_CONFIG) {
91+
return;
92+
}
93+
8794
// Will be set back to false in generateTaskConfigMap(...) if save succeeds
8895
this.failedSave = new FailedCredentials(params);
8996

0 commit comments

Comments
 (0)