Skip to content
Snippets Groups Projects
Select Git revision
  • d9b025bf63427475bdfc69ed2d0f379638c68020
  • main default protected
  • next
  • renovate/main-ghcr.io-renovatebot-base-image-10.x
  • renovate/main-ghcr.io-containerbase-devcontainer-13.x
  • 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
  • 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.27.1
  • 41.27.0
  • 41.26.2
  • 41.26.1
  • 41.26.0
41 results

index.ts

Blame
  • user avatar
    Michael Kriese authored and GitHub committed
    Co-authored-by: default avatarHonkingGoose <34918129+HonkingGoose@users.noreply.github.com>
    Co-authored-by: default avatarRhys Arkins <rhys@arkins.net>
    9bd857c8
    History
    index.ts 2.79 KiB
    import URL from 'url';
    import type { AllConfig } from '../../config/types';
    import type { PlatformId } from '../../constants';
    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: PlatformId): 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)
      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)
        matchHost: URL.parse(returnConfig.endpoint).hostname!,
      };
      // There might have been platform-specific modifications to the token
      if (returnConfig.token) {
        config.token = returnConfig.token;
      }
      (
        ['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;
    }