Add support for diff web links
Plugins can now add web links to the diff screen that will be displayed next to the navigation icons. Change-Id: Ifa01103054c3e81e3121c219ee5b69db2fefb3bf Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
@@ -18,9 +18,11 @@ import com.google.common.base.Function;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.FluentIterable;
|
||||
import com.google.gerrit.extensions.common.DiffWebLinkInfo;
|
||||
import com.google.gerrit.extensions.common.WebLinkInfo;
|
||||
import com.google.gerrit.extensions.registration.DynamicSet;
|
||||
import com.google.gerrit.extensions.webui.BranchWebLink;
|
||||
import com.google.gerrit.extensions.webui.DiffWebLink;
|
||||
import com.google.gerrit.extensions.webui.FileWebLink;
|
||||
import com.google.gerrit.extensions.webui.PatchSetWebLink;
|
||||
import com.google.gerrit.extensions.webui.ProjectWebLink;
|
||||
@@ -53,16 +55,19 @@ public class WebLinks {
|
||||
|
||||
private final DynamicSet<PatchSetWebLink> patchSetLinks;
|
||||
private final DynamicSet<FileWebLink> fileLinks;
|
||||
private final DynamicSet<DiffWebLink> diffLinks;
|
||||
private final DynamicSet<ProjectWebLink> projectLinks;
|
||||
private final DynamicSet<BranchWebLink> branchLinks;
|
||||
|
||||
@Inject
|
||||
public WebLinks(DynamicSet<PatchSetWebLink> patchSetLinks,
|
||||
DynamicSet<FileWebLink> fileLinks,
|
||||
DynamicSet<DiffWebLink> diffLinks,
|
||||
DynamicSet<ProjectWebLink> projectLinks,
|
||||
DynamicSet<BranchWebLink> branchLinks) {
|
||||
this.patchSetLinks = patchSetLinks;
|
||||
this.fileLinks = fileLinks;
|
||||
this.diffLinks = diffLinks;
|
||||
this.projectLinks = projectLinks;
|
||||
this.branchLinks = branchLinks;
|
||||
}
|
||||
@@ -102,6 +107,34 @@ public class WebLinks {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param project Project name.
|
||||
* @param patchSetIdA Patch set ID of side A, <code>null</code> if no base
|
||||
* patch set was selected.
|
||||
* @param revisionA SHA1 of revision of side A.
|
||||
* @param fileA File name of side A.
|
||||
* @param patchSetIdB Patch set ID of side B.
|
||||
* @param revisionB SHA1 of revision of side B.
|
||||
* @param fileB File name of side B.
|
||||
* @return Links for file diffs.
|
||||
*/
|
||||
public FluentIterable<DiffWebLinkInfo> getDiffLinks(final String project, final int changeId,
|
||||
final Integer patchSetIdA, final String revisionA, final String fileA,
|
||||
final int patchSetIdB, final String revisionB, final String fileB) {
|
||||
return FluentIterable
|
||||
.from(diffLinks)
|
||||
.transform(new Function<WebLink, DiffWebLinkInfo>() {
|
||||
@Override
|
||||
public DiffWebLinkInfo apply(WebLink webLink) {
|
||||
return ((DiffWebLink) webLink).getDiffLink(project, changeId,
|
||||
patchSetIdA, revisionA, fileA,
|
||||
patchSetIdB, revisionB, fileB);
|
||||
}
|
||||
})
|
||||
.filter(INVALID_WEBLINK);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param project Project name.
|
||||
|
||||
@@ -23,6 +23,7 @@ import com.google.common.collect.Lists;
|
||||
import com.google.gerrit.common.data.PatchScript;
|
||||
import com.google.gerrit.common.data.PatchScript.DisplayMethod;
|
||||
import com.google.gerrit.common.data.PatchScript.FileMode;
|
||||
import com.google.gerrit.extensions.common.DiffWebLinkInfo;
|
||||
import com.google.gerrit.extensions.common.WebLinkInfo;
|
||||
import com.google.gerrit.extensions.restapi.AuthException;
|
||||
import com.google.gerrit.extensions.restapi.CacheControl;
|
||||
@@ -81,6 +82,9 @@ public class GetDiff implements RestReadView<FileResource> {
|
||||
@Option(name = "--intraline")
|
||||
boolean intraline;
|
||||
|
||||
@Option(name = "--weblinks-only")
|
||||
boolean webLinksOnly;
|
||||
|
||||
@Inject
|
||||
GetDiff(ProjectCache projectCache,
|
||||
PatchScriptFactory.Factory patchScriptFactoryFactory,
|
||||
@@ -148,49 +152,63 @@ public class GetDiff implements RestReadView<FileResource> {
|
||||
projectCache.get(resource.getRevision().getChange().getProject());
|
||||
|
||||
Result result = new Result();
|
||||
if (ps.getDisplayMethodA() != DisplayMethod.NONE) {
|
||||
result.metaA = new FileMeta();
|
||||
result.metaA.name = MoreObjects.firstNonNull(ps.getOldName(),
|
||||
ps.getNewName());
|
||||
setContentType(result.metaA, state, ps.getFileModeA(), ps.getMimeTypeA());
|
||||
result.metaA.lines = ps.getA().size();
|
||||
// TODO referring to the parent commit by refs/changes/12/60012/1^1
|
||||
// will likely not work for inline edits
|
||||
String revA = basePatchSet != null
|
||||
? basePatchSet.getRefName()
|
||||
: resource.getRevision().getPatchSet().getRefName() + "^1";
|
||||
String revB = resource.getRevision().getEdit().isPresent()
|
||||
? resource.getRevision().getEdit().get().getRefName()
|
||||
: resource.getRevision().getPatchSet().getRefName();
|
||||
|
||||
// TODO referring to the parent commit by refs/changes/12/60012/1^1
|
||||
// will likely not work for inline edits
|
||||
String rev = basePatchSet != null
|
||||
? basePatchSet.getRefName()
|
||||
: resource.getRevision().getPatchSet().getRefName() + "^1";
|
||||
result.metaA.webLinks =
|
||||
getFileWebLinks(state.getProject(), rev, result.metaA.name);
|
||||
}
|
||||
FluentIterable<DiffWebLinkInfo> links =
|
||||
webLinks.getDiffLinks(state.getProject().getName(),
|
||||
resource.getPatchKey().getParentKey().getParentKey().get(),
|
||||
basePatchSet != null ? basePatchSet.getId().get() : null,
|
||||
revA,
|
||||
MoreObjects.firstNonNull(ps.getOldName(), ps.getNewName()),
|
||||
resource.getPatchKey().getParentKey().get(),
|
||||
revB,
|
||||
ps.getNewName());
|
||||
result.webLinks = links.isEmpty() ? null : links.toList();
|
||||
|
||||
if (ps.getDisplayMethodB() != DisplayMethod.NONE) {
|
||||
result.metaB = new FileMeta();
|
||||
result.metaB.name = ps.getNewName();
|
||||
setContentType(result.metaB, state, ps.getFileModeB(), ps.getMimeTypeB());
|
||||
result.metaB.lines = ps.getB().size();
|
||||
String rev = resource.getRevision().getEdit().isPresent()
|
||||
? resource.getRevision().getEdit().get().getRefName()
|
||||
: resource.getRevision().getPatchSet().getRefName();
|
||||
result.metaB.webLinks =
|
||||
getFileWebLinks(state.getProject(), rev, result.metaB.name);
|
||||
}
|
||||
|
||||
if (intraline) {
|
||||
if (ps.hasIntralineTimeout()) {
|
||||
result.intralineStatus = IntraLineStatus.TIMEOUT;
|
||||
} else if (ps.hasIntralineFailure()) {
|
||||
result.intralineStatus = IntraLineStatus.FAILURE;
|
||||
} else {
|
||||
result.intralineStatus = IntraLineStatus.OK;
|
||||
if (!webLinksOnly) {
|
||||
if (ps.getDisplayMethodA() != DisplayMethod.NONE) {
|
||||
result.metaA = new FileMeta();
|
||||
result.metaA.name = MoreObjects.firstNonNull(ps.getOldName(),
|
||||
ps.getNewName());
|
||||
setContentType(result.metaA, state, ps.getFileModeA(), ps.getMimeTypeA());
|
||||
result.metaA.lines = ps.getA().size();
|
||||
result.metaA.webLinks =
|
||||
getFileWebLinks(state.getProject(), revA, result.metaA.name);
|
||||
}
|
||||
|
||||
if (ps.getDisplayMethodB() != DisplayMethod.NONE) {
|
||||
result.metaB = new FileMeta();
|
||||
result.metaB.name = ps.getNewName();
|
||||
setContentType(result.metaB, state, ps.getFileModeB(), ps.getMimeTypeB());
|
||||
result.metaB.lines = ps.getB().size();
|
||||
result.metaB.webLinks =
|
||||
getFileWebLinks(state.getProject(), revB, result.metaB.name);
|
||||
}
|
||||
|
||||
if (intraline) {
|
||||
if (ps.hasIntralineTimeout()) {
|
||||
result.intralineStatus = IntraLineStatus.TIMEOUT;
|
||||
} else if (ps.hasIntralineFailure()) {
|
||||
result.intralineStatus = IntraLineStatus.FAILURE;
|
||||
} else {
|
||||
result.intralineStatus = IntraLineStatus.OK;
|
||||
}
|
||||
}
|
||||
|
||||
result.changeType = ps.getChangeType();
|
||||
if (ps.getPatchHeader().size() > 0) {
|
||||
result.diffHeader = ps.getPatchHeader();
|
||||
}
|
||||
result.content = content.lines;
|
||||
}
|
||||
|
||||
result.changeType = ps.getChangeType();
|
||||
if (ps.getPatchHeader().size() > 0) {
|
||||
result.diffHeader = ps.getPatchHeader();
|
||||
}
|
||||
result.content = content.lines;
|
||||
Response<Result> r = Response.ok(result);
|
||||
if (resource.isCacheable()) {
|
||||
r.caching(CacheControl.PRIVATE(7, TimeUnit.DAYS));
|
||||
@@ -217,6 +235,7 @@ public class GetDiff implements RestReadView<FileResource> {
|
||||
ChangeType changeType;
|
||||
List<String> diffHeader;
|
||||
List<ContentEntry> content;
|
||||
List<DiffWebLinkInfo> webLinks;
|
||||
}
|
||||
|
||||
static class FileMeta {
|
||||
|
||||
@@ -33,6 +33,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.BranchWebLink;
|
||||
import com.google.gerrit.extensions.webui.DiffWebLink;
|
||||
import com.google.gerrit.extensions.webui.FileWebLink;
|
||||
import com.google.gerrit.extensions.webui.PatchSetWebLink;
|
||||
import com.google.gerrit.extensions.webui.ProjectWebLink;
|
||||
@@ -287,6 +288,7 @@ public class GerritGlobalModule extends FactoryModule {
|
||||
DynamicMap.mapOf(binder(), ProjectConfigEntry.class);
|
||||
DynamicSet.setOf(binder(), PatchSetWebLink.class);
|
||||
DynamicSet.setOf(binder(), FileWebLink.class);
|
||||
DynamicSet.setOf(binder(), DiffWebLink.class);
|
||||
DynamicSet.setOf(binder(), ProjectWebLink.class);
|
||||
DynamicSet.setOf(binder(), BranchWebLink.class);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user