Skip to content
Snippets Groups Projects
Select Git revision
21 results Searching

branchify.ts

Blame
  • branchify.ts 6.47 KiB
    import { clean as cleanGitRef } from 'clean-git-ref';
    import slugify from 'slugify';
    import { RenovateConfig, ValidationMessage } from '../../../config';
    import { addMeta, logger, removeMeta } from '../../../logger';
    import * as template from '../../../util/template';
    import { BranchConfig, BranchUpgradeConfig } from '../../common';
    import { getChangeLogJSON } from '../../pr/changelog';
    import { flattenUpdates } from './flatten';
    import { generateBranchConfig } from './generate';
    import { Merge } from 'type-fest';
    
    /**
     * 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
    }
    
    export type BranchifiedConfig = Merge<
      RenovateConfig,
      {
        branches: BranchConfig[];
        branchList: string[];
      }
    >;
    export async function branchifyUpgrades(
      config: RenovateConfig,
      packageFiles: Record<string, any[]>
    ): Promise<BranchifiedConfig> {
      logger.debug('branchifyUpgrades');
      const updates = await flattenUpdates(config, packageFiles);
      logger.debug(
        `${updates.length} flattened updates found: ${updates
          .map((u) => u.depName)
          .join(', ')}`
      );
      const errors: ValidationMessage[] = [];
      const warnings: ValidationMessage[] = [];
      const branchUpgrades: Record<string, BranchUpgradeConfig[]> = {};
      const branches: BranchConfig[] = [];
      for (const u of updates) {
        // extract parentDir and baseDir from packageFile
        if (u.packageFile) {
          const packagePath = u.packageFile.split('/');
          if (packagePath.length > 0) {
            packagePath.splice(-1, 1);
          }
          if (packagePath.length > 0) {
            u.parentDir = packagePath[packagePath.length - 1];
            u.baseDir = packagePath.join('/');
          } else {
            u.parentDir = '';
            u.baseDir = '';
          }
        }
        const update: BranchUpgradeConfig = { ...u } as any;
        // Massage legacy vars just in case
        update.currentVersion = update.currentValue;
        update.newVersion = update.newVersion || update.newValue;
        const upper = (str: string): string =>
          str.charAt(0).toUpperCase() + str.substr(1);
        if (update.updateType) {