Skip to content
Snippets Groups Projects
Select Git revision
  • 36516198c9a07b1c58eee5ffd7380ced8921735a
  • main default protected
  • fix/36615b-branch-reuse-no-cache
  • renovate/main-redis-5.x
  • next
  • revert-31645-feat/rename-gradle-wrapper-validation-action
  • 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.38.1
  • 41.38.0
  • 41.37.12
  • 41.37.11
  • 41.37.10
  • 41.37.9
  • 41.37.8
  • 41.37.7
  • 41.37.6
  • 41.37.5
  • 41.37.4
  • 41.37.3
  • 41.37.2
  • 41.37.1
  • 41.37.0
  • 41.36.2
  • 41.36.1
  • 41.36.0
  • 41.35.2
  • 41.35.1
41 results

comment.spec.ts

Blame
  • common.ts 2.29 KiB
    import JSON5 from 'json5';
    import {
      BITBUCKET_API_USING_HOST_TYPES,
      GITEA_API_USING_HOST_TYPES,
      GITHUB_API_USING_HOST_TYPES,
      GITLAB_API_USING_HOST_TYPES,
    } from '../constants';
    import { logger } from '../logger';
    import * as hostRules from './host-rules';
    import { parseUrl } from './url';
    
    /**
     * Tries to detect the `platform` from a url.
     *
     * @param url the url to detect `platform` from
     * @returns matched `platform` if found, otherwise `null`
     */
    export function detectPlatform(
      url: string
    ): 'azure' | 'bitbucket' | 'gitea' | 'github' | 'gitlab' | null {
      const { hostname } = parseUrl(url) ?? {};
      if (hostname === 'dev.azure.com' || hostname?.endsWith('.visualstudio.com')) {
        return 'azure';
      }
      if (hostname === 'bitbucket.org' || hostname?.includes('bitbucket')) {
        return 'bitbucket';
      }
      if (
        hostname &&
        (['gitea.com', 'codeberg.org'].includes(hostname) ||
          hostname.includes('gitea') ||
          hostname.includes('forgejo'))
      ) {
        return 'gitea';
      }
      if (hostname === 'github.com' || hostname?.includes('github')) {
        return 'github';
      }
      if (hostname === 'gitlab.com' || hostname?.includes('gitlab')) {
        return 'gitlab';
      }
    
      const hostType = hostRules.hostType({ url });
    
      if (!hostType) {
        return null;
      }
    
      if (BITBUCKET_API_USING_HOST_TYPES.includes(hostType)) {
        return 'bitbucket';
      }
      if (GITEA_API_USING_HOST_TYPES.includes(hostType)) {
        return 'gitea';
      }
      if (GITHUB_API_USING_HOST_TYPES.includes(hostType)) {
        return 'github';
      }
      if (GITLAB_API_USING_HOST_TYPES.includes(hostType)) {
        return 'gitlab';
      }
    
      return null;
    }
    
    export function parseJson(content: string | null, filename: string): unknown {
      if (!content) {
        return null;
      }
    
      return filename.endsWith('.json5')
        ? JSON5.parse(content)
        : parseJsonWithFallback(content, filename);
    }
    
    export function parseJsonWithFallback(
      content: string,
      context: string
    ): unknown {
      let parsedJson: unknown;
    
      try {
        parsedJson = JSON.parse(content);
      } catch (err) {
        parsedJson = JSON5.parse(content);
        logger.warn(
          { context },
          'File contents are invalid JSON but parse using JSON5. Support for this will be removed in a future release so please change to a support .json5 file name or ensure correct JSON syntax.'
        );
      }
    
      return parsedJson;
    }