Merge branch 'stable-3.2'

* stable-3.2:
  Upgrade bazlets to latest master to build with 3.2.3 API
  Upgrade bazlets to latest stable-3.1 to build with 3.1.8 API
  Upgrade bazlets to latest stable-3.0 to build with 3.0.12 API
  Upgrade bazlets to latest stable-2.16 to build with 2.16.22 API

Change-Id: Ic8b07a62a64d6fa56e0e7607e3c766f9036920d1
diff --git a/WORKSPACE b/WORKSPACE
index 194662e..0267c7e 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -12,4 +12,4 @@
     "gerrit_api",
 )
 
-gerrit_api()
+gerrit_api(version = "3.3.0-SNAPSHOT")
diff --git a/src/main/java/com/ericsson/gerrit/plugins/projectgroupstructure/DefaultAccessRights.java b/src/main/java/com/ericsson/gerrit/plugins/projectgroupstructure/DefaultAccessRights.java
index b491d16..974b231 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/projectgroupstructure/DefaultAccessRights.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/projectgroupstructure/DefaultAccessRights.java
@@ -14,11 +14,11 @@
 
 package com.ericsson.gerrit.plugins.projectgroupstructure;
 
-import com.google.gerrit.common.data.AccessSection;
-import com.google.gerrit.common.data.GroupReference;
-import com.google.gerrit.common.data.Permission;
-import com.google.gerrit.common.data.PermissionRule;
+import com.google.gerrit.entities.AccessSection;
 import com.google.gerrit.entities.AccountGroup;
+import com.google.gerrit.entities.GroupReference;
+import com.google.gerrit.entities.Permission;
+import com.google.gerrit.entities.PermissionRule;
 import com.google.gerrit.entities.Project;
 import com.google.gerrit.exceptions.InvalidNameException;
 import com.google.gerrit.extensions.annotations.PluginData;
@@ -118,27 +118,30 @@
   private void setAccessRights(ProjectConfig config, ProjectState project) {
     for (String refName : defaultAccessRightsConfig.getSubsections(ProjectConfig.ACCESS)) {
       if (AccessSection.isValidRefSectionName(refName) && isValidRegex(refName)) {
-        AccessSection as = config.getAccessSection(refName, true);
-        getPermissions(refName, as);
-        setPermissions(refName, as, getOwnerGroupName(project));
+        config.upsertAccessSection(
+            refName,
+            as -> {
+              getPermissions(refName, as);
+              setPermissions(refName, as, getOwnerGroupName(project));
+            });
       }
     }
   }
 
