LCOV - code coverage report
Current view: top level - server/restapi/account - GetWatchedProjects.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 26 26 100.0 %
Date: 2022-11-19 15:00:39 Functions: 7 7 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.account;
      16             : 
      17             : import static java.util.Comparator.comparing;
      18             : import static java.util.stream.Collectors.toList;
      19             : 
      20             : import com.google.common.base.Strings;
      21             : import com.google.common.collect.ImmutableSet;
      22             : import com.google.gerrit.common.Nullable;
      23             : import com.google.gerrit.entities.Account;
      24             : import com.google.gerrit.entities.NotifyConfig.NotifyType;
      25             : import com.google.gerrit.extensions.client.ProjectWatchInfo;
      26             : import com.google.gerrit.extensions.restapi.AuthException;
      27             : import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
      28             : import com.google.gerrit.extensions.restapi.Response;
      29             : import com.google.gerrit.extensions.restapi.RestReadView;
      30             : import com.google.gerrit.server.IdentifiedUser;
      31             : import com.google.gerrit.server.account.AccountResource;
      32             : import com.google.gerrit.server.account.AccountState;
      33             : import com.google.gerrit.server.account.Accounts;
      34             : import com.google.gerrit.server.account.ProjectWatches.ProjectWatchKey;
      35             : import com.google.gerrit.server.permissions.GlobalPermission;
      36             : import com.google.gerrit.server.permissions.PermissionBackend;
      37             : import com.google.gerrit.server.permissions.PermissionBackendException;
      38             : import com.google.inject.Inject;
      39             : import com.google.inject.Provider;
      40             : import com.google.inject.Singleton;
      41             : import java.io.IOException;
      42             : import java.util.List;
      43             : import org.eclipse.jgit.errors.ConfigInvalidException;
      44             : 
      45             : /**
      46             :  * REST endpoint to get the project watches of an account.
      47             :  *
      48             :  * <p>This REST endpoint handles {@code GET /accounts/<account-identifier>/watched.projects}
      49             :  * requests.
      50             :  */
      51             : @Singleton
      52             : public class GetWatchedProjects implements RestReadView<AccountResource> {
      53             :   private final PermissionBackend permissionBackend;
      54             :   private final Provider<IdentifiedUser> self;
      55             :   private final Accounts accounts;
      56             : 
      57             :   @Inject
      58             :   public GetWatchedProjects(
      59         148 :       PermissionBackend permissionBackend, Provider<IdentifiedUser> self, Accounts accounts) {
      60         148 :     this.permissionBackend = permissionBackend;
      61         148 :     this.self = self;
      62         148 :     this.accounts = accounts;
      63         148 :   }
      64             : 
      65             :   @Override
      66             :   public Response<List<ProjectWatchInfo>> apply(AccountResource rsrc)
      67             :       throws AuthException, IOException, ConfigInvalidException, PermissionBackendException,
      68             :           ResourceNotFoundException {
      69          16 :     if (!self.get().hasSameAccountId(rsrc.getUser())) {
      70           3 :       permissionBackend.currentUser().check(GlobalPermission.ADMINISTRATE_SERVER);
      71             :     }
      72             : 
      73          16 :     Account.Id accountId = rsrc.getUser().getAccountId();
      74          16 :     AccountState account = accounts.get(accountId).orElseThrow(ResourceNotFoundException::new);
      75          16 :     return Response.ok(
      76          16 :         account.projectWatches().entrySet().stream()
      77          16 :             .map(e -> toProjectWatchInfo(e.getKey(), e.getValue()))
      78          16 :             .sorted(
      79          16 :                 comparing((ProjectWatchInfo pwi) -> pwi.project)
      80          16 :                     .thenComparing(pwi -> Strings.nullToEmpty(pwi.filter)))
      81          16 :             .collect(toList()));
      82             :   }
      83             : 
      84             :   private static ProjectWatchInfo toProjectWatchInfo(
      85             :       ProjectWatchKey key, ImmutableSet<NotifyType> watchTypes) {
      86          16 :     ProjectWatchInfo pwi = new ProjectWatchInfo();
      87          16 :     pwi.filter = key.filter();
      88          16 :     pwi.project = key.project().get();
      89          16 :     pwi.notifyAbandonedChanges = toBoolean(watchTypes.contains(NotifyType.ABANDONED_CHANGES));
      90          16 :     pwi.notifyNewChanges = toBoolean(watchTypes.contains(NotifyType.NEW_CHANGES));
      91          16 :     pwi.notifyNewPatchSets = toBoolean(watchTypes.contains(NotifyType.NEW_PATCHSETS));
      92          16 :     pwi.notifySubmittedChanges = toBoolean(watchTypes.contains(NotifyType.SUBMITTED_CHANGES));
      93          16 :     pwi.notifyAllComments = toBoolean(watchTypes.contains(NotifyType.ALL_COMMENTS));
      94          16 :     return pwi;
      95             :   }
      96             : 
      97             :   @Nullable
      98             :   private static Boolean toBoolean(boolean value) {
      99          16 :     return value ? true : null;
     100             :   }
     101             : }

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