summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Zarna2013-07-03 13:20:29 (EDT)
committerTomasz Zarna2013-07-12 08:43:02 (EDT)
commit995a703a48fd744585c753e382ef71aae1433c99 (patch)
tree79d171c522c59545ec5be89c78e3c60d6692cc14
parentc603cfc349a6deed355ea6388166665d71c8fb96 (diff)
downloadorg.eclipse.mylyn.reviews-995a703a48fd744585c753e382ef71aae1433c99.zip
org.eclipse.mylyn.reviews-995a703a48fd744585c753e382ef71aae1433c99.tar.gz
org.eclipse.mylyn.reviews-995a703a48fd744585c753e382ef71aae1433c99.tar.bz2
395059: detect Gerrit server versionrefs/changes/32/14232/10
Bug: 395059 Change-Id: Id71ae9af7178445240d06ead81b1ead83105138f Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=395059 Signed-off-by: Tomasz Zarna <tomasz.zarna@tasktop.com>
-rw-r--r--org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritClient.java57
-rw-r--r--org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java26
-rw-r--r--org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritSystemInfo.java13
-rw-r--r--org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritVersion.java56
-rw-r--r--org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/AllGerritTests.java4
-rw-r--r--org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/GerritClientTest.java24
-rw-r--r--org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/GerritVersionTest.java96
7 files changed, 256 insertions, 20 deletions
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 47f6059..d067f4b 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 (.+)</p>"); //$NON-NLS-1$
+
private abstract class Operation<T> implements AsyncCallback<T> {
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<ApprovalType> approvals = new ArrayList<ApprovalType>();
@@ -346,6 +353,7 @@ public class GerritClient extends ReviewsClient {
}
public GerritSystemInfo getInfo(IProgressMonitor monitor) throws GerritException {
+ Version version = getCachedVersion(monitor);
List<ContributorAgreement> 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<IRepository, IReview> 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<Version>() {
+ @Override
+ public void execute(IProgressMonitor monitor) throws GerritException {
+ try {
+ Request<String> request = new Request<String>() {
+ @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 46e5708..7064a1d 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> T execute(Request<T> request, IProgressMonitor monitor) throws IOException, GerritException {
+ return execute(request, true, monitor);
+ }
+
+ public <T> T execute(Request<T> 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 4213397..11810ba 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<ContributorAgreement> contributorAgreements;
- public GerritSystemInfo(List<ContributorAgreement> contributorAgreements, Account account) {
+ public GerritSystemInfo(Version version, List<ContributorAgreement> contributorAgreements, Account account) {
+ this.version = version;
this.contributorAgreements = contributorAgreements;
this.account = account;
}
+ public Version getVersion() {
+ return version;
+ }
+
public List<ContributorAgreement> 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 0000000..7ddb5a3
--- /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 f0aeb55..10b6042 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 a06c6d8..fa53a57 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<a href=\"http://tracker.mylyn.org/$2\">$2</a>"));
client = harness.client();
- GerritConfiguration config = client.refreshConfig(null);
+ GerritConfiguration config = client.refreshConfig(new NullProgressMonitor());
List<CommentLink> 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 0000000..e00197b
--- /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());
+ }
+}