Automatically import missing groups on project import
If a project that is imported has access rights for groups that do not
exist in the target system, these groups are now automatically
imported during the project import.
Change-Id: I9f7dc9850e618c932d248bb57feff9514fd96975
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/ImportGroupsStep.java b/src/main/java/com/googlesource/gerrit/plugins/importer/ImportGroupsStep.java
new file mode 100644
index 0000000..7381cbd
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/ImportGroupsStep.java
@@ -0,0 +1,100 @@
+// Copyright (C) 2015 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.googlesource.gerrit.plugins.importer;
+
+import com.google.gerrit.common.errors.NoSuchAccountException;
+import com.google.gerrit.extensions.restapi.BadRequestException;
+import com.google.gerrit.extensions.restapi.PreconditionFailedException;
+import com.google.gerrit.extensions.restapi.ResourceConflictException;
+import com.google.gerrit.reviewdb.client.AccountGroup;
+import com.google.gerrit.reviewdb.client.Project;
+import com.google.gerrit.server.account.GroupCache;
+import com.google.gerrit.server.config.ConfigResource;
+import com.google.gerrit.server.git.ProjectConfig;
+import com.google.gerrit.server.project.ProjectCache;
+import com.google.gwtorm.server.OrmException;
+import com.google.inject.Inject;
+import com.google.inject.assistedinject.Assisted;
+
+import org.eclipse.jgit.lib.ProgressMonitor;
+
+import java.io.IOException;
+import java.util.Set;
+
+public class ImportGroupsStep {
+
+ interface Factory {
+ ImportGroupsStep create(
+ @Assisted("from") String fromGerrit,
+ @Assisted("user") String user,
+ @Assisted("password") String password,
+ Project.NameKey project,
+ ProgressMonitor pm);
+ }
+
+ private final ProjectCache projectCache;
+ private final GroupCache groupCache;
+ private final ImportGroup.Factory importGroupFactory;
+ private final String fromGerrit;
+ private final String user;
+ private final String password;
+ private final Project.NameKey project;
+ private final ProgressMonitor pm;
+
+ @Inject
+ ImportGroupsStep(
+ ProjectCache projectCache,
+ GroupCache groupCache,
+ ImportGroup.Factory importGroupFactory,
+ @Assisted("from") String fromGerrit,
+ @Assisted("user") String user,
+ @Assisted("password") String password,
+ @Assisted Project.NameKey project,
+ @Assisted ProgressMonitor pm) {
+ this.projectCache = projectCache;
+ this.groupCache = groupCache;
+ this.importGroupFactory = importGroupFactory;
+ this.fromGerrit = fromGerrit;
+ this.user = user;
+ this.password = password;
+ this.project = project;
+ this.pm = pm;
+ }
+
+ void importGroups() throws PreconditionFailedException, BadRequestException,
+ NoSuchAccountException, OrmException, IOException {
+ ProjectConfig projectConfig = projectCache.get(project).getConfig();
+ Set<AccountGroup.UUID> groupUUIDs = projectConfig.getAllGroupUUIDs();
+ pm.beginTask("Import Groups", groupUUIDs.size());
+ for (AccountGroup.UUID groupUUID : groupUUIDs) {
+ if (groupCache.get(groupUUID) == null) {
+ ImportGroup.Input input = new ImportGroup.Input();
+ input.from = fromGerrit;
+ input.user = user;
+ input.pass = password;
+ try {
+ importGroupFactory.create(
+ new AccountGroup.NameKey(projectConfig.getGroup(groupUUID).getName()))
+ .apply(new ConfigResource(), input);
+ } catch (ResourceConflictException e) {
+ // should not happen
+ throw new IllegalStateException(e);
+ }
+ }
+ pm.update(1);
+ }
+ pm.endTask();
+ }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/ImportProject.java b/src/main/java/com/googlesource/gerrit/plugins/importer/ImportProject.java
index 935d588..706cd8a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/importer/ImportProject.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/ImportProject.java
@@ -87,6 +87,7 @@
private final GitFetchStep gitFetchStep;
private final ConfigureProjectStep configProjectStep;
private final ReplayChangesStep.Factory replayChangesFactory;
+ private final ImportGroupsStep.Factory importGroupsStepFactory;
private final Provider<CurrentUser> currentUser;
private final ImportJson importJson;
private final ImportLog importLog;
@@ -107,6 +108,7 @@
GitFetchStep gitFetchStep,
ConfigureProjectStep configProjectStep,
ReplayChangesStep.Factory replayChangesFactory,
+ ImportGroupsStep.Factory importGroupsStepFactory,
Provider<CurrentUser> currentUser,
ImportJson importJson,
ImportLog importLog,
@@ -118,6 +120,7 @@
this.gitFetchStep = gitFetchStep;
this.configProjectStep = configProjectStep;
this.replayChangesFactory = replayChangesFactory;
+ this.importGroupsStepFactory = importGroupsStepFactory;
this.currentUser = currentUser;
this.importJson = importJson;
this.importLog = importLog;
@@ -195,6 +198,8 @@
configProjectStep.configure(targetProject, parent, pm);
replayChangesFactory.create(input.from, input.user, input.pass, repo,
srcProject, targetProject, force, resume, statistic, pm).replay();
+ importGroupsStepFactory.create(input.from, input.user, input.pass,
+ targetProject, pm).importGroups();
} finally {
repo.close();
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/importer/Module.java b/src/main/java/com/googlesource/gerrit/plugins/importer/Module.java
index 0a121be..2d10f6e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/importer/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/importer/Module.java
@@ -73,6 +73,7 @@
factory(AddApprovalsStep.Factory.class);
factory(AddHashtagsStep.Factory.class);
factory(InsertLinkToOriginalChangeStep.Factory.class);
+ factory(ImportGroupsStep.Factory.class);
DynamicSet.bind(binder(), TopMenu.class).to(ImportProjectMenu.class);
factory(ImportGroup.Factory.class);
}