Skip to content

Commit

Permalink
improved: also test the given GoCD-credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfhergert committed Dec 11, 2017
1 parent 41acb59 commit 6e5ad02
Showing 1 changed file with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.haufelexware.gocd.plugin.octane.settings.OctaneGoCDPluginSettings;
import com.haufelexware.gocd.plugin.octane.settings.OctaneGoCDPluginSettingsWrapper;
import com.haufelexware.gocd.plugin.octane.settings.SettingsValidator;
import com.haufelexware.gocd.service.GoGetPipelineGroupsAsTest;
import com.haufelexware.util.MapBuilder;
import com.haufelexware.util.Streams;
import com.haufelexware.gocd.plugin.octane.validation.ValidationIssue;
Expand All @@ -25,6 +26,7 @@
import com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse;
import com.thoughtworks.go.plugin.api.response.GoApiResponse;
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
import org.apache.http.HttpResponse;

import java.io.IOException;
import java.net.InetAddress;
Expand Down Expand Up @@ -139,9 +141,11 @@ public GoPluginApiResponse handle(GoPluginApiRequest request) throws UnhandledRe
final OctaneGoCDPluginSettings settings = wrapper.getPluginSettings();
final List<ValidationIssue> issues = new SettingsValidator().validate(settings);
if (issues.isEmpty()) { // test the connection if no validation issues have been found so far.
((GoPluginServices)OctaneSDK.getInstance().getPluginServices()).setSettings(settings);
final GoPluginServices pluginServices = (GoPluginServices)OctaneSDK.getInstance().getPluginServices();
pluginServices.setSettings(settings);
try {
final OctaneConfiguration config = OctaneSDK.getInstance().getPluginServices().getOctaneConfiguration();
// the the connection towards Octane.
final OctaneConfiguration config = pluginServices.getOctaneConfiguration();
OctaneResponse response = OctaneSDK.getInstance().getConfigurationService().validateConfiguration(config);
if (response.getStatus() == 401) { // authentication failed
issues.add(new ValidationIssue("clientID", "Could not authenticate with Octane. Response: " + response.getStatus() + " " + response.getBody()));
Expand All @@ -154,6 +158,11 @@ public GoPluginApiResponse handle(GoPluginApiRequest request) throws UnhandledRe
} catch (IllegalArgumentException|IOException e) {
issues.add(new ValidationIssue("serverURL", "Could not connect to Octane. Exception thrown: " + e));
}
// test the connection towards GoCD.
HttpResponse httpResponse = new GoGetPipelineGroupsAsTest(pluginServices.createGoApiClient()).getHttpResponse();
if (httpResponse.getStatusLine().getStatusCode() != 200) {
issues.add(new ValidationIssue("goUsername", "Could not authenticate with GoCD. Response: " + httpResponse.getStatusLine().getStatusCode() + " " + httpResponse.getStatusLine().getReasonPhrase()));
}
}
return new DefaultGoPluginApiResponse(200, new Gson().toJson(issues));
} else if ("notifications-interested-in".equals(request.requestName())) {
Expand Down

0 comments on commit 6e5ad02

Please sign in to comment.