LCOV - code coverage report
Current view: top level - server/git - UsersSelfAdvertiseRefsHook.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 28 32 87.5 %
Date: 2022-11-19 15:00:39 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2020 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.git;
      16             : 
      17             : import com.google.common.flogger.FluentLogger;
      18             : import com.google.gerrit.entities.Account;
      19             : import com.google.gerrit.entities.RefNames;
      20             : import com.google.gerrit.server.CurrentUser;
      21             : import com.google.inject.Inject;
      22             : import com.google.inject.Provider;
      23             : import com.google.inject.Singleton;
      24             : import java.io.IOException;
      25             : import java.util.Map;
      26             : import org.eclipse.jgit.lib.Ref;
      27             : import org.eclipse.jgit.lib.RefDatabase;
      28             : import org.eclipse.jgit.lib.SymbolicRef;
      29             : import org.eclipse.jgit.transport.AdvertiseRefsHook;
      30             : import org.eclipse.jgit.transport.ReceivePack;
      31             : import org.eclipse.jgit.transport.ServiceMayNotContinueException;
      32             : import org.eclipse.jgit.transport.UploadPack;
      33             : 
      34             : /**
      35             :  * Advertises {@code refs/users/self} for authenticated users when interacting with the {@code
      36             :  * All-Users} repository.
      37             :  */
      38             : @Singleton
      39             : public class UsersSelfAdvertiseRefsHook implements AdvertiseRefsHook {
      40         138 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      41             : 
      42             :   private final Provider<CurrentUser> userProvider;
      43             : 
      44             :   @Inject
      45         138 :   public UsersSelfAdvertiseRefsHook(Provider<CurrentUser> userProvider) {
      46         138 :     this.userProvider = userProvider;
      47         138 :   }
      48             : 
      49             :   @Override
      50             :   public void advertiseRefs(UploadPack uploadPack) throws ServiceMayNotContinueException {
      51           6 :     CurrentUser user = userProvider.get();
      52           6 :     if (!user.isIdentifiedUser()) {
      53           0 :       return;
      54             :     }
      55             : 
      56           6 :     addSelfSymlinkIfNecessary(
      57           6 :         uploadPack.getRepository().getRefDatabase(),
      58           6 :         HookUtil.ensureAllRefsAdvertised(uploadPack),
      59           6 :         user.getAccountId());
      60           6 :   }
      61             : 
      62             :   @Override
      63             :   public void advertiseRefs(ReceivePack receivePack) throws ServiceMayNotContinueException {
      64           6 :     CurrentUser user = userProvider.get();
      65           6 :     if (!user.isIdentifiedUser()) {
      66           0 :       return;
      67             :     }
      68             : 
      69           6 :     addSelfSymlinkIfNecessary(
      70           6 :         receivePack.getRepository().getRefDatabase(),
      71           6 :         HookUtil.ensureAllRefsAdvertised(receivePack),
      72           6 :         user.getAccountId());
      73           6 :   }
      74             : 
      75             :   private static void addSelfSymlinkIfNecessary(
      76             :       RefDatabase refDatabase, Map<String, Ref> advertisedRefs, Account.Id accountId)
      77             :       throws ServiceMayNotContinueException {
      78           6 :     String refName = RefNames.refsUsers(accountId);
      79             :     try {
      80           6 :       Ref r = refDatabase.exactRef(refName);
      81           6 :       if (r == null) {
      82           2 :         logger.atWarning().log("User ref %s not found", refName);
      83           2 :         return;
      84             :       }
      85             : 
      86           6 :       SymbolicRef s = new SymbolicRef(RefNames.REFS_USERS_SELF, r);
      87           6 :       advertisedRefs.put(s.getName(), s);
      88           6 :       logger.atFinest().log("Added %s as alias for user ref %s", RefNames.REFS_USERS_SELF, refName);
      89           0 :     } catch (IOException e) {
      90           0 :       throw new ServiceMayNotContinueException(e);
      91           6 :     }
      92           6 :   }
      93             : }

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