Plugin support for revision weblink

Support for an extension point that allows for weblinks to
several external tools.

First effort, weblinks for patch sets.

At the present time GitWebLink is integrated. But the aim is to
extend support to engulf entire GitWebLink and provide backwards
support by adding a plugin that has the same functionality as
GitWebLink has today.

Change-Id: I19f4d00325d2991df9514ee8833a8873649c27bc
This commit is contained in:
Sven Selberg
2014-02-14 14:24:29 +01:00
committed by David Pursehouse
parent 1724b8d2cc
commit ae1a10ce48
14 changed files with 269 additions and 16 deletions

View File

@@ -1706,6 +1706,39 @@ commands by implementing
The download schemes and download commands which are used most often
are provided by the Gerrit core plugin `download-commands`.
[[links-to-external-tools]]
== Links To External Tools
Gerrit has extension points that enables development of a
light-weight plugin that links commits to external
tools (GitBlit, CGit, company specific resources etc).
PatchSetWebLinks will appear to the right of the commit-SHA1 in the UI.
[source, java]
----
import com.google.gerrit.extensions.annotations.Listen;
import com.google.gerrit.extensions.webui.PatchSetWebLink;;
@Listen
public class MyWeblinkPlugin implements PatchSetWebLink {
private String name = "MyLink";
private String placeHolderUrlProjectCommit = "http://my.tool.com/project=%s/commit=%s";
@Override
public String getLinkName() {
return name ;
}
@Override
public String getPatchSetUrl(String project, String commit) {
return String.format(placeHolderUrlProjectCommit, project, commit);
}
}
----
[[documentation]]
== Documentation

View File

@@ -242,6 +242,11 @@ default. Optional fields are:
authenticated and has commented on the current revision.
--
[[patch-set-links]]
--
* `PATCHSET_LINKS`: include the webLinks field.
--
.Request
----
GET /changes/?q=97&o=CURRENT_REVISION&o=CURRENT_COMMIT&o=CURRENT_FILES&o=DOWNLOAD_COMMANDS HTTP/1.0
@@ -3243,6 +3248,8 @@ link:#file-info[FileInfo] entities.
Actions the caller might be able to perform on this revision. The
information is a map of view name to link:#action-info[ActionInfo]
entities.
|'webLinks' |optional|
Links to patch set in external tools as a list.
|===========================
[[rule-input]]

View File

