LCOV - code coverage report
Current view: top level - server/restapi/project - ConfigInfoCreator.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 67 89 75.3 %
Date: 2022-11-19 15:00:39 Functions: 3 4 75.0 %

          Line data    Source code
       1             : // Copyright (C) 2013 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.restapi.project;
      16             : 
      17             : import com.google.common.base.MoreObjects;
      18             : import com.google.common.base.Strings;
      19             : import com.google.common.collect.Iterables;
      20             : import com.google.gerrit.common.Nullable;
      21             : import com.google.gerrit.entities.BooleanProjectConfig;
      22             : import com.google.gerrit.entities.Project;
      23             : import com.google.gerrit.extensions.api.projects.CommentLinkInfo;
      24             : import com.google.gerrit.extensions.api.projects.ConfigInfo;
      25             : import com.google.gerrit.extensions.api.projects.ConfigInfo.ConfigParameterInfo;
      26             : import com.google.gerrit.extensions.api.projects.ConfigInfo.InheritedBooleanInfo;
      27             : import com.google.gerrit.extensions.api.projects.ConfigInfo.MaxObjectSizeLimitInfo;
      28             : import com.google.gerrit.extensions.api.projects.ConfigInfo.SubmitTypeInfo;
      29             : import com.google.gerrit.extensions.api.projects.ProjectConfigEntryType;
      30             : import com.google.gerrit.extensions.common.ActionInfo;
      31             : import com.google.gerrit.extensions.registration.DynamicMap;
      32             : import com.google.gerrit.extensions.registration.Extension;
      33             : import com.google.gerrit.extensions.restapi.RestView;
      34             : import com.google.gerrit.extensions.webui.UiAction;
      35             : import com.google.gerrit.server.CurrentUser;
      36             : import com.google.gerrit.server.config.AllProjectsName;
      37             : import com.google.gerrit.server.config.PluginConfig;
      38             : import com.google.gerrit.server.config.PluginConfigFactory;
      39             : import com.google.gerrit.server.config.ProjectConfigEntry;
      40             : import com.google.gerrit.server.extensions.webui.UiActions;
      41             : import com.google.gerrit.server.project.BooleanProjectConfigTransformations;
      42             : import com.google.gerrit.server.project.ProjectResource;
      43             : import com.google.gerrit.server.project.ProjectState;
      44             : import com.google.gerrit.server.project.ProjectState.EffectiveMaxObjectSizeLimit;
      45             : import java.util.Arrays;
      46             : import java.util.LinkedHashMap;
      47             : import java.util.Map;
      48             : import java.util.TreeMap;
      49             : 
      50             : public class ConfigInfoCreator {
      51             :   /** do not instantiate this class. */
      52             :   private ConfigInfoCreator() {}
      53             : 
      54             :   @SuppressWarnings("deprecation")
      55             :   public static ConfigInfo constructInfo(
      56             :       boolean serverEnableSignedPush,
      57             :       ProjectState projectState,
      58             :       CurrentUser user,
      59             :       DynamicMap<ProjectConfigEntry> pluginConfigEntries,
      60             :       PluginConfigFactory cfgFactory,
      61             :       AllProjectsName allProjects,
      62             :       UiActions uiActions,
      63             :       DynamicMap<RestView<ProjectResource>> views) {
      64          22 :     ConfigInfo configInfo = new ConfigInfo();
      65          22 :     Project p = projectState.getProject();
      66          22 :     configInfo.description = Strings.emptyToNull(p.getDescription());
      67             : 
      68          22 :     ProjectState parentState = Iterables.getFirst(projectState.parents(), null);
      69          22 :     for (BooleanProjectConfig cfg : BooleanProjectConfig.values()) {
      70          22 :       InheritedBooleanInfo info = new InheritedBooleanInfo();
      71          22 :       info.configuredValue = p.getBooleanConfig(cfg);
      72          22 :       if (parentState != null) {
      73          22 :         info.inheritedValue = parentState.is(cfg);
      74             :       }
      75          22 :       BooleanProjectConfigTransformations.set(cfg, configInfo, info);
      76             :     }
      77             : 
      78          22 :     if (!serverEnableSignedPush) {
      79          22 :       configInfo.enableSignedPush = null;
      80          22 :       configInfo.requireSignedPush = null;
      81             :     }
      82             : 
      83          22 :     configInfo.maxObjectSizeLimit = getMaxObjectSizeLimit(projectState, p);
      84             : 
      85          22 :     configInfo.defaultSubmitType = new SubmitTypeInfo();
      86          22 :     configInfo.defaultSubmitType.value = projectState.getSubmitType();
      87          22 :     configInfo.defaultSubmitType.configuredValue =
      88          22 :         MoreObjects.firstNonNull(
      89          22 :             projectState.getConfig().getProject().getSubmitType(), Project.DEFAULT_SUBMIT_TYPE);
      90             :     ProjectState parent =
      91          22 :         projectState.isAllProjects() ? projectState : projectState.parents().get(0);
      92          22 :     configInfo.defaultSubmitType.inheritedValue = parent.getSubmitType();
      93             : 
      94          22 :     configInfo.submitType = configInfo.defaultSubmitType.value;
      95             : 
      96          22 :     configInfo.state =
      97          22 :         p.getState() != com.google.gerrit.extensions.client.ProjectState.ACTIVE
      98           4 :             ? p.getState()
      99          22 :             : null;
     100             : 
     101          22 :     configInfo.commentlinks = new LinkedHashMap<>();
     102          22 :     for (CommentLinkInfo cl : projectState.getCommentLinks()) {
     103           1 :       configInfo.commentlinks.put(cl.name, cl);
     104           1 :     }
     105             : 
     106          22 :     configInfo.pluginConfig =
     107          22 :         getPluginConfig(projectState, pluginConfigEntries, cfgFactory, allProjects);
     108             : 
     109          22 :     configInfo.actions = new TreeMap<>();
     110          22 :     for (UiAction.Description d : uiActions.from(views, new ProjectResource(projectState, user))) {
     111           0 :       configInfo.actions.put(d.getId(), new ActionInfo(d));
     112           0 :     }
     113             : 
     114          22 :     configInfo.extensionPanelNames = projectState.getConfig().getExtensionPanelSections();
     115          22 :     return configInfo;
     116             :   }
     117             : 
     118             :   private static MaxObjectSizeLimitInfo getMaxObjectSizeLimit(
     119             :       ProjectState projectState, Project p) {
     120          22 :     MaxObjectSizeLimitInfo info = new MaxObjectSizeLimitInfo();
     121          22 :     EffectiveMaxObjectSizeLimit limit = projectState.getEffectiveMaxObjectSizeLimit();
     122          22 :     long value = limit.value;
     123          22 :     info.value = value == 0 ? null : String.valueOf(value);
     124          22 :     info.configuredValue = p.getMaxObjectSizeLimit();
     125          22 :     info.summary = limit.summary;
     126          22 :     return info;
     127             :   }
     128             : 
     129             :   @Nullable
     130             :   private static Map<String, Map<String, ConfigParameterInfo>> getPluginConfig(
     131             :       ProjectState project,
     132             :       DynamicMap<ProjectConfigEntry> pluginConfigEntries,
     133             :       PluginConfigFactory cfgFactory,
     134             :       AllProjectsName allProjects) {
     135          22 :     TreeMap<String, Map<String, ConfigParameterInfo>> pluginConfig = new TreeMap<>();
     136          22 :     for (Extension<ProjectConfigEntry> e : pluginConfigEntries) {
     137           1 :       ProjectConfigEntry configEntry = e.getProvider().get();
     138           1 :       PluginConfig cfg = cfgFactory.getFromProjectConfig(project, e.getPluginName());
     139           1 :       String configuredValue = cfg.getString(e.getExportName());
     140           1 :       ConfigParameterInfo p = new ConfigParameterInfo();
     141           1 :       p.displayName = configEntry.getDisplayName();
     142           1 :       p.description = configEntry.getDescription();
     143           1 :       p.warning = configEntry.getWarning(project);
     144           1 :       p.type = configEntry.getType();
     145           1 :       p.permittedValues = configEntry.getPermittedValues();
     146           1 :       p.editable = configEntry.isEditable(project) ? true : null;
     147           1 :       if (configEntry.isInheritable() && !allProjects.equals(project.getNameKey())) {
     148           0 :         PluginConfig cfgWithInheritance =
     149           0 :             cfgFactory.getFromProjectConfigWithInheritance(project, e.getPluginName());
     150           0 :         p.inheritable = true;
     151           0 :         p.value =
     152           0 :             configEntry.onRead(
     153             :                 project,
     154           0 :                 cfgWithInheritance.getString(e.getExportName(), configEntry.getDefaultValue()));
     155           0 :         p.configuredValue = configuredValue;
     156           0 :         p.inheritedValue = getInheritedValue(project, cfgFactory, e);
     157           0 :       } else {
     158           1 :         if (configEntry.getType() == ProjectConfigEntryType.ARRAY) {
     159           0 :           p.values =
     160           0 :               configEntry.onRead(project, Arrays.asList(cfg.getStringList(e.getExportName())));
     161             :         } else {
     162           1 :           p.value =
     163           1 :               configEntry.onRead(
     164             :                   project,
     165           1 :                   configuredValue != null ? configuredValue : configEntry.getDefaultValue());
     166             :         }
     167             :       }
     168           1 :       Map<String, ConfigParameterInfo> pc = pluginConfig.get(e.getPluginName());
     169           1 :       if (pc == null) {
     170           1 :         pc = new TreeMap<>();
     171           1 :         pluginConfig.put(e.getPluginName(), pc);
     172             :       }
     173           1 :       pc.put(e.getExportName(), p);
     174           1 :     }
     175          22 :     return !pluginConfig.isEmpty() ? pluginConfig : null;
     176             :   }
     177             : 
     178             :   private static String getInheritedValue(
     179             :       ProjectState project, PluginConfigFactory cfgFactory, Extension<ProjectConfigEntry> e) {
     180           0 :     ProjectConfigEntry configEntry = e.getProvider().get();
     181           0 :     ProjectState parent = Iterables.getFirst(project.parents(), null);
     182           0 :     String inheritedValue = configEntry.getDefaultValue();
     183           0 :     if (parent != null) {
     184           0 :       PluginConfig parentCfgWithInheritance =
     185           0 :           cfgFactory.getFromProjectConfigWithInheritance(parent, e.getPluginName());
     186           0 :       inheritedValue =
     187           0 :           parentCfgWithInheritance.getString(e.getExportName(), configEntry.getDefaultValue());
     188             :     }
     189           0 :     return inheritedValue;
     190             :   }
     191             : }

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