Skip to content
Snippets Groups Projects
Select Git revision
  • 11aa3baf2e54da405b73f0d0cf801277f3d65263
  • main default protected
  • next
  • revert-31645-feat/rename-gradle-wrapper-validation-action
  • renovate/main-redis-5.x
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • fix/36615-branch-reuse-bug
  • 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
  • 41.34.1
  • 41.34.0
  • 41.33.0
  • 41.32.3
  • 41.32.2
  • 41.32.1
  • 41.32.0
  • 41.31.1
  • 41.31.0
  • 41.30.5
  • 41.30.4
  • 41.30.3
  • 41.30.2
  • 41.30.1
  • 41.30.0
  • 41.29.1
  • 41.29.0
  • 41.28.2
  • 41.28.1
  • 41.28.0
41 results

host-rules.ts

Blame
  • user avatar
    Michael Kriese authored and GitHub committed
    * fix(core/http): fallback to github /gitlab hosttype
    
    * fix: remove generic host rule
    
    * Update lib/util/http/index.ts
    
    Co-authored-by: default avatarRhys Arkins <rhys@arkins.net>
    8785f70b
    History
    host-rules.ts 2.45 KiB
    import {
      GITHUB_API_USING_HOST_TYPES,
      GITLAB_API_USING_HOST_TYPES,
      PLATFORM_TYPE_GITHUB,
      PLATFORM_TYPE_GITLAB,
    } from '../../constants/platforms';
    import { logger } from '../../logger';
    import { hasProxy } from '../../proxy';
    import type { HostRule } from '../../types';
    import * as hostRules from '../host-rules';
    import type { GotOptions } from './types';
    
    function findMatchingRules(options: GotOptions, url: string): HostRule {
      const { hostType } = options;
      let res = hostRules.find({ hostType, url });
    
      // Fallback to `github` hostType
      if (
        GITHUB_API_USING_HOST_TYPES.includes(hostType) &&
        hostType !== PLATFORM_TYPE_GITHUB
      ) {
        res = {
          ...hostRules.find({
            hostType: PLATFORM_TYPE_GITHUB,
            url,
          }),
          ...res,
        };
      }
    
      // Fallback to `gitlab` hostType
      if (
        GITLAB_API_USING_HOST_TYPES.includes(hostType) &&
        hostType !== PLATFORM_TYPE_GITLAB
      ) {
        res = {
          ...hostRules.find({
            hostType: PLATFORM_TYPE_GITLAB,
            url,
          }),
          ...res,
        };
      }
    
      return res;
    }
    
    // Apply host rules to requests
    export function applyHostRules(url: string, inOptions: GotOptions): GotOptions {
      const options: GotOptions = { ...inOptions };
      const foundRules = findMatchingRules(options, url);
      const { username, password, token, enabled, authType } = foundRules;
      if (options.noAuth) {
        logger.trace({ url }, `Authorization disabled`);
      } else if (
        options.headers?.authorization ||
        options.password ||
        options.token
      ) {
        logger.trace({ url }, `Authorization already set`);
      } else if (password !== undefined) {
        logger.trace({ url }, `Applying Basic authentication`);
        options.username = username;
        options.password = password;
      } else if (token) {
        logger.trace({ url }, `Applying Bearer authentication`);
        options.token = token;
        options.context = { ...options.context, authType };
      } else if (enabled === false) {
        options.enabled = false;
      }
      // Apply optional params
      ['abortOnError', 'abortIgnoreStatusCodes', 'timeout'].forEach((param) => {
        if (foundRules[param]) {
          options[param] = foundRules[param];
        }
      });
    
      if (!hasProxy() && foundRules.enableHttp2 === true) {
        options.http2 = true;
      }
      return options;
    }
    
    export function getRequestLimit(url: string): number | null {
      const hostRule = hostRules.find({
        url,
      });
      const limit = hostRule.concurrentRequestLimit;
      return typeof limit === 'number' && limit > 0 ? limit : null;
    }