From d522cd7efafdd6a81de63c202d3961dfd35bbb5b Mon Sep 17 00:00:00 2001 From: Edwin Kempin Date: Tue, 12 May 2015 10:29:50 +0200 Subject: [PATCH] Retrieve clone commands from /config/server/info REST endpoint Instead of hard-coding the clone commands on client-side, retrieve them from the /config/server/info REST endpoint. The clone commands are now implemented in the download-commands plugin, hence this plugin needs to be updated so that the clone commands are available. Change-Id: I58abd64e9597815ce23b3cd5ebf156185cad4c6a Signed-off-by: Edwin Kempin --- .../client/admin/ProjectInfoScreen.java | 19 ++-- .../gerrit/client/config/DownloadInfo.java | 19 ++++ .../client/download/DownloadCommandLink.java | 90 +++---------------- .../client/download/DownloadCommandPanel.java | 20 ++--- .../gerrit/client/download/DownloadPanel.java | 38 ++++---- .../client/download/DownloadUrlLink.java | 31 +++---- .../client/download/DownloadUrlPanel.java | 8 +- plugins/download-commands | 2 +- 8 files changed, 81 insertions(+), 146 deletions(-) diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/admin/ProjectInfoScreen.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/admin/ProjectInfoScreen.java index 08ab2c08f0..104ef934c8 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/admin/ProjectInfoScreen.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/admin/ProjectInfoScreen.java @@ -21,6 +21,8 @@ import com.google.gerrit.client.access.ProjectAccessInfo; import com.google.gerrit.client.actions.ActionButton; import com.google.gerrit.client.actions.ActionInfo; import com.google.gerrit.client.change.Resources; +import com.google.gerrit.client.config.DownloadInfo.DownloadCommandInfo; +import com.google.gerrit.client.config.DownloadInfo.DownloadSchemeInfo; import com.google.gerrit.client.download.DownloadPanel; import com.google.gerrit.client.projects.ConfigInfo; import com.google.gerrit.client.projects.ConfigInfo.ConfigParameterInfo; @@ -65,6 +67,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; public class ProjectInfoScreen extends ProjectScreen { private boolean isOwner; @@ -677,23 +680,11 @@ public class ProjectInfoScreen extends ProjectScreen { } @Override - public void populateDownloadCommandLinks() { - if (!urls.isEmpty()) { - commands.add(cmdLinkfactory.new CloneCommandLink()); - if (Gerrit.getConfig().getSshdAddress() != null && hasUserName()) { - commands.add( - cmdLinkfactory.new CloneWithCommitMsgHookCommandLink(getProjectKey())); - } - } + protected Set getCommands(DownloadSchemeInfo schemeInfo) { + return schemeInfo.cloneCommands(project); } } - private static boolean hasUserName() { - return Gerrit.isSignedIn() - && Gerrit.getUserAccount().getUserName() != null - && Gerrit.getUserAccount().getUserName().length() > 0; - } - private static class LabeledWidgetsGrid extends FlexTable { private String labelSuffix; diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/config/DownloadInfo.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/config/DownloadInfo.java index fc914f4665..187ad598ed 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/config/DownloadInfo.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/config/DownloadInfo.java @@ -63,6 +63,23 @@ public class DownloadInfo extends JavaScriptObject { return command(commandName).replaceAll("\\$\\{project\\}", project); } + public final Set cloneCommandNames() { + return Natives.keys(_cloneCommands()); + } + + public final Set cloneCommands(String project) { + Set commands = new HashSet<>(); + for (String commandName : cloneCommandNames()) { + commands.add(new DownloadCommandInfo(commandName, cloneCommand( + commandName, project))); + } + return commands; + } + + public final String cloneCommand(String commandName, String project) { + return cloneCommand(commandName).replaceAll("\\$\\{project\\}", project); + } + public final String getUrl(String project) { return url().replaceAll("\\$\\{project\\}", project); } @@ -72,7 +89,9 @@ public class DownloadInfo extends JavaScriptObject { public final native boolean isAuthRequired() /*-{ return this.is_auth_required || false; }-*/; public final native boolean isAuthSupported() /*-{ return this.is_auth_supported || false; }-*/; public final native String command(String n) /*-{ return this.commands[n]; }-*/; + public final native String cloneCommand(String n) /*-{ return this.clone_commands[n]; }-*/; private final native NativeMap _commands() /*-{ return this.commands; }-*/; + private final native NativeMap _cloneCommands() /*-{ return this.clone_commands; }-*/; protected DownloadSchemeInfo() { } diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadCommandLink.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadCommandLink.java index d4237f9561..2cdc27b909 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadCommandLink.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadCommandLink.java @@ -15,92 +15,24 @@ package com.google.gerrit.client.download; import com.google.gerrit.client.Gerrit; -import com.google.gerrit.reviewdb.client.Project; +import com.google.gerrit.client.config.DownloadInfo.DownloadCommandInfo; import com.google.gwt.aria.client.Roles; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; -import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.Anchor; import com.google.gwt.user.client.ui.Widget; import com.google.gwtexpui.clippy.client.CopyableLabel; -public abstract class DownloadCommandLink extends Anchor implements ClickHandler { - public static class CopyableCommandLinkFactory { - protected CopyableLabel copyLabel = null; - protected Widget widget; +public class DownloadCommandLink extends Anchor implements ClickHandler { + private final CopyableLabel copyLabel; + private final String command; - public class CloneCommandLink extends DownloadCommandLink { - public CloneCommandLink() { - super("clone"); - } + public DownloadCommandLink(CopyableLabel copyLabel, + DownloadCommandInfo commandInfo) { + super(commandInfo.name()); + this.copyLabel = copyLabel; + this.command = commandInfo.command(); - @Override - protected void setCurrentUrl(DownloadUrlLink link) { - widget.setVisible(true); - copyLabel.setText("git clone " + link.getUrlData()); - } - } - - public class CloneWithCommitMsgHookCommandLink extends DownloadCommandLink { - private final Project.NameKey project; - - public CloneWithCommitMsgHookCommandLink(Project.NameKey project) { - super("clone with commit-msg hook"); - this.project = project; - } - - @Override - protected void setCurrentUrl(DownloadUrlLink link) { - widget.setVisible(true); - - String sshPort = null; - String sshAddr = Gerrit.getConfig().getSshdAddress(); - int p = sshAddr.lastIndexOf(':'); - if (p != -1 && !sshAddr.endsWith(":")) { - sshPort = sshAddr.substring(p + 1); - } - - StringBuilder cmd = new StringBuilder(); - cmd.append("git clone "); - cmd.append(link.getUrlData()); - cmd.append(" && scp -p "); - if (sshPort != null) { - cmd.append("-P "); - cmd.append(sshPort); - cmd.append(" "); - } - cmd.append(Gerrit.getUserAccount().getUserName()); - cmd.append("@"); - - if (sshAddr.startsWith("*:") || p == -1) { - cmd.append(Window.Location.getHostName()); - } else { - cmd.append(sshAddr.substring(0, p)); - } - - cmd.append(":hooks/commit-msg "); - - p = project.get().lastIndexOf('/'); - if (p != -1) { - cmd.append(project.get().substring(p + 1)); - } else { - cmd.append(project.get()); - } - - cmd.append("/.git/hooks/"); - - copyLabel.setText(cmd.toString()); - } - } - - public CopyableCommandLinkFactory(CopyableLabel label, Widget widget) { - copyLabel = label; - this.widget = widget; - } - } - - public DownloadCommandLink(String text) { - super(text); setStyleName(Gerrit.RESOURCES.css().downloadLink()); Roles.getTabRole().set(getElement()); addClickHandler(this); @@ -115,6 +47,8 @@ public abstract class DownloadCommandLink extends Anchor implements ClickHandler } void select() { + copyLabel.setText(command); + DownloadCommandPanel parent = (DownloadCommandPanel) getParent(); for (Widget w : parent) { if (w != this && w instanceof DownloadCommandLink) { @@ -124,6 +58,4 @@ public abstract class DownloadCommandLink extends Anchor implements ClickHandler parent.setCurrentCommand(this); addStyleName(Gerrit.RESOURCES.css().downloadLink_Active()); } - - protected abstract void setCurrentUrl(DownloadUrlLink link); } diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadCommandPanel.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadCommandPanel.java index 5b7d015bbf..21c33e2eab 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadCommandPanel.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadCommandPanel.java @@ -21,7 +21,6 @@ import com.google.gwt.user.client.ui.Widget; public class DownloadCommandPanel extends FlowPanel { private DownloadCommandLink currentCommand; - private DownloadUrlLink currentUrl; public DownloadCommandPanel() { setStyleName(Gerrit.RESOURCES.css().downloadLinkList()); @@ -37,7 +36,12 @@ public class DownloadCommandPanel extends FlowPanel { for (Widget w : this) { if (w instanceof DownloadCommandLink) { - final DownloadCommandLink d = (DownloadCommandLink) w; + DownloadCommandLink d = (DownloadCommandLink) w; + if (currentCommand != null + && d.getText().equals(currentCommand.getText())) { + d.select(); + return; + } if (first == null) { first = d; } @@ -51,19 +55,7 @@ public class DownloadCommandPanel extends FlowPanel { } } - void setCurrentUrl(DownloadUrlLink link) { - currentUrl = link; - update(); - } - void setCurrentCommand(DownloadCommandLink cmd) { currentCommand = cmd; - update(); - } - - private void update() { - if (currentCommand != null && currentUrl != null) { - currentCommand.setCurrentUrl(currentUrl); - } } } diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadPanel.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadPanel.java index 19c65a91e5..9e6537a85d 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadPanel.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadPanel.java @@ -15,34 +15,32 @@ package com.google.gerrit.client.download; import com.google.gerrit.client.Gerrit; +import com.google.gerrit.client.config.DownloadInfo.DownloadCommandInfo; +import com.google.gerrit.client.config.DownloadInfo.DownloadSchemeInfo; import com.google.gerrit.reviewdb.client.AccountGeneralPreferences; import com.google.gwt.user.client.ui.FlowPanel; import com.google.gwt.user.client.ui.InlineLabel; import com.google.gwtexpui.clippy.client.CopyableLabel; +import java.util.Set; + public abstract class DownloadPanel extends FlowPanel { - protected String projectName; + protected final String project; - protected DownloadCommandLink.CopyableCommandLinkFactory cmdLinkfactory; - - protected DownloadCommandPanel commands = new DownloadCommandPanel(); - protected DownloadUrlPanel urls = new DownloadUrlPanel(commands); - protected CopyableLabel copyLabel = new CopyableLabel(""); + private final DownloadCommandPanel commands = new DownloadCommandPanel(); + private final DownloadUrlPanel urls = new DownloadUrlPanel(); + private final CopyableLabel copyLabel = new CopyableLabel(""); public DownloadPanel(String project, boolean allowAnonymous) { - this.projectName = project; - + this.project = project; copyLabel.setStyleName(Gerrit.RESOURCES.css().downloadLinkCopyLabel()); - urls.add(DownloadUrlLink.createDownloadUrlLinks(project, allowAnonymous)); - cmdLinkfactory = new DownloadCommandLink.CopyableCommandLinkFactory( - copyLabel, urls); + urls.add(DownloadUrlLink.createDownloadUrlLinks(allowAnonymous, this)); - populateDownloadCommandLinks(); setupWidgets(); } - protected void setupWidgets() { - if (!commands.isEmpty()) { + private void setupWidgets() { + if (!urls.isEmpty()) { final AccountGeneralPreferences pref; if (Gerrit.isSignedIn()) { pref = Gerrit.getUserAccount().getGeneralPreferences(); @@ -50,7 +48,6 @@ public abstract class DownloadPanel extends FlowPanel { pref = new AccountGeneralPreferences(); pref.resetToDefaults(); } - commands.select(); urls.select(pref.getDownloadUrl()); FlowPanel p = new FlowPanel(); @@ -66,5 +63,14 @@ public abstract class DownloadPanel extends FlowPanel { } } - protected abstract void populateDownloadCommandLinks(); + void populateDownloadCommandLinks(DownloadSchemeInfo schemeInfo) { + commands.clear(); + for (DownloadCommandInfo cmd : getCommands(schemeInfo)) { + commands.add(new DownloadCommandLink(copyLabel, cmd)); + } + commands.select(); + } + + protected abstract Set getCommands( + DownloadSchemeInfo schemeInfo); } diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadUrlLink.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadUrlLink.java index 79a31e2935..d1f53a5b46 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadUrlLink.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadUrlLink.java @@ -56,8 +56,8 @@ public class DownloadUrlLink extends Anchor implements ClickHandler { } } - public static List createDownloadUrlLinks(String project, - boolean allowAnonymous) { + public static List createDownloadUrlLinks( + boolean allowAnonymous, DownloadPanel downloadPanel) { List urls = new ArrayList<>(); for (String s : Gerrit.info().download().schemes()) { DownloadSchemeInfo scheme = Gerrit.info().download().scheme(s); @@ -67,34 +67,34 @@ public class DownloadUrlLink extends Anchor implements ClickHandler { KnownScheme knownScheme = KnownScheme.get(s); if (knownScheme != null) { - urls.add(new DownloadUrlLink(knownScheme.downloadScheme, - knownScheme.text, scheme.getUrl(project))); + urls.add(new DownloadUrlLink(downloadPanel, scheme, + knownScheme.downloadScheme, knownScheme.text)); } else { - urls.add(new DownloadUrlLink(s, scheme.getUrl(project))); + urls.add(new DownloadUrlLink(downloadPanel, scheme, s)); } } return urls; } + private final DownloadPanel downloadPanel; + private final DownloadSchemeInfo schemeInfo; private final DownloadScheme urlType; - private final String urlData; - public DownloadUrlLink(String text, String urlData) { - this(null, text, urlData); + public DownloadUrlLink(DownloadPanel downloadPanel, + DownloadSchemeInfo schemeInfo, String text) { + this(downloadPanel, schemeInfo, null, text); } - public DownloadUrlLink(DownloadScheme urlType, String text, String urlData) { + public DownloadUrlLink(DownloadPanel downloadPanel, + DownloadSchemeInfo schemeInfo, DownloadScheme urlType, String text) { super(text); setStyleName(Gerrit.RESOURCES.css().downloadLink()); Roles.getTabRole().set(getElement()); addClickHandler(this); + this.downloadPanel = downloadPanel; + this.schemeInfo = schemeInfo; this.urlType = urlType; - this.urlData = urlData; - } - - public String getUrlData() { - return urlData; } public DownloadScheme getUrlType() { @@ -128,13 +128,14 @@ public class DownloadUrlLink extends Anchor implements ClickHandler { } void select() { + downloadPanel.populateDownloadCommandLinks(schemeInfo); + DownloadUrlPanel parent = (DownloadUrlPanel) getParent(); for (Widget w : parent) { if (w != this && w instanceof DownloadUrlLink) { w.removeStyleName(Gerrit.RESOURCES.css().downloadLink_Active()); } } - parent.setCurrentUrl(this); addStyleName(Gerrit.RESOURCES.css().downloadLink_Active()); } } diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadUrlPanel.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadUrlPanel.java index d71e5f396a..f4a7e8aa87 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadUrlPanel.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/download/DownloadUrlPanel.java @@ -23,10 +23,8 @@ import com.google.gwt.user.client.ui.Widget; import java.util.Collection; public class DownloadUrlPanel extends FlowPanel { - private final DownloadCommandPanel commandPanel; - public DownloadUrlPanel(final DownloadCommandPanel commandPanel) { - this.commandPanel = commandPanel; + public DownloadUrlPanel() { setStyleName(Gerrit.RESOURCES.css().downloadLinkList()); Roles.getTablistRole().set(getElement()); } @@ -58,10 +56,6 @@ public class DownloadUrlPanel extends FlowPanel { } } - void setCurrentUrl(DownloadUrlLink link) { - commandPanel.setCurrentUrl(link); - } - public void add(Collection links) { for (Widget link: links) { add(link); diff --git a/plugins/download-commands b/plugins/download-commands index 1cf69212a7..c0f1b84e49 160000 --- a/plugins/download-commands +++ b/plugins/download-commands @@ -1 +1 @@ -Subproject commit 1cf69212a7489e88d8c73377f0f77f8a5965db75 +Subproject commit c0f1b84e494c8bab73f4786642dc768ae60468d9