<commit_before>import { getAdminConfig } from '../../config/admin';
import { logger } from '../../logger';
import { Pr, platform } from '../../platform';
import { BranchStatus } from '../../types';
import { isBranchModified } from '../../util/git';
import { BranchConfig } from '../types';

export async function checkAutoMerge(
  pr: Pr,
  config: BranchConfig
): Promise<boolean> {
  logger.trace({ config }, 'checkAutoMerge');
  const {
    branchName,
    automerge,
    automergeType,
    automergeComment,
    requiredStatusChecks,
    rebaseRequested,
  } = config;
  logger.debug(
    { automerge, automergeType, automergeComment },
    `Checking #${pr.number} for automerge`
  );
  if (automerge) {
    logger.debug('PR is configured for automerge');
    // Return if PR not ready for automerge
    if (pr.isConflicted) {
      logger.debug('PR is conflicted');
      logger.debug({ pr });
      return false;
    }
    if (requiredStatusChecks && pr.canMerge !== true) {
      logger.debug(
        { canMergeReason: pr.canMergeReason },
        'PR is not ready for merge'
      );
      return false;
    }
    const branchStatus = await platform.getBranchStatus(
      branchName,
      requiredStatusChecks
    );
    if (branchStatus !== BranchStatus.green) {
      logger.debug(
        `PR is not ready for merge (branch status is ${branchStatus})`
      );
      return false;
    }
    // Check if it's been touched
    if (await isBranchModified(branchName)) {
      logger.debug('PR is ready for automerge but has been modified');
      return false;
    }
    if (automergeType === 'pr-comment') {
      logger.debug(`Applying automerge comment: ${automergeComment}`);
      // istanbul ignore if
      if (getAdminConfig().dryRun) {
        logger.info(
          `DRY-RUN: Would add PR automerge comment to PR #${pr.number}`
        );
        return false;
      }
      if (rebaseRequested) {
        await platform.ensureCommentRemoval({
          number: pr.number,
          content: automergeComment,
        });
      }
      return platform.ensureComment({
        number: pr.number,
        topic: null,
        content: automergeComment,
      });
    }
    // Let's merge this
    logger.debug(`Automerging #${pr.number}`);
    // istanbul ignore if
    if (getAdminConfig().dryRun) {
      logger.info(`DRY-RUN: Would merge PR #${pr.number}`);
      return false;
    }
    const res = await platform.mergePr(pr.number, branchName);
    if (res) {
      logger.info({ pr: pr.number, prTitle: pr.title }, 'PR automerged');
    }
    return res;
  }
  logger.debug('No automerge');
  return false;
}<commit_msg>refactor: simplify pr automerge function<commit_after>import { getAdminConfig } from '../../config/admin';
import { logger } from '../../logger';
import { Pr, platform } from '../../platform';
import { BranchStatus } from '../../types';
import { isBranchModified } from '../../util/git';
import { BranchConfig } from '../types';

export async function checkAutoMerge(
  pr: Pr,
  config: BranchConfig
): Promise<boolean> {
  logger.trace({ config }, 'checkAutoMerge');
  const {
    branchName,
    automerge,
    automergeType,
    automergeComment,
    requiredStatusChecks,
    rebaseRequested,
  } = config;
  if (!automerge) {
    logger.debug('No automerge');
    return false;
  }
  logger.debug('PR is configured for automerge');
  // Return if PR not ready for automerge
  if (pr.isConflicted) {
    logger.debug('PR is conflicted');
    logger.debug({ pr });
    return false;
  }
  if (requiredStatusChecks && pr.canMerge !== true) {
    logger.debug(
      { canMergeReason: pr.canMergeReason },
      'PR is not ready for merge'
    );
    return false;
  }
  const branchStatus = await platform.getBranchStatus(
    branchName,
    requiredStatusChecks
  );
  if (branchStatus !== BranchStatus.green) {
    logger.debug(
      `PR is not ready for merge (branch status is ${branchStatus})`
    );
    return false;
  }
  // Check if it's been touched
  if (await isBranchModified(branchName)) {
    logger.debug('PR is ready for automerge but has been modified');
    return false;
  }
  if (automergeType === 'pr-comment') {
    logger.debug(`Applying automerge comment: ${automergeComment}`);
    // istanbul ignore if
    if (getAdminConfig().dryRun) {
      logger.info(
        `DRY-RUN: Would add PR automerge comment to PR #${pr.number}`
      );
      return false;
    }
    if (rebaseRequested) {
      await platform.ensureCommentRemoval({
        number: pr.number,
        content: automergeComment,
      });
    }
    return platform.ensureComment({
      number: pr.number,
      topic: null,
      content: automergeComment,
    });
  }
  // Let's merge this
  logger.debug(`Automerging #${pr.number}`);
  // istanbul ignore if
  if (getAdminConfig().dryRun) {
    logger.info(`DRY-RUN: Would merge PR #${pr.number}`);
    return false;
  }
  const res = await platform.mergePr(pr.number, branchName);
  if (res) {
    logger.info({ pr: pr.number, prTitle: pr.title }, 'PR automerged');
  }
  return res;
}
{'fraction_non_alphanumeric': 0.1345926800472255, 'fraction_numerical': 0.0, 'mean_word_length': 2.6568345323741007, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '4794280', 'n_tokens_mistral': 1546, 'n_tokens_neox': 1540, 'n_words': 414}