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

          Line data    Source code
       1             : // Copyright (C) 2012 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.extensions.api.changes.TopicInput;
      19             : import com.google.gerrit.extensions.restapi.BadRequestException;
      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.ChangeUtil;
      25             : import com.google.gerrit.server.change.ChangeResource;
      26             : import com.google.gerrit.server.change.SetTopicOp;
      27             : import com.google.gerrit.server.permissions.ChangePermission;
      28             : import com.google.gerrit.server.permissions.PermissionBackendException;
      29             : import com.google.gerrit.server.update.BatchUpdate;
      30             : import com.google.gerrit.server.update.UpdateException;
      31             : import com.google.gerrit.server.util.time.TimeUtil;
      32             : import com.google.inject.Inject;
      33             : import com.google.inject.Singleton;
      34             : 
      35             : @Singleton
      36             : public class PutTopic
      37             :     implements RestModifyView<ChangeResource, TopicInput>, UiAction<ChangeResource> {
      38             :   private final BatchUpdate.Factory updateFactory;
      39             :   private final SetTopicOp.Factory topicOpFactory;
      40             : 
      41             :   @Inject
      42         145 :   PutTopic(BatchUpdate.Factory updateFactory, SetTopicOp.Factory topicOpFactory) {
      43         145 :     this.updateFactory = updateFactory;
      44         145 :     this.topicOpFactory = topicOpFactory;
      45         145 :   }
      46             : 
      47             :   @Override
      48             :   public Response<String> apply(ChangeResource req, TopicInput input)
      49             :       throws UpdateException, RestApiException, PermissionBackendException {
      50          13 :     req.permissions().check(ChangePermission.EDIT_TOPIC_NAME);
      51             : 
      52          13 :     if (input != null
      53             :         && input.topic != null
      54          12 :         && input.topic.length() > ChangeUtil.TOPIC_MAX_LENGTH) {
      55           1 :       throw new BadRequestException(
      56           1 :           String.format("topic length exceeds the limit (%s)", ChangeUtil.TOPIC_MAX_LENGTH));
      57             :     }
      58             : 
      59          13 :     TopicInput sanitizedInput = input == null ? new TopicInput() : input;
      60          13 :     if (sanitizedInput.topic != null) {
      61          12 :       sanitizedInput.topic = sanitizedInput.topic.trim();
      62             :     }
      63             : 
      64          13 :     SetTopicOp op = topicOpFactory.create(sanitizedInput.topic);
      65          13 :     try (BatchUpdate u =
      66          13 :         updateFactory.create(req.getChange().getProject(), req.getUser(), TimeUtil.now())) {
      67          13 :       u.addOp(req.getId(), op);
      68          13 :       u.execute();
      69             :     }
      70             : 
      71          13 :     if (Strings.isNullOrEmpty(sanitizedInput.topic)) {
      72           3 :       return Response.none();
      73             :     }
      74             : 
      75          12 :     return Response.ok(sanitizedInput.topic);
      76             :   }
      77             : 
      78             :   @Override
      79             :   public UiAction.Description getDescription(ChangeResource rsrc) {
      80          57 :     return new UiAction.Description()
      81          57 :         .setLabel("Edit Topic")
      82          57 :         .setVisible(rsrc.permissions().testCond(ChangePermission.EDIT_TOPIC_NAME));
      83             :   }
      84             : }

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