Skip to content
Snippets Groups Projects
Select Git revision
  • b070646186ae41b4b8bb75ffe1562a9e9cc64f22
  • main default protected
  • fix/36615b-branch-reuse-no-cache
  • renovate/main-redis-5.x
  • next
  • revert-31645-feat/rename-gradle-wrapper-validation-action
  • chore/punycode
  • 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
  • gh-readonly-queue/next/pr-35009-046ebf7cb84ab859f7fefceb5fa53a54ce9736f8
  • gh-readonly-queue/next/pr-35009-9d5e583b7d7251148ab0d11ee8dd38149618d162
  • 41.38.1
  • 41.38.0
  • 41.37.12
  • 41.37.11
  • 41.37.10
  • 41.37.9
  • 41.37.8
  • 41.37.7
  • 41.37.6
  • 41.37.5
  • 41.37.4
  • 41.37.3
  • 41.37.2
  • 41.37.1
  • 41.37.0
  • 41.36.2
  • 41.36.1
  • 41.36.0
  • 41.35.2
  • 41.35.1
41 results

validation.spec.ts

Blame
  • modules.ts 4.79 KiB
    import { logger } from '../../../logger';
    import { detectPlatform } from '../../../util/common';
    import { regEx } from '../../../util/regex';
    import { BitbucketTagsDatasource } from '../../datasource/bitbucket-tags';
    import { GitTagsDatasource } from '../../datasource/git-tags';
    import { GiteaTagsDatasource } from '../../datasource/gitea-tags';
    import { GithubTagsDatasource } from '../../datasource/github-tags';
    import { GitlabTagsDatasource } from '../../datasource/gitlab-tags';
    import { TerraformModuleDatasource } from '../../datasource/terraform-module';
    import type { PackageDependency } from '../types';
    import { extractTerragruntProvider } from './providers';
    import type { ExtractionResult, TerraformManagerData } from './types';
    
    export const githubRefMatchRegex = regEx(
      /github\.com([/:])(?<project>[^/]+\/[a-z0-9-_.]+).*\?(depth=\d+&)?ref=(?<tag>.*?)(&depth=\d+)?$/i,
    );
    export const gitTagsRefMatchRegex = regEx(
      /(?:git::)?(?<url>(?:http|https|ssh):\/\/(?:.*@)?(?<host>[^/]*)\/(?<path>.*))\?(depth=\d+&)?ref=(?<tag>.*?)(&depth=\d+)?$/,
    );
    export const tfrVersionMatchRegex = regEx(
      /tfr:\/\/(?<registry>.*?)\/(?<org>[^/]+?)\/(?<name>[^/]+?)\/(?<cloud>[^/?]+).*\?(?:ref|version)=(?<currentValue>.*?)$/,
    );
    const hostnameMatchRegex = regEx(/^(?<hostname>([\w|\d]+\.)+[\w|\d]+)/);
    
    export function extractTerragruntModule(
      startingLine: number,
      lines: string[],
    ): ExtractionResult {
      const moduleName = 'terragrunt';
      const result = extractTerragruntProvider(startingLine, lines, moduleName);
      result.dependencies.forEach((dep) => {
        // TODO #22198
        dep.managerData!.terragruntDependencyType = 'terraform';
      });
      return result;
    }
    
    function detectGitTagDatasource(registryUrl: string): string {
      const platform = detectPlatform(registryUrl);
      switch (platform) {
        case 'gitlab':
          return GitlabTagsDatasource.id;
        case 'bitbucket':
          return BitbucketTagsDatasource.id;
        case 'gitea':
          return GiteaTagsDatasource.id;
        default:
          return GitTagsDatasource.id;
      }
    }
    
    export function analyseTerragruntModule(
      dep: PackageDependency<TerraformManagerData>,
    ): void {
      // TODO #22198
      const source = dep.managerData!.source;
      const githubRefMatch = githubRefMatchRegex.exec(source ?? '');
      const gitTagsRefMatch = gitTagsRefMatchRegex.exec(source ?? '');
      const tfrVersionMatch = tfrVersionMatchRegex.exec(source ?? '');
    
      if (githubRefMatch?.groups) {
        dep.depType = 'github';
        dep.packageName = githubRefMatch.groups.project.replace(
          regEx(/\.git$/),
          '',
        );
        dep.depName = 'github.com/' + dep.packageName;
        dep.currentValue = githubRefMatch.groups.tag;
        dep.datasource = GithubTagsDatasource.id;
      } else if (gitTagsRefMatch?.groups) {
        const { url, tag } = gitTagsRefMatch.groups;
        const { hostname, host, origin, pathname, protocol } = new URL(url);
        const containsSubDirectory = pathname.includes('//');
        if (containsSubDirectory) {
          logger.debug('Terragrunt module contains subdirectory');
        }
        dep.depType = 'gitTags';
        // We don't want to have leading slash, .git or subdirectory in the repository path
        const repositoryPath = pathname
          .replace(regEx(/^\//), '')
          .split('//')[0]
          .replace(regEx('.git$'), '');
        dep.depName = `${hostname}/${repositoryPath}`;
        dep.currentValue = tag;
        dep.datasource = detectGitTagDatasource(url);
        if (dep.datasource === GitTagsDatasource.id) {
          if (containsSubDirectory) {
            dep.packageName = `${origin}${pathname.split('//')[0]}`;
          } else {
            dep.packageName = url;
          }
        } else {
          // The packageName should only contain the path to the repository
          dep.packageName = repositoryPath;
          dep.registryUrls = [
            protocol === 'https:' ? `https://${host}` : `https://${hostname}`,
          ];
        }
      } else if (tfrVersionMatch?.groups) {
        dep.depType = 'terragrunt';
        dep.depName =
          tfrVersionMatch.groups.org +
          '/' +
          tfrVersionMatch.groups.name +
          '/' +
          tfrVersionMatch.groups.cloud;
        dep.currentValue = tfrVersionMatch.groups.currentValue;
        dep.datasource = TerraformModuleDatasource.id;
        if (tfrVersionMatch.groups.registry) {
          dep.registryUrls = [`https://${tfrVersionMatch.groups.registry}`];
        }
      } else if (source) {
        const moduleParts = source.split('//')[0].split('/');
        if (moduleParts[0] === '..') {
          dep.skipReason = 'local';
        } else if (moduleParts.length >= 3) {
          const hostnameMatch = hostnameMatchRegex.exec(source);
          if (hostnameMatch?.groups) {
            dep.registryUrls = [`https://${hostnameMatch.groups.hostname}`];
          }
          dep.depType = 'terragrunt';
          dep.depName = moduleParts.join('/');
          dep.datasource = TerraformModuleDatasource.id;
        }
      } else {
        logger.debug({ dep }, 'terragrunt dep has no source');
        dep.skipReason = 'no-source';
      }
    }