LCOV - code coverage report
Current view: top level - server/restapi/change - TestSubmitRule.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 29 57 50.9 %
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.change;
      16             : 
      17             : import com.google.common.base.MoreObjects;
      18             : import com.google.gerrit.entities.Project;
      19             : import com.google.gerrit.entities.SubmitRecord;
      20             : import com.google.gerrit.extensions.common.AccountInfo;
      21             : import com.google.gerrit.extensions.common.TestSubmitRuleInfo;
      22             : import com.google.gerrit.extensions.common.TestSubmitRuleInput;
      23             : import com.google.gerrit.extensions.common.TestSubmitRuleInput.Filters;
      24             : import com.google.gerrit.extensions.restapi.AuthException;
      25             : import com.google.gerrit.extensions.restapi.BadRequestException;
      26             : import com.google.gerrit.extensions.restapi.Response;
      27             : import com.google.gerrit.extensions.restapi.RestModifyView;
      28             : import com.google.gerrit.server.account.AccountLoader;
      29             : import com.google.gerrit.server.change.RevisionResource;
      30             : import com.google.gerrit.server.permissions.PermissionBackendException;
      31             : import com.google.gerrit.server.project.ProjectCache;
      32             : import com.google.gerrit.server.project.ProjectState;
      33             : import com.google.gerrit.server.query.change.ChangeData;
      34             : import com.google.gerrit.server.rules.PrologOptions;
      35             : import com.google.gerrit.server.rules.PrologRule;
      36             : import com.google.gerrit.server.rules.RulesCache;
      37             : import com.google.inject.Inject;
      38             : import java.util.LinkedHashMap;
      39             : import java.util.Optional;
      40             : import org.kohsuke.args4j.Option;
      41             : 
      42             : public class TestSubmitRule implements RestModifyView<RevisionResource, TestSubmitRuleInput> {
      43             :   private final ChangeData.Factory changeDataFactory;
      44             :   private final RulesCache rules;
      45             :   private final AccountLoader.Factory accountInfoFactory;
      46             :   private final ProjectCache projectCache;
      47             :   private final PrologRule prologRule;
      48             : 
      49          58 :   @Option(name = "--filters", usage = "impact of filters in parent projects")
      50             :   private Filters filters = Filters.RUN;
      51             : 
      52             :   @Inject
      53             :   TestSubmitRule(
      54             :       ChangeData.Factory changeDataFactory,
      55             :       RulesCache rules,
      56             :       AccountLoader.Factory infoFactory,
      57             :       ProjectCache projectCache,
      58          58 :       PrologRule prologRule) {
      59          58 :     this.changeDataFactory = changeDataFactory;
      60          58 :     this.rules = rules;
      61          58 :     this.accountInfoFactory = infoFactory;
      62          58 :     this.projectCache = projectCache;
      63          58 :     this.prologRule = prologRule;
      64          58 :   }
      65             : 
      66             :   @Override
      67             :   public Response<TestSubmitRuleInfo> apply(RevisionResource rsrc, TestSubmitRuleInput input)
      68             :       throws AuthException, PermissionBackendException, BadRequestException {
      69           2 :     if (input == null) {
      70           0 :       input = new TestSubmitRuleInput();
      71             :     }
      72           2 :     if (input.rule == null) {
      73           1 :       throw new BadRequestException("rule is required");
      74             :     }
      75           1 :     if (!rules.isProjectRulesEnabled()) {
      76           0 :       throw new AuthException("project rules are disabled");
      77             :     }
      78           1 :     input.filters = MoreObjects.firstNonNull(input.filters, filters);
      79             : 
      80           1 :     Project.NameKey name = rsrc.getProject();
      81           1 :     Optional<ProjectState> project = projectCache.get(name);
      82           1 :     if (!project.isPresent()) {
      83           0 :       throw new BadRequestException("project not found " + name);
      84             :     }
      85           1 :     ChangeData cd = changeDataFactory.create(rsrc.getNotes());
      86           1 :     SubmitRecord record =
      87           1 :         prologRule.evaluate(
      88           1 :             cd, PrologOptions.dryRunOptions(input.rule, input.filters == Filters.SKIP));
      89             : 
      90           1 :     AccountLoader accounts = accountInfoFactory.create(true);
      91           1 :     TestSubmitRuleInfo out = newSubmitRuleInfo(record, accounts);
      92           1 :     accounts.fill();
      93           1 :     return Response.ok(out);
      94             :   }
      95             : 
      96             :   private static TestSubmitRuleInfo newSubmitRuleInfo(SubmitRecord r, AccountLoader accounts) {
      97           1 :     TestSubmitRuleInfo info = new TestSubmitRuleInfo();
      98           1 :     info.status = r.status.name();
      99           1 :     info.errorMessage = r.errorMessage;
     100             : 
     101           1 :     if (r.labels != null) {
     102           0 :       for (SubmitRecord.Label n : r.labels) {
     103           0 :         AccountInfo who = n.appliedBy != null ? accounts.get(n.appliedBy) : new AccountInfo(null);
     104           0 :         label(info, n, who);
     105           0 :       }
     106             :     }
     107           1 :     return info;
     108             :   }
     109             : 
     110             :   private static void label(TestSubmitRuleInfo info, SubmitRecord.Label n, AccountInfo who) {
     111           0 :     switch (n.status) {
     112             :       case OK:
     113           0 :         if (info.ok == null) {
     114           0 :           info.ok = new LinkedHashMap<>();
     115             :         }
     116           0 :         info.ok.put(n.label, who);
     117           0 :         break;
     118             :       case REJECT:
     119           0 :         if (info.reject == null) {
     120           0 :           info.reject = new LinkedHashMap<>();
     121             :         }
     122           0 :         info.reject.put(n.label, who);
     123           0 :         break;
     124             :       case NEED:
     125           0 :         if (info.need == null) {
     126           0 :           info.need = new LinkedHashMap<>();
     127             :         }
     128           0 :         info.need.put(n.label, TestSubmitRuleInfo.None.INSTANCE);
     129           0 :         break;
     130             :       case MAY:
     131           0 :         if (info.may == null) {
     132           0 :           info.may = new LinkedHashMap<>();
     133             :         }
     134           0 :         info.may.put(n.label, who);
     135           0 :         break;
     136             :       case IMPOSSIBLE:
     137           0 :         if (info.impossible == null) {
     138           0 :           info.impossible = new LinkedHashMap<>();
     139             :         }
     140           0 :         info.impossible.put(n.label, TestSubmitRuleInfo.None.INSTANCE);
     141             :         break;
     142             :     }
     143           0 :   }
     144             : }

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