view mercurial-agent/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialAgentSideVcsSupport.java @ 328:41529b72c059 Eluru-6.0.x

Add ability to specify timeout for pull operation * * * Add option teamcity.hg.pull.timeout.seconds, default value is 600
author Dmitry Neverov <dmitry.neverov@jetbrains.com>
date Fri, 28 Oct 2011 15:08:17 +0300
parents ebcca5974cb4
children
line wrap: on
line source
/*
 * Copyright 2000-2011 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.BuildProgressLogger;
import jetbrains.buildServer.agent.vcs.AgentVcsSupport;
import jetbrains.buildServer.agent.vcs.IncludeRuleUpdater;
import jetbrains.buildServer.agent.vcs.UpdateByIncludeRules2;
import jetbrains.buildServer.agent.vcs.UpdatePolicy;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.*;
import jetbrains.buildServer.vcs.CheckoutRules;
import jetbrains.buildServer.vcs.IncludeRule;
import jetbrains.buildServer.vcs.VcsException;
import jetbrains.buildServer.vcs.VcsRoot;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;

public class MercurialAgentSideVcsSupport extends AgentVcsSupport implements UpdateByIncludeRules2 {

  private final static String AGENT_PULL_TIMEOUT = "teamcity.hg.pull.timeout.seconds";

  private void updateWorkingDir(final Settings settings, final String version, final BuildProgressLogger logger) throws VcsException {
    logger.message("Updating working directory from the local repository copy");
    UpdateCommand uc = new UpdateCommand(settings);
    ChangeSet cs = new ChangeSet(version);
    uc.setToId(cs.getId());
    uc.execute();
    logger.message("Working directory updated successfully");
  }

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

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

  public IncludeRuleUpdater getUpdater(@NotNull final VcsRoot root,
                                       @NotNull CheckoutRules checkoutRules,
                                       @NotNull final String toVersion,
                                       @NotNull File checkoutDirectory,
                                       @NotNull final AgentRunningBuild build,
                                       boolean cleanCheckoutRequested) throws VcsException {
    final BuildProgressLogger logger = build.getBuildLogger();
    return new IncludeRuleUpdater() {
      public void process(@NotNull final IncludeRule includeRule, @NotNull final File workingDir) throws VcsException {
        if (includeRule.getTo() != null && includeRule.getTo().length() > 0) {
          if (!".".equals(includeRule.getFrom()) && includeRule.getFrom().length() != 0) {
            throw new VcsException("Invalid include rule: " + includeRule.toString() + ", Mercurial plugin supports mapping of the form: +:.=>dir only.");
          }
        }

        Settings settings = new Settings(workingDir, root);
        settings.setWorkingDir(workingDir);
        if (!settings.hasCopyOfRepository()) {
          logger.message("Init repository at " + workingDir.getAbsolutePath());
          new Init(settings).execute();
        } else {
          logger.message("Repository is found at " + workingDir.getAbsolutePath());
        }
        logger.message("Start pulling changes to repository at " + workingDir.getAbsolutePath());
        new PullCommand(settings).execute(getPullTimeout(build));
        logger.message("Changes successfully pulled");
        updateWorkingDir(settings, toVersion, logger);
      }

      public void dispose() throws VcsException {
      }
    };
  }


  private int getPullTimeout(@NotNull AgentRunningBuild build) {
    Integer timeout = parseTimeout(build.getSharedConfigParameters().get(AGENT_PULL_TIMEOUT));
    if (timeout != null)
      return timeout;

    return CommandUtil.DEFAULT_COMMAND_TIMEOUT_SEC;
  }


  @Nullable
  public Integer parseTimeout(@Nullable String timeoutStr) {
    if (timeoutStr == null)
      return null;
    try {
      int timeout = Integer.parseInt(timeoutStr);
      if (timeout > 0)
        return timeout;
      else
        return null;
    } catch (NumberFormatException e) {
      return null;
    }
  }
}