LCOV - code coverage report
Current view: top level - extensions/api/changes - ReviewInput.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 59 62 95.2 %
Date: 2022-11-19 15:00:39 Functions: 22 23 95.7 %

          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.extensions.api.changes;
      16             : 
      17             : import static com.google.gerrit.extensions.client.ReviewerState.CC;
      18             : import static com.google.gerrit.extensions.client.ReviewerState.REVIEWER;
      19             : 
      20             : import com.google.errorprone.annotations.CanIgnoreReturnValue;
      21             : import com.google.gerrit.extensions.client.Comment;
      22             : import com.google.gerrit.extensions.client.ReviewerState;
      23             : import com.google.gerrit.extensions.common.FixSuggestionInfo;
      24             : import com.google.gerrit.extensions.restapi.DefaultInput;
      25             : import java.util.ArrayList;
      26             : import java.util.Collections;
      27             : import java.util.LinkedHashMap;
      28             : import java.util.List;
      29             : import java.util.Map;
      30             : import java.util.Objects;
      31             : 
      32             : /** Input passed to {@code POST /changes/[id]/revisions/[id]/review}. */
      33          65 : public class ReviewInput {
      34             :   @DefaultInput public String message;
      35             : 
      36             :   public String tag;
      37             : 
      38             :   public Map<String, Short> labels;
      39             :   public Map<String, List<CommentInput>> comments;
      40             :   public Map<String, List<RobotCommentInput>> robotComments;
      41             : 
      42             :   /**
      43             :    * How to process draft comments already in the database that were not also described in this
      44             :    * input request.
      45             :    *
      46             :    * <p>If not set, the default is {@link DraftHandling#KEEP}. If {@link #onBehalfOf} is set, then
      47             :    * no other value besides {@code KEEP} is allowed.
      48             :    */
      49             :   public DraftHandling drafts;
      50             : 
      51             :   /** A list of draft IDs that should be published. */
      52             :   public List<String> draftIdsToPublish;
      53             : 
      54             :   /** Who to send email notifications to after review is stored. */
      55             :   public NotifyHandling notify;
      56             : 
      57             :   public Map<RecipientType, NotifyInfo> notifyDetails;
      58             : 
      59             :   /** If true check to make sure that the comments being posted aren't already present. */
      60             :   public boolean omitDuplicateComments;
      61             : 
      62             :   /**
      63             :    * Account ID, name, email address or username of another user. The review will be posted/updated
      64             :    * on behalf of this named user instead of the caller. Caller must have the labelAs-$NAME
      65             :    * permission granted for each label that appears in {@link #labels}. This is in addition to the
      66             :    * named user also needing to have permission to use the labels.
      67             :    */
      68             :   public String onBehalfOf;
      69             : 
      70             :   /** Reviewers that should be added to this change or removed from it. */
      71             :   public List<ReviewerInput> reviewers;
      72             : 
      73             :   /**
      74             :    * If true mark the change as work in progress. It is an error for both {@link #workInProgress}
      75             :    * and {@link #ready} to be true.
      76             :    */
      77             :   public boolean workInProgress;
      78             : 
      79             :   /**
      80             :    * If true mark the change as ready for review. It is an error for both {@link #workInProgress}
      81             :    * and {@link #ready} to be true.
      82             :    */
      83             :   public boolean ready;
      84             : 
      85             :   /** Users that should be added to the attention set of this change. */
      86             :   public List<AttentionSetInput> addToAttentionSet;
      87             : 
      88             :   /** Users that should be removed from the attention set of this change. */
      89             :   public List<AttentionSetInput> removeFromAttentionSet;
      90             : 
      91             :   /**
      92             :    * Users in the attention set will only be added and removed based on {@link #addToAttentionSet}
      93             :    * and {@link #removeFromAttentionSet}. Normally, they are also added and removed when some events
      94             :    * occur. E.g, adding/removing reviewers, marking a change ready for review or work in progress,
      95             :    * and replying on changes.
      96             :    */
      97             :   public boolean ignoreAutomaticAttentionSetRules;
      98             : 
      99          65 :   public enum DraftHandling {
     100             :     /** Leave pending drafts alone. */
     101          65 :     KEEP,
     102             : 
     103             :     /** Publish pending drafts on this revision only. */
     104          65 :     PUBLISH,
     105             : 
     106             :     /** Publish pending drafts on all revisions. */
     107          65 :     PUBLISH_ALL_REVISIONS
     108             :   }
     109             : 
     110          18 :   public static class CommentInput extends Comment {
     111             :     public Boolean unresolved;
     112             :   }
     113             : 
     114           7 :   public static class RobotCommentInput extends Comment {
     115             :     public String robotId;
     116             :     public String robotRunId;
     117             :     public String url;
     118             :     public Map<String, String> properties;
     119             :     public List<FixSuggestionInfo> fixSuggestions;
     120             :   }
     121             : 
     122             :   @CanIgnoreReturnValue
     123             :   public ReviewInput message(String msg) {
     124          12 :     message = msg != null && !msg.isEmpty() ? msg : null;
     125          12 :     return this;
     126             :   }
     127             : 
     128             :   @CanIgnoreReturnValue
     129             :   public ReviewInput patchSetLevelComment(String message) {
     130           1 :     Objects.requireNonNull(message);
     131           1 :     CommentInput comment = new CommentInput();
     132           1 :     comment.message = message;
     133             :     // TODO(davido): Because of cyclic dependency, we cannot use here Patch.PATCHSET_LEVEL constant
     134           1 :     comments = Collections.singletonMap("/PATCHSET_LEVEL", Collections.singletonList(comment));
     135           1 :     return this;
     136             :   }
     137             : 
     138             :   @CanIgnoreReturnValue
     139             :   public ReviewInput label(String name, short value) {
     140          57 :     if (name == null || name.isEmpty()) {
     141           0 :       throw new IllegalArgumentException();
     142             :     }
     143          57 :     if (labels == null) {
     144          57 :       labels = new LinkedHashMap<>(4);
     145             :     }
     146          57 :     labels.put(name, value);
     147          57 :     return this;
     148             :   }
     149             : 
     150             :   @CanIgnoreReturnValue
     151             :   public ReviewInput label(String name, int value) {
     152          57 :     if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
     153           0 :       throw new IllegalArgumentException();
     154             :     }
     155          57 :     return label(name, (short) value);
     156             :   }
     157             : 
     158             :   @CanIgnoreReturnValue
     159             :   public ReviewInput label(String name) {
     160           0 :     return label(name, (short) 1);
     161             :   }
     162             : 
     163             :   @CanIgnoreReturnValue
     164             :   public ReviewInput reviewer(String reviewer) {
     165          10 :     return reviewer(reviewer, REVIEWER, /* confirmed= */ false);
     166             :   }
     167             : 
     168             :   @CanIgnoreReturnValue
     169             :   public ReviewInput cc(String cc) {
     170           2 :     return reviewer(cc, CC, /* confirmed= */ false);
     171             :   }
     172             : 
     173             :   @CanIgnoreReturnValue
     174             :   public ReviewInput reviewer(String reviewer, ReviewerState state, boolean confirmed) {
     175          10 :     ReviewerInput input = new ReviewerInput();
     176          10 :     input.reviewer = reviewer;
     177          10 :     input.state = state;
     178          10 :     input.confirmed = confirmed;
     179          10 :     if (reviewers == null) {
     180          10 :       reviewers = new ArrayList<>();
     181             :     }
     182          10 :     reviewers.add(input);
     183          10 :     return this;
     184             :   }
     185             : 
     186             :   @CanIgnoreReturnValue
     187             :   public ReviewInput addUserToAttentionSet(String user, String reason) {
     188           7 :     AttentionSetInput input = new AttentionSetInput();
     189           7 :     input.user = user;
     190           7 :     input.reason = reason;
     191           7 :     if (addToAttentionSet == null) {
     192           7 :       addToAttentionSet = new ArrayList<>();
     193             :     }
     194           7 :     addToAttentionSet.add(input);
     195           7 :     return this;
     196             :   }
     197             : 
     198             :   @CanIgnoreReturnValue
     199             :   public ReviewInput removeUserFromAttentionSet(String user, String reason) {
     200           1 :     AttentionSetInput input = new AttentionSetInput();
     201           1 :     input.user = user;
     202           1 :     input.reason = reason;
     203           1 :     if (removeFromAttentionSet == null) {
     204           1 :       removeFromAttentionSet = new ArrayList<>();
     205             :     }
     206           1 :     removeFromAttentionSet.add(input);
     207           1 :     return this;
     208             :   }
     209             : 
     210             :   @CanIgnoreReturnValue
     211             :   public ReviewInput blockAutomaticAttentionSetRules() {
     212           2 :     ignoreAutomaticAttentionSetRules = true;
     213           2 :     return this;
     214             :   }
     215             : 
     216             :   @CanIgnoreReturnValue
     217             :   public ReviewInput setWorkInProgress(boolean workInProgress) {
     218           3 :     this.workInProgress = workInProgress;
     219           3 :     ready = !workInProgress;
     220           3 :     return this;
     221             :   }
     222             : 
     223             :   @CanIgnoreReturnValue
     224             :   public ReviewInput setReady(boolean ready) {
     225           2 :     this.ready = ready;
     226           2 :     workInProgress = !ready;
     227           2 :     return this;
     228             :   }
     229             : 
     230             :   public static ReviewInput recommend() {
     231          12 :     return new ReviewInput().label("Code-Review", 1);
     232             :   }
     233             : 
     234             :   public static ReviewInput dislike() {
     235           7 :     return new ReviewInput().label("Code-Review", -1);
     236             :   }
     237             : 
     238             :   public static ReviewInput noScore() {
     239           8 :     return new ReviewInput().label("Code-Review", 0);
     240             :   }
     241             : 
     242             :   public static ReviewInput approve() {
     243          52 :     return new ReviewInput().label("Code-Review", 2);
     244             :   }
     245             : 
     246             :   public static ReviewInput reject() {
     247          10 :     return new ReviewInput().label("Code-Review", -2);
     248             :   }
     249             : 
     250             :   public static ReviewInput create() {
     251           4 :     return new ReviewInput();
     252             :   }
     253             : }

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