Adapt to new API in Gerrit core Change-Id: I8cd6954e30f8baa7f6cda2400b85bf75b88f8c27 Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/Module.java b/src/main/java/com/googlesource/gerrit/plugins/download/Module.java index b578a51..3f6da18 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/download/Module.java +++ b/src/main/java/com/googlesource/gerrit/plugins/download/Module.java
@@ -14,19 +14,18 @@ package com.googlesource.gerrit.plugins.download; +import com.google.gerrit.extensions.annotations.Exports; import com.google.gerrit.extensions.config.DownloadCommand; import com.google.gerrit.extensions.config.DownloadScheme; -import com.google.gerrit.extensions.registration.DynamicSet; import com.google.inject.AbstractModule; import com.googlesource.gerrit.plugins.download.command.CheckoutCommand; import com.googlesource.gerrit.plugins.download.command.CherryPickCommand; -import com.googlesource.gerrit.plugins.download.command.CloneCommand; import com.googlesource.gerrit.plugins.download.command.FormatPatchCommand; import com.googlesource.gerrit.plugins.download.command.PullCommand; import com.googlesource.gerrit.plugins.download.command.RepoCommand; -import com.googlesource.gerrit.plugins.download.scheme.AnonymousGitScheme; import com.googlesource.gerrit.plugins.download.scheme.AnonymousHttpScheme; +import com.googlesource.gerrit.plugins.download.scheme.GitScheme; import com.googlesource.gerrit.plugins.download.scheme.HttpScheme; import com.googlesource.gerrit.plugins.download.scheme.RepoScheme; import com.googlesource.gerrit.plugins.download.scheme.SshScheme; @@ -34,17 +33,26 @@ class Module extends AbstractModule { @Override protected void configure() { - DynamicSet.bind(binder(), DownloadScheme.class).to(AnonymousHttpScheme.class); - DynamicSet.bind(binder(), DownloadScheme.class).to(AnonymousGitScheme.class); - DynamicSet.bind(binder(), DownloadScheme.class).to(HttpScheme.class); - DynamicSet.bind(binder(), DownloadScheme.class).to(RepoScheme.class); - DynamicSet.bind(binder(), DownloadScheme.class).to(SshScheme.class); + bind(DownloadScheme.class).annotatedWith(Exports.named("anonymous http")) + .to(AnonymousHttpScheme.class); + bind(DownloadScheme.class).annotatedWith(Exports.named("git")) + .to(GitScheme.class); + bind(DownloadScheme.class).annotatedWith(Exports.named("http")) + .to(HttpScheme.class); + bind(DownloadScheme.class).annotatedWith(Exports.named("repo")) + .to(RepoScheme.class); + bind(DownloadScheme.class).annotatedWith(Exports.named("ssh")) + .to(SshScheme.class); - DynamicSet.bind(binder(), DownloadCommand.class).to(CloneCommand.class); - DynamicSet.bind(binder(), DownloadCommand.class).to(CheckoutCommand.class); - DynamicSet.bind(binder(), DownloadCommand.class).to(CherryPickCommand.class); - DynamicSet.bind(binder(), DownloadCommand.class).to(FormatPatchCommand.class); - DynamicSet.bind(binder(), DownloadCommand.class).to(PullCommand.class); - DynamicSet.bind(binder(), DownloadCommand.class).to(RepoCommand.class); + bind(DownloadCommand.class).annotatedWith(Exports.named("Checkout")) + .to(CheckoutCommand.class); + bind(DownloadCommand.class).annotatedWith(Exports.named("Cherry-Pick")) + .to(CherryPickCommand.class); + bind(DownloadCommand.class).annotatedWith(Exports.named("Format-Patch")) + .to(FormatPatchCommand.class); + bind(DownloadCommand.class).annotatedWith(Exports.named("Pull")) + .to(PullCommand.class); + bind(DownloadCommand.class).annotatedWith(Exports.named("Repo-Download")) + .to(RepoCommand.class); } }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/CheckoutCommand.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/CheckoutCommand.java index d0c70ba..349e56a 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/download/command/CheckoutCommand.java +++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/CheckoutCommand.java
@@ -28,7 +28,7 @@ } @Override - public String getCommand(String url) { - return "git fetch " + url + " ${ref} && git checkout FETCH_HEAD"; + public String getCommand(String url, String ref) { + return "git fetch " + url + " " + ref + " && git checkout FETCH_HEAD"; } }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/CherryPickCommand.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/CherryPickCommand.java index a28bd0d..a357aa8 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/download/command/CherryPickCommand.java +++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/CherryPickCommand.java
@@ -28,7 +28,7 @@ } @Override - public String getCommand(String url) { - return "git fetch " + url + " ${ref} && git cherry-pick FETCH_HEAD"; + public String getCommand(String url, String ref) { + return "git fetch " + url + " " + ref + " && git cherry-pick FETCH_HEAD"; } }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneCommand.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneCommand.java deleted file mode 100644 index 1fd3431..0000000 --- a/src/main/java/com/googlesource/gerrit/plugins/download/command/CloneCommand.java +++ /dev/null
@@ -1,39 +0,0 @@ -// Copyright (C) 2013 The Android Open Source Project -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.googlesource.gerrit.plugins.download.command; - -import static com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadCommand.CHECKOUT; - -import com.google.gerrit.extensions.annotations.Listen; -import com.google.gerrit.server.config.DownloadConfig; -import com.google.inject.Inject; - -@Listen -public class CloneCommand extends GitDownloadCommand { - @Inject - CloneCommand(DownloadConfig downloadConfig) { - super(downloadConfig, CHECKOUT); - } - - @Override - public String getName() { - return "clone"; - } - - @Override - public String getCommand(String url) { - return "git clone " + url; - } -}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/FormatPatchCommand.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/FormatPatchCommand.java index c02d15a..aefa23f 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/download/command/FormatPatchCommand.java +++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/FormatPatchCommand.java
@@ -28,7 +28,7 @@ } @Override - public String getCommand(String url) { - return "git fetch " + url + " ${ref} && git format-patch -1 --stdout FETCH_HEAD"; + public String getCommand(String url, String ref) { + return "git fetch " + url + " " + ref + " && git format-patch -1 --stdout FETCH_HEAD"; } }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/GitDownloadCommand.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/GitDownloadCommand.java index 184ed2a..61e3fbd 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/download/command/GitDownloadCommand.java +++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/GitDownloadCommand.java
@@ -21,43 +21,23 @@ import com.google.gerrit.reviewdb.client.AccountGeneralPreferences; import com.google.gerrit.server.config.DownloadConfig; -import com.googlesource.gerrit.plugins.download.scheme.AnonymousGitScheme; import com.googlesource.gerrit.plugins.download.scheme.AnonymousHttpScheme; +import com.googlesource.gerrit.plugins.download.scheme.GitScheme; import com.googlesource.gerrit.plugins.download.scheme.HttpScheme; import com.googlesource.gerrit.plugins.download.scheme.SshScheme; public abstract class GitDownloadCommand extends DownloadCommand { - private final AccountGeneralPreferences.DownloadCommand cmd; private final boolean commandAllowed; GitDownloadCommand( DownloadConfig downloadConfig, AccountGeneralPreferences.DownloadCommand cmd) { - this.cmd = cmd; this.commandAllowed = downloadConfig.getDownloadCommands().contains(cmd) || downloadConfig.getDownloadCommands().contains(DEFAULT_DOWNLOADS); } @Override - public String getName() { - return upperUnderscoreToUpperHyphen(cmd.name()); - } - - /** Converts UPPER_UNDERSCORE to Upper-Hyphen */ - private static String upperUnderscoreToUpperHyphen(String upperUnderscore) { - StringBuilder upperHyphen = new StringBuilder(upperUnderscore.length()); - String[] words = upperUnderscore.split("_"); - for (int i = 0, l = words.length; i < l; ++i) { - if (i > 0) { - upperHyphen.append("-"); - } - upperHyphen.append(Character.toUpperCase(words[i].charAt(0))).append( - words[i].substring(1).toLowerCase()); - } - return upperHyphen.toString(); - } - - @Override - public final String getCommand(DownloadScheme scheme, String project) { + public final String getCommand(DownloadScheme scheme, String project, + String ref) { if (!commandAllowed) { return null; } @@ -65,12 +45,16 @@ if (scheme instanceof SshScheme || scheme instanceof HttpScheme || scheme instanceof AnonymousHttpScheme - || scheme instanceof AnonymousGitScheme) { - return getCommand(scheme.getUrl(project)); + || scheme instanceof GitScheme) { + String url = scheme.getUrl(project); + if (url != null) { + return getCommand(url, ref); + } else + return null; } else { return null; } } - public abstract String getCommand(String url); + public abstract String getCommand(String url, String ref); }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/PullCommand.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/PullCommand.java index 07933f6..d86a630 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/download/command/PullCommand.java +++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/PullCommand.java
@@ -28,7 +28,7 @@ } @Override - public String getCommand(String url) { - return "git pull " + url + " ${ref}"; + public String getCommand(String url, String ref) { + return "git pull " + url + " " + ref; } }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/command/RepoCommand.java b/src/main/java/com/googlesource/gerrit/plugins/download/command/RepoCommand.java index d96ab90..26a40ec 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/download/command/RepoCommand.java +++ b/src/main/java/com/googlesource/gerrit/plugins/download/command/RepoCommand.java
@@ -16,7 +16,6 @@ import static com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadCommand.REPO_DOWNLOAD; -import com.google.common.base.CaseFormat; import com.google.gerrit.extensions.config.DownloadCommand; import com.google.gerrit.extensions.config.DownloadScheme; import com.google.gerrit.server.config.DownloadConfig; @@ -33,16 +32,11 @@ } @Override - public String getName() { - return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_HYPHEN, REPO_DOWNLOAD.name()); - } - - @Override - public String getCommand(DownloadScheme scheme, String project) { + public String getCommand(DownloadScheme scheme, String project, String ref) { if (!commandAllowed || !(scheme instanceof RepoScheme)) { return null; } - return scheme.getUrl(project) + " ${ref}"; + return scheme.getUrl(project) + " " + ref; } }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/scheme/AnonymousHttpScheme.java b/src/main/java/com/googlesource/gerrit/plugins/download/scheme/AnonymousHttpScheme.java index fbb9545..905b0b6 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/download/scheme/AnonymousHttpScheme.java +++ b/src/main/java/com/googlesource/gerrit/plugins/download/scheme/AnonymousHttpScheme.java
@@ -44,11 +44,6 @@ } @Override - public String getName() { - return "Anonymous HTTP"; - } - - @Override public String getUrl(String project) { if (!isEnabled()) { return null; @@ -71,6 +66,11 @@ return schemeAllowed && (gitHttpUrl != null || canonicalWebUrl != null); } + @Override + public boolean isAuthRequired() { + return false; + } + private static String ensureSlash(String in) { if (in != null && !in.endsWith("/")) { return in + "/";
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/scheme/AnonymousGitScheme.java b/src/main/java/com/googlesource/gerrit/plugins/download/scheme/GitScheme.java similarity index 91% rename from src/main/java/com/googlesource/gerrit/plugins/download/scheme/AnonymousGitScheme.java rename to src/main/java/com/googlesource/gerrit/plugins/download/scheme/GitScheme.java index 4350165..77b12b0 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/download/scheme/AnonymousGitScheme.java +++ b/src/main/java/com/googlesource/gerrit/plugins/download/scheme/GitScheme.java
@@ -24,13 +24,13 @@ import org.eclipse.jgit.lib.Config; -public class AnonymousGitScheme extends DownloadScheme { +public class GitScheme extends DownloadScheme { private final String gitDaemonUrl; private final boolean schemeAllowed; @Inject - public AnonymousGitScheme(@GerritServerConfig Config cfg, + public GitScheme(@GerritServerConfig Config cfg, DownloadConfig downloadConfig) { this.gitDaemonUrl = ensureSlash(cfg.getString("gerrit", null, "canonicalGitUrl")); @@ -39,11 +39,6 @@ } @Override - public String getName() { - return "Anonymous GIT"; - } - - @Override public String getUrl(String project) { StringBuilder r = new StringBuilder(); r.append(gitDaemonUrl); @@ -56,6 +51,11 @@ return schemeAllowed && gitDaemonUrl != null; } + @Override + public boolean isAuthRequired() { + return false; + } + private static String ensureSlash(String in) { if (in != null && !in.endsWith("/")) { return in + "/";
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/scheme/HttpScheme.java b/src/main/java/com/googlesource/gerrit/plugins/download/scheme/HttpScheme.java index a69c556..b97a1bd 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/download/scheme/HttpScheme.java +++ b/src/main/java/com/googlesource/gerrit/plugins/download/scheme/HttpScheme.java
@@ -19,6 +19,7 @@ import com.google.gerrit.common.Nullable; import com.google.gerrit.extensions.config.DownloadScheme; +import com.google.gerrit.server.CurrentUser; import com.google.gerrit.server.config.CanonicalWebUrl; import com.google.gerrit.server.config.DownloadConfig; import com.google.gerrit.server.config.GerritServerConfig; @@ -31,26 +32,24 @@ private final String gitHttpUrl; private final String canonicalWebUrl; + private final Provider<CurrentUser> userProvider; private final boolean schemeAllowed; @Inject public HttpScheme(@GerritServerConfig Config cfg, - @CanonicalWebUrl @Nullable Provider<String> provider, + @CanonicalWebUrl @Nullable Provider<String> urlProvider, + Provider<CurrentUser> userProvider, DownloadConfig downloadConfig) { this.gitHttpUrl = ensureSlash(cfg.getString("gerrit", null, "gitHttpUrl")); - this.canonicalWebUrl = provider != null ? provider.get() : null; + this.canonicalWebUrl = urlProvider != null ? urlProvider.get() : null; + this.userProvider = userProvider; this.schemeAllowed = downloadConfig.getDownloadSchemes().contains(HTTP) || downloadConfig.getDownloadSchemes().contains(DEFAULT_DOWNLOADS); } @Override - public String getName() { - return "HTTP"; - } - - @Override public String getUrl(String project) { - if (!isEnabled()) { + if (!isEnabled() || !userProvider.get().isIdentifiedUser()) { return null; } @@ -66,7 +65,8 @@ } String host = base.substring(p + 3, s); r.append(base.substring(0, p + 3)); - r.append("${username}@"); + r.append(userProvider.get().getUserName()); + r.append("@"); r.append(host); r.append(base.substring(s)); } else { @@ -81,6 +81,11 @@ return schemeAllowed && (gitHttpUrl != null || canonicalWebUrl != null); } + @Override + public boolean isAuthRequired() { + return true; + } + private static String ensureSlash(String in) { if (in != null && !in.endsWith("/")) { return in + "/";
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/scheme/RepoScheme.java b/src/main/java/com/googlesource/gerrit/plugins/download/scheme/RepoScheme.java index f6ef690..388a7b4 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/download/scheme/RepoScheme.java +++ b/src/main/java/com/googlesource/gerrit/plugins/download/scheme/RepoScheme.java
@@ -32,20 +32,17 @@ } @Override - public String getName() { - return "REPO"; - } - - @Override public String getUrl(String project) { - final StringBuilder r = new StringBuilder(); - r.append("repo download "); - r.append(project); - return r.toString(); + return "repo download " + project; } @Override public boolean isEnabled() { return schemeAllowed; } + + @Override + public boolean isAuthRequired() { + return false; + } }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/download/scheme/SshScheme.java b/src/main/java/com/googlesource/gerrit/plugins/download/scheme/SshScheme.java index e1db802..5e3c0da 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/download/scheme/SshScheme.java +++ b/src/main/java/com/googlesource/gerrit/plugins/download/scheme/SshScheme.java
@@ -17,56 +17,55 @@ import static com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme.DEFAULT_DOWNLOADS; import static com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme.SSH; +import com.google.gerrit.common.Nullable; import com.google.gerrit.extensions.annotations.Listen; import com.google.gerrit.extensions.config.DownloadScheme; +import com.google.gerrit.server.CurrentUser; +import com.google.gerrit.server.config.CanonicalWebUrl; import com.google.gerrit.server.config.DownloadConfig; import com.google.gerrit.server.ssh.SshAdvertisedAddresses; import com.google.inject.Inject; +import com.google.inject.Provider; +import java.net.MalformedURLException; +import java.net.URL; import java.util.List; @Listen public class SshScheme extends DownloadScheme { - private final String sshUrl; private final String sshdAddress; + private final Provider<CurrentUser> userProvider; private final boolean schemeAllowed; @Inject SshScheme(@SshAdvertisedAddresses List<String> sshAddresses, - DownloadConfig downloadConfig) { - this.sshUrl = - !sshAddresses.isEmpty() ? ensureSlash(sshAddresses.get(0)) : null; - if (sshUrl != null) { - String sshAddr = sshUrl; - StringBuilder r = new StringBuilder(); - r.append("ssh://${username}@"); - if (sshAddr.startsWith("*:") || "".equals(sshAddr)) { - r.append("${hostname}"); + @CanonicalWebUrl @Nullable Provider<String> urlProvider, + Provider<CurrentUser> userProvider, DownloadConfig downloadConfig) { + String sshAddr = !sshAddresses.isEmpty() ? sshAddresses.get(0) : null; + if (sshAddr != null && (sshAddr.startsWith("*:") || "".equals(sshAddr)) + && urlProvider != null) { + try { + sshAddr = (new URL(urlProvider.get())).getHost() + sshAddr.substring(1); + } catch (MalformedURLException e) { + // ignore, then this scheme will be disabled } - if (sshAddr.startsWith("*")) { - sshAddr = sshAddr.substring(1); - } - r.append(sshAddr); - sshdAddress = r.toString(); - } else { - sshdAddress = null; } + this.sshdAddress = sshAddr; + this.userProvider = userProvider; this.schemeAllowed = downloadConfig.getDownloadSchemes().contains(SSH) || downloadConfig.getDownloadSchemes().contains(DEFAULT_DOWNLOADS); } @Override - public String getName() { - return "SSH"; - } - - @Override public String getUrl(String project) { - if (!isEnabled()) { + if (!isEnabled() || !userProvider.get().isIdentifiedUser()) { return null; } StringBuilder r = new StringBuilder(); + r.append("ssh://"); + r.append(userProvider.get().getUserName()); + r.append("@"); r.append(ensureSlash(sshdAddress)); r.append(project); return r.toString(); @@ -74,7 +73,12 @@ @Override public boolean isEnabled() { - return schemeAllowed && sshUrl != null; + return schemeAllowed && sshdAddress != null; + } + + @Override + public boolean isAuthRequired() { + return true; } private static String ensureSlash(String in) {
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md index 0afeb99..e4eae75 100644 --- a/src/main/resources/Documentation/about.md +++ b/src/main/resources/Documentation/about.md
@@ -39,9 +39,6 @@ Command to fetch a patch set and cherry-pick it onto the current commit. -* `Clone`: -Command to clone a project. - * `Format-Patch`: Command to fetch a patch set and feed it into the `format-patch` command.