Add utility class to write code owner messages to avoid code duplication

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: I63262218ea8be2c6155bbce05d42245391bba808
diff --git a/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnersChangeMessageUtil.java b/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnersChangeMessageUtil.java
new file mode 100644
index 0000000..1175cd9
--- /dev/null
+++ b/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnersChangeMessageUtil.java
@@ -0,0 +1,40 @@
+// 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.plugins.codeowners.backend;
+
+import com.google.gerrit.entities.ChangeMessage;
+import com.google.gerrit.plugins.codeowners.util.JgitPath;
+import java.nio.file.Path;
+import java.util.stream.Stream;
+
+/** Utility functions to write code owners {@link ChangeMessage}s. */
+public class CodeOwnersChangeMessageUtil {
+  /**
+   * Appends the given paths to the given message builder.
+   *
+   * @param message message builder to which the paths should be appended
+   * @param pathsToAppend the paths to append to the message builder
+   */
+  public static void appendPaths(StringBuilder message, Stream<Path> pathsToAppend) {
+    pathsToAppend.forEach(path -> message.append(String.format("* %s\n", JgitPath.of(path).get())));
+  }
+
+  /**
+   * Private constructor to prevent instantiation of this class.
+   *
+   * <p>The class only contains static methods, hence the class never needs to be instantiated.
+   */
+  private CodeOwnersChangeMessageUtil() {}
+}
diff --git a/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnersOnAddReviewer.java b/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnersOnAddReviewer.java
index 78f0056..3ce8e01 100644
--- a/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnersOnAddReviewer.java
+++ b/java/com/google/gerrit/plugins/codeowners/backend/CodeOwnersOnAddReviewer.java
@@ -14,6 +14,7 @@
 
 package com.google.gerrit.plugins.codeowners.backend;
 
+import static com.google.gerrit.plugins.codeowners.backend.CodeOwnersChangeMessageUtil.appendPaths;
 import static com.google.gerrit.server.update.context.RefUpdateContext.RefUpdateType.CHANGE_MODIFICATION;
 import static com.google.gerrit.server.update.context.RefUpdateContext.RefUpdateType.PLUGIN;
 import static java.util.stream.Collectors.joining;
@@ -29,7 +30,6 @@
 import com.google.gerrit.plugins.codeowners.backend.config.CodeOwnersPluginConfiguration;
 import com.google.gerrit.plugins.codeowners.backend.config.CodeOwnersPluginProjectConfigSnapshot;
 import com.google.gerrit.plugins.codeowners.metrics.CodeOwnerMetrics;
-import com.google.gerrit.plugins.codeowners.util.JgitPath;
 import com.google.gerrit.server.ChangeMessagesUtil;
 import com.google.gerrit.server.CurrentUser;
 import com.google.gerrit.server.git.WorkQueue;
@@ -49,7 +49,6 @@
 import java.time.Instant;
 import java.util.List;
 import java.util.Optional;
-import java.util.stream.Stream;
 
 /**
  * Callback that is invoked when a user is added as a reviewer.
@@ -160,9 +159,9 @@
               "addCodeOwnersMessageOnAddReviewer",
               updateFactory -> {
                 try (BatchUpdate batchUpdate =
-                    updateFactory.create(projectName, currentUser, when);
+                        updateFactory.create(projectName, currentUser, when);
                     RefUpdateContext pluginCtx = RefUpdateContext.open(PLUGIN);
-                RefUpdateContext changeCtx = RefUpdateContext.open(CHANGE_MODIFICATION)) {
+                    RefUpdateContext changeCtx = RefUpdateContext.open(CHANGE_MODIFICATION)) {
                   batchUpdate.addOp(changeId, new Op(reviewers, maxPathsInChangeMessages));
                   batchUpdate.execute();
                 }
@@ -249,10 +248,5 @@
 
       return Optional.of(message.toString());
     }
-
-    private void appendPaths(StringBuilder message, Stream<Path> pathsToAppend) {
-      pathsToAppend.forEach(
-          path -> message.append(String.format("* %s\n", JgitPath.of(path).get())));
-    }
   }
 }
diff --git a/java/com/google/gerrit/plugins/codeowners/backend/OnCodeOwnerApproval.java b/java/com/google/gerrit/plugins/codeowners/backend/OnCodeOwnerApproval.java
index b3417dc..ab700ec 100644
--- a/java/com/google/gerrit/plugins/codeowners/backend/OnCodeOwnerApproval.java
+++ b/java/com/google/gerrit/plugins/codeowners/backend/OnCodeOwnerApproval.java
@@ -16,6 +16,7 @@
 
 import static com.google.common.base.Preconditions.checkState;
 import static com.google.common.collect.ImmutableMap.toImmutableMap;
+import static com.google.gerrit.plugins.codeowners.backend.CodeOwnersChangeMessageUtil.appendPaths;
 import static com.google.gerrit.server.update.context.RefUpdateContext.RefUpdateType.CHANGE_MODIFICATION;
 import static com.google.gerrit.server.update.context.RefUpdateContext.RefUpdateType.PLUGIN;
 
@@ -32,7 +33,6 @@
 import com.google.gerrit.plugins.codeowners.backend.config.CodeOwnersPluginProjectConfigSnapshot;
 import com.google.gerrit.plugins.codeowners.backend.config.RequiredApproval;
 import com.google.gerrit.plugins.codeowners.metrics.CodeOwnerMetrics;
-import com.google.gerrit.plugins.codeowners.util.JgitPath;
 import com.google.gerrit.server.ChangeMessagesUtil;
 import com.google.gerrit.server.CurrentUser;
 import com.google.gerrit.server.IdentifiedUser;
@@ -55,7 +55,6 @@
 import java.time.Instant;
 import java.util.Map;
 import java.util.Optional;
-import java.util.stream.Stream;
 
 /**
  * Callback that is invoked on post review and that extends the change message if a code owner
@@ -423,10 +422,6 @@
     return Optional.of(message.toString());
   }
 
-  private void appendPaths(StringBuilder message, Stream<Path> pathsToAppend) {
-    pathsToAppend.forEach(path -> message.append(String.format("* %s\n", JgitPath.of(path).get())));
-  }
-
   private boolean isIgnoredDueToSelfApproval(
       IdentifiedUser user, PatchSet patchSet, RequiredApproval requiredApproval) {
     return patchSet.uploader().equals(user.getAccountId())