Add proto representation for HumanComment along with a converter
Bug: Google b/289357382
Release-Notes: skip
Change-Id: Ib0407a7113e58925bbab30af9adb503b347ef74f
Forward-Compatible: checked
diff --git a/java/com/google/gerrit/entities/Comment.java b/java/com/google/gerrit/entities/Comment.java
index e1e143c..baac18f 100644
--- a/java/com/google/gerrit/entities/Comment.java
+++ b/java/com/google/gerrit/entities/Comment.java
@@ -216,7 +216,7 @@
public int lineNbr;
public Identity author;
- protected Identity realAuthor;
+ public Identity realAuthor;
// TODO(issue-15525): Migrate this field from Timestamp to Instant
public Timestamp writtenOn;
diff --git a/java/com/google/gerrit/entities/converter/HumanCommentProtoConverter.java b/java/com/google/gerrit/entities/converter/HumanCommentProtoConverter.java
new file mode 100644
index 0000000..d14aa97
--- /dev/null
+++ b/java/com/google/gerrit/entities/converter/HumanCommentProtoConverter.java
@@ -0,0 +1,142 @@
+// Copyright (C) 2023 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.entities.converter;
+
+import static com.google.gerrit.entities.Patch.PATCHSET_LEVEL;
+
+import com.google.errorprone.annotations.Immutable;
+import com.google.gerrit.entities.Account;
+import com.google.gerrit.entities.Comment;
+import com.google.gerrit.entities.Comment.Range;
+import com.google.gerrit.entities.HumanComment;
+import com.google.gerrit.proto.Entities;
+import com.google.gerrit.proto.Entities.HumanComment.InFilePosition;
+import com.google.gerrit.proto.Entities.HumanComment.InFilePosition.Side;
+import com.google.protobuf.Parser;
+import java.time.Instant;
+import java.util.Optional;
+import org.eclipse.jgit.lib.ObjectId;
+
+/** Proto converter between {@link HumanComment} and {@link Entities.HumanComment}. */
+@Immutable
+public enum HumanCommentProtoConverter
+ implements ProtoConverter<Entities.HumanComment, HumanComment> {
+ INSTANCE;
+
+ private final ProtoConverter<Entities.Account_Id, Account.Id> accountIdConverter =
+ AccountIdProtoConverter.INSTANCE;
+ private final ProtoConverter<Entities.ObjectId, ObjectId> objectIdConverter =
+ ObjectIdProtoConverter.INSTANCE;
+
+ @Override
+ public Entities.HumanComment toProto(HumanComment val) {
+
+ Entities.HumanComment.Builder res =
+ Entities.HumanComment.newBuilder()
+ .setPatchsetId(val.key.patchSetId)
+ .setAccountId(accountIdConverter.toProto(val.author.getId()))
+ .setCommentUuid(val.key.uuid)
+ .setCommentText(val.message)
+ .setUnresolved(val.unresolved)
+ .setWrittenOnMillis(val.writtenOn.toInstant().toEpochMilli())
+ .setServerId(val.serverId);
+ if (!val.key.filename.equals(PATCHSET_LEVEL)) {
+ InFilePosition.Builder inFilePos =
+ InFilePosition.newBuilder()
+ .setFilePath(val.key.filename)
+ .setSide(val.side <= 0 ? Side.PARENT : Side.REVISION);
+ if (val.range != null) {
+ inFilePos.setPositionRange(
+ InFilePosition.Range.newBuilder()
+ .setStartLine(val.range.startLine)
+ .setStartChar(val.range.startChar)
+ .setEndLine(val.range.endLine)
+ .setEndChar(val.range.endChar));
+ }
+ if (val.lineNbr != 0) {
+ inFilePos.setLineNumber(val.lineNbr);
+ }
+ res.setInFilePosition(inFilePos);
+ }
+
+ if (val.parentUuid != null) {
+ res.setParentCommentUuid(val.parentUuid);
+ }
+ if (val.tag != null) {
+ res.setTag(val.tag);
+ }
+ if (val.realAuthor != null) {
+ res.setRealAuthor(accountIdConverter.toProto(val.realAuthor.getId()));
+ }
+ if (val.getCommitId() != null) {
+ res.setDestCommitId(objectIdConverter.toProto(val.getCommitId()));
+ }
+
+ return res.build();
+ }
+
+ @Override
+ public HumanComment fromProto(Entities.HumanComment proto) {
+ Optional<InFilePosition> optInFilePosition =
+ proto.hasInFilePosition() ? Optional.of(proto.getInFilePosition()) : Optional.empty();
+ Comment.Key key =
+ new Comment.Key(
+ proto.getCommentUuid(),
+ optInFilePosition.isPresent() ? optInFilePosition.get().getFilePath() : PATCHSET_LEVEL,
+ proto.getPatchsetId());
+ HumanComment res =
+ new HumanComment(
+ key,
+ accountIdConverter.fromProto(proto.getAccountId()),
+ Instant.ofEpochMilli(proto.getWrittenOnMillis()),
+ optInFilePosition.isPresent()
+ ? (short) optInFilePosition.get().getSide().getNumber()
+ : Side.REVISION_VALUE,
+ proto.getCommentText(),
+ proto.getServerId(),
+ proto.getUnresolved());
+
+ res.parentUuid = proto.hasParentCommentUuid() ? proto.getParentCommentUuid() : null;
+ res.tag = proto.hasTag() ? proto.getTag() : null;
+ if (proto.hasRealAuthor()) {
+ res.realAuthor = new Comment.Identity(accountIdConverter.fromProto(proto.getRealAuthor()));
+ }
+ if (proto.hasDestCommitId()) {
+ res.setCommitId(objectIdConverter.fromProto(proto.getDestCommitId()));
+ }
+
+ optInFilePosition.ifPresent(
+ inFilePosition -> {
+ if (inFilePosition.hasPositionRange()) {
+ var range = inFilePosition.getPositionRange();
+ res.range =
+ new Range(
+ range.getStartLine(),
+ range.getStartChar(),
+ range.getEndLine(),
+ range.getEndChar());
+ }
+ if (inFilePosition.hasLineNumber()) {
+ res.lineNbr = inFilePosition.getLineNumber();
+ }
+ });
+ return res;
+ }
+
+ @Override
+ public Parser<Entities.HumanComment> getParser() {
+ return Entities.HumanComment.parser();
+ }
+}
diff --git a/javatests/com/google/gerrit/entities/converter/BUILD b/javatests/com/google/gerrit/entities/converter/BUILD
index 6c4d1e4..0ca9478 100644
--- a/javatests/com/google/gerrit/entities/converter/BUILD
+++ b/javatests/com/google/gerrit/entities/converter/BUILD
@@ -5,6 +5,7 @@
srcs = glob(["*.java"]),
deps = [
"//java/com/google/gerrit/entities",
+ "//java/com/google/gerrit/extensions:api",
"//java/com/google/gerrit/proto/testing",
"//java/com/google/gerrit/server",
"//lib:guava",
diff --git a/javatests/com/google/gerrit/entities/converter/HumanCommentProtoConverterTest.java b/javatests/com/google/gerrit/entities/converter/HumanCommentProtoConverterTest.java
new file mode 100644
index 0000000..a6aaf36
--- /dev/null
+++ b/javatests/com/google/gerrit/entities/converter/HumanCommentProtoConverterTest.java
@@ -0,0 +1,130 @@
+// Copyright (C) 2023 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.entities.converter;
+
+import static com.google.common.truth.Truth.assertThat;
+import static com.google.gerrit.entities.Patch.PATCHSET_LEVEL;
+
+import com.google.gerrit.entities.Account;
+import com.google.gerrit.entities.Comment;
+import com.google.gerrit.entities.CommentRange;
+import com.google.gerrit.entities.HumanComment;
+import java.time.Instant;
+import org.eclipse.jgit.lib.ObjectId;
+import org.junit.Test;
+
+public class HumanCommentProtoConverterTest {
+ private static final ObjectId VALID_OBJECT_ID =
+ ObjectId.fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef");
+
+ private final HumanCommentProtoConverter converter = HumanCommentProtoConverter.INSTANCE;
+
+ @Test
+ public void fileLevelCommentWithAllOptionalFields() {
+ HumanComment orig =
+ new HumanComment(
+ new Comment.Key("uuid", "a.txt", 42),
+ Account.id(314),
+ Instant.ofEpochMilli(12345),
+ (short) 1,
+ "message",
+ "server",
+ /* unresolved= */ true);
+ orig.tag = "tag";
+ orig.setCommitId(VALID_OBJECT_ID);
+ orig.setRealAuthor(Account.id(271));
+
+ HumanComment res = converter.fromProto(converter.toProto(orig));
+
+ assertThat(res).isEqualTo(orig);
+ }
+
+ @Test
+ public void patchsetLevelComment() {
+ HumanComment orig =
+ new HumanComment(
+ new Comment.Key("uuid", PATCHSET_LEVEL, 42),
+ Account.id(314),
+ Instant.ofEpochMilli(12345),
+ (short) 1,
+ "message",
+ "server",
+ /* unresolved= */ false);
+
+ HumanComment res = converter.fromProto(converter.toProto(orig));
+
+ assertThat(res).isEqualTo(orig);
+ }
+
+ @Test
+ public void lineComment() {
+ HumanComment orig =
+ new HumanComment(
+ new Comment.Key("uuid", "a.txt", 42),
+ Account.id(314),
+ Instant.ofEpochMilli(12345),
+ (short) 1,
+ "message",
+ "server",
+ /* unresolved= */ true);
+ orig.setLineNbrAndRange(7, null);
+
+ HumanComment res = converter.fromProto(converter.toProto(orig));
+
+ assertThat(res).isEqualTo(orig);
+ }
+
+ @Test
+ public void rangeComment() {
+ HumanComment orig =
+ new HumanComment(
+ new Comment.Key("uuid", "a.txt", 42),
+ Account.id(314),
+ Instant.ofEpochMilli(12345),
+ (short) 1,
+ "message",
+ "server",
+ /* unresolved= */ true);
+ orig.setRange(new CommentRange(2, 3, 5, 7));
+
+ HumanComment res = converter.fromProto(converter.toProto(orig));
+
+ assertThat(res).isEqualTo(orig);
+ }
+
+ @Test
+ public void extensionRangeComment() {
+ HumanComment orig =
+ new HumanComment(
+ new Comment.Key("uuid", "a.txt", 42),
+ Account.id(314),
+ Instant.ofEpochMilli(12345),
+ (short) 1,
+ "message",
+ "server",
+ /* unresolved= */ false);
+ com.google.gerrit.extensions.client.Comment.Range range =
+ new com.google.gerrit.extensions.client.Comment.Range();
+ range.startLine = 2;
+ range.startCharacter = 3;
+ range.endLine = 5;
+ range.endCharacter = 7;
+ orig.setLineNbrAndRange(null, range);
+
+ HumanComment res = converter.fromProto(converter.toProto(orig));
+
+ assertThat(res).isEqualTo(orig);
+ }
+}
diff --git a/proto/entities.proto b/proto/entities.proto
index 3412291..335db62 100644
--- a/proto/entities.proto
+++ b/proto/entities.proto
@@ -311,3 +311,64 @@
}
optional EditPreferencesInfo edit_preferences_info = 3;
}
+
+// Next Id: 13
+message HumanComment {
+ // Required. Note that the equivalent Java struct does not contain the change
+ // ID, so we keep the same format here.
+ optional int32 patchset_id = 1;
+ optional ObjectId dest_commit_id = 2;
+ // Required.
+ optional Account_Id account_id = 3;
+ optional Account_Id real_author = 4;
+
+ // Next Id: 5
+ message InFilePosition {
+ optional string file_path = 1;
+ enum Side {
+ // Should match the logic in
+ // http://google3/third_party/java_src/gerritcodereview/gerrit/java/com/google/gerrit/extensions/client/Side.java?rcl=579772037&l=24
+ PARENT = 0;
+ REVISION = 1;
+ }
+ // Default should match
+ // http://google3/third_party/java_src/gerritcodereview/gerrit/Documentation/rest-api-changes.txt?l=7423
+ optional Side side = 2 [default = REVISION];
+ message Range {
+ // 1-based
+ optional int32 start_line = 1;
+ // 0-based
+ optional int32 start_char = 2;
+ // 1-based
+ optional int32 end_line = 3;
+ // 0-based
+ optional int32 end_char = 4;
+ }
+ // If neither range nor line number set, the comment is on the file level. It is possible
+ // (though not required) for both values to be set. in this case, it is expected that the line
+ // number is identical to the range's end line.
+ optional Range position_range = 3;
+ // 1-based
+ optional int32 line_number = 4;
+ }
+
+ // If not set, the comment is on the patchset level.
+ optional InFilePosition in_file_position = 5;
+
+ // Required.
+ optional string comment_text = 6;
+ // Might be set by the user while creating the draft.
+ // See http://go/gerrit-rest-api-change#comment-info.
+ optional string tag = 7;
+ optional bool unresolved = 8 [default = false];
+
+ // Required.
+ optional string comment_uuid = 9;
+ // Required.
+ optional string parent_comment_uuid = 10;
+
+ // Required. Epoch millis.
+ optional fixed64 written_on_millis = 11;
+ // Required.
+ optional string server_id = 12;
+}
\ No newline at end of file