Allow to set Code-Review label in AllProjects(Users)Creator

Change-Id: Id14e295889091af129f11d910e9732a56a60a856
This commit is contained in:
Changcheng Xiao
2018-08-29 19:24:22 +02:00
parent ee987b1050
commit bfb427ff9a
2 changed files with 33 additions and 14 deletions

View File

@@ -81,6 +81,7 @@ public class AllProjectsCreator {
@Nullable private GroupReference batch;
private String message;
private int firstChangeId = ReviewDb.FIRST_CHANGE_ID;
private LabelType codeReviewLabel;
private List<LabelType> additionalLabelType;
@Inject
@@ -98,6 +99,7 @@ public class AllProjectsCreator {
this.anonymous = systemGroupBackend.getGroup(ANONYMOUS_USERS);
this.registered = systemGroupBackend.getGroup(REGISTERED_USERS);
this.owners = systemGroupBackend.getGroup(PROJECT_OWNERS);
this.codeReviewLabel = getDefaultCodeReviewLabel();
this.additionalLabelType = new ArrayList<>();
}
@@ -125,6 +127,15 @@ public class AllProjectsCreator {
return this;
}
/** If called, the provided "Code-Review" label will be used rather than the default. */
@UsedAt(UsedAt.Project.GOOGLE)
public AllProjectsCreator setCodeReviewLabel(LabelType labelType) {
checkArgument(
labelType.getName().equals("Code-Review"), "label should have 'Code-Review' as its name");
this.codeReviewLabel = labelType;
return this;
}
@UsedAt(UsedAt.Project.GOOGLE)
public AllProjectsCreator addAdditionalLabel(LabelType labelType) {
additionalLabelType.add(labelType);
@@ -190,8 +201,7 @@ public class AllProjectsCreator {
stream.add(rule(config, batch));
}
LabelType codeReviewLabel = initCodeReviewLabel(config);
initAdditionalLabels(config);
initLabels(config);
grant(config, heads, codeReviewLabel, -1, 1, registered);
grant(config, heads, codeReviewLabel, -2, 2, admin, owners);
@@ -222,12 +232,6 @@ public class AllProjectsCreator {
}
}
public static LabelType initCodeReviewLabel(ProjectConfig c) {
LabelType type = getDefaultCodeReviewLabel();
c.getLabelSections().put(type.getName(), type);
return type;
}
@UsedAt(UsedAt.Project.GOOGLE)
public static LabelType getDefaultCodeReviewLabel() {
LabelType type =
@@ -244,10 +248,8 @@ public class AllProjectsCreator {
return type;
}
private void initAdditionalLabels(ProjectConfig projectConfig) {
if (additionalLabelType.isEmpty()) {
return;
}
private void initLabels(ProjectConfig projectConfig) {
projectConfig.getLabelSections().put(codeReviewLabel.getName(), codeReviewLabel);
additionalLabelType.forEach(t -> projectConfig.getLabelSections().put(t.getName(), t));
}

View File

@@ -14,8 +14,10 @@
package com.google.gerrit.server.schema;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
import static com.google.gerrit.server.schema.AclUtil.grant;
import static com.google.gerrit.server.schema.AllProjectsCreator.getDefaultCodeReviewLabel;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.Version;
@@ -26,6 +28,7 @@ import com.google.gerrit.common.data.Permission;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.UsedAt;
import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
import com.google.gerrit.server.git.GitRepositoryManager;
@@ -50,6 +53,7 @@ public class AllUsersCreator {
private final GroupReference registered;
@Nullable private GroupReference admin;
private LabelType codeReviewLabel;
@Inject
AllUsersCreator(
@@ -61,6 +65,7 @@ public class AllUsersCreator {
this.allUsersName = allUsersName;
this.serverUser = serverUser;
this.registered = systemGroupBackend.getGroup(REGISTERED_USERS);
this.codeReviewLabel = getDefaultCodeReviewLabel();
}
/**
@@ -72,6 +77,15 @@ public class AllUsersCreator {
return this;
}
/** If called, the provided "Code-Review" label will be used rather than the default. */
@UsedAt(UsedAt.Project.GOOGLE)
public AllUsersCreator setCodeReviewLabel(LabelType labelType) {
checkArgument(
labelType.getName().equals("Code-Review"), "label should have 'Code-Review' as its name");
this.codeReviewLabel = labelType;
return this;
}
public void create() throws IOException, ConfigInvalidException {
try (Repository git = mgr.openRepository(allUsersName)) {
initAllUsers(git);
@@ -100,11 +114,14 @@ public class AllUsersCreator {
AccessSection users =
config.getAccessSection(
RefNames.REFS_USERS + "${" + RefPattern.USERID_SHARDED + "}", true);
LabelType cr = AllProjectsCreator.initCodeReviewLabel(config);
// Initialize "Code-Review" label.
config.getLabelSections().put(codeReviewLabel.getName(), codeReviewLabel);
grant(config, users, Permission.READ, false, true, registered);
grant(config, users, Permission.PUSH, false, true, registered);
grant(config, users, Permission.SUBMIT, false, true, registered);
grant(config, users, cr, -2, 2, true, registered);
grant(config, users, codeReviewLabel, -2, 2, true, registered);
if (admin != null) {
AccessSection defaults = config.getAccessSection(RefNames.REFS_USERS_DEFAULT, true);