Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn')
-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
4 files changed, 138 insertions, 14 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 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 (.+)</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 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> 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 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<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 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$
+ }
+}

Back to the top