LCOV - code coverage report
Current view: top level - server/config - RepositoryConfig.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 22 22 100.0 %
Date: 2022-11-19 15:00:39 Functions: 11 11 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2014 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.config;
      16             : 
      17             : import static com.google.common.collect.ImmutableList.toImmutableList;
      18             : import static java.util.Comparator.comparing;
      19             : 
      20             : import com.google.common.collect.ImmutableList;
      21             : import com.google.gerrit.common.Nullable;
      22             : import com.google.gerrit.entities.Project;
      23             : import com.google.gerrit.extensions.client.SubmitType;
      24             : import com.google.inject.Inject;
      25             : import com.google.inject.Singleton;
      26             : import java.nio.file.Path;
      27             : import java.nio.file.Paths;
      28             : import java.util.Objects;
      29             : import org.eclipse.jgit.lib.Config;
      30             : 
      31             : @Singleton
      32             : public class RepositoryConfig {
      33             : 
      34             :   static final String SECTION_NAME = "repository";
      35             :   static final String OWNER_GROUP_NAME = "ownerGroup";
      36             :   static final String DEFAULT_SUBMIT_TYPE_NAME = "defaultSubmitType";
      37             :   static final String BASE_PATH_NAME = "basePath";
      38             : 
      39         147 :   static final SubmitType DEFAULT_SUBMIT_TYPE = SubmitType.INHERIT;
      40             : 
      41             :   private final Config cfg;
      42             : 
      43             :   @Inject
      44         147 :   public RepositoryConfig(@GerritServerConfig Config cfg) {
      45         147 :     this.cfg = cfg;
      46         147 :   }
      47             : 
      48             :   public SubmitType getDefaultSubmitType(Project.NameKey project) {
      49         144 :     return cfg.getEnum(
      50         144 :         SECTION_NAME, findSubSection(project.get()), DEFAULT_SUBMIT_TYPE_NAME, DEFAULT_SUBMIT_TYPE);
      51             :   }
      52             : 
      53             :   public ImmutableList<String> getOwnerGroups(Project.NameKey project) {
      54         144 :     return ImmutableList.copyOf(
      55         144 :         cfg.getStringList(SECTION_NAME, findSubSection(project.get()), OWNER_GROUP_NAME));
      56             :   }
      57             : 
      58             :   @Nullable
      59             :   public Path getBasePath(Project.NameKey project) {
      60           1 :     String basePath = cfg.getString(SECTION_NAME, findSubSection(project.get()), BASE_PATH_NAME);
      61           1 :     return basePath != null ? Paths.get(basePath) : null;
      62             :   }
      63             : 
      64             :   public ImmutableList<Path> getAllBasePaths() {
      65          16 :     return cfg.getSubsections(SECTION_NAME).stream()
      66          16 :         .map(sub -> cfg.getString(SECTION_NAME, sub, BASE_PATH_NAME))
      67          16 :         .filter(Objects::nonNull)
      68          16 :         .map(Paths::get)
      69          16 :         .collect(toImmutableList());
      70             :   }
      71             : 
      72             :   /**
      73             :    * Find the subsection to get repository configuration from.
      74             :    *
      75             :    * <p>Subsection can use the * pattern so if project name matches more than one section, return
      76             :    * the more precise one. E.g if the following subsections are defined:
      77             :    *
      78             :    * <pre>
      79             :    * [repository "somePath/*"]
      80             :    *   name = value
      81             :    * [repository "somePath/somePath/*"]
      82             :    *   name = value
      83             :    * </pre>
      84             :    *
      85             :    * and this method is called with "somePath/somePath/someProject" as project name, it will return
      86             :    * the subsection "somePath/somePath/*"
      87             :    *
      88             :    * @param project Name of the project
      89             :    * @return the name of the subsection, null if none is found
      90             :    */
      91             :   @Nullable
      92             :   private String findSubSection(String project) {
      93         144 :     return cfg.getSubsections(SECTION_NAME).stream()
      94         144 :         .filter(ss -> isMatch(ss, project))
      95         144 :         .max(comparing(String::length))
      96         144 :         .orElse(null);
      97             :   }
      98             : 
      99             :   private boolean isMatch(String subSection, String project) {
     100           2 :     return project.equals(subSection)
     101           2 :         || (subSection.endsWith("*")
     102           2 :             && project.startsWith(subSection.substring(0, subSection.length() - 1)));
     103             :   }
     104             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750