<commit_before>import yaml from 'js-yaml';
import { logger } from '../../logger';
import { getDep } from '../dockerfile/extract';

import { PackageFile, PackageDependency } from '../common';
import {
  matchesHelmValuesDockerHeuristic,
  HelmDockerImageDependency,
} from './util';

/**
 * Recursively find all supported dependencies in the yaml object.
 *
 * @param parsedContent
 */
function findDependencies(
  parsedContent: object | HelmDockerImageDependency,
  packageDependencies: Array<PackageDependency>
): Array<PackageDependency> {
  if (!parsedContent || typeof parsedContent !== 'object') {
    return packageDependencies;
  }

  Object.keys(parsedContent).forEach(key => {
    if (matchesHelmValuesDockerHeuristic(key, parsedContent[key])) {
      const currentItem = parsedContent[key];

      const registry = currentItem.registry ? `${currentItem.registry}/` : '';
      packageDependencies.push(
        getDep(`${registry}${currentItem.repository}:${currentItem.tag}`)
      );
    } else {
      findDependencies(parsedContent[key], packageDependencies);
    }
  });
  return packageDependencies;
}

export function extractPackageFile(content: string): PackageFile {
  try {
    // a parser that allows extracting line numbers would be preferable, with
    // the current approach we need to match anything we find again during the update
    const parsedContent = yaml.safeLoad(content, { json: true });

    logger.debug(
      { parsedContent },
      'Trying to find dependencies in helm-values'
    );
    const deps = findDependencies(parsedContent, []);

    if (deps.length) {
      logger.debug({ deps }, 'Found dependencies in helm-values');
      return { deps };
    }
  } catch (err) {
    logger.error({ err }, 'Failed to parse helm-values file');
  }

  return null;
}<commit_msg>fix(helm-values): better extract error handling<commit_after>import yaml from 'js-yaml';
import { logger } from '../../logger';
import { getDep } from '../dockerfile/extract';

import { PackageFile, PackageDependency } from '../common';
import {
  matchesHelmValuesDockerHeuristic,
  HelmDockerImageDependency,
} from './util';

/**
 * Recursively find all supported dependencies in the yaml object.
 *
 * @param parsedContent
 */
function findDependencies(
  parsedContent: object | HelmDockerImageDependency,
  packageDependencies: Array<PackageDependency>
): Array<PackageDependency> {
  if (!parsedContent || typeof parsedContent !== 'object') {
    return packageDependencies;
  }

  Object.keys(parsedContent).forEach(key => {
    if (matchesHelmValuesDockerHeuristic(key, parsedContent[key])) {
      const currentItem = parsedContent[key];

      const registry = currentItem.registry ? `${currentItem.registry}/` : '';
      packageDependencies.push(
        getDep(`${registry}${currentItem.repository}:${currentItem.tag}`)
      );
    } else {
      findDependencies(parsedContent[key], packageDependencies);
    }
  });
  return packageDependencies;
}

export function extractPackageFile(content: string): PackageFile {
  let parsedContent;
  try {
    // a parser that allows extracting line numbers would be preferable, with
    // the current approach we need to match anything we find again during the update
    parsedContent = yaml.safeLoad(content, { json: true });
  } catch (err) {
    logger.info({ err }, 'Failed to parse helm-values YAML');
    return null;
  }
  try {
    const deps = findDependencies(parsedContent, []);
    if (deps.length) {
      logger.debug({ deps }, 'Found dependencies in helm-values');
      return { deps };
    }
  } catch (err) /* istanbul ignore next */ {
    logger.error({ err }, 'Error parsing helm-values parsed content');
  }
  return null;
}
{'fraction_non_alphanumeric': 0.12261360580801291, 'fraction_numerical': 0.0, 'mean_word_length': 3.8124191461837, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '5506121', 'n_tokens_mistral': 1039, 'n_tokens_neox': 1005, 'n_words': 280}