Skip to content
Snippets Groups Projects
Select Git revision
  • 3f7cbd066831b78ef19381cc93f9891fbec41e6b
  • main default
  • bugfix/update-govmomi-for-vsphere-8
  • faleksic-main-patch-45738
  • sh-debug-docker-restart-error
  • mb-update-download-link-2024-01-31
  • il-central-1
  • update-aws-govcloud-regions
  • ajwalker/golang-upgrade
  • main-patch-3e07
  • dbickford-add-darwin-arm64-build
  • darren-patch-docs
  • debug-ssh-connections
  • DarrenEastman-main-patch-57526
  • add-command-for-base-certificates-regeneration
  • ak/go-modules
  • linux-s390x
  • contribution-warning
  • nelbacha-master-patch-89734
  • gitlab-0-14-0-release
  • test-longer-creation-waiting-time-for-DO
  • v0.16.2-gitlab.25
  • v0.16.2-gitlab.24
  • v0.16.2-gitlab.23
  • v0.16.2-gitlab.22
  • v0.16.2-gitlab.21
  • v0.16.2-gitlab.20
  • v0.16.2-gitlab.19
  • v0.16.2-gitlab.18
  • v0.16.2-gitlab.17
  • v0.16.2-gitlab.16
  • v0.16.2-gitlab.15
  • v0.16.2-gitlab.15-rc2
  • v0.16.2-gitlab.15-rc1
  • v0.16.2-gitlab.14
  • v0.16.2-gitlab.13
  • v0.16.2-gitlab.12
  • v0.16.2-gitlab.11
  • v0.16.2-gitlab.10
  • v0.16.2-gitlab.9
  • v0.16.2-gitlab.8
41 results

compute_util_test.go

