Plugin support for project weblinks

Plugins can now contribute project links that will be displayed on the
project list screen in the 'Repository Browser' column.

Change-Id: Ia96679b249dd9677d9138e8af6239e1e5ea6f1d4
This commit is contained in:
Edwin Kempin
2014-04-11 15:56:02 +02:00
parent bd885ffa8c
commit ea0047562f
17 changed files with 187 additions and 47 deletions

View File

@@ -1739,6 +1739,9 @@ public class MyWeblinkPlugin implements PatchSetWebLink {
}
----
ProjectWebLinks will appear in the project list in the
`Repository Browser` column.
[[documentation]]
== Documentation

View File

@@ -1501,6 +1501,9 @@ The name of the parent project. +
is increased for each non-visible project).
|`description` |optional|The description of the project.
|`branches` |optional|Map of branch names to HEAD revisions.
|'web_links' |optional|
Links to the project in external sites as a list of
link:rest-api-changes.html#web-link-info[WebLinkInfo] entries.
|===========================
[[project-input]]

View File

@@ -16,6 +16,7 @@ package com.google.gerrit.extensions.common;
import com.google.gerrit.extensions.api.projects.ProjectState;
import java.util.List;
import java.util.Map;
public class ProjectInfo {
@@ -25,4 +26,5 @@ public class ProjectInfo {
public String description;
public ProjectState state;
public Map<String, String> branches;
public List<WebLinkInfo> webLinks;
}

View File

@@ -27,14 +27,4 @@ public class RevisionInfo {
public Map<String, FileInfo> files;
public Map<String, ActionInfo> actions;
public List<WebLinkInfo> webLinks;
public static class WebLinkInfo {
public String name;
public String url;
public WebLinkInfo(String name, String url) {
this.name = name;
this.url = url;
}
}
}

View File

@@ -0,0 +1,25 @@
// 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.common;
public class WebLinkInfo {
public String name;
public String url;
public WebLinkInfo(String name, String url) {
this.name = name;
this.url = 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 ProjectWebLink extends WebLink {
/**
* URL to project in external service.
*
* @param projectName Name of the project
* @return url to project in external service.
*/
String getProjectUrl(String projectName);
}

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.client;
import com.google.gwt.core.client.JavaScriptObject;
public class WebLinkInfo extends JavaScriptObject {
public final native String name() /*-{ return this.name; }-*/;
public final native String url() /*-{ return this.url; }-*/;
protected WebLinkInfo() {
}
}

View File

@@ -19,9 +19,11 @@ import static com.google.gerrit.common.PageLinks.ADMIN_PROJECTS;
import com.google.gerrit.client.Dispatcher;
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.GitwebLink;
import com.google.gerrit.client.WebLinkInfo;
import com.google.gerrit.client.projects.ProjectInfo;
import com.google.gerrit.client.projects.ProjectMap;
import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.client.rpc.Natives;
import com.google.gerrit.client.ui.FilteredUserInterface;
import com.google.gerrit.client.ui.HighlightingInlineHyperlink;
import com.google.gerrit.client.ui.Hyperlink;
@@ -43,6 +45,8 @@ import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwtexpui.globalkey.client.NpTextBox;
import java.util.List;
public class ProjectListScreen extends Screen implements FilteredUserInterface {
private Hyperlink prev;
private Hyperlink next;
@@ -167,14 +171,12 @@ public class ProjectListScreen extends Screen implements FilteredUserInterface {
@Override
protected void initColumnHeaders() {
super.initColumnHeaders();
if (Gerrit.getGitwebLink() != null) {
table.setText(0, ProjectsTable.C_REPO_BROWSER,
Util.C.projectRepoBrowser());
table.getFlexCellFormatter().
addStyleName(0, ProjectsTable.C_REPO_BROWSER,
Gerrit.RESOURCES.css().dataHeader());
}
}
@Override
protected void onOpenRow(final int row) {
@@ -188,11 +190,8 @@ public class ProjectListScreen extends Screen implements FilteredUserInterface {
@Override
protected void insert(int row, ProjectInfo k) {
super.insert(row, k);
if (Gerrit.getGitwebLink() != null) {
table.getFlexCellFormatter().
addStyleName(row, ProjectsTable.C_REPO_BROWSER,
Gerrit.RESOURCES.css().dataCell());
}
table.getFlexCellFormatter().addStyleName(row,
ProjectsTable.C_REPO_BROWSER, Gerrit.RESOURCES.css().dataCell());
}
@Override
@@ -219,15 +218,33 @@ public class ProjectListScreen extends Screen implements FilteredUserInterface {
fp.add(new HighlightingInlineHyperlink(k.name(), link(k), subname));
table.setWidget(row, ProjectsTable.C_NAME, fp);
table.setText(row, ProjectsTable.C_DESCRIPTION, k.description());
GitwebLink l = Gerrit.getGitwebLink();
if (l != null) {
table.setWidget(row, ProjectsTable.C_REPO_BROWSER,
new Anchor(l.getLinkName(), false, l.toProject(k
.name_key())));
}
addWebLinks(row, k);
setRowItem(row, k);
}
private void addWebLinks(int row, ProjectInfo k) {
GitwebLink gitWebLink = Gerrit.getGitwebLink();
List<WebLinkInfo> webLinks = Natives.asList(k.web_links());
if (gitWebLink != null || (webLinks != null && !webLinks.isEmpty())) {
FlowPanel p = new FlowPanel();
table.setWidget(row, ProjectsTable.C_REPO_BROWSER, p);
if (gitWebLink != null) {
Anchor a = new Anchor();
a.setText(gitWebLink.getLinkName());
a.setHref(gitWebLink.toProject(k.name_key()));
p.add(a);
}
for (WebLinkInfo weblink : webLinks) {
Anchor a = new Anchor();
a.setText("(" + weblink.name() + ")");
a.setHref(weblink.url());
p.add(a);
}
}
}
};
projects.setSavePointerId(PageLinks.ADMIN_PROJECTS);

View File

@@ -17,11 +17,11 @@ package com.google.gerrit.client.change;
import com.google.gerrit.client.FormatUtil;
import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.GitwebLink;
import com.google.gerrit.client.WebLinkInfo;
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;

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.client.changes;
import com.google.gerrit.client.WebLinkInfo;
import com.google.gerrit.client.account.AccountInfo;
import com.google.gerrit.client.actions.ActionInfo;
import com.google.gerrit.client.diff.FileInfo;
@@ -297,11 +298,4 @@ public class ChangeInfo extends JavaScriptObject {
protected IncludedInInfo() {
}
}
public static class WebLinkInfo extends JavaScriptObject {
public final native String name() /*-{ return this.name; }-*/;
public final native String url() /*-{ return this.url; }-*/;
protected WebLinkInfo() {
}
}
}

View File

@@ -14,9 +14,11 @@
package com.google.gerrit.client.projects;
import com.google.gerrit.client.WebLinkInfo;
import com.google.gerrit.extensions.api.projects.ProjectState;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.user.client.ui.SuggestOracle;
public class ProjectInfo
@@ -28,6 +30,7 @@ public class ProjectInfo
public final native String name() /*-{ return this.name; }-*/;
public final native String description() /*-{ return this.description; }-*/;
public final native JsArray<WebLinkInfo> web_links() /*-{ return this.web_links; }-*/;
public final ProjectState state() {
return ProjectState.valueOf(getStringState());

View File

@@ -11,26 +11,30 @@
// 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.gerrit.extensions.webui.ProjectWebLink;
import com.google.inject.Inject;
import java.util.List;
public class WebLinks {
private DynamicSet<PatchSetWebLink> patchSetLinks;
private final DynamicSet<PatchSetWebLink> patchSetLinks;
private final DynamicSet<ProjectWebLink> projectLinks;
@Inject
public WebLinks(final DynamicSet<PatchSetWebLink> patchSetLinks) {
public WebLinks(DynamicSet<PatchSetWebLink> patchSetLinks,
DynamicSet<ProjectWebLink> projectLinks) {
this.patchSetLinks = patchSetLinks;
this.projectLinks = projectLinks;
}
public Iterable<Link> getPatchSetLinks(final String project,
final String commit) {
public Iterable<Link> getPatchSetLinks(String project, String commit) {
List<Link> links = Lists.newArrayList();
for (PatchSetWebLink webLink : patchSetLinks) {
links.add(new Link(webLink.getLinkName(),
@@ -39,6 +43,15 @@ public class WebLinks {
return links;
}
public Iterable<Link> getProjectLinks(String project) {
List<Link> links = Lists.newArrayList();
for (ProjectWebLink webLink : projectLinks) {
links.add(new Link(webLink.getLinkName(),
webLink.getProjectUrl(project)));
}
return links;
}
public class Link {
public String name;
public String url;

View File

@@ -11,25 +11,30 @@
// 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.gerrit.extensions.webui.ProjectWebLink;
import com.google.inject.Inject;
import com.google.inject.Provider;
public class WebLinksProvider implements Provider<WebLinks> {
private DynamicSet<PatchSetWebLink> patchSetLinks;
private final DynamicSet<PatchSetWebLink> patchSetLinks;
private final DynamicSet<ProjectWebLink> projectLinks;
@Inject
public WebLinksProvider(DynamicSet<PatchSetWebLink> patchSetLinks) {
public WebLinksProvider(DynamicSet<PatchSetWebLink> patchSetLinks,
DynamicSet<ProjectWebLink> projectLinks) {
this.patchSetLinks = patchSetLinks;
this.projectLinks = projectLinks;
}
@Override
public WebLinks get() {
WebLinks webLinks = new WebLinks(patchSetLinks);
WebLinks webLinks = new WebLinks(patchSetLinks, projectLinks);
return webLinks;
}
}

View File

@@ -60,6 +60,7 @@ import com.google.gerrit.extensions.common.FetchInfo;
import com.google.gerrit.extensions.common.GitPerson;
import com.google.gerrit.extensions.common.ListChangesOption;
import com.google.gerrit.extensions.common.RevisionInfo;
import com.google.gerrit.extensions.common.WebLinkInfo;
import com.google.gerrit.extensions.config.DownloadCommand;
import com.google.gerrit.extensions.config.DownloadScheme;
import com.google.gerrit.extensions.registration.DynamicMap;
@@ -882,7 +883,7 @@ public class ChangeJson {
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));
out.webLinks.add(new WebLinkInfo(link.name, link.url));
}
}
return out;

View File

@@ -32,6 +32,7 @@ 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.ProjectWebLink;
import com.google.gerrit.extensions.webui.TopMenu;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.rules.PrologModule;
@@ -271,6 +272,7 @@ public class GerritGlobalModule extends FactoryModule {
DynamicMap.mapOf(binder(), DownloadCommand.class);
DynamicMap.mapOf(binder(), ProjectConfigEntry.class);
DynamicSet.setOf(binder(), PatchSetWebLink.class);
DynamicSet.setOf(binder(), ProjectWebLink.class);
bind(AnonymousUser.class);

View File

@@ -22,6 +22,7 @@ import com.google.common.collect.Maps;
import com.google.gerrit.common.data.GroupReference;
import com.google.gerrit.common.errors.NoSuchGroupException;
import com.google.gerrit.extensions.common.ProjectInfo;
import com.google.gerrit.extensions.common.WebLinkInfo;
import com.google.gerrit.extensions.restapi.BinaryResult;
import com.google.gerrit.extensions.restapi.RestReadView;
import com.google.gerrit.extensions.restapi.TopLevelResource;
@@ -32,12 +33,14 @@ import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.OutputFormat;
import com.google.gerrit.server.StringUtil;
import com.google.gerrit.server.WebLinks;
import com.google.gerrit.server.account.GroupCache;
import com.google.gerrit.server.account.GroupControl;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.util.TreeFormatter;
import com.google.gson.reflect.TypeToken;
import com.google.inject.Inject;
import com.google.inject.Provider;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Constants;
@@ -106,6 +109,7 @@ public class ListProjects implements RestReadView<TopLevelResource> {
private final GroupControl.Factory groupControlFactory;
private final GitRepositoryManager repoManager;
private final ProjectNode.Factory projectNodeFactory;
private final Provider<WebLinks> webLinks;
@Deprecated
@Option(name = "--format", usage = "(deprecated) output format")
@@ -179,13 +183,15 @@ public class ListProjects implements RestReadView<TopLevelResource> {
@Inject
protected ListProjects(CurrentUser currentUser, ProjectCache projectCache,
GroupCache groupCache, GroupControl.Factory groupControlFactory,
GitRepositoryManager repoManager, ProjectNode.Factory projectNodeFactory) {
GitRepositoryManager repoManager, ProjectNode.Factory projectNodeFactory,
Provider<WebLinks> webLinks) {
this.currentUser = currentUser;
this.projectCache = projectCache;
this.groupCache = groupCache;
this.groupControlFactory = groupControlFactory;
this.repoManager = repoManager;
this.projectNodeFactory = projectNodeFactory;
this.webLinks = webLinks;
}
public List<String> getShowBranch() {
@@ -368,6 +374,13 @@ public class ListProjects implements RestReadView<TopLevelResource> {
log.warn("Unexpected error reading " + projectName, err);
continue;
}
info.webLinks = Lists.newArrayList();
for (WebLinks.Link link : webLinks.get().getProjectLinks(projectName.get())) {
if (!Strings.isNullOrEmpty(link.name) && !Strings.isNullOrEmpty(link.url)) {
info.webLinks.add(new WebLinkInfo(link.name, link.url));
}
}
}
if (foundIndex++ < start) {

View File

@@ -15,19 +15,25 @@
package com.google.gerrit.server.project;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.gerrit.extensions.common.ProjectInfo;
import com.google.gerrit.extensions.common.WebLinkInfo;
import com.google.gerrit.extensions.restapi.Url;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.WebLinks;
import com.google.gerrit.server.config.AllProjectsName;
import com.google.inject.Inject;
import com.google.inject.Provider;
public class ProjectJson {
private final AllProjectsName allProjects;
private final Provider<WebLinks> webLinks;
@Inject
ProjectJson(AllProjectsName allProjects) {
ProjectJson(AllProjectsName allProjects, Provider<WebLinks> webLinks) {
this.allProjects = allProjects;
this.webLinks = webLinks;
}
public ProjectInfo format(ProjectResource rsrc) {
@@ -42,6 +48,14 @@ public class ProjectJson {
info.description = Strings.emptyToNull(p.getDescription());
info.state = p.getState();
info.id = Url.encode(info.name);
info.webLinks = Lists.newArrayList();
for (WebLinks.Link link : webLinks.get().getProjectLinks(p.getName())) {
if (!Strings.isNullOrEmpty(link.name) && !Strings.isNullOrEmpty(link.url)) {
info.webLinks.add(new WebLinkInfo(link.name, link.url));
}
}
return info;
}
}