<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.hive.ptest.execution;

import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;

import org.apache.commons.io.FilenameUtils;
import org.apache.hive.ptest.api.server.TestLogger;
import org.apache.hive.ptest.execution.conf.Context;
import org.apache.hive.ptest.execution.conf.TestConfiguration;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

class JIRAService {
  static final int MAX_MESSAGES = 200;
  static final String TRIMMED_MESSAGE = "**** This message was trimmed, see log for full details ****";
  private final Logger mLogger;
  private final String mName;
  private final String mBuildTag;
  private final String mPatch;
  private final String mUrl;
  private final String mUser;
  private final String mPassword;
  private final String mJenkinsURL;
  private final String mLogsURL;

  public JIRAService(Logger logger, TestConfiguration configuration, String buildTag) {
    mLogger = logger;
    mName = configuration.getJiraName();
    mBuildTag = buildTag;
    mPatch = configuration.getPatch();
    mUrl = configuration.getJiraUrl();
    mUser = configuration.getJiraUser();
    mPassword = configuration.getJiraPassword();
    mJenkinsURL = configuration.getJenkinsURL();
    mLogsURL = configuration.getLogsURL();
  }

  void postComment(boolean error, int numTestsExecuted, SortedSet<String> failedTests,
      List<String> messages) {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    try {
      BuildInfo buildInfo = formatBuildTag(mBuildTag);
      String buildTagForLogs = formatBuildTagForLogs(mBuildTag);
      List<String> comments = Lists.newArrayList();
      comments.add("");
      comments.add("");
      if(!failedTests.isEmpty()) {
        comments.add("{color:red}Overall{color}: -1 at least one tests failed");
      } else if(numTestsExecuted == 0) {
        comments.add("{color:red}Overall{color}: -1 no tests executed");
      } else if (error) {
        comments.add("{color:red}Overall{color}: -1 build exited with an error");
      } else {
        comments.add("{color:green}Overall{color}: +1 all checks pass");
      }
      comments.add("");
      if(!mPatch.isEmpty()) {
        comments.add("Here are the results of testing the latest attachment:");
        comments.add(mPatch);
      }
      comments.add("");
      if(numTestsExecuted > 0) {
        if (failedTests.isEmpty()) {
          comments.add(formatSuccess("+1 "+ numTestsExecuted + " tests passed"));
        } else {
          comments.add(formatError("-1 due to " + failedTests.size()
              + " failed/errored test(s), " + numTestsExecuted + " tests executed"));
          comments.add("*Failed tests:*");
          comments.add("{noformat}");
          comments.addAll(failedTests);
          comments.add("{noformat}");
        }
        comments.add("");
      }
      comments.add("Test results: " + mJenkinsURL + "/" +
          buildInfo.getFormattedBuildTag() + "/testReport");
      comments.add("Console output: " + mJenkinsURL + "/" +
          buildInfo.getFormattedBuildTag() + "/console");
      comments.add("Test logs: " + mLogsURL + buildTagForLogs);
      comments.add("");
      if(!messages.isEmpty()) {
        comments.add("Messages:");
        comments.add("{noformat}");
        comments.addAll(trimMessages(messages));
        comments.add("{noformat}");
        comments.add("");
      }
      comments.add("This message is automatically generated.");
      String attachmentId = parseAttachementId(mPatch);
      if(!attachmentId.isEmpty()) {
        comments.add("");
        comments.add("ATTACHMENT ID: " + attachmentId);
      }
      if(!buildInfo.getBuildName().isEmpty()) {
        comments.add(" - " + buildInfo.getBuildName());
      }
      mLogger.info("Comment: " + Joiner.on("\n").join(comments));
      String body = Joiner.on("\n").join(comments);
      String url = String.format("%s/rest/api/2/issue/%s/comment", mUrl, mName);
      URL apiURL = new URL(mUrl);
      httpClient.getCredentialsProvider()
      .setCredentials(
          new AuthScope(apiURL.getHost(), apiURL.getPort(),
              AuthScope.ANY_REALM),
              new UsernamePasswordCredentials(mUser, mPassword));<commit_msg>HIVE-8491 - Fix build name in ptest pre-commit message (<NAME> via Brock)<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.hive.ptest.execution;

import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.SortedSet;

import org.apache.commons.io.FilenameUtils;
import org.apache.hive.ptest.api.server.TestLogger;
import org.apache.hive.ptest.execution.conf.Context;
import org.apache.hive.ptest.execution.conf.TestConfiguration;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScheme;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

class JIRAService {
  static final int MAX_MESSAGES = 200;
  static final String TRIMMED_MESSAGE = "**** This message was trimmed, see log for full details ****";
  private final Logger mLogger;
  private final String mName;
  private final String mBuildTag;
  private final String mPatch;
  private final String mUrl;
  private final String mUser;
  private final String mPassword;
  private final String mJenkinsURL;
  private final String mLogsURL;

  public JIRAService(Logger logger, TestConfiguration configuration, String buildTag) {
    mLogger = logger;
    mName = configuration.getJiraName();
    mBuildTag = buildTag;
    mPatch = configuration.getPatch();
    mUrl = configuration.getJiraUrl();
    mUser = configuration.getJiraUser();
    mPassword = configuration.getJiraPassword();
    mJenkinsURL = configuration.getJenkinsURL();
    mLogsURL = configuration.getLogsURL();
  }

  void postComment(boolean error, int numTestsExecuted, SortedSet<String> failedTests,
      List<String> messages) {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    try {
      BuildInfo buildInfo = formatBuildTag(mBuildTag);
      String buildTagForLogs = formatBuildTagForLogs(mBuildTag);
      List<String> comments = Lists.newArrayList();
      comments.add("");
      comments.add("");
      if(!failedTests.isEmpty()) {
        comments.add("{color:red}Overall{color}: -1 at least one tests failed");
      } else if(numTestsExecuted == 0) {
        comments.add("{color:red}Overall{color}: -1 no tests executed");
      } else if (error) {
        comments.add("{color:red}Overall{color}: -1 build exited with an error");
      } else {
        comments.add("{color:green}Overall{color}: +1 all checks pass");
      }
      comments.add("");
      if(!mPatch.isEmpty()) {
        comments.add("Here are the results of testing the latest attachment:");
        comments.add(mPatch);
      }
      comments.add("");
      if(numTestsExecuted > 0) {
        if (failedTests.isEmpty()) {
          comments.add(formatSuccess("+1 "+ numTestsExecuted + " tests passed"));
        } else {
          comments.add(formatError("-1 due to " + failedTests.size()
              + " failed/errored test(s), " + numTestsExecuted + " tests executed"));
          comments.add("*Failed tests:*");
          comments.add("{noformat}");
          comments.addAll(failedTests);
          comments.add("{noformat}");
        }
        comments.add("");
      }
      comments.add("Test results: " + mJenkinsURL + "/" +
          buildInfo.getFormattedBuildTag() + "/testReport");
      comments.add("Console output: " + mJenkinsURL + "/" +
          buildInfo.getFormattedBuildTag() + "/console");
      comments.add("Test logs: " + mLogsURL + buildTagForLogs);
      comments.add("");
      if(!messages.isEmpty()) {
        comments.add("Messages:");
        comments.add("{noformat}");
        comments.addAll(trimMessages(messages));
        comments.add("{noformat}");
        comments.add("");
      }
      comments.add("This message is automatically generated.");
      String attachmentId = parseAttachementId(mPatch);
      comments.add("");
      comments.add("ATTACHMENT ID: " + attachmentId +
          " - " + buildInfo.getBuildName());
      mLogger.info("Comment: " + Joiner.on("\n").join(comments));
      String body = Joiner.on("\n").join(comments);
      String url = String.format("%s/rest/api/2/issue/%s/comment", mUrl, mName);
      URL apiURL = new URL(mUrl);
      httpClient.getCredentialsProvider()
      .setCredentials(
          new AuthScope(apiURL.getHost(), apiURL.getPort(),
              AuthScope.ANY_REALM),
              new UsernamePasswordCredentials(mUser, mPassword));
{'fraction_non_alphanumeric': 0.12061880589799372, 'fraction_numerical': 0.003061800016114737, 'mean_word_length': 3.648689138576779, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 12, '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': '6650746', 'n_tokens_mistral': 3547, 'n_tokens_neox': 3519, 'n_words': 854}