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 <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin 2015-05-12 10:29:50 +02:00
parent eafde882be
commit d522cd7efa
8 changed files with 81 additions and 146 deletions

View File

@ -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<DownloadCommandInfo> 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;

View File

@ -63,6 +63,23 @@ public class DownloadInfo extends JavaScriptObject {
return command(commandName).replaceAll("\\$\\{project\\}", project);
}
public final Set<String> cloneCommandNames() {
return Natives.keys(_cloneCommands());
}
public final Set<DownloadCommandInfo> cloneCommands(String project) {
Set<DownloadCommandInfo> 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<NativeString> _commands() /*-{ return this.commands; }-*/;
private final native NativeMap<NativeString> _cloneCommands() /*-{ return this.clone_commands; }-*/;
protected DownloadSchemeInfo() {
}

View File

@ -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);
}

View File

@ -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);
}
}
}

View File

@ -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<DownloadCommandInfo> getCommands(
DownloadSchemeInfo schemeInfo);
}

View File

@ -56,8 +56,8 @@ public class DownloadUrlLink extends Anchor implements ClickHandler {
}
}
public static List<DownloadUrlLink> createDownloadUrlLinks(String project,
boolean allowAnonymous) {
public static List<DownloadUrlLink> createDownloadUrlLinks(
boolean allowAnonymous, DownloadPanel downloadPanel) {
List<DownloadUrlLink> 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());
}
}

View File

@ -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<DownloadUrlLink> links) {
for (Widget link: links) {
add(link);

@ -1 +1 @@
Subproject commit 1cf69212a7489e88d8c73377f0f77f8a5965db75
Subproject commit c0f1b84e494c8bab73f4786642dc768ae60468d9