Skip to content
Snippets Groups Projects
Select Git revision
  • 53bd90b30faddcc9bf916179adecaa902ab99beb
  • main default protected
  • renovate/main-ghcr.io-renovatebot-base-image-11.x
  • refactor/pin-new-value
  • fix/user-agent
  • feat/37517-base64-private-key
  • next
  • feat/gnupg
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • 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
  • 41.93.3
  • 41.93.2
  • 41.93.1
  • 41.93.0
  • 41.92.1
  • 41.92.0
  • 41.91.4
  • 41.91.3
  • 41.91.2
  • 41.91.1
  • 41.91.0
  • 41.90.1
  • 41.90.0
  • 41.89.3
  • 41.89.2
  • 41.89.1
  • 41.89.0
  • 41.88.2
  • 41.88.1
  • 41.88.0
41 results

index.ts

Blame
  • user avatar
    Michael Kriese authored and GitHub committed
    53bd90b3
    History
    index.ts 2.75 KiB
    import URL from 'url';
    import type { AllConfig } from '../../config/types';
    import { PLATFORM_NOT_FOUND } from '../../constants/error-messages';
    import { logger } from '../../logger';
    import type { HostRule } from '../../types';
    import { setGitAuthor, setNoVerify, setPrivateKey } from '../../util/git';
    import * as hostRules from '../../util/host-rules';
    import platforms from './api';
    import type { Platform } from './types';
    
    export * from './types';
    
    export const getPlatformList = (): string[] => Array.from(platforms.keys());
    export const getPlatforms = (): Map<string, Platform> => platforms;
    
    let _platform: Platform | undefined;
    
    const handler: ProxyHandler<Platform> = {
      get(_target: Platform, prop: keyof Platform) {
        if (!_platform) {
          throw new Error(PLATFORM_NOT_FOUND);
        }
        return _platform[prop];
      },
    };
    
    export const platform = new Proxy<Platform>({} as any, handler);
    
    export function setPlatformApi(name: string): void {
      if (!platforms.has(name)) {
        throw new Error(
          `Init: Platform "${name}" not found. Must be one of: ${getPlatformList().join(
            ', '
          )}`
        );
      }
      _platform = platforms.get(name);
    }
    
    export async function initPlatform(config: AllConfig): Promise<AllConfig> {
      setPrivateKey(config.gitPrivateKey);
      setNoVerify(config.gitNoVerify ?? []);
      // TODO: `platform` #7154
      // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
      setPlatformApi(config.platform!);
      // TODO: types
      const platformInfo = await platform.initPlatform(config);
      const returnConfig: any = { ...config, ...platformInfo };
      // istanbul ignore else
      if (config?.gitAuthor) {
        logger.debug(`Using configured gitAuthor (${config.gitAuthor})`);
        returnConfig.gitAuthor = config.gitAuthor;
      } else if (platformInfo?.gitAuthor) {
        logger.debug(`Using platform gitAuthor: ${String(platformInfo.gitAuthor)}`);
        returnConfig.gitAuthor = platformInfo.gitAuthor;
      }
      // This is done for validation and will be overridden later once repo config is incorporated
      setGitAuthor(returnConfig.gitAuthor);
      const platformRule: HostRule = {
        // TODO: null check #7154
        // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
        matchHost: URL.parse(returnConfig.endpoint).hostname!,
      };
      (
        ['token', 'username', 'password'] as ('token' | 'username' | 'password')[]
      ).forEach((field) => {
        if (config[field]) {
          // TODO: types #7154
          platformRule[field] = config[field] as string;
          delete returnConfig[field];
        }
      });
      returnConfig.hostRules = returnConfig.hostRules || [];
      const typedPlatformRule = {
        ...platformRule,
        hostType: returnConfig.platform,
      };
      returnConfig.hostRules.push(typedPlatformRule);
      hostRules.add(typedPlatformRule);
      return returnConfig;
    }