Merge "Allow to get/set notes related config parameter from REST and WebUI"
diff --git a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/GetConfig.java b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/GetConfig.java
index 40d6137..43afe00 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/GetConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/GetConfig.java
@@ -37,16 +37,21 @@
     ConfigInfo info = new ConfigInfo();
     info.info = Strings.emptyToNull(cfg.getString("infoMessage"));
     info.onSuccess = Strings.emptyToNull(cfg.getString("onSuccessMessage"));
-    info.allowEmail = cfg.getBoolean("allowEmail", false);
-    if (!info.allowEmail) {
-      info.allowEmail = null;
-    }
+    info.allowEmail = toBoolean(cfg.getBoolean("allowEmail", false));
+    info.createNotes = toBoolean(cfg.getBoolean("createNotes", true));
+    info.createNotesAsync = toBoolean(cfg.getBoolean("createNotesAsync", false));
     return info;
   }
 
+  private static Boolean toBoolean(boolean v) {
+    return v ? v : null;
+  }
+
   public class ConfigInfo {
     String info;
     String onSuccess;
     Boolean allowEmail;
+    Boolean createNotes;
+    Boolean createNotesAsync;
   }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/PutConfig.java b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/PutConfig.java
index e966590..524afb8 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/PutConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/PutConfig.java
@@ -28,6 +28,7 @@
 import com.googlesource.gerrit.plugins.serviceuser.PutConfig.Input;
 
 import org.eclipse.jgit.errors.ConfigInvalidException;
+import org.eclipse.jgit.lib.Config;
 import org.eclipse.jgit.storage.file.FileBasedConfig;
 import org.eclipse.jgit.util.FS;
 
@@ -39,6 +40,8 @@
     public String info;
     public String onSuccess;
     public Boolean allowEmail;
+    public Boolean createNotes;
+    public Boolean createNotesAsync;
   }
 
   private final PluginConfigFactory cfgFactory;
@@ -68,14 +71,28 @@
           Strings.emptyToNull(input.onSuccess));
     }
     if (input.allowEmail != null) {
-      if (input.allowEmail) {
-        cfg.setBoolean("plugin", pluginName, "allowEmail", true);
-      } else {
-        cfg.unset("plugin", pluginName, "allowEmail");
-      }
+      setBoolean(cfg, "allowEmail", input.allowEmail);
+    }
+    if (input.createNotes != null) {
+      setBoolean(cfg, "createNotes", input.createNotes, true);
+    }
+    if (input.createNotesAsync != null) {
+      setBoolean(cfg, "createNotesAsync", input.createNotesAsync);
     }
     cfg.save();
     cfgFactory.getFromGerritConfig(pluginName, true);
     return Response.<String> ok("OK");
   }
+
+  private void setBoolean(Config cfg, String name, boolean value) {
+    setBoolean(cfg, name, value, false);
+  }
+
+  private void setBoolean(Config cfg, String name, boolean value, boolean defaultValue) {
+    if (value == defaultValue) {
+      cfg.unset("plugin", pluginName, name);
+    } else {
+      cfg.setBoolean("plugin", pluginName, name, value);
+    }
+  }
 }
diff --git a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/client/ConfigInfo.java b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/client/ConfigInfo.java
index 95305b6..ae81ca4 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/client/ConfigInfo.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/client/ConfigInfo.java
@@ -20,10 +20,14 @@
   final native String getInfoMessage() /*-{ return this.info }-*/;
   final native String getOnSuccessMessage() /*-{ return this.on_success }-*/;
   final native boolean getAllowEmail() /*-{ return this.allow_email ? true : false; }-*/;
+  final native boolean getCreateNotes() /*-{ return this.create_notes ? true : false; }-*/;
+  final native boolean getCreateNotesAsync() /*-{ return this.create_notes_async ? true : false; }-*/;
 
   final native void setInfoMessage(String s) /*-{ this.info = s; }-*/;
   final native void setOnSuccessMessage(String s) /*-{ this.on_success = s; }-*/;
   final native void setAllowEmail(boolean s) /*-{ this.allow_email = s; }-*/;