@@ -49,7 +49,10 @@ public enum ListChangesOption {
DRAFT_COMMENTS(12),
/** Include download commands for the caller. */
DOWNLOAD_COMMANDS(13);
DOWNLOAD_COMMANDS(13),
/** Include patch set weblinks. */
WEB_LINKS(14);
private final int value;

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.extensions.common;
import java.util.List;
import java.util.Map;
public class RevisionInfo {
@@ -25,4 +26,15 @@ public class RevisionInfo {
public CommitInfo commit;
public Map<String, FileInfo> files;
public Map<String, ActionInfo> actions;
public List<WebLinkInfo> webLinks;
public static class WebLinkInfo {
public String linkName;
public String linkUrl;
public WebLinkInfo(String name, String url) {
linkName = name;
linkUrl = url;
}
}
}

View File

@@ -0,0 +1,29 @@
// Copyright (C) 2014 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.google.gerrit.extensions.webui;
import com.google.gerrit.extensions.annotations.ExtensionPoint;
@ExtensionPoint
public interface PatchSetWebLink extends WebLink {
/**
* URL to patch set in external service.
*
* @param projectName Name of the project
* @param commit Commit of the patch set
* @return url to patch set in external service.
*/
String getPatchSetUrl(final String projectName, final String commit);
}

View File

@@ -0,0 +1,26 @@
// Copyright (C) 2014 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.google.gerrit.extensions.webui;
import com.google.gerrit.extensions.annotations.ExtensionPoint;
public interface WebLink {
/**
* The link-name displayed in UI.
*
* @return name of link.
*/
String getLinkName();
}

View File

@@ -202,7 +202,8 @@ public class ChangeScreen2 extends Screen {
RestApi call = ChangeApi.detail(changeId.get());
ChangeList.addOptions(call, EnumSet.of(
ListChangesOption.CURRENT_ACTIONS,
ListChangesOption.ALL_REVISIONS));
ListChangesOption.ALL_REVISIONS,
ListChangesOption.WEB_LINKS));
if (!fg) {
call.background();
}

View File

@@ -21,24 +21,27 @@ import com.google.gerrit.client.changes.ChangeInfo;
import com.google.gerrit.client.changes.ChangeInfo.CommitInfo;
import com.google.gerrit.client.changes.ChangeInfo.GitPerson;
import com.google.gerrit.client.changes.ChangeInfo.RevisionInfo;
import com.google.gerrit.client.changes.ChangeInfo.WebLinkInfo;
import com.google.gerrit.client.rpc.Natives;
import com.google.gerrit.client.ui.CommentLinkProcessor;
import com.google.gerrit.client.ui.InlineHyperlink;
import com.google.gerrit.common.PageLinks;
import com.google.gerrit.reviewdb.client.Change.Status;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.AnchorElement;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.TableCellElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwtexpui.clippy.client.CopyableLabel;
import com.google.gwtexpui.safehtml.client.SafeHtmlBuilder;
@@ -53,7 +56,7 @@ class CommitBox extends Composite {
@UiField Style style;
@UiField CopyableLabel commitName;
@UiField AnchorElement browserLink;
@UiField TableCellElement webLinkCell;
@UiField InlineHyperlink authorNameEmail;
@UiField Element authorDate;
@UiField InlineHyperlink committerNameEmail;
@@ -100,14 +103,31 @@ class CommitBox extends Composite {
committerDate, change.status());
text.setHTML(commentLinkProcessor.apply(
new SafeHtmlBuilder().append(commit.message()).linkify()));
setWebLinks(change, revision, revInfo);
}
private void setWebLinks(ChangeInfo change, String revision,
RevisionInfo revInfo) {
GitwebLink gw = Gerrit.getGitwebLink();
if (gw != null && gw.canLink(revInfo)) {
browserLink.setInnerText(gw.getLinkName());
browserLink.setHref(gw.toRevision(change.project(), revision));
} else {
UIObject.setVisible(browserLink, false);
addWebLink(gw.toRevision(change.project(), revision), gw.getLinkName());
}
JsArray<WebLinkInfo> links = revInfo.web_links();
if (links != null) {
for (WebLinkInfo link : Natives.asList(links)) {
addWebLink(link.link_url(), link.link_name());
}
}
}
private void addWebLink(String href, String name) {
Anchor a = new Anchor();
a.setHref(href);
a.setText(parenthesize(name));
Element el = a.getElement();
webLinkCell.appendChild(el);
}
private static void formatLink(GitPerson person, InlineHyperlink name,
@@ -118,6 +138,14 @@ class CommitBox extends Composite {
date.setInnerText(FormatUtil.mediumFormat(person.date()));
}
private static String parenthesize(String str) {
return new StringBuilder()
.append("(")
.append(str)
.append(")")
.toString();
}
private static String renderName(GitPerson person) {
return person.name() + " <" + person.email() + ">";
}

View File

@@ -79,6 +79,10 @@ limitations under the License.
top: 0px;
right: -16px;
}
<!-- To make room for the copyableLabel from the adjacent column -->
.webLinkCell a:first-child {
margin-left:16px;
}
</ui:style>
<g:HTMLPanel>
<g:ScrollPanel styleName='{style.scroll}' ui:field='scroll'>
@@ -114,7 +118,7 @@ limitations under the License.
<tr>
<th><ui:msg>Commit</ui:msg></th>
<td><clippy:CopyableLabel styleName='{style.clippy}' ui:field='commitName'/></td>
<td><a style="margin-left:16px;" ui:field='browserLink' href=""/></td>
<td ui:field='webLinkCell' class='{style.webLinkCell}'></td>
</tr>
<tr>
<th><ui:msg>Change-Id</ui:msg></th>

View File

@@ -218,6 +218,7 @@ public class ChangeInfo extends JavaScriptObject {
public final native boolean has_fetch() /*-{ return this.hasOwnProperty('fetch') }-*/;
public final native NativeMap<FetchInfo> fetch() /*-{ return this.fetch; }-*/;
public final native JsArray<WebLinkInfo> web_links() /*-{ return this.web_links; }-*/;
public static void sortRevisionInfoByNumber(JsArray<RevisionInfo> list) {
Collections.sort(Natives.asList(list), new Comparator<RevisionInfo>() {
@@ -296,4 +297,11 @@ public class ChangeInfo extends JavaScriptObject {
protected IncludedInInfo() {
}
}
public static class WebLinkInfo extends JavaScriptObject {
public final native String link_name() /*-{ return this.link_name; }-*/;
public final native String link_url() /*-{ return this.link_url; }-*/;
protected WebLinkInfo() {
}
}
}

View File

@@ -0,0 +1,51 @@
// Copyright (C) 2014 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.google.gerrit.server;
import com.google.common.collect.Lists;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.extensions.webui.PatchSetWebLink;
import com.google.inject.Inject;
import java.util.List;
public class WebLinks {
private DynamicSet<PatchSetWebLink> patchSetLinks;
@Inject
public WebLinks(final DynamicSet<PatchSetWebLink> patchSetLinks) {
this.patchSetLinks = patchSetLinks;
}
public Iterable<Link> getPatchSetLinks(final String project,
final String commit) {
List<Link> links = Lists.newArrayList();
for (PatchSetWebLink webLink : patchSetLinks) {
links.add(new Link(webLink.getLinkName(),
webLink.getPatchSetUrl(project, commit)));
}
return links;
}
public class Link {
public String name;
public String url;
public Link(String name, String url) {
this.name = name;
this.url = url;
}
}
}

View File

@@ -0,0 +1,35 @@
// Copyright (C) 2014 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.google.gerrit.server;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.extensions.webui.PatchSetWebLink;
import com.google.inject.Inject;
import com.google.inject.Provider;
public class WebLinksProvider implements Provider<WebLinks> {
private DynamicSet<PatchSetWebLink> patchSetLinks;
@Inject
public WebLinksProvider(DynamicSet<PatchSetWebLink> patchSetLinks) {
this.patchSetLinks = patchSetLinks;
}
@Override
public WebLinks get() {
WebLinks webLinks = new WebLinks(patchSetLinks);
return webLinks;
}
}

View File

@@ -28,6 +28,7 @@ import static com.google.gerrit.extensions.common.ListChangesOption.DRAFT_COMMEN
import static com.google.gerrit.extensions.common.ListChangesOption.LABELS;
import static com.google.gerrit.extensions.common.ListChangesOption.MESSAGES;
import static com.google.gerrit.extensions.common.ListChangesOption.REVIEWED;
import static com.google.gerrit.extensions.common.ListChangesOption.WEB_LINKS;
import com.google.common.base.Joiner;
import com.google.common.base.Objects;
@@ -79,6 +80,7 @@ import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.AnonymousUser;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.WebLinks;
import com.google.gerrit.server.account.AccountInfo;
import com.google.gerrit.server.extensions.webui.UiActions;
import com.google.gerrit.server.git.LabelNormalizer;
@@ -152,6 +154,7 @@ public class ChangeJson {
private ChangeControl lastControl;
private Set<Change.Id> reviewed;
private LoadingCache<Project.NameKey, ProjectControl> projectControls;
private Provider<WebLinks> webLinks;
@Inject
ChangeJson(
@@ -169,7 +172,8 @@ public class ChangeJson {
DynamicMap<DownloadScheme> downloadSchemes,
DynamicMap<DownloadCommand> downloadCommands,
DynamicMap<RestView<ChangeResource>> changeViews,
Revisions revisions) {
Revisions revisions,
Provider<WebLinks> webLinks) {
this.db = db;
this.labelNormalizer = ln;
this.userProvider = user;
@@ -185,6 +189,7 @@ public class ChangeJson {
this.downloadCommands = downloadCommands;
this.changeViews = changeViews;
this.revisions = revisions;
this.webLinks = webLinks;
options = EnumSet.noneOf(ListChangesOption.class);
projectControls = CacheBuilder.newBuilder()
@@ -328,7 +333,7 @@ public class ChangeJson {
if (has(ALL_REVISIONS)
|| has(CURRENT_REVISION)
|| limitToPsId.isPresent()) {
out.revisions = revisions(cd, limitToPsId);
out.revisions = revisions(cd, limitToPsId, out.project);
if (out.revisions != null) {
for (Map.Entry<String, RevisionInfo> entry : out.revisions.entrySet()) {
if (entry.getValue().isCurrent) {
@@ -790,7 +795,7 @@ public class ChangeJson {
}
private Map<String, RevisionInfo> revisions(ChangeData cd,
Optional<PatchSet.Id> limitToPsId) throws OrmException {
Optional<PatchSet.Id> limitToPsId, String project) throws OrmException {
ChangeControl ctl = control(cd);
if (ctl == null) {
return null;
@@ -819,13 +824,13 @@ public class ChangeJson {
Map<String, RevisionInfo> res = Maps.newLinkedHashMap();
for (PatchSet in : src) {
if (ctl.isPatchVisible(in, db.get())) {
res.put(in.getRevision().get(), toRevisionInfo(cd, in));
res.put(in.getRevision().get(), toRevisionInfo(cd, in, project));
}
}
return res;
}
private RevisionInfo toRevisionInfo(ChangeData cd, PatchSet in)
private RevisionInfo toRevisionInfo(ChangeData cd, PatchSet in, String project)
throws OrmException {
RevisionInfo out = new RevisionInfo();
out.isCurrent = in.getId().equals(cd.change().currentPatchSetId());
@@ -873,6 +878,13 @@ public class ChangeJson {
: null;
}
if (has(WEB_LINKS)) {
out.webLinks = Lists.newArrayList();
for (WebLinks.Link link : webLinks.get().getPatchSetLinks(
project, in.getRevision().get())) {
out.webLinks.add(new RevisionInfo.WebLinkInfo(link.name, link.url));
}
}
return out;
}
@@ -885,7 +897,6 @@ public class ChangeJson {
commit.committer = toGitPerson(info.getCommitter());
commit.subject = info.getSubject();
commit.message = info.getMessage();
for (ParentInfo parent : info.getParents()) {
CommitInfo i = new CommitInfo();
i.commit = parent.id.get();

View File

@@ -31,6 +31,7 @@ import com.google.gerrit.extensions.registration.DynamicItem;
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.extensions.systemstatus.MessageOfTheDay;
import com.google.gerrit.extensions.webui.PatchSetWebLink;
import com.google.gerrit.extensions.webui.TopMenu;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.rules.PrologModule;
@@ -42,6 +43,8 @@ import com.google.gerrit.server.FileTypeRegistry;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.MimeUtilFileTypeRegistry;
import com.google.gerrit.server.PluginUser;
import com.google.gerrit.server.WebLinks;
import com.google.gerrit.server.WebLinksProvider;
import com.google.gerrit.server.account.AccountByEmailCacheImpl;
import com.google.gerrit.server.account.AccountCacheImpl;
import com.google.gerrit.server.account.AccountControl;
@@ -224,6 +227,7 @@ public class GerritGlobalModule extends FactoryModule {
.in(SINGLETON);
bind(FromAddressGenerator.class).toProvider(
FromAddressGeneratorProvider.class).in(SINGLETON);
bind(WebLinks.class).toProvider(WebLinksProvider.class).in(SINGLETON);
bind(PatchSetInfoFactory.class);
bind(IdentifiedUser.GenericFactory.class).in(SINGLETON);
@@ -266,6 +270,7 @@ public class GerritGlobalModule extends FactoryModule {
DynamicMap.mapOf(binder(), DownloadScheme.class);
DynamicMap.mapOf(binder(), DownloadCommand.class);
DynamicMap.mapOf(binder(), ProjectConfigEntry.class);
DynamicSet.setOf(binder(), PatchSetWebLink.class);
bind(AnonymousUser.class);