<commit_before>import { createHash } from 'crypto';
import { coerce } from 'semver';
import { parseUrlPath } from './extract';
import { skip, isSpace, removeComments } from './util';
import got from '../../util/got';
import { logger } from '../../logger';
import { Upgrade } from '../common';

// TODO: Refactor
export async function updateDependency(
  content: string,
  upgrade: Upgrade
): Promise<string> {
  logger.trace('updateDependency()');
  /*
    1. Update url field
    2. Update sha256 field
   */
  let newContent = content;
  let newUrl: string;
  let file: string;
  // Example urls:
  // "https://github.com/bazelbuild/bazel-watcher/archive/v0.8.2.tar.gz"
  // "https://github.com/aide/aide/releases/download/v0.16.1/aide-0.16.1.tar.gz"
  const oldParsedUrlPath = parseUrlPath(upgrade.managerData.url);
  if (!oldParsedUrlPath) {
    logger.debug(
      `Failed to update - upgrade.managerData.url is invalid ${upgrade.depName}`
    );
    return content;
  }
  try {
    newUrl = `https://github.com/${upgrade.managerData.ownerName}/${
      upgrade.managerData.repoName
    }/releases/download/${upgrade.newValue}/${
      upgrade.managerData.repoName
    }-${coerce(upgrade.newValue)}.tar.gz`;
    file = (await got(newUrl, {
      encoding: null,
    })).body;
  } catch (errOuter) {
    logger.debug(
      `Failed to download release download for ${upgrade.depName} - trying archive instead`
    );
    try {
      newUrl = `https://github.com/${upgrade.managerData.ownerName}/${upgrade.managerData.repoName}/archive/${upgrade.newValue}.tar.gz`;
      file = (await got(newUrl, { encoding: null })).body;
    } catch (errInner) {
      logger.debug(
        `Failed to download archive download for ${upgrade.depName} - update failed`
      );
      return content;
    }
  }
  const newParsedUrlPath = parseUrlPath(newUrl);
  if (!newParsedUrlPath) {
    logger.debug(`Failed to update url for dependency ${upgrade.depName}`);
    return content;
  }
  if (upgrade.newValue !== newParsedUrlPath.currentValue) {
    logger.debug(`Failed to update url for dependency ${upgrade.depName}`);
    return content;
  }
  const newSha256 = createHash('sha256')
    .update(file)
    .digest('hex');
  newContent = updateUrl(content, upgrade.managerData.url, newUrl);
  if (!newContent) {
    logger.debug(`Failed to update url for dependency ${upgrade.depName}`);
    return content;
  }
  newContent = updateSha256(newContent, upgrade.managerData.sha256, newSha256);
  if (!newContent) {
    logger.debug(`Failed to update sha256 for dependency ${upgrade.depName}`);
    return content;
  }
  return newContent;
}

function updateUrl(
  content: string,
  oldUrl: string,
  newUrl: string
): string | null {
  const urlRegExp = /(^|\s)url(\s)/;
  let i = content.search(urlRegExp);
  if (i === -1) {<commit_msg>fix(homebrew): try/catch sha256<commit_after>import { createHash } from 'crypto';
import { coerce } from 'semver';
import { parseUrlPath } from './extract';
import { skip, isSpace, removeComments } from './util';
import got from '../../util/got';
import { logger } from '../../logger';
import { Upgrade } from '../common';

// TODO: Refactor
export async function updateDependency(
  content: string,
  upgrade: Upgrade
): Promise<string> {
  logger.trace('updateDependency()');
  /*
    1. Update url field
    2. Update sha256 field
   */
  let newContent = content;
  let newUrl: string;
  let file: string;
  // Example urls:
  // "https://github.com/bazelbuild/bazel-watcher/archive/v0.8.2.tar.gz"
  // "https://github.com/aide/aide/releases/download/v0.16.1/aide-0.16.1.tar.gz"
  const oldParsedUrlPath = parseUrlPath(upgrade.managerData.url);
  if (!oldParsedUrlPath) {
    logger.debug(
      `Failed to update - upgrade.managerData.url is invalid ${upgrade.depName}`
    );
    return content;
  }
  try {
    newUrl = `https://github.com/${upgrade.managerData.ownerName}/${
      upgrade.managerData.repoName
    }/releases/download/${upgrade.newValue}/${
      upgrade.managerData.repoName
    }-${coerce(upgrade.newValue)}.tar.gz`;
    file = (await got(newUrl, {
      encoding: null,
    })).body;
  } catch (errOuter) {
    logger.debug(
      `Failed to download release download for ${upgrade.depName} - trying archive instead`
    );
    try {
      newUrl = `https://github.com/${upgrade.managerData.ownerName}/${upgrade.managerData.repoName}/archive/${upgrade.newValue}.tar.gz`;
      file = (await got(newUrl, { encoding: null })).body;
    } catch (errInner) {
      logger.debug(
        `Failed to download archive download for ${upgrade.depName} - update failed`
      );
      return content;
    }
  }
  const newParsedUrlPath = parseUrlPath(newUrl);
  if (!newParsedUrlPath) {
    logger.debug(`Failed to update url for dependency ${upgrade.depName}`);
    return content;
  }
  if (upgrade.newValue !== newParsedUrlPath.currentValue) {
    logger.debug(`Failed to update url for dependency ${upgrade.depName}`);
    return content;
  }
  let newSha256;
  try {
    newSha256 = createHash('sha256')
      .update(file)
      .digest('hex');
  } catch (err) /* istanbul ignore next */ {
    logger.warn({ err }, 'Failed to generate new sha256 for homebrew');
  }
  newContent = updateUrl(content, upgrade.managerData.url, newUrl);
  if (!newContent) {
    logger.debug(`Failed to update url for dependency ${upgrade.depName}`);
    return content;
  }
  newContent = updateSha256(newContent, upgrade.managerData.sha256, newSha256);
  if (!newContent) {
    logger.debug(`Failed to update sha256 for dependency ${upgrade.depName}`);
    return content;
  }
  return newContent;
}

function updateUrl(
  content: string,
  oldUrl: string,
  newUrl: string
): string | null {
  const urlRegExp = /(^|\s)url(\s)/;
  let i = content.search(urlRegExp);
  if (i === -1) {
{'fraction_non_alphanumeric': 0.14919216225507048, 'fraction_numerical': 0.013578549329666552, 'mean_word_length': 3.3296130952380953, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6515112', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1867, 'n_words': 436}