+  final native void setCreateNotes(boolean s) /*-{ this.create_notes = s; }-*/;
+  final native void setCreateNotesAsync(boolean s) /*-{ this.create_notes_async = s; }-*/;
 
   static ConfigInfo create() {
     ConfigInfo g = (ConfigInfo) createObject();
diff --git a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/client/ServiceUserSettingsScreen.java b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/client/ServiceUserSettingsScreen.java
index 7613892..d142154 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/serviceuser/client/ServiceUserSettingsScreen.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/serviceuser/client/ServiceUserSettingsScreen.java
@@ -22,6 +22,8 @@
 import com.google.gwt.event.dom.client.ClickHandler;
 import com.google.gwt.event.dom.client.KeyPressEvent;
 import com.google.gwt.event.dom.client.KeyPressHandler;
+import com.google.gwt.event.logical.shared.ValueChangeEvent;
+import com.google.gwt.event.logical.shared.ValueChangeHandler;
 import com.google.gwt.user.client.rpc.AsyncCallback;
 import com.google.gwt.user.client.ui.Button;
 import com.google.gwt.user.client.ui.CheckBox;
@@ -44,6 +46,8 @@
   private TextArea infoMsgTxt;
   private TextArea onSuccessMsgTxt;
   private CheckBox allowEmailCheckBox;
+  private CheckBox createNotesCheckBox;
+  private CheckBox createNotesAsyncCheckBox;
   private Button saveButton;
 
   ServiceUserSettingsScreen() {
@@ -121,6 +125,38 @@
     allowEmailPanel.add(allowEmailInfo);
     add(allowEmailPanel);
 
+    Panel createNotesPanel = new HorizontalPanel();
+    createNotesCheckBox = new CheckBox("Create Git Notes");
+    createNotesCheckBox.setValue(info.getCreateNotes());
+    createNotesPanel.add(createNotesCheckBox);
+    Image createNotesInfo = new Image(ServiceUserPlugin.RESOURCES.info());
+    createNotesInfo.setTitle("Whether commits of a service user should be "
+        + "annotated by a Git note that contains information about the current "
+        + "owners of the service user. This allows to find a real person that "
+        + "is responsible for this commit. To get such a Git note for each commit "
+        + "of a service user the 'Forge Committer' access right must be blocked "
+        + "for service users.");
+    createNotesPanel.add(createNotesInfo);
+    add(createNotesPanel);
+
+    Panel createNotesAsyncPanel = new HorizontalPanel();
+    createNotesAsyncCheckBox = new CheckBox("Create Git Notes Asynchronously");
+    createNotesAsyncCheckBox.setValue(info.getCreateNotesAsync());
+    createNotesAsyncCheckBox.setEnabled(info.getCreateNotes());
+    createNotesAsyncPanel.add(createNotesAsyncCheckBox);
+    Image createNotesAsyncInfo = new Image(ServiceUserPlugin.RESOURCES.info());
+    createNotesAsyncInfo.setTitle("Whether the Git notes on commits that are "
+        + "pushed by a service user should be created asynchronously.");
+    createNotesAsyncPanel.add(createNotesAsyncInfo);
+    add(createNotesAsyncPanel);
+
+    createNotesCheckBox.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
+      @Override
+      public void onValueChange(ValueChangeEvent<Boolean> event) {
+        createNotesAsyncCheckBox.setEnabled(event.getValue());
+      }
+    });
+
     HorizontalPanel buttons = new HorizontalPanel();
     add(buttons);
 
@@ -137,6 +173,8 @@
     OnEditEnabler onEditEnabler = new OnEditEnabler(saveButton, infoMsgTxt);
     onEditEnabler.listenTo(onSuccessMsgTxt);
     onEditEnabler.listenTo(allowEmailCheckBox);
+    onEditEnabler.listenTo(createNotesCheckBox);
+    onEditEnabler.listenTo(createNotesAsyncCheckBox);
 
     infoMsgTxt.setFocus(true);
     saveButton.setEnabled(false);
@@ -147,6 +185,10 @@
     in.setInfoMessage(infoMsgTxt.getValue());
     in.setOnSuccessMessage(onSuccessMsgTxt.getValue());
     in.setAllowEmail(allowEmailCheckBox.getValue());
+    in.setCreateNotes(createNotesCheckBox.getValue());
+    if (createNotesAsyncCheckBox.isEnabled()) {
+      in.setCreateNotesAsync(createNotesAsyncCheckBox.getValue());
+    }
     new RestApi("config").id("server").view(Plugin.get().getPluginName(), "config")
         .put(in, new AsyncCallback<JavaScriptObject>() {
 
diff --git a/src/main/resources/Documentation/rest-api-config.md b/src/main/resources/Documentation/rest-api-config.md
index 41072f7..f06090a 100644
--- a/src/main/resources/Documentation/rest-api-config.md
+++ b/src/main/resources/Documentation/rest-api-config.md
@@ -612,7 +612,8 @@
 
   )]}'
   {
-    "on_success": "Don\u0027t forget to assign \u003ca href\u003d\"Documentation/access-control.html\"\u003eaccess rights\u003c/a\u003e to the service user."
+    "on_success": "Don\u0027t forget to assign \u003ca href\u003d\"Documentation/access-control.html\"\u003eaccess rights\u003c/a\u003e to the service user.",
+    "create_notes": true
   }
 ```
 
@@ -650,6 +651,12 @@
   a service user was successfully created.
 * _allow\_email_: Whether it is allowed to provide an email address for
   a service user (not set if `false`).
+* _create\_notes_: Whether commits of a service user should be
+  annotated by a Git note that contains information about the current
+  owners of the service user (not set if `false`).
+* _create\_notes\_async_: Whether the Git notes on commits that are
+  pushed by a service user should be created asynchronously (not set if
+  `false`).
 
 ### <a id="email-input"></a>EmailInput