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

          Line data    Source code
       1             : // Copyright (C) 2016 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.config;
      16             : 
      17             : import com.google.common.flogger.FluentLogger;
      18             : import com.google.gerrit.entities.ContributorAgreement;
      19             : import com.google.gerrit.entities.GroupReference;
      20             : import com.google.gerrit.exceptions.NoSuchGroupException;
      21             : import com.google.gerrit.exceptions.StorageException;
      22             : import com.google.gerrit.extensions.common.AgreementInfo;
      23             : import com.google.gerrit.server.CurrentUser;
      24             : import com.google.gerrit.server.IdentifiedUser;
      25             : import com.google.gerrit.server.account.GroupControl;
      26             : import com.google.gerrit.server.group.GroupResource;
      27             : import com.google.gerrit.server.permissions.PermissionBackendException;
      28             : import com.google.gerrit.server.restapi.group.GroupJson;
      29             : import com.google.inject.Inject;
      30             : import com.google.inject.Provider;
      31             : 
      32             : public class AgreementJson {
      33         149 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      34             : 
      35             :   private final Provider<CurrentUser> self;
      36             :   private final IdentifiedUser.GenericFactory identifiedUserFactory;
      37             :   private final GroupControl.GenericFactory genericGroupControlFactory;
      38             :   private final GroupJson groupJson;
      39             : 
      40             :   @Inject
      41             :   AgreementJson(
      42             :       Provider<CurrentUser> self,
      43             :       IdentifiedUser.GenericFactory identifiedUserFactory,
      44             :       GroupControl.GenericFactory genericGroupControlFactory,
      45         149 :       GroupJson groupJson) {
      46         149 :     this.self = self;
      47         149 :     this.identifiedUserFactory = identifiedUserFactory;
      48         149 :     this.genericGroupControlFactory = genericGroupControlFactory;
      49         149 :     this.groupJson = groupJson;
      50         149 :   }
      51             : 
      52             :   public AgreementInfo format(ContributorAgreement ca) throws PermissionBackendException {
      53           1 :     AgreementInfo info = new AgreementInfo();
      54           1 :     info.name = ca.getName();
      55           1 :     info.description = ca.getDescription();
      56           1 :     info.url = ca.getAgreementUrl();
      57           1 :     GroupReference autoVerifyGroup = ca.getAutoVerify();
      58           1 :     if (autoVerifyGroup != null && self.get().isIdentifiedUser()) {
      59           1 :       IdentifiedUser user = identifiedUserFactory.create(self.get().getAccountId());
      60             :       try {
      61           1 :         GroupControl gc = genericGroupControlFactory.controlFor(user, autoVerifyGroup.getUUID());
      62           1 :         GroupResource group = new GroupResource(gc);
      63           1 :         info.autoVerifyGroup = groupJson.format(group);
      64           0 :       } catch (NoSuchGroupException | StorageException e) {
      65           0 :         logger.atWarning().log(
      66             :             "autoverify group \"%s\" does not exist, referenced in CLA \"%s\"",
      67           0 :             autoVerifyGroup.getName(), ca.getName());
      68           1 :       }
      69             :     }
      70           1 :     return info;
      71             :   }
      72             : }

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