<commit_before>import { logger } from '../../logger';
import { PackageDependency, UpdateDependencyConfig } from '../common';
import { extractDep } from './extract';
import * as cdnjs from '../../datasource/cdnjs';
import got from '../../util/got';
import { CdnjsAsset } from '../../datasource/cdnjs';

const integrityRegexp = /\s+integrity\s*=\s*"(?<hash>[^"]+)"/;

function extractHash(tag: string): string | null {
  let result = null;
  const match = integrityRegexp.exec(tag);
  if (match) result = match.groups.hash;
  return result;
}

async function fetchNewHash(
  dep: PackageDependency,
  newValue: string
): Promise<string | null> {
  let result = null;
  const { depName, lookupName } = dep;
  const url = cdnjs.depUrl(depName);
  const assetName = lookupName.replace(`${depName}/`, '');

  let res = null;
  try {
    res = await got(url, { json: true });
  } catch (e) /* istanbul ignore next */ {
    return null;
  }

  const assets: CdnjsAsset[] = res.body && res.body.assets;
  const asset = assets && assets.find(({ version }) => version === newValue);
  const hash = asset && asset.sri && asset.sri[assetName];
  if (hash) result = hash;
  return result;
}

export async function updateDependency({
  fileContent,
  upgrade,
}: UpdateDependencyConfig): Promise<string> {
  const { currentValue, newValue, managerData } = upgrade;
  const { tagPosition, tagLength } = managerData;
  const leftPart = fileContent.slice(0, tagPosition);
  const tagPart = fileContent.slice(tagPosition, tagPosition + tagLength);
  const rightPart = fileContent.slice(tagPosition + tagLength);
  const dep = extractDep(tagPart);
  if (dep.currentValue === newValue) {
    return fileContent;
  }
  if (dep.currentValue === currentValue) {
    const currentHash = extractHash(tagPart);
    let tag = tagPart.replace(currentValue, newValue);
    if (currentHash) {
      const newHash = await fetchNewHash(dep, newValue);
      if (!newHash) {
        logger.error(`Could not upgrade SRI hash`);
        return null;
      }
      tag = tag.replace(currentHash, newHash);
    }
    return `${leftPart}${tag}${rightPart}`;
  }
  return null;
}<commit_msg>fix(html): Log and return for wrong managerData (#5513)<commit_after>import { logger } from '../../logger';
import { PackageDependency, UpdateDependencyConfig } from '../common';
import { extractDep } from './extract';
import * as cdnjs from '../../datasource/cdnjs';
import got from '../../util/got';
import { CdnjsAsset } from '../../datasource/cdnjs';

const integrityRegexp = /\s+integrity\s*=\s*"(?<hash>[^"]+)"/;

function extractHash(tag: string): string | null {
  let result = null;
  const match = integrityRegexp.exec(tag);
  if (match) result = match.groups.hash;
  return result;
}

async function fetchNewHash(
  dep: PackageDependency,
  newValue: string
): Promise<string | null> {
  let result = null;
  const { depName, lookupName } = dep;
  const url = cdnjs.depUrl(depName);
  const assetName = lookupName.replace(`${depName}/`, '');

  let res = null;
  try {
    res = await got(url, { json: true });
  } catch (e) /* istanbul ignore next */ {
    return null;
  }

  const assets: CdnjsAsset[] = res.body && res.body.assets;
  const asset = assets && assets.find(({ version }) => version === newValue);
  const hash = asset && asset.sri && asset.sri[assetName];
  if (hash) result = hash;
  return result;
}

export async function updateDependency({
  fileContent,
  upgrade,
}: UpdateDependencyConfig): Promise<string> {
  const { currentValue, newValue, managerData } = upgrade;
  const { tagPosition, tagLength } = managerData;
  const leftPart = fileContent.slice(0, tagPosition);
  const tagPart = fileContent.slice(tagPosition, tagPosition + tagLength);
  const rightPart = fileContent.slice(tagPosition + tagLength);
  const dep = extractDep(tagPart);
  /* istanbul ignore if */
  if (!dep) {
    logger.warn(`Could not upgrade html`);
    return null;
  }
  if (dep.currentValue === newValue) {
    return fileContent;
  }
  if (dep.currentValue === currentValue) {
    const currentHash = extractHash(tagPart);
    let tag = tagPart.replace(currentValue, newValue);
    if (currentHash) {
      const newHash = await fetchNewHash(dep, newValue);
      if (!newHash) {
        logger.error(`Could not upgrade SRI hash`);
        return null;
      }
      tag = tag.replace(currentHash, newHash);
    }
    return `${leftPart}${tag}${rightPart}`;
  }
  return null;
}
{'fraction_non_alphanumeric': 0.14834794335805798, 'fraction_numerical': 0.0013486176668914363, 'mean_word_length': 3.6842105263157894, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '5255768', 'n_tokens_mistral': 1375, 'n_tokens_neox': 1368, 'n_words': 374}