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

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