LCOV - code coverage report
Current view: top level - server/restapi/change - PutDescription.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 39 39 100.0 %
Date: 2022-11-19 15:00:39 Functions: 5 5 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.change;
      16             : 
      17             : import com.google.common.base.Strings;
      18             : import com.google.gerrit.entities.PatchSet;
      19             : import com.google.gerrit.extensions.common.DescriptionInput;
      20             : import com.google.gerrit.extensions.restapi.Response;
      21             : import com.google.gerrit.extensions.restapi.RestApiException;
      22             : import com.google.gerrit.extensions.restapi.RestModifyView;
      23             : import com.google.gerrit.extensions.webui.UiAction;
      24             : import com.google.gerrit.server.ChangeMessagesUtil;
      25             : import com.google.gerrit.server.PatchSetUtil;
      26             : import com.google.gerrit.server.change.RevisionResource;
      27             : import com.google.gerrit.server.notedb.ChangeUpdate;
      28             : import com.google.gerrit.server.permissions.ChangePermission;
      29             : import com.google.gerrit.server.permissions.PermissionBackendException;
      30             : import com.google.gerrit.server.update.BatchUpdate;
      31             : import com.google.gerrit.server.update.BatchUpdateOp;
      32             : import com.google.gerrit.server.update.ChangeContext;
      33             : import com.google.gerrit.server.update.UpdateException;
      34             : import com.google.gerrit.server.util.time.TimeUtil;
      35             : import com.google.inject.Inject;
      36             : import com.google.inject.Singleton;
      37             : 
      38             : @Singleton
      39             : public class PutDescription
      40             :     implements RestModifyView<RevisionResource, DescriptionInput>, UiAction<RevisionResource> {
      41             :   private final BatchUpdate.Factory updateFactory;
      42             :   private final ChangeMessagesUtil cmUtil;
      43             :   private final PatchSetUtil psUtil;
      44             : 
      45             :   @Inject
      46             :   PutDescription(
      47         145 :       BatchUpdate.Factory updateFactory, ChangeMessagesUtil cmUtil, PatchSetUtil psUtil) {
      48         145 :     this.updateFactory = updateFactory;
      49         145 :     this.cmUtil = cmUtil;
      50         145 :     this.psUtil = psUtil;
      51         145 :   }
      52             : 
      53             :   @Override
      54             :   public Response<String> apply(RevisionResource rsrc, DescriptionInput input)
      55             :       throws UpdateException, RestApiException, PermissionBackendException {
      56           2 :     rsrc.permissions().check(ChangePermission.EDIT_DESCRIPTION);
      57             : 
      58           2 :     Op op = new Op(input != null ? input : new DescriptionInput(), rsrc.getPatchSet().id());
      59           2 :     try (BatchUpdate u =
      60           2 :         updateFactory.create(rsrc.getChange().getProject(), rsrc.getUser(), TimeUtil.now())) {
      61           2 :       u.addOp(rsrc.getChange().getId(), op);
      62           2 :       u.execute();
      63             :     }
      64           2 :     return Strings.isNullOrEmpty(op.newDescription)
      65           2 :         ? Response.none()
      66           1 :         : Response.ok(op.newDescription);
      67             :   }
      68             : 
      69             :   private class Op implements BatchUpdateOp {
      70             :     private final DescriptionInput input;
      71             :     private final PatchSet.Id psId;
      72             : 
      73             :     private String oldDescription;
      74             :     private String newDescription;
      75             : 
      76           2 :     Op(DescriptionInput input, PatchSet.Id psId) {
      77           2 :       this.input = input;
      78           2 :       this.psId = psId;
      79           2 :     }
      80             : 
      81             :     @Override
      82             :     public boolean updateChange(ChangeContext ctx) {
      83           2 :       ChangeUpdate update = ctx.getUpdate(psId);
      84           2 :       newDescription = Strings.nullToEmpty(input.description);
      85           2 :       oldDescription = psUtil.get(ctx.getNotes(), psId).description().orElse("");
      86           2 :       if (oldDescription.equals(newDescription)) {
      87           1 :         return false;
      88             :       }
      89           1 :       update.setPsDescription(newDescription);
      90             : 
      91             :       String summary;
      92           1 :       if (oldDescription.isEmpty()) {
      93           1 :         summary =
      94           1 :             String.format("Description of patch set %d set to \"%s\"", psId.get(), newDescription);
      95           1 :       } else if (newDescription.isEmpty()) {
      96           1 :         summary =
      97           1 :             String.format(
      98           1 :                 "Description \"%s\" removed from patch set %d", oldDescription, psId.get());
      99             :       } else {
     100           1 :         summary =
     101           1 :             String.format(
     102           1 :                 "Description of patch set %d changed to \"%s\"", psId.get(), newDescription);
     103             :       }
     104           1 :       cmUtil.setChangeMessage(update, summary, ChangeMessagesUtil.TAG_SET_DESCRIPTION);
     105           1 :       return true;
     106             :     }
     107             :   }
     108             : 
     109             :   @Override
     110             :   public UiAction.Description getDescription(RevisionResource rsrc) {
     111          57 :     return new UiAction.Description()
     112          57 :         .setLabel("Edit Description")
     113          57 :         .setVisible(rsrc.permissions().testCond(ChangePermission.EDIT_DESCRIPTION));
     114             :   }
     115             : }

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