Skip to content

Commit 0914107

Browse files
Fix passing variables after failed save. Fix save issue when changing creds. Improve refresh message logic. More error messages. Disable project dropdown on failed explicit refresh. Fix fields changing after failed save.
1 parent 9db1fb3 commit 0914107

2 files changed

Lines changed: 208 additions & 69 deletions

File tree

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

Lines changed: 112 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,49 @@ public class CodeDxScanTaskConfigurator extends AbstractTaskConfigurator {
2121

2222
private static final Severity[] severities = Severity.all;
2323

24+
// Tracks if we are going into an edit after a failed save attempt and remembers the credentials the user put in
25+
private FailedCredentials failedSave = null;
26+
27+
private static class FailedCredentials {
28+
private String useDefaults;
29+
private String url;
30+
private String apiKey;
31+
private String fingerprint;
32+
33+
public boolean missingSelectedProject;
34+
35+
public FailedCredentials(final ActionParametersMap params) {
36+
useDefaults = params.getString("useDefaults");
37+
url = params.getString("url");
38+
apiKey = params.getString("apiKey");
39+
fingerprint = params.getString("fingerprint");
40+
41+
missingSelectedProject = StringUtils.isEmpty(params.getString("selectedProjectId"));
42+
43+
if (url == null && Boolean.parseBoolean(useDefaults)) {
44+
url = emptyIfNull(ServerConfigManager.getUrl());
45+
apiKey = emptyIfNull(ServerConfigManager.getApiKey());
46+
fingerprint = emptyIfNull(ServerConfigManager.getFingerprint());
47+
}
48+
}
49+
50+
public void setContext(Map<String, Object> context) {
51+
context.put("useDefaults", useDefaults);
52+
context.put("url", url);
53+
context.put("apiKey", apiKey);
54+
context.put("fingerprint", fingerprint);
55+
}
56+
}
57+
2458
@java.lang.Override
2559
public Map<String, String> generateTaskConfigMap(final ActionParametersMap params, final TaskDefinition previousTaskDefinition) {
2660

2761
final Map<String, String> config = super.generateTaskConfigMap(params, previousTaskDefinition);
2862

29-
config.put("useDefaults", params.getBoolean("useDefaults") ? "true" : "false");
63+
// Save succeeded. Set this back to false.
64+
this.failedSave = null;
65+
66+
config.put("useDefaults", params.getString("useDefaults"));
3067
config.put("url", params.getString("url"));
3168
config.put("apiKey", params.getString("apiKey"));
3269
config.put("fingerprint", params.getString("fingerprint"));
@@ -36,16 +73,20 @@ public Map<String, String> generateTaskConfigMap(final ActionParametersMap param
3673
config.put("excludePaths", params.getString("excludePaths"));
3774
config.put("toolOutputFiles", params.getString("toolOutputFiles"));
3875
config.put("reportArchiveName", params.getString("reportArchiveName"));
39-
config.put("waitForResults", params.getBoolean("waitForResults") ? "true" : "false");
76+
config.put("waitForResults", params.getString("waitForResults"));
4077
config.put("selectedFailureSeverity", params.getString("selectedFailureSeverity"));
4178
config.put("onlyConsiderNewFindings", params.getString("onlyConsiderNewFindings"));
79+
4280
return config;
4381
}
4482

4583
public void validate(final ActionParametersMap params, final ErrorCollection errorCollection) {
4684

4785
super.validate(params, errorCollection);
4886

87+
// Will be set back to false in generateTaskConfigMap(...) if save succeeds
88+
this.failedSave = new FailedCredentials(params);
89+
4990
final String analysisName = params.getString("analysisName");
5091
if (StringUtils.isEmpty(analysisName)) {
5192
errorCollection.addError("analysisName", "Missing Code Dx Analysis Name.");
@@ -84,7 +125,8 @@ private static List<Project> getProjectList(Map<String, Object> context) {
84125
String apiKey = null;
85126
String fingerprint = null;
86127

87-
if ((Boolean)context.get("useDefaults")) {
128+
// We check defaultsSet in case the defaults were erased. We have what they used to be in the task configuration.
129+
if (context.get("useDefaults").equals("true") && ServerConfigManager.getDefaultsSet()) {
88130
url = ServerConfigManager.getUrl();
89131
apiKey = ServerConfigManager.getApiKey();
90132
fingerprint = ServerConfigManager.getFingerprint();
@@ -124,13 +166,14 @@ public void populateContextForCreate(final Map<String, Object> context) {
124166
super.populateContextForCreate(context);
125167

126168
boolean defaultsSet = ServerConfigManager.getDefaultsSet();
127-
context.put("defaultsSet", defaultsSet);
169+
context.put("defaultsSet", String.valueOf(defaultsSet));
170+
context.put("failedSave", String.valueOf(failedSave));
128171

129172
String defaultUrl = emptyIfNull(ServerConfigManager.getUrl());
130173
String defaultApiKey = emptyIfNull(ServerConfigManager.getApiKey());
131174
String defaultFingerprint = emptyIfNull(ServerConfigManager.getFingerprint());
132175

133-
context.put("useDefaults", defaultsSet);
176+
context.put("useDefaults", String.valueOf(defaultsSet));
134177
if (defaultsSet) {
135178
context.put("url", defaultUrl);
136179
context.put("apiKey", defaultApiKey);
@@ -169,51 +212,86 @@ public void populateContextForEdit(Map<String, Object> context, TaskDefinition t
169212

170213
super.populateContextForEdit(context, taskDefinition);
171214

215+
// Get saved user config
216+
Map<String, String> config = taskDefinition.getConfiguration();
217+
218+
// Keep track of weather or not we got here after failing to save a configuration
219+
context.put("failedSave", String.valueOf(failedSave));
220+
221+
// To populate the dropdown of severity thresholds
222+
context.put("severities", severities);
223+
224+
// Boolean to denote weather or not the user set up default Code Dx server credentials on the admin page
172225
boolean defaultsSet = ServerConfigManager.getDefaultsSet();
173-
context.put("defaultsSet", defaultsSet);
226+
context.put("defaultsSet", String.valueOf(defaultsSet));
174227

228+
// The default Code Dx server credentials - if they were set up by the user on the admin page
175229
String defaultUrl = emptyIfNull(ServerConfigManager.getUrl());
176230
String defaultApiKey = emptyIfNull(ServerConfigManager.getApiKey());
177231
String defaultFingerprint = emptyIfNull(ServerConfigManager.getFingerprint());
178-
179-
boolean useDefaults = Boolean.parseBoolean(taskDefinition.getConfiguration().get("useDefaults"));
180-
context.put("useDefaults", useDefaults);
181-
if (defaultsSet && useDefaults) {
182-
context.put("url", defaultUrl);
183-
context.put("apiKey", defaultApiKey);
184-
context.put("fingerprint", defaultFingerprint);
185-
} else {
186-
context.put("url", taskDefinition.getConfiguration().get("url"));
187-
context.put("apiKey", taskDefinition.getConfiguration().get("apiKey"));
188-
context.put("fingerprint", taskDefinition.getConfiguration().get("fingerprint"));
189-
}
190232
context.put("defaultUrl", defaultUrl);
191233
context.put("defaultApiKey", defaultApiKey);
192234
context.put("defaultFingerprint", defaultFingerprint);
193235

194-
context.put("analysisName", taskDefinition.getConfiguration().get("analysisName"));
195-
context.put("reachabilityMessage", "");
236+
// Populate context from saved task config
237+
context.put("analysisName", config.get("analysisName"));
238+
context.put("includePaths", config.get("includePaths"));
239+
context.put("excludePaths", config.get("excludePaths"));
240+
context.put("toolOutputFiles", config.get("toolOutputFiles"));
241+
context.put("reportArchiveName", config.get("reportArchiveName"));
242+
context.put("waitForResults", config.get("waitForResults"));
243+
context.put("selectedFailureSeverity", config.get("selectedFailureSeverity"));
244+
context.put("onlyConsiderNewFindings", config.get("onlyConsiderNewFindings"));
245+
246+
if (failedSave != null) {
247+
248+
failedSave.setContext(context);
249+
250+
} else {
251+
252+
// Weather or not the task is using the default Code Dx server credentials from the admin page.
253+
boolean useDefaults = Boolean.parseBoolean(config.get("useDefaults"));
196254

197-
List<Project> projects = getProjectList(context);
198-
String selectedProjectId = taskDefinition.getConfiguration().get("selectedProjectId");
199-
if (selectedProjectId != null && !selectedProjectId.isEmpty() && projects.size() == 0) {
255+
context.put("useDefaults", String.valueOf(useDefaults));
256+
257+
// We check defaultsSet in case the defaults were erased. In that case we have what they used to be in the task configuration.
258+
if (useDefaults && defaultsSet) {
259+
context.put("url", defaultUrl);
260+
context.put("apiKey", defaultApiKey);
261+
context.put("fingerprint", defaultFingerprint);
262+
} else {
263+
context.put("url", config.get("url"));
264+
context.put("apiKey", config.get("apiKey"));
265+
context.put("fingerprint", config.get("fingerprint"));
266+
}
267+
}
268+
269+
List<Project> projectList = getProjectList(context);
270+
271+
String selectedProjectId = config.get("selectedProjectId");
272+
context.put("selectedProjectId", selectedProjectId);
273+
274+
if (selectedProjectId != null && !selectedProjectId.isEmpty() && projectList.size() == 0 && (failedSave == null || !failedSave.missingSelectedProject)) {
200275
// Case where user already selected a project, but we can't communicate with CodeDx for the name. Just use a generic "name".
201276
Project p = new Project();
202277
p.setId(Integer.parseInt(selectedProjectId));
203278
p.setName("Project Id: " + selectedProjectId);
204-
projects.add(p);
279+
projectList.add(p);
280+
281+
// We get here if the previously selected project is not found
282+
if (context.get("reachabilityMessage") == null) {
283+
context.put("reachabilityMessage", "Unable to access previously selected project from Code Dx");
284+
}
205285
}
206286

207-
context.put("projectList", projects);
208-
context.put("selectedProjectId", selectedProjectId);
209-
context.put("includePaths", taskDefinition.getConfiguration().get("includePaths"));
210-
context.put("excludePaths", taskDefinition.getConfiguration().get("excludePaths"));
211-
context.put("toolOutputFiles", taskDefinition.getConfiguration().get("toolOutputFiles"));
212-
context.put("reportArchiveName", taskDefinition.getConfiguration().get("reportArchiveName"));
213-
context.put("waitForResults", taskDefinition.getConfiguration().get("waitForResults"));
214-
context.put("severities", severities);
215-
context.put("selectedFailureSeverity", taskDefinition.getConfiguration().get("selectedFailureSeverity"));
216-
context.put("onlyConsiderNewFindings", taskDefinition.getConfiguration().get("onlyConsiderNewFindings"));
287+
context.put("projectList", projectList);
288+
289+
if (context.get("reachabilityMessage") == null) {
290+
context.put("reachabilityMessage", "");
291+
}
292+
293+
// Reset this in case they cancel out of the page
294+
failedSave = null;
217295
}
218296

219297
// Helper

0 commit comments

Comments
 (0)