view mercurial-agent/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialAgentSideVcsSupport.java @ 1044:e7df2ef5064b

fixed extensions for recent hg versions, should fix tests
author victory.bedrosova
date Tue, 19 Jan 2021 21:35:53 +0100
parents 10dc26b32c35
children
line wrap: on
line source
/*
 * Copyright 2000-2018 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jetbrains.buildServer.buildTriggers.vcs.mercurial;

import jetbrains.buildServer.agent.AgentRunningBuild;
import jetbrains.buildServer.agent.vcs.*;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.HgVcsRoot;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.ext.CheckoutInfo;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.ext.MercurialExtension;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.ext.MercurialExtensionManager;
import jetbrains.buildServer.vcs.*;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.*;

public class MercurialAgentSideVcsSupport extends AgentVcsSupport implements UpdateByIncludeRules2 {

  private final AgentPluginConfig myConfig;
  private final MirrorManager myMirrorManager;
  private final AgentRepoFactory myRepoFactory;
  private final MercurialExtensionManager myExtentionManager;

  public MercurialAgentSideVcsSupport(@NotNull AgentPluginConfig pluginConfig,
                                      @NotNull MirrorManager mirrorManager,
                                      @NotNull AgentRepoFactory repoFactory,
                                      @NotNull MercurialExtensionManager extentionManager) {
    myConfig = pluginConfig;
    myMirrorManager = mirrorManager;
    myRepoFactory = repoFactory;
    myExtentionManager = extentionManager;
  }

  public IncludeRuleUpdater getUpdater(@NotNull final VcsRoot vcsRoot, @NotNull final CheckoutRules checkoutRules, @NotNull final String toVersion, @NotNull final File checkoutDirectory, @NotNull final AgentRunningBuild build, boolean cleanCheckoutRequested) throws VcsException {
    MercurialIncludeRuleUpdater updater;
    if (myConfig.shareLocalMirrors(build, vcsRoot)) {
      updater = new SharingMercurialUpdater(myConfig, myMirrorManager, myRepoFactory, vcsRoot, toVersion, build);
    } else {
      updater = new MercurialIncludeRuleUpdater(myConfig, myMirrorManager, myRepoFactory, vcsRoot, toVersion, build);
    }
    registerExtensions(vcsRoot, checkoutRules, updater);
    return updater;
  }

  @NotNull
  @Override
  public AgentCheckoutAbility canCheckout(@NotNull VcsRoot vcsRoot, @NotNull CheckoutRules checkoutRules, @NotNull AgentRunningBuild build) {
    CheckoutInfo info = new CheckoutInfo(myRepoFactory, new HgVcsRoot(vcsRoot), checkoutRules);
    try {
      info.getTempDirRepo().version().call();
    } catch (VcsException e) {
      return AgentCheckoutAbility.noVcsClientOnAgent(e.getMessage());
    }

    Set<String> targetDirs = new HashSet<>();
    try {
      for (IncludeRule rule : checkoutRules.getRootIncludeRules()) {
        MercurialIncludeRuleUpdater.checkRuleIsValid(rule);
        targetDirs.add(rule.getTo());
      }
    } catch (VcsException e) {
      return AgentCheckoutAbility.notSupportedCheckoutRules(e.getMessage());
    }

    List<VcsRootEntry> mercurialEntries = getMercurialEntries(build);
    if (mercurialEntries.size() > 1) {
      for (VcsRootEntry entry : mercurialEntries) {
        VcsRoot otherRoot = entry.getVcsRoot();
        if (vcsRoot.equals(otherRoot))
          continue;
        for (IncludeRule rule : getOtherRootRules(entry.getCheckoutRules().getRootIncludeRules())) {
          if (targetDirs.contains(rule.getTo()))
            return AgentCheckoutAbility.canNotCheckout("Cannot checkout VCS root '" + vcsRoot.getName() + "' into the same directory as VCS root '" + otherRoot.getName() + "'");
        }
      }
    }

    return AgentCheckoutAbility.canCheckout();
  }


  @NotNull
  private List<VcsRootEntry> getMercurialEntries(@NotNull AgentRunningBuild build) {
    List<VcsRootEntry> result = new ArrayList<>();
    for (VcsRootEntry entry : build.getVcsRootEntries()) {
      if (Constants.VCS_NAME.equals(entry.getVcsRoot().getVcsName()))
        result.add(entry);
    }
    return result;
  }


  @NotNull
  private List<IncludeRule> getOtherRootRules(@NotNull List<IncludeRule> rules) {
    List<IncludeRule> result = new ArrayList<>();
    for (IncludeRule rule : rules) {
      try {
        MercurialIncludeRuleUpdater.checkRuleIsValid(rule);
      } catch (VcsException e) {
        //return empty root rules to skip checks; appropriate cannot checkout reason
        //will be returned during otherRoot's canCheckout() call
        return Collections.emptyList();
      }
      result.add(rule);
    }
    return result;
  }


  private void registerExtensions(@NotNull VcsRoot root, @NotNull CheckoutRules checkoutRules, @NotNull MercurialIncludeRuleUpdater updater) {
    CheckoutInfo info = new CheckoutInfo(myRepoFactory, new HgVcsRoot(root), checkoutRules);
    for (MercurialExtension ext :myExtentionManager.getExtensionsForCheckout(info)) {
      updater.registerExtension(ext);
    }
  }

  @NotNull
  @Override
  public String getName() {
    return Constants.VCS_NAME;
  }

  @NotNull
  @Override
  public UpdatePolicy getUpdatePolicy() {
    return this;
  }
}