Merge "Do not fail import if hashtags cannot be set due to missing permissions"
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/AddHashtagsStep.java b/src/main/java/com/googlesource/gerrit/plugins/importer/AddHashtagsStep.java
index f3bf629..f8aaef9 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/importer/AddHashtagsStep.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/AddHashtagsStep.java
@@ -14,11 +14,13 @@
 
 package com.googlesource.gerrit.plugins.importer;
 
+import com.google.gerrit.extensions.annotations.PluginName;
 import com.google.gerrit.extensions.api.changes.HashtagsInput;
 import com.google.gerrit.extensions.common.ChangeInfo;
 import com.google.gerrit.extensions.restapi.AuthException;
 import com.google.gerrit.reviewdb.client.Change;
 import com.google.gerrit.server.CurrentUser;
+import com.google.gerrit.server.change.ChangeTriplet;
 import com.google.gerrit.server.change.HashtagsUtil;
 import com.google.gerrit.server.project.ChangeControl;
 import com.google.gerrit.server.project.NoSuchChangeException;
@@ -27,6 +29,9 @@
 import com.google.inject.Inject;
 import com.google.inject.assistedinject.Assisted;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import java.io.IOException;
 import java.util.HashSet;
 
@@ -36,9 +41,13 @@
     AddHashtagsStep create(Change change, ChangeInfo changeInfo, boolean resume);
   }
 
+  private static final Logger log = LoggerFactory
+      .getLogger(AddHashtagsStep.class);
+
   private final HashtagsUtil hashtagsUtil;
   private final CurrentUser currentUser;
   private final ChangeControl.GenericFactory changeControlFactory;
+  private final String pluginName;
   private final Change change;
   private final ChangeInfo changeInfo;
   private final boolean resume;
@@ -47,14 +56,16 @@
   AddHashtagsStep(HashtagsUtil hashtagsUtil,
       CurrentUser currentUser,
       ChangeControl.GenericFactory changeControlFactory,
+      @PluginName String pluginName,
       @Assisted Change change,
       @Assisted ChangeInfo changeInfo,
       @Assisted boolean resume) {
     this.hashtagsUtil = hashtagsUtil;
-    this.change = change;
-    this.changeInfo = changeInfo;
     this.currentUser = currentUser;
     this.changeControlFactory = changeControlFactory;
+    this.pluginName = pluginName;
+    this.change = change;
+    this.changeInfo = changeInfo;
     this.resume = resume;
   }
 
@@ -62,14 +73,23 @@
       ValidationException, OrmException, NoSuchChangeException {
     ChangeControl ctrl = changeControlFactory.controlFor(change, currentUser);
 
-    if (resume) {
-      HashtagsInput input = new HashtagsInput();
-      input.remove = ctrl.getNotes().load().getHashtags();
-      hashtagsUtil.setHashtags(ctrl, input, false, false);
-    }
+    try {
+      if (resume) {
+        HashtagsInput input = new HashtagsInput();
+        input.remove = ctrl.getNotes().load().getHashtags();
+        hashtagsUtil.setHashtags(ctrl, input, false, false);
+      }
 
-    HashtagsInput input = new HashtagsInput();
-    input.add = new HashSet<>(changeInfo.hashtags);
-    hashtagsUtil.setHashtags(ctrl, input, false, false);
+      HashtagsInput input = new HashtagsInput();
+      input.add = new HashSet<>(changeInfo.hashtags);
+      hashtagsUtil.setHashtags(ctrl, input, false, false);
+    } catch (AuthException e) {
+      log.warn(String.format(
+          "[%s] Hashtags cannot be set on change %s because the importing"
+              + " user %s doesn't have permissions to edit hashtags"
+              + " (e.g. assign the 'Edit Hashtags' global capability"
+              + " and resume the import with the force option).",
+          pluginName, ChangeTriplet.format(change), currentUser.getUserName()));
+    }
   }
 }