Skip to content
Snippets Groups Projects
Select Git revision
  • 62b18dee78e8066aaea5e97a43b7cbd6e03747e3
  • main default protected
  • renovate/main-vitest-monorepo
  • next
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • fix/36615-branch-reuse-bug
  • renovate/main-redis-5.x
  • renovate/main-xmldoc-2.x
  • refactor/pin-new-value
  • feat/36219--git-x509-signing
  • feat/structured-logger
  • hotfix/39.264.1
  • feat/skip-dangling
  • gh-readonly-queue/next/pr-36034-7a061c4ca1024a19e2c295d773d9642625d1c2be
  • hotfix/39.238.3
  • refactor/gitlab-auto-approve
  • feat/template-strings
  • gh-readonly-queue/next/pr-35654-137d934242c784e0c45d4b957362214f0eade1d7
  • fix/32307-global-extends-merging
  • fix/32307-global-extends-repositories
  • 41.11.1
  • 41.11.0
  • 41.10.1
  • 41.10.0
  • 41.9.0
  • 41.8.0
  • 41.7.2
  • 41.7.1
  • 41.7.0
  • 41.6.4
  • 41.6.3
  • 41.6.2
  • 41.6.1
  • 41.6.0
  • 41.5.0
  • 41.4.0
  • 41.3.0
  • 41.2.0
  • 41.1.4
  • 41.1.3
41 results

branchify.ts

Blame
  • branchify.ts 4.70 KiB
    import slugify from 'slugify';
    import handlebars from 'handlebars';
    import { clean as cleanGitRef } from 'clean-git-ref';
    import { logger, setMeta } from '../../../logger';
    
    import { generateBranchConfig } from './generate';
    import { flattenUpdates } from './flatten';
    import { RenovateConfig, ValidationMessage } from '../../../config';
    
    /**
     * Clean git branch name
     *
     * Remove what clean-git-ref fails to:
     * - leading dot/leading dot after slash
     * - trailing dot
     * - whitespace
     */
    function cleanBranchName(branchName: string): string {
      return cleanGitRef(branchName)
        .replace(/^\.|\.$/, '') // leading or trailing dot
        .replace(/\/\./g, '/') // leading dot after slash
        .replace(/\s/g, ''); // whitespace
    }
    
    // TODO: fix return type
    export function branchifyUpgrades(
      config: RenovateConfig,
      packageFiles: Record<string, any[]>
    ): RenovateConfig {
      logger.debug('branchifyUpgrades');
      const updates = flattenUpdates(config, packageFiles);
      logger.debug(
        `${updates.length} flattened updates found: ${updates
          .map(u => u.depName)
          .join(', ')}`
      );
      const errors: ValidationMessage[] = [];
      const warnings: ValidationMessage[] = [];
      const branchUpgrades = {};
      const branches = [];
      for (const u of updates) {
        const update = { ...u };
        // Massage legacy vars just in case
        update.currentVersion = update.currentValue;
        update.newVersion = update.newVersion || update.newValue;
        // massage for handlebars
        const upper = (str: string): string =>
          str.charAt(0).toUpperCase() + str.substr(1);
        if (update.updateType) {
          update[`is${upper(update.updateType)}`] = true;
        }
        // Check whether to use a group name
        if (update.groupName) {
          logger.debug('Using group branchName template');
          logger.debug(
            `Dependency ${update.depName} is part of group ${update.groupName}`
          );
          update.groupSlug = slugify(update.groupSlug || update.groupName, {
            lower: true,
          });
          if (update.updateType === 'major' && update.separateMajorMinor) {
            if (update.separateMultipleMajor) {
              update.groupSlug = `major-${update.newMajor}-${update.groupSlug}`;
            } else {
              update.groupSlug = `major-${update.groupSlug}`;
            }
          }
          if (update.updateType === 'patch') {
            update.groupSlug = `patch-${update.groupSlug}`;
          }
          update.branchTopic = update.group.branchTopic || update.branchTopic;
          update.branchName = handlebars.compile(
            update.group.branchName || update.branchName
          )(update);
        } else {
          update.branchName = handlebars.compile(update.branchName)(update);
        }
        // Compile extra times in case of nested handlebars templates
        update.branchName = handlebars.compile(update.branchName)(update);
        update.branchName = cleanBranchName(
          handlebars.compile(update.branchName)(update)
        );
    
        branchUpgrades[update.branchName] = branchUpgrades[update.branchName] || [];
        branchUpgrades[update.branchName] = [update].concat(
          branchUpgrades[update.branchName]
        );
      }
      logger.debug(`Returning ${Object.keys(branchUpgrades).length} branch(es)`);
      for (const branchName of Object.keys(branchUpgrades)) {
        setMeta({
          repository: config.repository,
          branch: branchName,
        });
        const branch = generateBranchConfig(branchUpgrades[branchName]);
        branch.branchName = branchName;
        branches.push(branch);
      }
      setMeta({
        repository: config.repository,
      });
      logger.debug(`config.repoIsOnboarded=${config.repoIsOnboarded}`);
      const branchList = config.repoIsOnboarded
        ? branches.map(upgrade => upgrade.branchName)
        : config.branchList;
      // istanbul ignore next
      try {
        // Here we check if there are updates from the same source repo
        // that are not grouped into the same branch
        const branchUpdates = {};
        for (const branch of branches) {
          const { sourceUrl, branchName, depName, toVersion } = branch;
          if (sourceUrl && toVersion) {
            const key = `${sourceUrl}|${toVersion}`;
            branchUpdates[key] = branchUpdates[key] || {};
            if (!branchUpdates[key][branchName]) {
              branchUpdates[key][branchName] = depName;
            }
          }
        }
        for (const [key, value] of Object.entries(branchUpdates)) {
          if (Object.keys(value).length > 1) {
            const [sourceUrl, toVersion] = key.split('|');
            logger.info(
              { sourceUrl, toVersion, branches: value },
              'Found sourceUrl with multiple branches that should probably be combined into a group'
            );
          }
        }
      } catch (err) {
        logger.info({ err }, 'Error checking branch duplicates');
      }
      return {
        errors: config.errors.concat(errors),
        warnings: config.warnings.concat(warnings),
        branches,
        branchList,
      };
    }