github.com/argoproj/argo-cd/v3@v3.2.1/ui/src/app/shared/components/urls.ts (about)

     1  import {GitUrl} from 'git-url-parse';
     2  import {isSHA} from './revision';
     3  import {isValidURL} from '../../shared/utils';
     4  
     5  const GitUrlParse = require('git-url-parse');
     6  
     7  function supportedSource(parsed: GitUrl): boolean {
     8      return parsed.resource.startsWith('github') || ['gitlab.com', 'bitbucket.org'].indexOf(parsed.source) >= 0;
     9  }
    10  
    11  function protocol(proto: string): string {
    12      return proto === 'ssh' ? 'https' : proto;
    13  }
    14  
    15  export function repoUrl(url: string): string {
    16      try {
    17          const parsed = GitUrlParse(url);
    18  
    19          if (!supportedSource(parsed)) {
    20              return null;
    21          }
    22  
    23          const parsedUrl = `${protocol(parsed.protocol)}://${parsed.resource}/${parsed.owner}/${parsed.name}`;
    24          if (!isValidURL(parsedUrl)) {
    25              return null;
    26          }
    27          return parsedUrl;
    28      } catch {
    29          return null;
    30      }
    31  }
    32  
    33  export function revisionUrl(url: string, revision: string, forPath: boolean): string {
    34      let parsed;
    35      try {
    36          parsed = GitUrlParse(url);
    37      } catch {
    38          return null;
    39      }
    40      let urlSubPath = isSHA(revision) ? 'commit' : 'tree';
    41  
    42      if (url.indexOf('bitbucket') >= 0) {
    43          // The reason for the condition of 'forPath' is that when we build nested path, we need to use 'src'
    44          urlSubPath = isSHA(revision) && !forPath ? 'commits' : 'src';
    45      }
    46  
    47      // Gitlab changed the way urls to commit look like
    48      // Ref: https://docs.gitlab.com/ee/update/deprecations.html#legacy-urls-replaced-or-removed
    49      if (parsed.source === 'gitlab.com') {
    50          urlSubPath = '-/' + urlSubPath;
    51      }
    52  
    53      if (!supportedSource(parsed)) {
    54          return null;
    55      }
    56  
    57      return `${protocol(parsed.protocol)}://${parsed.resource}/${parsed.owner}/${parsed.name}/${urlSubPath}/${revision || 'HEAD'}`;
    58  }