<commit_before>/*
 * Copyright 2000-2016 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.runner.codedeploy;

import jetbrains.buildServer.agent.BuildProgressLogger;
import jetbrains.buildServer.util.CollectionsUtil;
import jetbrains.buildServer.util.StringUtil;
import jetbrains.buildServer.util.filters.Filter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

/**
 * @author vbedrosova
 */
public class LoggingDeploymentListener extends AWSClient.Listener {
  public static final String DEPLOY_APPLICATION = "deploy application";
  public static final String REGISTER_REVISION = "register revision";
  public static final String UPLOAD_REVISION = "upload revision";

  @NotNull
  private final Map<String, String> myRunnerParameters;
  @NotNull
  private final BuildProgressLogger myBuildLogger;
  @NotNull
  private final String myCheckoutDir;

  public LoggingDeploymentListener(@NotNull Map<String, String> runnerParameters, @NotNull BuildProgressLogger buildLogger, @NotNull String checkoutDir) {
    myRunnerParameters = runnerParameters;
    myBuildLogger = buildLogger;
    myCheckoutDir = checkoutDir;
  }

  @Override
  void uploadRevisionStarted(@NotNull File revision, @NotNull String s3BucketName, @NotNull String key) {
    open(UPLOAD_REVISION);
    log(String.format("Uploading application revision %s to S3 bucket %s using key %s", revision.getPath(), s3BucketName, key));
  }

  @Override
  void uploadRevisionFinished(@NotNull File revision, @NotNull String s3BucketName, @NotNull String key, @NotNull String url) {
    log("Uploaded application revision " + url);
    if (!CodeDeployUtil.isRegisterStepEnabled(myRunnerParameters)) {
      statusText("Uploaded " + url);
    }
    close(UPLOAD_REVISION);
  }

  @Override
  void registerRevisionStarted(@NotNull String applicationName, @NotNull String s3BucketName, @NotNull String s3ObjectKey, @NotNull String s3BundleType, @Nullable String s3ObjectVersion) {
    open(REGISTER_REVISION);
    log(String.format("Registering application %s revision from S3 bucket %s with key %s, bundle type %s and %s version", applicationName, s3BucketName, s3ObjectKey, s3BundleType, StringUtil.isEmptyOrSpaces(s3ObjectVersion) ? "latest" : s3ObjectVersion));
  }

  @Override
  void registerRevisionFinished(@NotNull String applicationName, @NotNull String s3BucketName, @NotNull String s3ObjectKey, @NotNull String s3BundleType, @Nullable String s3ObjectVersion) {
    if (!CodeDeployUtil.isDeployStepEnabled(myRunnerParameters)) {
      statusText("Registered revision");
    }
    close(REGISTER_REVISION);
  }

  @Override
  void createDeploymentStarted(@NotNull String applicationName, @NotNull String deploymentGroupName, @Nullable String deploymentConfigName) {
    open(DEPLOY_APPLICATION);
    log(String.format("Creating application %s deployment to deployment group %s with %s deployment configuration", applicationName, deploymentGroupName, StringUtil.isEmptyOrSpaces(deploymentConfigName) ? "default" : deploymentConfigName));
  }

  @Override
  void createDeploymentFinished(@NotNull String applicationName, @NotNull String deploymentGroupName, @Nullable String deploymentConfigName, @NotNull String deploymentId) {
    parameter(CodeDeployConstants.DEPLOYMENT_ID_BUILD_CONFIG_PARAM, deploymentId);
    log("Deployment " + deploymentId + " created");
  }

  @Override
  void deploymentWaitStarted(@NotNull String deploymentId) {
    log("Waiting for deployment finish");
  }


  @Override
  void deploymentInProgress(@NotNull String deploymentId, @Nullable InstancesStatus instancesStatus) {
    progress(deploymentDescription(instancesStatus, null, false));
  }