-  private void getPermissions(String refName, AccessSection as) {
+  private void getPermissions(String refName, AccessSection.Builder as) {
     for (String varName :
         defaultAccessRightsConfig.getStringList(
             ProjectConfig.ACCESS, refName, "exclusiveGroupPermissions")) {
       Arrays.stream(varName.split("[, \t]{1,}"))
           .filter(Permission::isPermission)
-          .forEach(n -> as.getPermission(n, true).setExclusiveGroup(true));
+          .forEach(n -> as.upsertPermission(n).setExclusiveGroup(true));
     }
   }
 
-  private void setPermissions(String refName, AccessSection as, String ownerGroupName) {
+  private void setPermissions(String refName, AccessSection.Builder as, String ownerGroupName) {
     for (String value : defaultAccessRightsConfig.getNames(ProjectConfig.ACCESS, refName)) {
       if (Permission.isPermission(value)) {
-        Permission perm = as.getPermission(value, true);
+        Permission.Builder perm = as.upsertPermission(value);
         setPermissionRules(ownerGroupName, perm, refName, value);
       } else {
         log.error("Invalid permission {}", value);
@@ -168,15 +171,16 @@
   }
 
   private void setPermissionRules(
-      String ownerGroupName, Permission perm, String refName, String value) {
+      String ownerGroupName, Permission.Builder perm, String refName, String value) {
     for (String ruleString :
         defaultAccessRightsConfig.getStringList(ProjectConfig.ACCESS, refName, value)) {
-      PermissionRule rule;
+      PermissionRule.Builder rule;
       try {
         rule =
             PermissionRule.fromString(
-                ruleString.replaceAll(Pattern.quote(OWNER_TOKEN), ownerGroupName),
-                Permission.hasRange(value));
+                    ruleString.replaceAll(Pattern.quote(OWNER_TOKEN), ownerGroupName),
+                    Permission.hasRange(value))
+                .toBuilder();
 
       } catch (IllegalArgumentException notRule) {
         log.error(
@@ -199,7 +203,7 @@
           log.error("Group {} not found", rule.getGroup().getName());
           continue;
         }
-        rule.setGroup(new GroupReference(group.get().getGroupUUID(), rule.getGroup().getName()));
+        rule.setGroup(GroupReference.create(group.get().getGroupUUID(), rule.getGroup().getName()));
       }
       perm.add(rule);
     }
diff --git a/src/main/java/com/ericsson/gerrit/plugins/projectgroupstructure/ProjectCreationValidator.java b/src/main/java/com/ericsson/gerrit/plugins/projectgroupstructure/ProjectCreationValidator.java
index 331c968..bff257e 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/projectgroupstructure/ProjectCreationValidator.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/projectgroupstructure/ProjectCreationValidator.java
@@ -16,8 +16,8 @@
 
 import com.google.common.base.Charsets;
 import com.google.common.hash.Hashing;
-import com.google.gerrit.common.data.GroupReference;
 import com.google.gerrit.entities.AccountGroup;
+import com.google.gerrit.entities.GroupReference;
 import com.google.gerrit.entities.Project;
 import com.google.gerrit.extensions.annotations.PluginCanonicalWebUrl;
 import com.google.gerrit.extensions.annotations.PluginName;
@@ -114,12 +114,6 @@
   public void validateNewProject(CreateProjectArgs args) throws ValidationException {
     String name = args.getProjectName();
     log.debug("validating creation of {}", name);
-    if (name.contains(" ")) {
-      throw new ValidationException(
-          String.format(PROJECT_CANNOT_CONTAINS_SPACES_MSG, documentationUrl));
-    }
-
-    Project.NameKey newParent = args.newParent;
 
     try {
       permissionBackend.user(self.get()).check(GlobalPermission.ADMINISTRATE_SERVER);
@@ -134,6 +128,13 @@
       // continuing
     }
 
+    if (name.contains(" ")) {
+      throw new ValidationException(
+          String.format(PROJECT_CANNOT_CONTAINS_SPACES_MSG, documentationUrl));
+    }
+
+    Project.NameKey newParent = args.newParent;
+
     if (allProjectsName.get().equals(newParent)) {
       validateRootProject(name, args.permissionsOnly);
     } else {
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md
index 216ccc2..7c0d4b4 100644
--- a/src/main/resources/Documentation/about.md
+++ b/src/main/resources/Documentation/about.md
@@ -3,6 +3,10 @@
 enforce for the project group structure, it also enforce other naming
 rules like project name cannot contain spaces.
 
+Below description of plugins functionality is not applicable if user is an
+administrator. Administrators continue creating projects like this plugin does
+not exist.
+
 To start creating a project group structure, simply create a root project, i.e.
 a project which inherits rights from `All-Projects`. The root project name
 cannot contains slashes, e.g. `some-organization` and must be created with
@@ -83,9 +87,9 @@
 - Click `Publish` button, review, vote and submit the change to apply new
 configuration
 
-Ownership of a project created by delegated user is given automatically to that
-user by adding him to a group named `<root-project-name>-admins`. It is
-possible to disable granting the ownership by configuring
+Ownership of a project `<project-name>` created by delegated user is given
+automatically to that user by adding him to a group named `<project-name>-admins`.
+It is possible to disable granting the ownership by configuring
 `disableGrantingProjectOwnership` in the `project.config` of
 `refs/meta/config` branch of the parent project:
 
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index ac9a12e..de4c600 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -1,4 +1,5 @@
 # Config
+
 The only configuration required is to grant `Create-group` and `Create-project`
 global permissions to `Registered Users` group.
 
@@ -22,5 +23,6 @@
   push = group ${owner}
   label-Code-Review = -2..+2 group ${owner}
   submit = group ${owner}
+```
 
-```
\ No newline at end of file
+Note: default access rights configuration is bypassed for projects created by admins.
diff --git a/src/test/java/com/ericsson/gerrit/plugins/projectgroupstructure/DefaultAccessRightsIT.java b/src/test/java/com/ericsson/gerrit/plugins/projectgroupstructure/DefaultAccessRightsIT.java
index 2f93c5b..1c2f3cd 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/projectgroupstructure/DefaultAccessRightsIT.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/projectgroupstructure/DefaultAccessRightsIT.java
@@ -21,10 +21,11 @@
 import com.google.gerrit.acceptance.LightweightPluginDaemonTest;
 import com.google.gerrit.acceptance.TestPlugin;
 import com.google.gerrit.acceptance.testsuite.project.ProjectOperations;
-import com.google.gerrit.common.data.AccessSection;
 import com.google.gerrit.common.data.GlobalCapability;
-import com.google.gerrit.common.data.Permission;
+import com.google.gerrit.entities.AccessSection;
 import com.google.gerrit.entities.AccountGroup;
+import com.google.gerrit.entities.CachedProjectConfig;
+import com.google.gerrit.entities.Permission;
 import com.google.gerrit.entities.Project;
 import com.google.gerrit.extensions.api.projects.ProjectInput;
 import com.google.gerrit.extensions.restapi.Url;
@@ -88,11 +89,11 @@
 
     Optional<ProjectState> projectState = projectCache.get(Project.nameKey(projectName));
     AccountGroup.UUID ownerUUID = projectState.get().getOwners().iterator().next();
-    ProjectConfig projectConfig = projectState.get().getConfig();
+    CachedProjectConfig projectConfig = projectState.get().getConfig();
 
     assertThat(projectConfig.getAccessSections().size()).isEqualTo(2);
 
-    AccessSection refsSection = projectConfig.getAccessSection("refs/*");
+    AccessSection refsSection = projectConfig.getAccessSection("refs/*").get();
     assertThat(refsSection.getPermissions().size()).isEqualTo(3);
     assertThat(refsSection.getPermission(Permission.OWNER).getRules().get(0).getGroup().getUUID())
         .isEqualTo(ownerUUID);
@@ -102,7 +103,7 @@
     assertThat(refsSection.getPermission(Permission.PUSH).getRules().get(0).getGroup().getName())
         .isEqualTo("Administrators");
 
-    AccessSection refsHeadsSection = projectConfig.getAccessSection("refs/heads/*");
+    AccessSection refsHeadsSection = projectConfig.getAccessSection("refs/heads/*").get();
     assertThat(refsHeadsSection.getPermissions().size()).isEqualTo(4);
     assertThat(
             refsHeadsSection
diff --git a/src/test/java/com/ericsson/gerrit/plugins/projectgroupstructure/ProjectCreationValidatorIT.java b/src/test/java/com/ericsson/gerrit/plugins/projectgroupstructure/ProjectCreationValidatorIT.java
index f85ab27..7e87c92 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/projectgroupstructure/ProjectCreationValidatorIT.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/projectgroupstructure/ProjectCreationValidatorIT.java
@@ -26,8 +26,8 @@
 import com.google.gerrit.acceptance.TestPlugin;
 import com.google.gerrit.acceptance.testsuite.project.ProjectOperations;
 import com.google.gerrit.common.data.GlobalCapability;
-import com.google.gerrit.common.data.GroupReference;
 import com.google.gerrit.entities.AccountGroup;
+import com.google.gerrit.entities.GroupReference;
 import com.google.gerrit.entities.Project;
 import com.google.gerrit.extensions.api.groups.GroupApi;
 import com.google.gerrit.extensions.api.projects.ProjectInput;
@@ -233,7 +233,7 @@
       cfg.getPluginConfig(PLUGIN_NAME)
           .setGroupReference(
               ProjectCreationValidator.DELEGATE_PROJECT_CREATION_TO,
-              new GroupReference(AccountGroup.UUID.parse(gId), delegatingGroup));
+              GroupReference.create(AccountGroup.UUID.parse(gId), delegatingGroup));
       cfgUpdate.save();
     }
     userRestSession.put("/projects/" + Url.encode(parent + "/childProject"), in).assertCreated();
@@ -256,7 +256,7 @@
       cfg.getPluginConfig(PLUGIN_NAME)
           .setGroupReference(
               ProjectCreationValidator.DELEGATE_PROJECT_CREATION_TO,
-              new GroupReference(AccountGroup.UUID.parse(gId), delegatingGroup));
+              GroupReference.create(AccountGroup.UUID.parse(gId), delegatingGroup));
       cfgUpdate.save();
     }
 
@@ -307,7 +307,7 @@
       cfg.getPluginConfig(PLUGIN_NAME)
           .setGroupReference(
               ProjectCreationValidator.DELEGATE_PROJECT_CREATION_TO,
-              new GroupReference(AccountGroup.UUID.parse(gId), delegatingGroup));
+              GroupReference.create(AccountGroup.UUID.parse(gId), delegatingGroup));
       cfg.getPluginConfig(PLUGIN_NAME)
           .setBoolean(ProjectCreationValidator.DISABLE_GRANTING_PROJECT_OWNERSHIP, true);
       cfgUpdate.save();
@@ -387,7 +387,7 @@
       cfg.getPluginConfig(PLUGIN_NAME)
           .setGroupReference(
               ProjectCreationValidator.DELEGATE_PROJECT_CREATION_TO,
-              new GroupReference(AccountGroup.UUID.parse(gId), delegatingGroup));
+              GroupReference.create(AccountGroup.UUID.parse(gId), delegatingGroup));
       cfgUpdate.save();
     }
     userRestSession.put("/projects/" + Url.encode(parent + "/childProject"), in).assertCreated();
