Skip to content
Snippets Groups Projects
Select Git revision
  • 0f86d313da0c03dd94e1add7e18ddede0332d999
  • main default protected
  • feat/gnupg
  • next
  • fix/36615b-branch-reuse-no-cache
  • renovate/main-redis-5.x
  • 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.43.5
  • 41.43.4
  • 41.43.3
  • 41.43.2
  • 41.43.1
  • 41.43.0
  • 41.42.12
  • 41.42.11
  • 41.42.10
  • 41.42.9
  • 41.42.8
  • 41.42.7
  • 41.42.6
  • 41.42.5
  • 41.42.4
  • 41.42.3
  • 41.42.2
  • 41.42.1
  • 41.42.0
  • 41.41.0
41 results

utils.ts

Blame
  • user avatar
    RahulGautamSingh authored and GitHub committed
    d869c946
    History
    utils.ts 2.10 KiB
    import type { MergeStrategy } from '../../../config/types';
    import { CONFIG_GIT_URL_UNAVAILABLE } from '../../../constants/error-messages';
    import { logger } from '../../../logger';
    import * as hostRules from '../../../util/host-rules';
    import { regEx } from '../../../util/regex';
    import { parseUrl } from '../../../util/url';
    import type { GitUrlOption } from '../types';
    import type { PRMergeMethod, Repo } from './types';
    
    export function smartLinks(body: string): string {
      return body?.replace(regEx(/\]\(\.\.\/pull\//g), '](pulls/');
    }
    
    export function trimTrailingApiPath(url: string): string {
      return url?.replace(regEx(/api\/v1\/?$/g), '');
    }
    
    export function getRepoUrl(
      repo: Repo,
      gitUrl: GitUrlOption | undefined,
      endpoint: string
    ): string {
      if (gitUrl === 'ssh') {
        if (!repo.ssh_url) {
          throw new Error(CONFIG_GIT_URL_UNAVAILABLE);
        }
        logger.debug(`Using SSH URL: ${repo.ssh_url}`);
        return repo.ssh_url;
      }
    
      // Find options for current host and determine Git endpoint
      const opts = hostRules.find({
        hostType: 'gitea',
        url: endpoint,
      });
    
      if (gitUrl === 'endpoint') {
        const url = parseUrl(endpoint);
        if (!url) {
          throw new Error(CONFIG_GIT_URL_UNAVAILABLE);
        }
        url.username = opts.token ?? '';
        url.pathname = `${url.pathname}${repo.full_name}.git`;
        logger.debug(
          { url: url.toString() },
          'using URL based on configured endpoint'
        );
        return url.toString();
      }
    
      if (!repo.clone_url) {
        throw new Error(CONFIG_GIT_URL_UNAVAILABLE);
      }
    
      logger.debug(`Using HTTP URL: ${repo.clone_url}`);
      const repoUrl = parseUrl(repo.clone_url);
      if (!repoUrl) {
        throw new Error(CONFIG_GIT_URL_UNAVAILABLE);
      }
      repoUrl.username = opts.token ?? '';
      return repoUrl.toString();
    }
    
    export function getMergeMethod(
      strategy: MergeStrategy | undefined
    ): PRMergeMethod | null {
      switch (strategy) {
        case 'fast-forward':
          return 'rebase';
        case 'merge-commit':
          return 'merge';
        case 'rebase':
          return 'rebase-merge';
        case 'squash':
          return strategy;
        case 'auto':
        default:
          return null;
      }
    }