Blame
  • range.ts 7.62 KiB
    import is from '@sindresorhus/is';
    import semver from 'semver';
    import semverUtils from 'semver-utils';
    import { logger } from '../../../logger';
    import { regEx } from '../../../util/regex';
    import { isSemVerXRange } from '../semver/common';
    import type { NewValueConfig } from '../types';
    
    const {
      inc: increment,
      valid: isVersion,
      major,
      minor,
      patch,
      prerelease,
      satisfies,
    } = semver;
    
    function replaceCaretValue(oldValue: string, newValue: string): string {
      const toVersionMajor = major(newValue);
      const toVersionMinor = minor(newValue);
      const toVersionPatch = patch(newValue);
    
      const currentMajor = major(oldValue);
      const currentMinor = minor(oldValue);
      const currentPatch = patch(oldValue);
    
      const oldTuple = [currentMajor, currentMinor, currentPatch];
      const newTuple = [toVersionMajor, toVersionMinor, toVersionPatch];
      const resultTuple = [];
    
      let leadingZero = true;
      let needReplace = false;
      for (let idx = 0; idx < 3; idx += 1) {
        const oldVal = oldTuple[idx];
        const newVal = newTuple[idx];
    
        let leadingDigit = false;
        if (oldVal !== 0 || newVal !== 0) {
          if (leadingZero) {
            leadingZero = false;
            leadingDigit = true;
          }
        }
    
        if (leadingDigit && newVal > oldVal) {
          needReplace = true;
        }
    
        if (!needReplace && newVal < oldVal) {
          return newValue;
        }
    
        resultTuple.push(leadingDigit ? newVal : 0);
      }
    
      return needReplace ? resultTuple.join('.') : oldValue;
    }
    
    // TODO: #7154
    export function getNewValue({
      currentValue,
      rangeStrategy,
      currentVersion,
      newVersion,
    }: NewValueConfig): string | null {
      if (
        !['pin', 'update-lockfile'].includes(rangeStrategy) &&
        isSemVerXRange(currentValue)
      ) {
        return null;
      }
      if (rangeStrategy === 'pin' || isVersion(currentValue)) {
        return newVersion;
      }
      if (rangeStrategy === 'update-lockfile') {
        if (satisfies(newVersion, currentValue)) {
          return currentValue;
        }
        return getNewValue({
          currentValue,
          rangeStrategy: 'replace',
          currentVersion,
          newVersion,
        });
      }
      const parsedRange = semverUtils.parseRange(currentValue);
      const element = parsedRange[parsedRange.length - 1];
      if (rangeStrategy === 'widen') {
        if (satisfies(newVersion, currentValue)) {
          return currentValue;
        }
        const newValue = getNewValue({
          currentValue,
          rangeStrategy: 'replace',
          currentVersion,
          newVersion,
        });
        if (element.operator?.startsWith('<')) {
          // TODO fix this
          const splitCurrent = currentValue.split(element.operator);
          splitCurrent.pop();
          // TODO: types (#7154)
          return `${splitCurrent.join(element.operator)}${newValue!}`;
        }
        if (parsedRange.length > 1) {
          const previousElement = parsedRange[parsedRange.length - 2];
          if (previousElement.operator === '-') {
            const splitCurrent = currentValue.split('-');
            splitCurrent.pop();
            // TODO: types (#7154)
            return `${splitCurrent.join('-')}- ${newValue!}`;
          }
          if (element.operator?.startsWith('>')) {
            logger.warn(`Complex ranges ending in greater than are not supported`);
            return null;
          }
        }
        // TODO: types (#7154)
        return `${currentValue} || ${newValue!}`;
      }
      const toVersionMajor = major(newVersion);
      const toVersionMinor = minor(newVersion);
      const toVersionPatch = patch(newVersion);
      const toNewVersion = prerelease(newVersion);
      const suffix = toNewVersion ? `-${toNewVersion[0]}` : '';
      // Simple range
      if (rangeStrategy === 'bump') {
        if (parsedRange.length === 1) {
          if (!element.operator) {
            return getNewValue({
              currentValue,
              rangeStrategy: 'replace',
              currentVersion,
              newVersion,
            });
          }
          if (element.operator === '^') {
            const split = currentValue.split('.');
            if (suffix.length) {
              return `^${newVersion}`;
            }
            if (split.length === 1) {
              // ^4
              return `^${toVersionMajor}`;
            }
            if (split.length === 2) {
              // ^4.1
              return `^${toVersionMajor}.${toVersionMinor}`;
            }
            return `^${newVersion}`;
          }
          if (element.operator === '~') {
            const split = currentValue.split('.');
            if (suffix.length) {
              return `~${newVersion}`;
            }
            if (split.length === 1) {
              // ~4
              return `~${toVersionMajor}`;
            }
            if (split.length === 2) {
              // ~4.1
              return `~${toVersionMajor}.${toVersionMinor}`;
            }
            return `~${newVersion}`;
          }
          if (element.operator === '=') {
            return `=${newVersion}`;
          }
          if (element.operator === '>=') {
            return currentValue.includes('>= ')
              ? `>= ${newVersion}`
              : `>=${newVersion}`;
          }
          if (element.operator.startsWith('<')) {
            return currentValue;
          }
        } else {
          return semverUtils
            .parseRange(currentValue)
            .map((x) => x.semver)
            .filter(is.string)
            .map((subRange) => {
              const bumpedSubRange = getNewValue({
                currentValue: subRange,
                rangeStrategy: 'bump',
                currentVersion,
                newVersion,
              });
              if (bumpedSubRange && satisfies(newVersion, bumpedSubRange)) {
                return bumpedSubRange;
              }
    
              return getNewValue({
                currentValue: subRange,
                rangeStrategy: 'replace',
                currentVersion,
                newVersion,
              });
            })
            .filter((x) => x !== null && x !== '')
            .join(' ');
        }
        logger.debug(
          'Unsupported range type for rangeStrategy=bump: ' + currentValue
        );
        return null;
      }
      if (element.operator === '~>') {
        return `~> ${toVersionMajor}.${toVersionMinor}.0`;
      }
      if (element.operator === '^') {
        if (suffix.length || !currentVersion) {
          return `^${toVersionMajor}.${toVersionMinor}.${toVersionPatch}${suffix}`;
        }
        return `^${replaceCaretValue(currentVersion, newVersion)}`;
      }
      if (element.operator === '=') {
        return `=${newVersion}`;
      }
      if (element.operator === '~') {
        if (suffix.length) {
          return `~${toVersionMajor}.${toVersionMinor}.${toVersionPatch}${suffix}`;
        }
        return `~${toVersionMajor}.${toVersionMinor}.0`;
      }
      if (element.operator === '<=') {
        let res;
        if (element.patch || suffix.length) {
          res = `<=${newVersion}`;
        } else if (element.minor) {
          res = `<=${toVersionMajor}.${toVersionMinor}`;
        } else {
          res = `<=${toVersionMajor}`;
        }
        if (currentValue.includes('<= ')) {
          res = res.replace('<=', '<= ');
        }
        return res;
      }
      if (element.operator === '<') {
        let res;
        if (currentValue.endsWith('.0.0')) {
          const newMajor = toVersionMajor + 1;
          res = `<${newMajor}.0.0`;
        } else if (element.patch) {
          // TODO: types (#7154)
          res = `<${increment(newVersion, 'patch')!}`;
        } else if (element.minor) {
          res = `<${toVersionMajor}.${toVersionMinor + 1}`;
        } else {
          res = `<${toVersionMajor + 1}`;
        }
        if (currentValue.includes('< ')) {
          res = res.replace(regEx(/</g), '< ');
        }
        return res;
      }
      if (!element.operator) {
        if (element.minor) {
          if (element.minor === 'x') {
            return `${toVersionMajor}.x`;
          }
          if (element.minor === '*') {
            return `${toVersionMajor}.*`;
          }
          if (element.patch === 'x') {
            return `${toVersionMajor}.${toVersionMinor}.x`;
          }
          if (element.patch === '*') {
            return `${toVersionMajor}.${toVersionMinor}.*`;
          }
          return `${toVersionMajor}.${toVersionMinor}`;
        }
        return `${toVersionMajor}`;
      }
      return newVersion;
    }