From 995a703a48fd744585c753e382ef71aae1433c99 Mon Sep 17 00:00:00 2001
From: Tomasz Zarna
Date: Wed, 3 Jul 2013 19:20:29 +0200
Subject: 395059: detect Gerrit server version
Bug: 395059
Change-Id: Id71ae9af7178445240d06ead81b1ead83105138f
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=395059
Signed-off-by: Tomasz Zarna
---
.../internal/gerrit/core/client/GerritClient.java | 57 ++++++++++++-
.../gerrit/core/client/GerritHttpClient.java | 26 +++---
.../gerrit/core/client/GerritSystemInfo.java | 13 ++-
.../internal/gerrit/core/client/GerritVersion.java | 56 +++++++++++++
.../eclipse/mylyn/gerrit/tests/AllGerritTests.java | 4 +-
.../gerrit/tests/core/client/GerritClientTest.java | 24 ++++--
.../tests/core/client/GerritVersionTest.java | 96 ++++++++++++++++++++++
7 files changed, 256 insertions(+), 20 deletions(-)
create mode 100644 org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritVersion.java
create mode 100644 org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/GerritVersionTest.java
diff --git a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritClient.java b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritClient.java
index 47f605955..d067f4b88 100644
--- a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritClient.java
+++ b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritClient.java
@@ -1,5 +1,5 @@
/*********************************************************************
- * Copyright (c) 2010 Sony Ericsson/ST Ericsson and others.
+ * Copyright (c) 2010, 2013 Sony Ericsson/ST Ericsson and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -24,6 +24,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpMethodBase;
@@ -52,6 +54,7 @@ import org.eclipse.mylyn.reviews.core.spi.ReviewsClient;
import org.eclipse.mylyn.reviews.core.spi.remote.emf.AbstractRemoteEmfFactoryProvider;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.osgi.util.NLS;
+import org.osgi.framework.Version;
import com.google.gerrit.common.data.AccountDashboardInfo;
import com.google.gerrit.common.data.AccountService;
@@ -91,6 +94,8 @@ import com.google.gwtjsonrpc.client.VoidResult;
*/
public class GerritClient extends ReviewsClient {
+ private static final Pattern GERRIT_VERSION_PATTERN = Pattern.compile("Powered by Gerrit Code Review (.+)
"); //$NON-NLS-1$
+
private abstract class Operation implements AsyncCallback {
private Throwable exception;
@@ -179,6 +184,8 @@ public class GerritClient extends ReviewsClient {
private AccountDiffPreference myDiffPreference;
+ private Version myVersion;
+
// private GerritConfig createDefaultConfig() {
// GerritConfig config = new GerritConfig();
// List approvals = new ArrayList();
@@ -346,6 +353,7 @@ public class GerritClient extends ReviewsClient {
}
public GerritSystemInfo getInfo(IProgressMonitor monitor) throws GerritException {
+ Version version = getCachedVersion(monitor);
List contributorAgreements = null;
Account account = null;
if (!isAnonymous()) {
@@ -366,7 +374,7 @@ public class GerritClient extends ReviewsClient {
executeQuery(monitor, "status:open"); //$NON-NLS-1$
}
refreshConfigOnce(monitor);
- return new GerritSystemInfo(contributorAgreements, account);
+ return new GerritSystemInfo(version, contributorAgreements, account);
}
public PatchScript getPatchScript(final Patch.Key key, final PatchSet.Id leftId, final PatchSet.Id rightId,
@@ -930,4 +938,49 @@ public class GerritClient extends ReviewsClient {
public AbstractRemoteEmfFactoryProvider createFactoryProvider() {
return new GerritRemoteFactoryProvider(this);
}
+
+ private Version getCachedVersion(IProgressMonitor monitor) throws GerritException {
+ synchronized (this) {
+ if (myVersion != null) {
+ return myVersion;
+ }
+ }
+ Version version = getVersion(monitor);
+
+ synchronized (this) {
+ myVersion = version;
+ }
+ return myVersion;
+ }
+
+ public Version getVersion(IProgressMonitor monitor) throws GerritException {
+ return execute(monitor, new Operation() {
+ @Override
+ public void execute(IProgressMonitor monitor) throws GerritException {
+ try {
+ Request request = new Request() {
+ @Override
+ public HttpMethodBase createMethod() throws IOException {
+ return new GetMethod(client.getUrl() + "/tools/hooks/"); //$NON-NLS-1$
+ }
+
+ @Override
+ public String process(HttpMethodBase method) throws IOException {
+ String content = method.getResponseBodyAsString();
+ Matcher matcher = GERRIT_VERSION_PATTERN.matcher(content);
+ if (matcher.find()) {
+ return matcher.group(1);
+ }
+ return null;
+ }
+ };
+ String result = client.execute(request, false, monitor);
+ Version version = GerritVersion.parseGerritVersion(result);
+ onSuccess(version);
+ } catch (Exception e) {
+ onFailure(e);
+ }
+ }
+ });
+ }
}
diff --git a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java
index 46e5708b3..7064a1d65 100644
--- a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java
+++ b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java
@@ -1,5 +1,5 @@
/*********************************************************************
- * Copyright (c) 2010 Sony Ericsson/ST Ericsson and others.
+ * Copyright (c) 2010, 2013 Sony Ericsson/ST Ericsson and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -160,21 +160,27 @@ public class GerritHttpClient {
}
public T execute(Request request, IProgressMonitor monitor) throws IOException, GerritException {
+ return execute(request, true, monitor);
+ }
+
+ public T execute(Request request, boolean authenticateIfNeeded, IProgressMonitor monitor)
+ throws IOException, GerritException {
String openIdProvider = getOpenIdProvider();
hostConfiguration = WebUtil.createHostConfiguration(httpClient, location, monitor);
for (int attempt = 0; attempt < 2; attempt++) {
- // force authentication
- if (needsAuthentication()) {
- AuthenticationCredentials credentials = location.getCredentials(AuthenticationType.REPOSITORY);
- if (openIdProvider != null || credentials != null) {
- authenticate(openIdProvider, monitor);
+ if (authenticateIfNeeded) {
+ // force authentication
+ if (needsAuthentication()) {
+ AuthenticationCredentials credentials = location.getCredentials(AuthenticationType.REPOSITORY);
+ if (openIdProvider != null || credentials != null) {
+ authenticate(openIdProvider, monitor);
+ }
+ }
+ if (!obtainedXsrfKey) {
+ updateXsrfKey(monitor);
}
- }
-
- if (!obtainedXsrfKey) {
- updateXsrfKey(monitor);
}
HttpMethodBase method = request.createMethod();
diff --git a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritSystemInfo.java b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritSystemInfo.java
index 421339733..11810ba50 100644
--- a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritSystemInfo.java
+++ b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritSystemInfo.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2010 Tasktop Technologies and others.
+ * Copyright (c) 2010, 2013 Tasktop Technologies and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -13,6 +13,8 @@ package org.eclipse.mylyn.internal.gerrit.core.client;
import java.util.List;
+import org.osgi.framework.Version;
+
import com.google.gerrit.reviewdb.Account;
import com.google.gerrit.reviewdb.ContributorAgreement;
@@ -21,15 +23,22 @@ import com.google.gerrit.reviewdb.ContributorAgreement;
*/
public class GerritSystemInfo {
+ private final Version version;
+
private final Account account;
private final List contributorAgreements;
- public GerritSystemInfo(List contributorAgreements, Account account) {
+ public GerritSystemInfo(Version version, List contributorAgreements, Account account) {
+ this.version = version;
this.contributorAgreements = contributorAgreements;
this.account = account;
}
+ public Version getVersion() {
+ return version;
+ }
+
public List getContributorAgreements() {
return contributorAgreements;
}
diff --git a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritVersion.java b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritVersion.java
new file mode 100644
index 000000000..7ddb5a375
--- /dev/null
+++ b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritVersion.java
@@ -0,0 +1,56 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Tasktop Technologies and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.internal.gerrit.core.client;
+
+import static java.lang.Integer.parseInt;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.eclipse.core.runtime.Assert;
+import org.osgi.framework.Version;
+
+public class GerritVersion extends Version {
+
+ // e.g. 2.6 or 2.6.0
+ private static final Pattern MAJOR_MINOR_MICRO_VERSION_PATTERN = Pattern.compile("\\d+\\.\\d+(\\.\\d+)?"); //$NON-NLS-1$
+
+ // e.g. 2.6-rc3
+ private static final Pattern MAJOR_MINOR_QUALIFIER_VERSION_PATTERN = Pattern.compile("(\\d+)\\.(\\d+)-([-\\w]+)"); //$NON-NLS-1$
+
+ // e.g. 2.6.1-rc1
+ private static final Pattern MAJOR_MINOR_MICRO_QUALIFIER_VERSION_PATTERN = Pattern.compile("(\\d+)\\.(\\d+)\\.(\\d+)-([-\\w]+)"); //$NON-NLS-1$
+
+ public GerritVersion(String version) {
+ super(version);
+ }
+
+ public static Version parseGerritVersion(String version) {
+ Assert.isLegal(version != null);
+ Assert.isLegal(!version.isEmpty());
+
+ Matcher matcher = MAJOR_MINOR_MICRO_VERSION_PATTERN.matcher(version);
+ if (matcher.matches()) {
+ return Version.parseVersion(version);
+ }
+ matcher = MAJOR_MINOR_QUALIFIER_VERSION_PATTERN.matcher(version);
+ if (matcher.matches()) {
+ return new Version(parseInt(matcher.group(1)), parseInt(matcher.group(2)), 0, matcher.group(3));
+ }
+ matcher = MAJOR_MINOR_MICRO_QUALIFIER_VERSION_PATTERN.matcher(version);
+ if (matcher.matches()) {
+ return new Version(parseInt(matcher.group(1)), parseInt(matcher.group(2)), parseInt(matcher.group(3)),
+ matcher.group(4));
+ }
+ throw new IllegalArgumentException("Unrecognized version pattern : " + version); //$NON-NLS-1$
+ }
+}
diff --git a/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/AllGerritTests.java b/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/AllGerritTests.java
index f0aeb5591..10b604246 100644
--- a/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/AllGerritTests.java
+++ b/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/AllGerritTests.java
@@ -21,11 +21,12 @@ import org.eclipse.mylyn.commons.sdk.util.TestConfiguration;
import org.eclipse.mylyn.gerrit.tests.core.GerritConnectorTest;
import org.eclipse.mylyn.gerrit.tests.core.GerritSynchronizationTest;
import org.eclipse.mylyn.gerrit.tests.core.client.GerritClientTest;
+import org.eclipse.mylyn.gerrit.tests.core.client.GerritVersionTest;
import org.eclipse.mylyn.gerrit.tests.core.client.OpenIdAuthenticationTest;
import org.eclipse.mylyn.gerrit.tests.support.GerritFixture;
import org.eclipse.mylyn.gerrit.tests.ui.GerritUrlHandlerTest;
-import org.eclipse.mylyn.internal.gerrit.core.remote.GerritReviewRemoteFactoryTest;
import org.eclipse.mylyn.internal.gerrit.core.remote.GerritDataLocatorTest;
+import org.eclipse.mylyn.internal.gerrit.core.remote.GerritReviewRemoteFactoryTest;
import org.eclipse.mylyn.internal.gerrit.core.remote.PatchSetRemoteFactoryTest;
/**
@@ -46,6 +47,7 @@ public class AllGerritTests {
}
private static void addTests(TestSuite suite, TestConfiguration configuration) {
+ suite.addTestSuite(GerritVersionTest.class);
if (!configuration.isLocalOnly()) {
// network tests
suite.addTestSuite(GerritUrlHandlerTest.class);
diff --git a/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/GerritClientTest.java b/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/GerritClientTest.java
index a06c6d85d..fa53a5701 100644
--- a/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/GerritClientTest.java
+++ b/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/GerritClientTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2011 Tasktop Technologies and others.
+ * Copyright (c) 2011, 2013 Tasktop Technologies and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -18,6 +18,7 @@ import java.util.List;
import junit.framework.TestCase;
import org.apache.commons.httpclient.Cookie;
+import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.mylyn.commons.net.WebLocation;
import org.eclipse.mylyn.commons.net.WebUtil;
import org.eclipse.mylyn.commons.sdk.util.CommonTestUtil;
@@ -59,7 +60,7 @@ public class GerritClientTest extends TestCase {
@Test
public void testRefreshConfig() throws Exception {
- GerritConfiguration config = client.refreshConfig(null);
+ GerritConfiguration config = client.refreshConfig(new NullProgressMonitor());
assertNotNull(config);
assertNotNull(config.getGerritConfig());
assertNotNull(config.getProjects());
@@ -70,7 +71,7 @@ public class GerritClientTest extends TestCase {
if (!GerritFixture.current().canAuthenticate()) {
return; // skip
}
- Account account = client.getAccount(null);
+ Account account = client.getAccount(new NullProgressMonitor());
assertEquals(CommonTestUtil.getShortUserName(harness.readCredentials()), account.getUserName());
}
@@ -78,7 +79,7 @@ public class GerritClientTest extends TestCase {
public void testGetAccountAnonymous() throws Exception {
client = harness.clientAnonymous();
try {
- client.getAccount(null);
+ client.getAccount(new NullProgressMonitor());
fail("Expected GerritException");
} catch (GerritException e) {
assertEquals("Not Signed In", e.getMessage());
@@ -97,7 +98,7 @@ public class GerritClientTest extends TestCase {
expected.add(new CommentLink("([Tt]ask:\\s+)(\\d+)", "$1$2"));
client = harness.client();
- GerritConfiguration config = client.refreshConfig(null);
+ GerritConfiguration config = client.refreshConfig(new NullProgressMonitor());
List links = config.getGerritConfig().getCommentLinks2();
assertEquals(expected, links);
}
@@ -115,4 +116,17 @@ public class GerritClientTest extends TestCase {
client.getAccount(null);
}
+ @Test
+ public void testGetVersion() throws Exception {
+ assertEquals(getCurrentVersion(), client.getVersion(new NullProgressMonitor()).toString());
+ }
+
+ private static String getCurrentVersion() {
+ String simpleInfo = GerritFixture.current().getSimpleInfo();
+ if (simpleInfo.indexOf('/') != -1) {
+ return simpleInfo.substring(0, simpleInfo.indexOf('/'));
+ }
+ return simpleInfo;
+ }
+
}
diff --git a/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/GerritVersionTest.java b/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/GerritVersionTest.java
new file mode 100644
index 000000000..e00197b2a
--- /dev/null
+++ b/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/GerritVersionTest.java
@@ -0,0 +1,96 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Tasktop Technologies and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.gerrit.tests.core.client;
+
+import static org.eclipse.mylyn.internal.gerrit.core.client.GerritVersion.parseGerritVersion;
+import junit.framework.TestCase;
+
+import org.junit.Test;
+import org.osgi.framework.Version;
+
+public class GerritVersionTest extends TestCase {
+
+ @Test
+ public void testParse_null() throws Exception {
+ try {
+ parseGerritVersion(null);
+ fail("Expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testParse_empty() throws Exception {
+ try {
+ parseGerritVersion("");
+ fail("Expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testParse_invalid() throws Exception {
+ try {
+ parseGerritVersion("invalid");
+ fail("Expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ assertTrue(e.getMessage().startsWith("Unrecognized version"));
+ }
+ }
+
+ @Test
+ public void testParse_21() throws Exception {
+ Version v = parseGerritVersion("2.1");
+ assertEquals(2, v.getMajor());
+ assertEquals(1, v.getMinor());
+ assertEquals(0, v.getMicro());
+ assertTrue(v.getQualifier().isEmpty());
+ }
+
+ @Test
+ public void testParse_254() throws Exception {
+ Version v = parseGerritVersion("2.5.4");
+ assertEquals(2, v.getMajor());
+ assertEquals(5, v.getMinor());
+ assertEquals(4, v.getMicro());
+ assertTrue(v.getQualifier().isEmpty());
+ }
+
+ @Test
+ public void testParse_26rc3() throws Exception {
+ Version v = parseGerritVersion("2.6-rc3");
+ assertEquals(2, v.getMajor());
+ assertEquals(6, v.getMinor());
+ assertEquals(0, v.getMicro());
+ assertEquals("rc3", v.getQualifier());
+ }
+
+ @Test
+ public void testParse_27rc2637g76c7890() throws Exception {
+ Version v = parseGerritVersion("2.7-rc2-637-g76c7890");
+ assertEquals(2, v.getMajor());
+ assertEquals(7, v.getMinor());
+ assertEquals(0, v.getMicro());
+ assertEquals("rc2-637-g76c7890", v.getQualifier());
+ }
+
+ @Test
+ public void testParse_123q() throws Exception {
+ Version v = parseGerritVersion("1.2.3-q");
+ assertEquals(1, v.getMajor());
+ assertEquals(2, v.getMinor());
+ assertEquals(3, v.getMicro());
+ assertEquals("q", v.getQualifier());
+ }
+}
--
cgit v1.2.3