Skip to content
Snippets Groups Projects
Select Git revision
  • 549c3aebe1d8e5824d190f815004de1712bb41db
  • main default protected
  • feat/37531-npm-install-twice
  • renovate/main-ghcr.io-renovatebot-base-image-11.x
  • feat/37517-base64-private-key
  • next
  • feat/gnupg
  • renovate/main-redis-5.x
  • chore/update-static-data
  • feat/poetry/supersede-pep621
  • fix/markdown/linking
  • fix/36615b-branch-reuse-no-cache
  • 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
  • 41.73.2
  • 41.73.1
  • 41.73.0
  • 41.72.1
  • 41.72.0
  • 42.0.0-next.2
  • 42.0.0-next.1
  • 41.71.1
  • 41.71.0
  • 41.70.3
  • 41.70.2
  • 41.70.1
  • 41.70.0
  • 41.69.1
  • 41.69.0
  • 41.68.0
  • 41.67.0
  • 41.66.3
  • 41.66.2
  • 41.66.1
41 results

url.ts

Blame
  • user avatar
    Aleksandr Mezin authored and GitHub committed
    d560187d
    History
    url.ts 2.29 KiB
    import gitUrlParse from 'git-url-parse';
    import { logger } from '../../logger';
    import { detectPlatform } from '../common';
    import * as hostRules from '../host-rules';
    import { regEx } from '../regex';
    
    export function parseGitUrl(url: string): gitUrlParse.GitUrl {
      return gitUrlParse(url);
    }
    
    export function getHttpUrl(url: string, token?: string): string {
      const parsedUrl = parseGitUrl(url);
    
      let { protocol } = parsedUrl;
      const origProtocol = protocol;
    
      // Convert non-https URLs to https and strip port
      if (!regEx(/^https?$/).test(protocol)) {
        parsedUrl.port = 443;
        protocol = 'https';
      }
    
      parsedUrl.user = '';
      parsedUrl.token = token ?? '';
    
      switch (detectPlatform(parsedUrl.toString(protocol))) {
        case 'gitlab':
          if (token) {
            parsedUrl.token = token.includes(':')
              ? token
              : `gitlab-ci-token:${token}`;
          }
          break;
        case 'github':
          if (token) {
            parsedUrl.token = token.includes(':')
              ? token
              : `x-access-token:${token}`;
          }
          break;
        case 'bitbucket-server':
          // SSH URLs look like ssh://git@git.my.com:7999/project/repo.git
          // HTTPS URLs look like https://git.my.com/scm/project/repo.git
          // git-url-parse can't detect bitbucket-server from SSH URL
          // and thus doesn't know it should insert '/scm/'
          if (origProtocol === 'ssh') {
            parsedUrl.source = 'bitbucket-server';
          }
          break;
      }
    
      return new URL(parsedUrl.toString(protocol)).href;
    }
    
    export function getRemoteUrlWithToken(url: string, hostType?: string): string {
      let coercedUrl: string;
    
      try {
        coercedUrl = getHttpUrl(url);
      } catch {
        logger.warn({ url }, `Attempting to use non-git url for git operations`);
    
        coercedUrl = url;
      }
    
      const hostRule = hostRules.find({ url: coercedUrl, hostType });
    
      if (hostRule?.token) {
        logger.debug(`Found hostRules token for url ${url}`);
    
        return getHttpUrl(url, encodeURIComponent(hostRule.token));
      }
    
      if (hostRule?.username && hostRule?.password) {
        logger.debug(`Found hostRules username and password for url ${url}`);
        const encodedUsername = encodeURIComponent(hostRule.username);
        const encodedPassword = encodeURIComponent(hostRule.password);
    
        return getHttpUrl(url, `${encodedUsername}:${encodedPassword}`);
      }
    
      return url;
    }