public void run()

in src/main/java/com/github/reviewassistant/reviewassistant/ReviewAssistant.java [405:513]


  public void run() {
    log.debug(
        "CONFIG: maxReviewers: "
            + maxReviewers
            + ", enableLoadBalancing: "
            + loadBalancing
            + ", plusTwoAge: "
            + plusTwoAge
            + ", plusTwoLimit: "
            + plusTwoLimit
            + ", plusTwoRequired: "
            + plusTwoRequired);
    PatchList patchList;
    try {
      patchList = patchListCache.get(change, ps);
    } catch (PatchListNotAvailableException e) {
      log.error("Patchlist is not available for {}", change.getKey(), e);
      return;
    }

    if (commit.getParentCount() != 1) {
      log.error("No merge/initial");
      return;
    }

    List<Entry<Account, Integer>> mergeCandidates = new ArrayList<>();
    if (plusTwoRequired) {
      mergeCandidates.addAll(getApprovalAccounts());
    }

    List<Entry<Account, Integer>> blameCandidates = new LinkedList<>();
    for (PatchListEntry entry : patchList.getPatches()) {
      /*
       * Only git blame at the moment. If other methods are used in the future,
       * other change types might be required.
       */
      if (entry.getChangeType() == ChangeType.MODIFIED
          || entry.getChangeType() == ChangeType.DELETED) {
        BlameResult blameResult = calculateBlame(commit.getParent(0), entry);
        if (blameResult != null) {
          List<Edit> edits = entry.getEdits();
          blameCandidates.addAll(getReviewers(edits, blameResult));
        } else {
          log.error("calculateBlame returned null for commit {}", commit);
        }
      }
    }

    if (loadBalancing) {
      blameCandidates = sortByOpenChanges(blameCandidates);
      if (!mergeCandidates.isEmpty()) {
        mergeCandidates = sortByOpenChanges(mergeCandidates);
      }
    }

    for (Entry<Account, Integer> e : mergeCandidates) {
      log.debug("Merge candidate: {}, score: {}", e.getKey().preferredEmail(), e.getValue());
    }

    for (Entry<Account, Integer> e : blameCandidates) {
      log.debug("Blame candidate: {}, score: {}", e.getKey().preferredEmail(), e.getValue());
    }

    Map<Account, AddReason> finalMap = new HashMap<>();
    if (blameCandidates.size() < maxReviewers) {
      Iterator<Entry<Account, Integer>> mergeItr = mergeCandidates.iterator();
      for (Entry<Account, Integer> e : blameCandidates) {
        finalMap.put(e.getKey(), AddReason.EXPERIENCE);
        log.debug("Added {} ({})", e.getKey().preferredEmail(), AddReason.EXPERIENCE);
      }
      boolean plusTwoAdded = false;
      while (finalMap.size() < maxReviewers && mergeItr.hasNext()) {
        Account account = mergeItr.next().getKey();
        if (!finalMap.containsKey(account)) {
          finalMap.put(account, AddReason.PLUS_TWO);
          log.debug("Added {} ({})", account.preferredEmail(), AddReason.PLUS_TWO);
          plusTwoAdded = true;
        }
      }
      if (!plusTwoAdded && plusTwoRequired && !mergeCandidates.isEmpty()) {
        finalMap.put(mergeCandidates.get(0).getKey(), AddReason.PLUS_TWO);
        log.debug(
            "Changed reason for {} to {}",
            mergeCandidates.get(0).getKey().preferredEmail(),
            AddReason.PLUS_TWO);
      }
    } else {
      Iterator<Entry<Account, Integer>> blameItr = blameCandidates.iterator();
      if (!mergeCandidates.isEmpty()) {
        finalMap.put(mergeCandidates.get(0).getKey(), AddReason.PLUS_TWO);
        log.debug(
            "Added {} ({})",
            mergeCandidates.get(0).getKey().preferredEmail(),
            AddReason.PLUS_TWO);
      }
      while (finalMap.size() < maxReviewers && blameItr.hasNext()) {
        Account account = blameItr.next().getKey();
        if (!finalMap.containsKey(account)) {
          finalMap.put(account, AddReason.EXPERIENCE);
          log.debug("Added {} ({})", account.preferredEmail(), AddReason.EXPERIENCE);
        }
      }
    }

    // TODO Move into addReviewers?
    realUser = true;
    addReviewers(change, finalMap);
    realUser = false;
  }