<commit_before>package com.hubspot.blazar.util;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.kohsuke.github.GHCommitState;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.slf4j.Logger;

import com.google.common.base.Objects;
import com.google.common.base.Splitter;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.hubspot.blazar.base.Build;
import com.hubspot.blazar.base.Build.State;
import com.hubspot.blazar.base.BuildDefinition;
import com.hubspot.blazar.data.service.BuildDefinitionService;

import io.dropwizard.Configuration;

@Singleton
public class GithubStatusHandler {

  private final Map<String, GitHub> gitHubByHost;
  private final BuildDefinitionService buildDefinitionService;
  private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(GithubStatusHandler.class);

  @Inject
  public GithubStatusHandler (EventBus eventBus, Map<String, GitHub> gitHubByHost, BuildDefinitionService buildDefinitionService) {
    this.gitHubByHost = gitHubByHost;
    this.buildDefinitionService = buildDefinitionService;

    eventBus.register(this);
  }

  @Subscribe
  public void handleBuildStateChange(Build build) throws IOException {
    LOG.info("Got event for a buildId: {}", build.getBuildNumber());
    if (build.getState() == State.QUEUED) {
      return;
    }

    BuildDefinition definition = buildDefinitionService.getByModule(build.getModuleId()).get();
    String whitelist = Objects.firstNonNull(System.getenv("BUILD_WHITELIST"), "");
    List<String> whitelistedRepos= Splitter.on(',').omitEmptyStrings().splitToList(whitelist);

    if (!whitelistedRepos.contains(definition.getGitInfo().getRepository())) {
      return;
    }

    String uiHost = "https://tools.hubteam.com";
    String appRoot = "blazar";
    String gitHost = definition.getGitInfo().getHost();
    String gitOrg = definition.getGitInfo().getOrganization();
    String gitRepo = definition.getGitInfo().getRepository();
    String gitBranch  = definition.getGitInfo().getBranch();
    String moduleName = definition.getModule().getName();
    String buildNumber = Integer.toString(build.getBuildNumber());

    String url = uiHost + "/" + appRoot + "/builds" + "/" + gitHost + "/" + gitOrg + "/" + gitRepo + "/" + gitBranch + "/" + moduleName + "/" + buildNumber;
    GHCommitState state = toGHCommitState(build.getState());
    String sha = build.getSha().get();
    String description = getStateDescription(build.getState());
    String context = "CI-blazar";

    GitHub gitHubApi = gitHubByHost.get(gitHost);

    GHRepository gitHubApiRepo = gitHubApi.getRepository(definition.getGitInfo().getFullRepositoryName());
    LOG.info("Setting status of commit {} on module {}:{} (moduleId: {}) to {}", sha, definition.getGitInfo().getFullRepositoryName(), gitBranch, build.getModuleId(), description);
    gitHubApiRepo.createCommitStatus(sha, state, url, description, context);
  }

  private String getStateDescription (State state) {
     switch (state) {
      case QUEUED:
        throw new IllegalArgumentException("Queued builds have no analagous GH Commit State");
      case LAUNCHING:
        return "The build is launching, we'll be building it soon!";
      case IN_PROGRESS:
        return "We're building your project!";
      case FAILED:
        return "The build failed.";
      case SUCCEEDED:
        return "The build succeeded!";
      case CANCELLED:
        return "Build was cancelled.";
       default:
         throw new IllegalArgumentException(String.format("No message for the build state: %s", state.toString()));
    }
  }

  private GHCommitState toGHCommitState (State state) {<commit_msg>Checkstyle fixes, and fast failing<commit_after>package com.hubspot.blazar.util;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.kohsuke.github.GHCommitState;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.slf4j.Logger;

import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.hubspot.blazar.base.Build;
import com.hubspot.blazar.base.Build.State;
import com.hubspot.blazar.base.BuildDefinition;
import com.hubspot.blazar.data.service.BuildDefinitionService;

@Singleton
public class GithubStatusHandler {

  private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(GithubStatusHandler.class);

  private final Map<String, GitHub> gitHubByHost;
  private final BuildDefinitionService buildDefinitionService;

  @Inject
  public GithubStatusHandler(EventBus eventBus, Map<String, GitHub> gitHubByHost, BuildDefinitionService buildDefinitionService) {
    this.gitHubByHost = gitHubByHost;
    this.buildDefinitionService = buildDefinitionService;

    eventBus.register(this);
  }

  @Subscribe
  public void handleBuildStateChange(Build build) throws IOException {
    LOG.info("Got event for a buildId: {}", build.getBuildNumber());
    if (build.getState() == State.QUEUED) {
      return;
    }

    BuildDefinition definition = buildDefinitionService.getByModule(build.getModuleId()).get();
    String whitelist = Objects.firstNonNull(System.getenv("BUILD_WHITELIST"), "");
    List<String> whitelistedRepos= Splitter.on(',').omitEmptyStrings().splitToList(whitelist);

    if (!whitelistedRepos.contains(definition.getGitInfo().getRepository())) {
      return;
    }

    String uiHost = "https://tools.hubteam.com";
    String appRoot = "blazar";
    String gitHost = definition.getGitInfo().getHost();
    String gitOrg = definition.getGitInfo().getOrganization();
    String gitRepo = definition.getGitInfo().getRepository();
    String gitBranch  = definition.getGitInfo().getBranch();
    String moduleName = definition.getModule().getName();
    String buildNumber = Integer.toString(build.getBuildNumber());

    String url = uiHost + "/" + appRoot + "/builds" + "/" + gitHost + "/" + gitOrg + "/" + gitRepo + "/" + gitBranch + "/" + moduleName + "/" + buildNumber;
    GHCommitState state = toGHCommitState(build.getState());
    String sha = build.getSha().get();
    String description = getStateDescription(build.getState());
    String context = "CI-blazar";

    GitHub gitHubApi = Preconditions.checkNotNull(gitHubByHost.get(gitHost));

    GHRepository gitHubApiRepo = gitHubApi.getRepository(definition.getGitInfo().getFullRepositoryName());
    LOG.info("Setting status of commit {} on module {}:{} (moduleId: {}) to {}", sha, definition.getGitInfo().getFullRepositoryName(), gitBranch, build.getModuleId(), description);
    gitHubApiRepo.createCommitStatus(sha, state, url, description, context);
  }

  private String getStateDescription (State state) {
     switch (state) {
      case QUEUED:
        throw new IllegalArgumentException("Queued builds have no analagous GH Commit State");
      case LAUNCHING:
        return "The build is launching, we'll be building it soon!";
      case IN_PROGRESS:
        return "We're building your project!";
      case FAILED:
        return "The build failed.";
      case SUCCEEDED:
        return "The build succeeded!";
      case CANCELLED:
        return "Build was cancelled.";
       default:
         throw new IllegalArgumentException(String.format("No message for the build state: %s", state.toString()));
    }
  }

  private GHCommitState toGHCommitState (State state) {
{'fraction_non_alphanumeric': 0.11770819799922047, 'fraction_numerical': 0.0005196829933740418, 'mean_word_length': 4.417311752287122, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '832184', 'n_tokens_mistral': 2279, 'n_tokens_neox': 2232, 'n_words': 491}