summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJacek Centkowski <jcentkowski@collab.net>2017-09-06 18:52:11 +0200
committerDavid Pursehouse <dpursehouse@collab.net>2018-07-06 14:37:55 +0900
commit1b27d904dee1d64e042fd4cbb160830aa9940b68 (patch)
treed9693af22d4a642162be5fac9e714c3fc3a9cb47
parent8ed34782356d5bb1c7d27c46187d793401130005 (diff)
Introduce Change Report formatter extension point
When change is pushed for review typical response looks like: remote: New Changes: remote: http://[server]/gerrit/[change-id] [change subject] It is not sufficient for sites that serve Gerrit as a service that is integrated under unified UI (with unified Code Browser). In such environment links to Gerrit are no longer valid and it should be possible to alter them with desired format. This patch introduces ChangeReportFormatter that can be replaced with instance loaded from plugin (DynamicItem pattern) with default implementation that preserves current behavior. Change-Id: Ia2de44c121b6a73fcf93e1aaf11690697adb9abf Signed-off-by: Jacek Centkowski <jcentkowski@collab.net> Signed-off-by: David Pursehouse <dpursehouse@collab.net>
-rw-r--r--gerrit-server/src/main/java/com/google/gerrit/server/ChangeUtil.java5
-rw-r--r--gerrit-server/src/main/java/com/google/gerrit/server/git/ChangeReportFormatter.java67
-rw-r--r--gerrit-server/src/main/java/com/google/gerrit/server/git/DefaultChangeReportFormatter.java60
-rw-r--r--gerrit-server/src/main/java/com/google/gerrit/server/git/GitModule.java3
-rw-r--r--gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java52
5 files changed, 149 insertions, 38 deletions
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/ChangeUtil.java b/gerrit-server/src/main/java/com/google/gerrit/server/ChangeUtil.java
index 10ae60c747..2859949ad7 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/ChangeUtil.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/ChangeUtil.java
@@ -18,6 +18,7 @@ import static java.util.Comparator.comparingInt;
import com.google.common.collect.Ordering;
import com.google.common.io.BaseEncoding;
+import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.inject.Singleton;
import java.io.IOException;
@@ -40,6 +41,10 @@ public class ChangeUtil {
public static final Ordering<PatchSet> PS_ID_ORDER =
Ordering.from(comparingInt(PatchSet::getPatchSetId));
+ public static String formatChangeUrl(String canonicalWebUrl, Change change) {
+ return canonicalWebUrl + change.getChangeId();
+ }
+
/** @return a new unique identifier for change message entities. */
public static String messageUuid() {
byte[] buf = new byte[8];
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/git/ChangeReportFormatter.java b/gerrit-server/src/main/java/com/google/gerrit/server/git/ChangeReportFormatter.java
new file mode 100644
index 0000000000..bc478ee0db
--- /dev/null
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/git/ChangeReportFormatter.java
@@ -0,0 +1,67 @@
+// Copyright (C) 2017 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.git;
+
+import com.google.gerrit.reviewdb.client.Change;
+
+public interface ChangeReportFormatter {
+ public static class Input {
+ private final Change change;
+ private String subject;
+ private Boolean draft;
+ private Boolean edit;
+
+ public Input(Change change) {
+ this.change = change;
+ }
+
+ public Input setDraft(boolean draft) {
+ this.draft = draft;
+ return this;
+ }
+
+ public Input setEdit(boolean edit) {
+ this.edit = edit;
+ return this;
+ }
+
+ public Input setSubject(String subject) {
+ this.subject = subject;
+ return this;
+ }
+
+ public Change getChange() {
+ return change;
+ }
+
+ public String getSubject() {
+ return subject == null ? change.getSubject() : subject;
+ }
+
+ public boolean isDraft() {
+ return draft == null ? Change.Status.DRAFT == change.getStatus() : draft;
+ }
+
+ public boolean isEdit() {
+ return edit == null ? false : edit;
+ }
+ }
+
+ String newChange(Input input);
+
+ String changeUpdated(Input input);
+
+ String changeClosed(Input input);
+}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/git/DefaultChangeReportFormatter.java b/gerrit-server/src/main/java/com/google/gerrit/server/git/DefaultChangeReportFormatter.java
new file mode 100644
index 0000000000..9ad3f0ad88
--- /dev/null
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/git/DefaultChangeReportFormatter.java
@@ -0,0 +1,60 @@
+// Copyright (C) 2017 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.git;
+
+import com.google.gerrit.server.ChangeUtil;
+import com.google.gerrit.server.config.CanonicalWebUrl;
+import com.google.inject.Inject;
+
+public class DefaultChangeReportFormatter implements ChangeReportFormatter {
+ private final String canonicalWebUrl;
+
+ @Inject
+ DefaultChangeReportFormatter(@CanonicalWebUrl String canonicalWebUrl) {
+ this.canonicalWebUrl = canonicalWebUrl;
+ }
+
+ @Override
+ public String newChange(ChangeReportFormatter.Input input) {
+ return formatChangeUrl(canonicalWebUrl, input);
+ }
+
+ @Override
+ public String changeUpdated(ChangeReportFormatter.Input input) {
+ return formatChangeUrl(canonicalWebUrl, input);
+ }
+
+ @Override
+ public String changeClosed(ChangeReportFormatter.Input input) {
+ return String.format(
+ "change %s closed", ChangeUtil.formatChangeUrl(canonicalWebUrl, input.getChange()));
+ }
+
+ private String formatChangeUrl(String url, Input input) {
+ StringBuilder m =
+ new StringBuilder()
+ .append(" ")
+ .append(ChangeUtil.formatChangeUrl(url, input.getChange()))
+ .append(" ")
+ .append(ChangeUtil.cropSubject(input.getSubject()));
+ if (input.isDraft()) {
+ m.append(" [DRAFT]");
+ }
+ if (input.isEdit()) {
+ m.append(" [EDIT]");
+ }
+ return m.toString();
+ }
+}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/git/GitModule.java b/gerrit-server/src/main/java/com/google/gerrit/server/git/GitModule.java
index 36805d676f..03910eba39 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/git/GitModule.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/git/GitModule.java
@@ -15,6 +15,7 @@
package com.google.gerrit.server.git;
import com.google.gerrit.extensions.config.FactoryModule;
+import com.google.gerrit.extensions.registration.DynamicItem;
import com.google.gerrit.extensions.registration.DynamicSet;
import org.eclipse.jgit.transport.PostUploadHook;
@@ -27,5 +28,7 @@ public class GitModule extends FactoryModule {
bind(MetaDataUpdate.Server.class);
bind(ReceiveConfig.class);
DynamicSet.bind(binder(), PostUploadHook.class).to(UploadPackMetricsHook.class);
+ DynamicItem.itemOf(binder(), ChangeReportFormatter.class);
+ DynamicItem.bind(binder(), ChangeReportFormatter.class).to(DefaultChangeReportFormatter.class);
}
}
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java b/gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java
index 47b9960108..4900e18e1c 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/git/ReceiveCommits.java
@@ -59,6 +59,7 @@ import com.google.gerrit.extensions.api.changes.NotifyHandling;
import com.google.gerrit.extensions.api.changes.RecipientType;
import com.google.gerrit.extensions.api.changes.SubmitInput;
import com.google.gerrit.extensions.api.projects.ProjectConfigEntryType;
+import com.google.gerrit.extensions.registration.DynamicItem;
import com.google.gerrit.extensions.registration.DynamicMap;
import com.google.gerrit.extensions.registration.DynamicMap.Entry;
import com.google.gerrit.extensions.registration.DynamicSet;
@@ -86,7 +87,6 @@ import com.google.gerrit.server.account.AccountResolver;
import com.google.gerrit.server.change.ChangeInserter;
import com.google.gerrit.server.change.SetHashtagsOp;
import com.google.gerrit.server.config.AllProjectsName;
-import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.gerrit.server.config.PluginConfig;
import com.google.gerrit.server.config.ProjectConfigEntry;
import com.google.gerrit.server.edit.ChangeEdit;
@@ -299,7 +299,6 @@ public class ReceiveCommits {
private final PatchSetInfoFactory patchSetInfoFactory;
private final PatchSetUtil psUtil;
private final ProjectCache projectCache;
- private final String canonicalWebUrl;
private final CommitValidators.Factory commitValidatorsFactory;
private final RefOperationValidators.Factory refValidatorsFactory;
private final TagCache tagCache;
@@ -351,6 +350,7 @@ public class ReceiveCommits {
private Task commandProgress;
private MessageSender messageSender;
private BatchRefUpdate batch;
+ private final ChangeReportFormatter changeFormatter;
@Inject
ReceiveCommits(
@@ -370,7 +370,6 @@ public class ReceiveCommits {
ChangeInserter.Factory changeInserterFactory,
CommitValidators.Factory commitValidatorsFactory,
RefOperationValidators.Factory refValidatorsFactory,
- @CanonicalWebUrl String canonicalWebUrl,
RequestScopePropagator requestScopePropagator,
SshInfo sshInfo,
AllProjectsName allProjectsName,
@@ -390,7 +389,8 @@ public class ReceiveCommits {
BatchUpdate.Factory batchUpdateFactory,
SetHashtagsOp.Factory hashtagsFactory,
ReplaceOp.Factory replaceOpFactory,
- MergedByPushOp.Factory mergedByPushOpFactory)
+ MergedByPushOp.Factory mergedByPushOpFactory,
+ DynamicItem<ChangeReportFormatter> changeFormatterProvider)
throws IOException {
this.user = projectControl.getUser().asIdentifiedUser();
this.db = db;
@@ -403,7 +403,6 @@ public class ReceiveCommits {
this.patchSetInfoFactory = patchSetInfoFactory;
this.psUtil = psUtil;
this.projectCache = projectCache;
- this.canonicalWebUrl = canonicalWebUrl;
this.tagCache = tagCache;
this.accountCache = accountCache;
this.changeInserterFactory = changeInserterFactory;
@@ -437,6 +436,7 @@ public class ReceiveCommits {
this.indexer = indexer;
this.messageSender = new ReceivePackMessageSender();
+ this.changeFormatter = changeFormatterProvider.get();
ProjectState ps = projectControl.getProjectState();
@@ -687,13 +687,7 @@ public class ReceiveCommits {
addMessage("");
addMessage("New Changes:");
for (CreateRequest c : created) {
- addMessage(
- formatChangeUrl(
- canonicalWebUrl,
- c.change,
- c.change.getSubject(),
- c.change.getStatus() == Change.Status.DRAFT,
- false));
+ addMessage(changeFormatter.newChange(new ChangeReportFormatter.Input(c.change)));
}
addMessage("");
}
@@ -722,36 +716,18 @@ public class ReceiveCommits {
} else {
subject = u.info.getSubject();
}
- addMessage(
- formatChangeUrl(
- canonicalWebUrl,
- u.notes.getChange(),
- subject,
- u.replaceOp != null && u.replaceOp.getPatchSet().isDraft(),
- edit));
+
+ ChangeReportFormatter.Input input =
+ new ChangeReportFormatter.Input(u.notes.getChange())
+ .setSubject(subject)
+ .setDraft(u.replaceOp != null && u.replaceOp.getPatchSet().isDraft())
+ .setEdit(edit);
+ addMessage(changeFormatter.changeUpdated(input));
}
addMessage("");
}
}
- private static String formatChangeUrl(
- String url, Change change, String subject, boolean draft, boolean edit) {
- StringBuilder m =
- new StringBuilder()
- .append(" ")
- .append(url)
- .append(change.getChangeId())
- .append(" ")
- .append(ChangeUtil.cropSubject(subject));
- if (draft) {
- m.append(" [DRAFT]");
- }
- if (edit) {
- m.append(" [EDIT]");
- }
- return m.toString();
- }
-
private void insertChangesAndPatchSets() {
int replaceCount = 0;
int okToInsert = 0;
@@ -1701,7 +1677,7 @@ public class ReceiveCommits {
private boolean requestReplace(
ReceiveCommand cmd, boolean checkMergedInto, Change change, RevCommit newCommit) {
if (change.getStatus().isClosed()) {
- reject(cmd, "change " + canonicalWebUrl + change.getId() + " closed");
+ reject(cmd, changeFormatter.changeClosed(new ChangeReportFormatter.Input(change)));
return false;
}