LCOV - code coverage report
Current view: top level - pgm/init - ExternalIdsOnInit.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 7 25 28.0 %
Date: 2022-11-19 15:00:39 Functions: 1 3 33.3 %

          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.pgm.init;
      16             : 
      17             : import com.google.gerrit.pgm.init.api.AllUsersNameOnInitProvider;
      18             : import com.google.gerrit.pgm.init.api.InitFlags;
      19             : import com.google.gerrit.server.GerritPersonIdentProvider;
      20             : import com.google.gerrit.server.account.externalids.ExternalId;
      21             : import com.google.gerrit.server.account.externalids.ExternalIdFactory;
      22             : import com.google.gerrit.server.account.externalids.ExternalIdNotes;
      23             : import com.google.gerrit.server.config.AllUsersName;
      24             : import com.google.gerrit.server.config.AuthConfig;
      25             : import com.google.gerrit.server.config.SitePaths;
      26             : import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
      27             : import com.google.gerrit.server.git.meta.MetaDataUpdate;
      28             : import com.google.inject.Inject;
      29             : import java.io.File;
      30             : import java.io.IOException;
      31             : import java.nio.file.Path;
      32             : import java.util.Collection;
      33             : import org.eclipse.jgit.errors.ConfigInvalidException;
      34             : import org.eclipse.jgit.internal.storage.file.FileRepository;
      35             : import org.eclipse.jgit.lib.PersonIdent;
      36             : import org.eclipse.jgit.lib.Repository;
      37             : import org.eclipse.jgit.lib.RepositoryCache.FileKey;
      38             : import org.eclipse.jgit.util.FS;
      39             : 
      40             : public class ExternalIdsOnInit {
      41             :   private final InitFlags flags;
      42             :   private final SitePaths site;
      43             :   private final AllUsersName allUsers;
      44             :   private final ExternalIdFactory externalIdFactory;
      45             :   private final AuthConfig authConfig;
      46             : 
      47             :   @Inject
      48             :   public ExternalIdsOnInit(
      49             :       InitFlags flags,
      50             :       SitePaths site,
      51             :       AllUsersNameOnInitProvider allUsers,
      52             :       ExternalIdFactory externalIdFactory,
      53          15 :       AuthConfig authConfig) {
      54          15 :     this.flags = flags;
      55          15 :     this.site = site;
      56          15 :     this.allUsers = new AllUsersName(allUsers.get());
      57          15 :     this.externalIdFactory = externalIdFactory;
      58          15 :     this.authConfig = authConfig;
      59          15 :   }
      60             : 
      61             :   public synchronized void insert(String commitMessage, Collection<ExternalId> extIds)
      62             :       throws IOException, ConfigInvalidException {
      63           0 :     File path = getPath();
      64           0 :     if (path != null) {
      65           0 :       try (Repository allUsersRepo = new FileRepository(path)) {
      66           0 :         ExternalIdNotes extIdNotes =
      67           0 :             ExternalIdNotes.load(
      68             :                 allUsers,
      69             :                 allUsersRepo,
      70             :                 externalIdFactory,
      71           0 :                 authConfig.isUserNameCaseInsensitiveMigrationMode());
      72           0 :         extIdNotes.insert(extIds);
      73           0 :         try (MetaDataUpdate metaDataUpdate =
      74             :             new MetaDataUpdate(GitReferenceUpdated.DISABLED, allUsers, allUsersRepo)) {
      75           0 :           PersonIdent serverIdent = new GerritPersonIdentProvider(flags.cfg).get();
      76           0 :           metaDataUpdate.getCommitBuilder().setAuthor(serverIdent);
      77           0 :           metaDataUpdate.getCommitBuilder().setCommitter(serverIdent);
      78           0 :           metaDataUpdate.getCommitBuilder().setMessage(commitMessage);
      79           0 :           extIdNotes.commit(metaDataUpdate);
      80             :         }
      81             :       }
      82             :     }
      83           0 :   }
      84             : 
      85             :   private File getPath() {
      86           0 :     Path basePath = site.resolve(flags.cfg.getString("gerrit", null, "basePath"));
      87           0 :     if (basePath == null) {
      88           0 :       throw new IllegalStateException("gerrit.basePath must be configured");
      89             :     }
      90           0 :     return FileKey.resolve(basePath.resolve(allUsers.get()).toFile(), FS.DETECTED);
      91             :   }
      92             : }

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