  @Override
  void deploymentFailed(@NotNull String deploymentId, @Nullable Integer timeoutSec, @Nullable ErrorInfo errorInfo, @Nullable InstancesStatus instancesStatus) {
    String msg = (timeoutSec == null ? "" : "Timeout " + timeoutSec + "sec exceeded, ");

    err(msg + deploymentDescription(instancesStatus, deploymentId, true).toLowerCase());
    msg += deploymentDescription(instancesStatus, deploymentId, false).toLowerCase();

    if (errorInfo != null) {
      if (StringUtil.isNotEmpty(errorInfo.message)) {
        err("Associated error: " + errorInfo.message);
        msg += ": " + errorInfo.message;
      }
      if (StringUtil.isNotEmpty(errorInfo.code)) {
        err("Error code: " + errorInfo.code);
      }
    }

    problem(getIdentity(timeoutSec, errorInfo, instancesStatus), timeoutSec == null ? CodeDeployConstants.FAILURE_BUILD_PROBLEM_TYPE : CodeDeployConstants.TIMEOUT_BUILD_PROBLEM_TYPE, msg);
<commit_msg>decapitalize only the first letter, made class package-local<commit_after>/*
 * Copyright 2000-2016 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.runner.codedeploy;

import jetbrains.buildServer.agent.BuildProgressLogger;
import jetbrains.buildServer.util.CollectionsUtil;
import jetbrains.buildServer.util.StringUtil;
import jetbrains.buildServer.util.filters.Filter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

/**
 * @author vbedrosova
 */
class LoggingDeploymentListener extends AWSClient.Listener {
  static final String DEPLOY_APPLICATION = "deploy application";
  static final String REGISTER_REVISION = "register revision";
  static final String UPLOAD_REVISION = "upload revision";

  @NotNull
  private final Map<String, String> myRunnerParameters;
  @NotNull
  private final BuildProgressLogger myBuildLogger;
  @NotNull
  private final String myCheckoutDir;

  LoggingDeploymentListener(@NotNull Map<String, String> runnerParameters, @NotNull BuildProgressLogger buildLogger, @NotNull String checkoutDir) {
    myRunnerParameters = runnerParameters;
    myBuildLogger = buildLogger;
    myCheckoutDir = checkoutDir;
  }

  @Override
  void uploadRevisionStarted(@NotNull File revision, @NotNull String s3BucketName, @NotNull String key) {
    open(UPLOAD_REVISION);
    log(String.format("Uploading application revision %s to S3 bucket %s using key %s", revision.getPath(), s3BucketName, key));
  }

  @Override
  void uploadRevisionFinished(@NotNull File revision, @NotNull String s3BucketName, @NotNull String key, @NotNull String url) {
    log("Uploaded application revision " + url);
    if (!CodeDeployUtil.isRegisterStepEnabled(myRunnerParameters)) {
      statusText("Uploaded " + url);
    }
    close(UPLOAD_REVISION);
  }

  @Override
  void registerRevisionStarted(@NotNull String applicationName, @NotNull String s3BucketName, @NotNull String s3ObjectKey, @NotNull String s3BundleType, @Nullable String s3ObjectVersion) {
    open(REGISTER_REVISION);
    log(String.format("Registering application %s revision from S3 bucket %s with key %s, bundle type %s and %s version", applicationName, s3BucketName, s3ObjectKey, s3BundleType, StringUtil.isEmptyOrSpaces(s3ObjectVersion) ? "latest" : s3ObjectVersion));
  }

  @Override
  void registerRevisionFinished(@NotNull String applicationName, @NotNull String s3BucketName, @NotNull String s3ObjectKey, @NotNull String s3BundleType, @Nullable String s3ObjectVersion) {
    if (!CodeDeployUtil.isDeployStepEnabled(myRunnerParameters)) {
      statusText("Registered revision");
    }
    close(REGISTER_REVISION);
  }

  @Override
  void createDeploymentStarted(@NotNull String applicationName, @NotNull String deploymentGroupName, @Nullable String deploymentConfigName) {
    open(DEPLOY_APPLICATION);
    log(String.format("Creating application %s deployment to deployment group %s with %s deployment configuration", applicationName, deploymentGroupName, StringUtil.isEmptyOrSpaces(deploymentConfigName) ? "default" : deploymentConfigName));
  }

  @Override
  void createDeploymentFinished(@NotNull String applicationName, @NotNull String deploymentGroupName, @Nullable String deploymentConfigName, @NotNull String deploymentId) {
    parameter(CodeDeployConstants.DEPLOYMENT_ID_BUILD_CONFIG_PARAM, deploymentId);
    log("Deployment " + deploymentId + " created");
  }

  @Override
  void deploymentWaitStarted(@NotNull String deploymentId) {
    log("Waiting for deployment finish");
  }


  @Override
  void deploymentInProgress(@NotNull String deploymentId, @Nullable InstancesStatus instancesStatus) {
    progress(deploymentDescription(instancesStatus, null, false));
  }

  @Override
  void deploymentFailed(@NotNull String deploymentId, @Nullable Integer timeoutSec, @Nullable ErrorInfo errorInfo, @Nullable InstancesStatus instancesStatus) {
    String msg = (timeoutSec == null ? "" : "Timeout " + timeoutSec + "sec exceeded, ");

    err(msg + StringUtil.decapitalize(deploymentDescription(instancesStatus, deploymentId, true)));
    msg += StringUtil.decapitalize(deploymentDescription(instancesStatus, deploymentId, false));

    if (errorInfo != null) {
      if (StringUtil.isNotEmpty(errorInfo.message)) {
        err("Associated error: " + errorInfo.message);
        msg += ": " + errorInfo.message;
      }
      if (StringUtil.isNotEmpty(errorInfo.code)) {
        err("Error code: " + errorInfo.code);
      }
    }

    problem(getIdentity(timeoutSec, errorInfo, instancesStatus), timeoutSec == null ? CodeDeployConstants.FAILURE_BUILD_PROBLEM_TYPE : CodeDeployConstants.TIMEOUT_BUILD_PROBLEM_TYPE, msg);

{'fraction_non_alphanumeric': 0.08936088786835056, 'fraction_numerical': 0.0057405281285878304, 'mean_word_length': 5.3274818401937045, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '382419', 'n_tokens_mistral': 2843, 'n_tokens_neox': 2773, 'n_words': 794}