Support wildcard matching in repository configuration

Per repository configuration was only supporting "*" as the repository
name so it was only possible to configure one default submit type and
same owner groups for all the new repositories.

[repository "*"]
  ownerGroup = Registered Users
  defaultSubmitType = MERGE_IF_NECESSARY

Now supports different repository configuration based on the name. The
only matching patterns supported are exact match or wildcard matching
which can be specified by ending the name by a *. Obviously, repository
name "*" still represents all repositories.

If a project matches more than one repository configuration, then the
configuration from the more precise match will be used. In the following
example, the default submit type for a project named project/plugins/a
would be CHERRY_PICK.

[repository "project/*"]
  defaultSubmitType = MERGE_IF_NECESSARY
[repository "project/plugins/*"]
  defaultSubmitType = CHERRY_PICK

Change-Id: I8b9c157f60a3ad1c6f542cef62e5de8fe9333126
This commit is contained in:
Hugo Arès
2014-11-25 15:33:42 -05:00
parent aeb86c3145
commit 7d2b942df7
10 changed files with 282 additions and 66 deletions

View File

@@ -0,0 +1,85 @@
// Copyright (C) 2014 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.google.gerrit.server.config;
import com.google.gerrit.extensions.client.SubmitType;
import com.google.gerrit.reviewdb.client.Project;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.eclipse.jgit.lib.Config;
@Singleton
public class RepositoryConfig {
static final String SECTION_NAME = "repository";
static final String OWNER_GROUP_NAME = "ownerGroup";
static final String DEFAULT_SUBMIT_TYPE_NAME = "defaultSubmitType";
private final Config cfg;
@Inject
public RepositoryConfig(@GerritServerConfig Config cfg) {
this.cfg = cfg;
}
public SubmitType getDefaultSubmitType(Project.NameKey project) {
return cfg.getEnum(SECTION_NAME, findSubSection(project.get()),
DEFAULT_SUBMIT_TYPE_NAME, SubmitType.MERGE_IF_NECESSARY);
}
public String[] getOwnerGroups(Project.NameKey project) {
return cfg.getStringList(SECTION_NAME, findSubSection(project.get()),
OWNER_GROUP_NAME);
}
/**
* Find the subSection to get repository configuration from.
* <p>
* SubSection can use the * pattern so if project name matches more than one
* section, return the more precise one. E.g if the following subSections are
* defined:
*
* <pre>
* [repository "somePath/*"]
* name = value
* [repository "somePath/somePath/*"]
* name = value
* </pre>
*
* and this method is called with "somePath/somePath/someProject" as project
* name, it will return the subSection "somePath/somePath/*"
*
* @param project Name of the project
* @return the name of the subSection, null if none is found
*/
private String findSubSection(String project) {
String subSectionFound = null;
for (String subSection : cfg.getSubsections(SECTION_NAME)) {
if (isMatch(subSection, project)
&& (subSectionFound == null || subSectionFound.length() < subSection
.length())) {
subSectionFound = subSection;
}
}
return subSectionFound;
}
private boolean isMatch(String subSection, String project) {
return project.equals(subSection)
|| (subSection.endsWith("*") && project.startsWith(subSection
.substring(0, subSection.length() - 1)));
}
}