IDEA-157507 Use "deploymentType" field in serverInfo to detect instances of JIRA in Cloud

It seems that at the moments most of existing JIRA Cloud servers have
this parameter, but just in case I left the old heuristics as a
fallback. Also whether the server is an instance of Cloud is shown in
the editor settings next to the version to ease debugging.
This commit is contained in:
Mikhail Golubev
2016-08-26 14:24:59 +03:00
parent 35e68aab1f
commit 84e58932ff
2 changed files with 24 additions and 8 deletions
@@ -16,6 +16,7 @@
package com.intellij.tasks.jira;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Comparing;
@@ -206,12 +207,10 @@ public class JiraRepository extends BaseRepositoryImpl {
// when JIRA 4.x support will be dropped 'versionNumber' array in response
// may be used instead version string parsing
myJiraVersion = serverInfo.get("version").getAsString();
final boolean hostedInCloud = hostEndsWith(serverInfo.get("baseUrl").getAsString(), "atlassian.net");
// Legacy JIRA onDemand versions contained "OD" abbreviation
myInCloud = StringUtil.notNullize(myJiraVersion).contains("OD") || hostedInCloud;
LOG.info("JIRA version (from serverInfo): " + myJiraVersion + (myInCloud ? " (Cloud)" : ""));
myInCloud = isHostedInCloud(serverInfo);
LOG.info("JIRA version (from serverInfo): " + getPresentableVersion());
if (isInCloud()) {
LOG.info("Connecting to JIRA on-Demand. Cookie authentication is enabled unless 'tasks.jira.basic.auth.only' VM flag is used.");
LOG.info("Connecting to JIRA Cloud. Cookie authentication is enabled unless 'tasks.jira.basic.auth.only' VM flag is used.");
}
JiraRestApi restApi = JiraRestApi.fromJiraVersion(myJiraVersion, this);
if (restApi == null) {
@@ -220,6 +219,20 @@ public class JiraRepository extends BaseRepositoryImpl {
return restApi;
}
private static boolean isHostedInCloud(@NotNull JsonObject serverInfo) {
final JsonElement deploymentType = serverInfo.get("deploymentType");
if (deploymentType != null) {
return deploymentType.getAsString().equals("Cloud");
}
// Legacy heuristics
final boolean atlassianSubDomain = hostEndsWith(serverInfo.get("baseUrl").getAsString(), ".atlassian.net");
if (atlassianSubDomain) {
return true;
}
// JIRA OnDemand versions contained "OD" abbreviation
return serverInfo.get("version").getAsString().contains("OD") ;
}
private static boolean hostEndsWith(@NotNull String url, @NotNull String suffix) {
try {
final URL parsed = new URL(url);
@@ -321,6 +334,11 @@ public class JiraRepository extends BaseRepositoryImpl {
myInCloud = inCloud;
}
@NotNull
String getPresentableVersion() {
return StringUtil.notNullize(myJiraVersion, "unknown") + (myInCloud ? " (Cloud)" : "");
}
private static boolean containsCookie(@NotNull HttpClient client, @NotNull String cookieName) {
for (Cookie cookie : client.getState().getCookies()) {
if (cookie.getName().equals(cookieName) && !cookie.isExpired()) {
@@ -16,7 +16,6 @@
package com.intellij.tasks.jira;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.tasks.config.BaseRepositoryEditor;
import com.intellij.tasks.jira.jql.JqlLanguage;
import com.intellij.ui.EditorTextField;
@@ -74,8 +73,7 @@ public class JiraRepositoryEditor extends BaseRepositoryEditor<JiraRepository> {
}
private void updateNote() {
myNoteLabel.setText("JQL search cannot be used in JIRA versions prior 4.2. " +
String.format("Your version: %s.", StringUtil.notNullize(myRepository.getJiraVersion(), "unknown")));
myNoteLabel.setText("JQL search cannot be used in JIRA versions prior 4.2. Your version: " + myRepository.getPresentableVersion());
}
@Override