@@ -422,7 +422,7 @@
       cfg.getPluginConfig(PLUGIN_NAME)
           .setGroupReference(
               ProjectCreationValidator.DELEGATE_PROJECT_CREATION_TO,
-              new GroupReference(AccountGroup.UUID.parse(gId), delegatingGroup));
+              GroupReference.create(AccountGroup.UUID.parse(gId), delegatingGroup));
       cfgUpdate.save();
     }
     userRestSession.put("/projects/" + Url.encode(parent + "/childProject"), in).assertConflict();
@@ -455,7 +455,7 @@
       cfg.getPluginConfig(PLUGIN_NAME)
           .setGroupReference(
               ProjectCreationValidator.DELEGATE_PROJECT_CREATION_TO,
-              new GroupReference(AccountGroup.UUID.parse(gId), delegatingGroup));
+              GroupReference.create(AccountGroup.UUID.parse(gId), delegatingGroup));
       cfgUpdate.save();
     }
     userRestSession.put("/projects/" + Url.encode(parent + "/childProject"), in).assertConflict();
@@ -493,11 +493,11 @@
       cfg.getPluginConfig("project-group-structure")
           .setGroupReference(
               ProjectCreationValidator.DELEGATE_PROJECT_CREATION_TO,
-              new GroupReference(AccountGroup.UUID.parse(gId), delegatingGroup));
+              GroupReference.create(AccountGroup.UUID.parse(gId), delegatingGroup));
       cfgUpdate.save();
 
       String newDelegatingGroup = name("groupC");
-      gApi.groups().id(delegatingGroup).name(newDelegatingGroup);
+      cfg.renameGroup(cfg.getGroup(delegatingGroup).getUUID(), newDelegatingGroup);
     }
 
     userRestSession.put("/projects/" + Url.encode(parent + "/childProject"), in).assertCreated();