Merge "Add comment count to review notes"
diff --git a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/CreateReviewNotes.java b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/CreateReviewNotes.java
index 7b850ff..abaf4aa 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/CreateReviewNotes.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/CreateReviewNotes.java
@@ -76,6 +76,7 @@
   private final String anonymousCowardName;
   private final LabelTypes labelTypes;
   private final ApprovalsUtil approvalsUtil;
+  private final ChangeData.Factory changeDataFactory;
   private final ChangeNotes.Factory notesFactory;
   private final NotesBranchUtil.Factory notesBranchUtilFactory;
   private final Provider<InternalChangeQuery> queryProvider;
@@ -95,6 +96,7 @@
       @AnonymousCowardName String anonymousCowardName,
       ProjectCache projectCache,
       ApprovalsUtil approvalsUtil,
+      ChangeData.Factory changeDataFactory,
       ChangeNotes.Factory notesFactory,
       NotesBranchUtil.Factory notesBranchUtilFactory,
       Provider<InternalChangeQuery> queryProvider,
@@ -116,6 +118,7 @@
       this.labelTypes = projectState.get().getLabelTypes();
     }
     this.approvalsUtil = approvalsUtil;
+    this.changeDataFactory = changeDataFactory;
     this.notesFactory = notesFactory;
     this.notesBranchUtilFactory = notesBranchUtilFactory;
     this.queryProvider = queryProvider;
@@ -287,6 +290,10 @@
     if (uf != null && uf.getWebUrl().isPresent()) {
       fmt.appendReviewedOn(uf, notes.getChange().getProject(), ps.id().changeId());
     }
+
+    ChangeData cd = changeDataFactory.create(notes);
+    fmt.appendCommentCount(cd.totalCommentCount(), cd.unresolvedCommentCount());
+
     fmt.appendProject(project.get());
     fmt.appendBranch(change.getDest().branch());
   }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/HeaderFormatter.java b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/HeaderFormatter.java
index 5b8ed6b..c85039d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/HeaderFormatter.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/reviewnotes/HeaderFormatter.java
@@ -135,6 +135,19 @@
         .append("\n");
   }
 
+  void appendCommentCount(int nTotal, int nUnresolved) {
+    if (nTotal > 0) {
+      sb.append("Comments-Total: ")
+          .append(nTotal)
+          .append("\n");
+    }
+    if (nUnresolved > 0) {
+      sb.append("Comments-Unresolved: ")
+          .append(nUnresolved)
+          .append("\n");
+    }
+  }
+
   @Override
   public String toString() {
     return sb.toString();
diff --git a/src/main/resources/Documentation/refs-notes-review.md b/src/main/resources/Documentation/refs-notes-review.md
index 20fd3c7..28b65db 100644
--- a/src/main/resources/Documentation/refs-notes-review.md
+++ b/src/main/resources/Documentation/refs-notes-review.md
@@ -79,6 +79,20 @@
 All review labels and scores present on the change at the time of submit are
 included.
 
+### Comments
+
+The number of comments at the time when the change was submitted.
+
+```
+  Comments-Total: 5
+  Comments-Unresolved: 1
+```
+
+Comments-Total is the total number of comments in all threads (also including robot comments),
+Comments-Unresolved is the number of comment threads which are still unresolved.
+
+These lines are only added when there are comments.
+
 ### Project
 
 The name of the project in which the commit was made.