From bdaa7b8e49733775e9e3439882fc5c10c64fa967 Mon Sep 17 00:00:00 2001 From: Sourav Das <souravdasslg95@gmail.com> Date: Thu, 6 Feb 2020 17:45:54 +0530 Subject: [PATCH] feat(constants): Host Types (#5171) --- lib/config/env.ts | 6 +- lib/datasource/cargo/index.ts | 3 +- lib/datasource/docker/index.ts | 5 +- lib/datasource/github/index.ts | 3 +- lib/datasource/go/index.ts | 7 ++- lib/datasource/hex/index.ts | 3 +- lib/datasource/maven/util.ts | 3 +- lib/datasource/metadata.ts | 3 +- lib/datasource/nuget/v2.ts | 5 +- lib/datasource/nuget/v3.ts | 10 +++- lib/datasource/packagist/index.ts | 6 +- lib/datasource/pypi/index.ts | 5 +- lib/datasource/rubygems/get.ts | 3 +- lib/datasource/terraform-provider/index.ts | 3 +- lib/datasource/terraform/index.ts | 3 +- lib/manager/composer/artifacts.ts | 11 +++- lib/manager/gomod/artifacts.ts | 3 +- lib/platform/azure/azure-got-wrapper.ts | 3 +- lib/platform/azure/index.ts | 3 +- .../bitbucket-server/bb-got-wrapper.ts | 3 +- lib/platform/bitbucket-server/index.ts | 3 +- lib/platform/bitbucket/bb-got-wrapper.ts | 3 +- lib/platform/bitbucket/index.ts | 3 +- lib/platform/github/gh-got-wrapper.ts | 3 +- lib/platform/github/index.ts | 5 +- lib/platform/gitlab/gl-got-wrapper.ts | 3 +- lib/platform/gitlab/index.ts | 3 +- lib/util/got/auth.ts | 8 ++- lib/workers/pr/changelog/source-github.ts | 3 +- test/config/cli.spec.ts | 5 +- test/datasource/maven.spec.ts | 2 +- test/platform/azure/azure-got-wrapper.spec.ts | 3 +- .../platform/bitbucket/bb-got-wrapper.spec.ts | 3 +- test/platform/gitlab/gl-got-wrapper.spec.ts | 3 +- test/util/host-rules.spec.ts | 58 ++++++++++--------- test/workers/global/index.spec.ts | 5 +- test/workers/pr/changelog/index.spec.ts | 7 ++- 37 files changed, 134 insertions(+), 77 deletions(-) diff --git a/lib/config/env.ts b/lib/config/env.ts index 89ccd8bba7..46109a4431 100644 --- a/lib/config/env.ts +++ b/lib/config/env.ts @@ -3,6 +3,8 @@ import is from '@sindresorhus/is'; import { getOptions, RenovateOptions } from './definitions'; import { RenovateConfig } from './common'; import { logger } from '../logger'; +import { DATASOURCE_DOCKER } from '../constants/data-binary-source'; +import { PLATFORM_TYPE_GITHUB } from '../constants/platforms'; export function getEnvName(option: Partial<RenovateOptions>): string { if (option.env === false) { @@ -57,7 +59,7 @@ export function getConfig(env: NodeJS.ProcessEnv): RenovateConfig { if (env.GITHUB_COM_TOKEN) { config.hostRules.push({ - hostType: 'github', + hostType: PLATFORM_TYPE_GITHUB, domainName: 'github.com', token: env.GITHUB_COM_TOKEN, }); @@ -65,7 +67,7 @@ export function getConfig(env: NodeJS.ProcessEnv): RenovateConfig { if (env.DOCKER_USERNAME && env.DOCKER_PASSWORD) { config.hostRules.push({ - hostType: 'docker', + hostType: DATASOURCE_DOCKER, username: env.DOCKER_USERNAME, password: env.DOCKER_PASSWORD, }); diff --git a/lib/datasource/cargo/index.ts b/lib/datasource/cargo/index.ts index f20c6f58e9..cc8d68005c 100644 --- a/lib/datasource/cargo/index.ts +++ b/lib/datasource/cargo/index.ts @@ -2,6 +2,7 @@ import { logger } from '../../logger'; import got from '../../util/got'; import { PkgReleaseConfig, ReleaseResult, Release } from '../common'; import { DATASOURCE_FAILURE } from '../../constants/error-messages'; +import { DATASOURCE_CARGO } from '../../constants/data-binary-source'; export async function getPkgReleases({ lookupName, @@ -40,7 +41,7 @@ export async function getPkgReleases({ const crateUrl = baseUrl + path; try { let res: any = await got(crateUrl, { - hostType: 'cargo', + hostType: DATASOURCE_CARGO, }); if (!res || !res.body) { logger.warn( diff --git a/lib/datasource/docker/index.ts b/lib/datasource/docker/index.ts index e06e7c66b6..46284f9a24 100644 --- a/lib/datasource/docker/index.ts +++ b/lib/datasource/docker/index.ts @@ -15,6 +15,7 @@ import * as hostRules from '../../util/host-rules'; import { PkgReleaseConfig, ReleaseResult } from '../common'; import { GotResponse } from '../../platform'; import { DATASOURCE_FAILURE } from '../../constants/error-messages'; +import { DATASOURCE_DOCKER } from '../../constants/data-binary-source'; // TODO: add got typings when available // TODO: replace www-authenticate with https://www.npmjs.com/package/auth-header ? @@ -46,7 +47,7 @@ export function getRegistryRepository( if (!/^https?:\/\//.exec(registry)) { registry = `https://${registry}`; } - const opts = hostRules.find({ hostType: 'docker', url: registry }); + const opts = hostRules.find({ hostType: DATASOURCE_DOCKER, url: registry }); if (opts && opts.insecureRegistry) { registry = registry.replace('https', 'http'); } @@ -110,7 +111,7 @@ async function getAuthHeaders( const opts: hostRules.HostRule & { headers?: Record<string, string>; - } = hostRules.find({ hostType: 'docker', url: apiCheckUrl }); + } = hostRules.find({ hostType: DATASOURCE_DOCKER, url: apiCheckUrl }); opts.json = true; if (ecrRegex.test(registry)) { const [, region] = ecrRegex.exec(registry); diff --git a/lib/datasource/github/index.ts b/lib/datasource/github/index.ts index ea95cdc9ef..f653f7a52e 100644 --- a/lib/datasource/github/index.ts +++ b/lib/datasource/github/index.ts @@ -8,6 +8,7 @@ import { import { logger } from '../../logger'; import got, { GotJSONOptions } from '../../util/got'; import { PLATFORM_FAILURE } from '../../constants/error-messages'; +import { DATASOURCE_GITHUB } from '../../constants/data-binary-source'; const { get: ghGot } = api; @@ -20,7 +21,7 @@ async function fetchJSONFile(repo: string, fileName: string): Promise<Preset> { : 'application/vnd.github.v3+json', }, json: true, - hostType: 'github', + hostType: DATASOURCE_GITHUB, }; let res: { body: { content: string } }; try { diff --git a/lib/datasource/go/index.ts b/lib/datasource/go/index.ts index 51cdb10d33..e1602c1cd4 100644 --- a/lib/datasource/go/index.ts +++ b/lib/datasource/go/index.ts @@ -3,7 +3,10 @@ import got from '../../util/got'; import * as github from '../github'; import { DigestConfig, PkgReleaseConfig, ReleaseResult } from '../common'; import { regEx } from '../../util/regex'; -import { DATASOURCE_GITHUB } from '../../constants/data-binary-source'; +import { + DATASOURCE_GITHUB, + DATASOURCE_GO, +} from '../../constants/data-binary-source'; interface DataSource { datasource: string; @@ -30,7 +33,7 @@ async function getDatasource(name: string): Promise<DataSource | null> { try { const res = ( await got(pkgUrl, { - hostType: 'go', + hostType: DATASOURCE_GO, }) ).body; const sourceMatch = res.match( diff --git a/lib/datasource/hex/index.ts b/lib/datasource/hex/index.ts index 999ec4eb5f..5837d45f05 100644 --- a/lib/datasource/hex/index.ts +++ b/lib/datasource/hex/index.ts @@ -2,6 +2,7 @@ import { logger } from '../../logger'; import got from '../../util/got'; import { ReleaseResult, PkgReleaseConfig } from '../common'; import { DATASOURCE_FAILURE } from '../../constants/error-messages'; +import { DATASOURCE_HEX } from '../../constants/data-binary-source'; interface HexRelease { html_url: string; @@ -28,7 +29,7 @@ export async function getPkgReleases({ try { const response = await got(hexUrl, { json: true, - hostType: 'hex', + hostType: DATASOURCE_HEX, }); const hexRelease: HexRelease = response.body; diff --git a/lib/datasource/maven/util.ts b/lib/datasource/maven/util.ts index a06b0cf837..4151b4ff45 100644 --- a/lib/datasource/maven/util.ts +++ b/lib/datasource/maven/util.ts @@ -2,6 +2,7 @@ import url from 'url'; import got from '../../util/got'; import { logger } from '../../logger'; import { DATASOURCE_FAILURE } from '../../constants/error-messages'; +import { DATASOURCE_MAVEN } from '../../constants/data-binary-source'; function isMavenCentral(pkgUrl: url.URL | string): boolean { return ( @@ -35,7 +36,7 @@ function isConnectionError(err: { code: string }): boolean { export async function downloadHttpProtocol( pkgUrl: url.URL | string, - hostType = 'maven' + hostType = DATASOURCE_MAVEN ): Promise<string | null> { let raw: { body: string }; try { diff --git a/lib/datasource/metadata.ts b/lib/datasource/metadata.ts index 5015ee01d2..947ff33375 100644 --- a/lib/datasource/metadata.ts +++ b/lib/datasource/metadata.ts @@ -2,6 +2,7 @@ import is from '@sindresorhus/is'; import parse from 'github-url-from-git'; import { ReleaseResult } from './common'; import * as hostRules from '../util/host-rules'; +import { DATASOURCE_GITHUB } from '../constants/data-binary-source'; // Use this object to define changelog URLs for packages // Only necessary when the changelog data cannot be found in the package's source repository @@ -110,7 +111,7 @@ export function addMetaData( } const extraBaseUrls = []; // istanbul ignore next - hostRules.hosts({ hostType: 'github' }).forEach(host => { + hostRules.hosts({ hostType: DATASOURCE_GITHUB }).forEach(host => { extraBaseUrls.push(host, `gist.${host}`); }); if (dep.sourceUrl) { diff --git a/lib/datasource/nuget/v2.ts b/lib/datasource/nuget/v2.ts index e8ac949d00..69b58f4d83 100644 --- a/lib/datasource/nuget/v2.ts +++ b/lib/datasource/nuget/v2.ts @@ -2,6 +2,7 @@ import { XmlDocument, XmlElement } from 'xmldoc'; import { logger } from '../../logger'; import got from '../../util/got'; import { ReleaseResult } from '../common'; +import { DATASOURCE_NUGET } from '../../constants/data-binary-source'; function getPkgProp(pkgInfo: XmlElement, propName: string): string { return pkgInfo.childNamed('m:properties').childNamed(`d:${propName}`).val; @@ -18,7 +19,9 @@ export async function getPkgReleases( try { let pkgUrlList = `${feedUrl}/FindPackagesById()?id=%27${pkgName}%27&$select=Version,IsLatestVersion,ProjectUrl`; do { - const pkgVersionsListRaw = await got(pkgUrlList, { hostType: 'nuget' }); + const pkgVersionsListRaw = await got(pkgUrlList, { + hostType: DATASOURCE_NUGET, + }); if (pkgVersionsListRaw.statusCode !== 200) { logger.debug( { dependency: pkgName, pkgVersionsListRaw }, diff --git a/lib/datasource/nuget/v3.ts b/lib/datasource/nuget/v3.ts index 7961648e5a..db9a9b3f74 100644 --- a/lib/datasource/nuget/v3.ts +++ b/lib/datasource/nuget/v3.ts @@ -3,6 +3,7 @@ import { XmlDocument } from 'xmldoc'; import { logger } from '../../logger'; import got from '../../util/got'; import { ReleaseResult } from '../common'; +import { DATASOURCE_NUGET } from '../../constants/data-binary-source'; // https://api.nuget.org/v3/index.json is a default official nuget feed const defaultNugetFeed = 'https://api.nuget.org/v3/index.json'; @@ -27,7 +28,10 @@ export async function getQueryUrl(url: string): Promise<string | null> { } try { - const servicesIndexRaw = await got(url, { json: true, hostType: 'nuget' }); + const servicesIndexRaw = await got(url, { + json: true, + hostType: DATASOURCE_NUGET, + }); if (servicesIndexRaw.statusCode !== 200) { logger.debug( { dependency: url, servicesIndexRaw }, @@ -75,7 +79,7 @@ export async function getPkgReleases( try { const pkgUrlListRaw = await got(queryUrl, { json: true, - hostType: 'nuget', + hostType: DATASOURCE_NUGET, }); if (pkgUrlListRaw.statusCode !== 200) { logger.debug( @@ -116,7 +120,7 @@ export async function getPkgReleases( const nugetOrgApi = `https://api.nuget.org/v3-flatcontainer/${pkgName.toLowerCase()}/${lastVersion}/${pkgName.toLowerCase()}.nuspec`; let metaresult: { body: string }; try { - metaresult = await got(nugetOrgApi, { hostType: 'nuget' }); + metaresult = await got(nugetOrgApi, { hostType: DATASOURCE_NUGET }); } catch (err) /* istanbul ignore next */ { logger.debug( `Cannot fetch metadata for ${pkgName} using popped version ${lastVersion}` diff --git a/lib/datasource/packagist/index.ts b/lib/datasource/packagist/index.ts index de6f639a39..ec0432dba1 100644 --- a/lib/datasource/packagist/index.ts +++ b/lib/datasource/packagist/index.ts @@ -9,12 +9,16 @@ import got, { GotJSONOptions } from '../../util/got'; import * as hostRules from '../../util/host-rules'; import { PkgReleaseConfig, ReleaseResult } from '../common'; import { DATASOURCE_FAILURE } from '../../constants/error-messages'; +import { DATASOURCE_PACKAGIST } from '../../constants/data-binary-source'; function getHostOpts(url: string): GotJSONOptions { const opts: GotJSONOptions = { json: true, }; - const { username, password } = hostRules.find({ hostType: 'packagist', url }); + const { username, password } = hostRules.find({ + hostType: DATASOURCE_PACKAGIST, + url, + }); if (username && password) { opts.auth = `${username}:${password}`; } diff --git a/lib/datasource/pypi/index.ts b/lib/datasource/pypi/index.ts index f8811d7353..9e02323512 100644 --- a/lib/datasource/pypi/index.ts +++ b/lib/datasource/pypi/index.ts @@ -5,6 +5,7 @@ import { logger } from '../../logger'; import { matches } from '../../versioning/pep440'; import got from '../../util/got'; import { PkgReleaseConfig, ReleaseResult } from '../common'; +import { DATASOURCE_PYPI } from '../../constants/data-binary-source'; function normalizeName(input: string): string { return input.toLowerCase().replace(/(-|\.)/g, '_'); @@ -38,7 +39,7 @@ async function getDependency( const dependency: ReleaseResult = { releases: null }; const rep = await got(url.parse(lookupUrl), { json: true, - hostType: 'pypi', + hostType: DATASOURCE_PYPI, }); const dep = rep && rep.body; if (!dep) { @@ -116,7 +117,7 @@ async function getSimpleDependency( try { const dependency: ReleaseResult = { releases: null }; const response = await got<string>(url.parse(lookupUrl), { - hostType: 'pypi', + hostType: DATASOURCE_PYPI, }); const dep = response && response.body; if (!dep) { diff --git a/lib/datasource/rubygems/get.ts b/lib/datasource/rubygems/get.ts index 76a627b7b4..86ba96d379 100644 --- a/lib/datasource/rubygems/get.ts +++ b/lib/datasource/rubygems/get.ts @@ -6,6 +6,7 @@ import retriable from './retriable'; import { UNAUTHORIZED, FORBIDDEN, NOT_FOUND } from './errors'; import { ReleaseResult } from '../common'; import { DATASOURCE_FAILURE } from '../../constants/error-messages'; +import { DATASOURCE_RUBYGEMS } from '../../constants/data-binary-source'; const INFO_PATH = '/api/v1/gems'; const VERSIONS_PATH = '/api/v1/versions'; @@ -36,7 +37,7 @@ const processError = ({ err, ...rest }): null => { }; const getHeaders = (): OutgoingHttpHeaders => { - return { hostType: 'rubygems' }; + return { hostType: DATASOURCE_RUBYGEMS }; }; const fetch = async ({ dependency, registry, path }): Promise<any> => { diff --git a/lib/datasource/terraform-provider/index.ts b/lib/datasource/terraform-provider/index.ts index fba18d1a02..3f25581a95 100644 --- a/lib/datasource/terraform-provider/index.ts +++ b/lib/datasource/terraform-provider/index.ts @@ -1,6 +1,7 @@ import { logger } from '../../logger'; import got from '../../util/got'; import { PkgReleaseConfig, ReleaseResult } from '../common'; +import { DATASOURCE_TERRAFORM } from '../../constants/data-binary-source'; interface TerraformProvider { namespace: string; @@ -36,7 +37,7 @@ export async function getPkgReleases({ const res: TerraformProvider = ( await got(pkgUrl, { json: true, - hostType: 'terraform', + hostType: DATASOURCE_TERRAFORM, }) ).body; // Simplify response before caching and returning diff --git a/lib/datasource/terraform/index.ts b/lib/datasource/terraform/index.ts index 121ecfe0a3..e042b4b636 100644 --- a/lib/datasource/terraform/index.ts +++ b/lib/datasource/terraform/index.ts @@ -2,6 +2,7 @@ import is from '@sindresorhus/is'; import { logger } from '../../logger'; import got from '../../util/got'; import { PkgReleaseConfig, ReleaseResult } from '../common'; +import { DATASOURCE_TERRAFORM } from '../../constants/data-binary-source'; interface RegistryRepository { registry: string; @@ -70,7 +71,7 @@ export async function getPkgReleases({ const res: TerraformRelease = ( await got(pkgUrl, { json: true, - hostType: 'terraform', + hostType: DATASOURCE_TERRAFORM, }) ).body; const returnedName = res.namespace + '/' + res.name + '/' + res.provider; diff --git a/lib/manager/composer/artifacts.ts b/lib/manager/composer/artifacts.ts index 3080dfc356..4e73fca627 100644 --- a/lib/manager/composer/artifacts.ts +++ b/lib/manager/composer/artifacts.ts @@ -8,6 +8,11 @@ import { logger } from '../../logger'; import * as hostRules from '../../util/host-rules'; import { platform } from '../../platform'; import { SYSTEM_INSUFFICIENT_DISK_SPACE } from '../../constants/error-messages'; +import { DATASOURCE_PACKAGIST } from '../../constants/data-binary-source'; +import { + PLATFORM_TYPE_GITHUB, + PLATFORM_TYPE_GITLAB, +} from '../../constants/platforms'; export async function updateArtifacts({ packageFileName, @@ -40,7 +45,7 @@ export async function updateArtifacts({ } const authJson = {}; let credentials = hostRules.find({ - hostType: 'github', + hostType: PLATFORM_TYPE_GITHUB, url: 'https://api.github.com/', }); // istanbul ignore if @@ -50,7 +55,7 @@ export async function updateArtifacts({ }; } credentials = hostRules.find({ - hostType: 'gitlab', + hostType: PLATFORM_TYPE_GITLAB, url: 'https://gitlab.com/api/v4/', }); // istanbul ignore if @@ -66,7 +71,7 @@ export async function updateArtifacts({ if (regUrl) { const { host } = URL.parse(regUrl); const hostRule = hostRules.find({ - hostType: 'packagist', + hostType: DATASOURCE_PACKAGIST, url: regUrl, }); // istanbul ignore else diff --git a/lib/manager/gomod/artifacts.ts b/lib/manager/gomod/artifacts.ts index f6ed13fda1..63b1fde5f6 100644 --- a/lib/manager/gomod/artifacts.ts +++ b/lib/manager/gomod/artifacts.ts @@ -7,6 +7,7 @@ import { logger } from '../../logger'; import { UpdateArtifact, UpdateArtifactsResult } from '../common'; import { platform } from '../../platform'; import { BinarySource } from '../../util/exec/common'; +import { PLATFORM_TYPE_GITHUB } from '../../constants/platforms'; export async function updateArtifacts({ packageFileName: goModFileName, @@ -53,7 +54,7 @@ export async function updateArtifacts({ cmd += `-w "${cwd}" `; cmd += `renovate/go `; const credentials = find({ - hostType: 'github', + hostType: PLATFORM_TYPE_GITHUB, url: 'https://api.github.com/', }); if (credentials && credentials.token) { diff --git a/lib/platform/azure/azure-got-wrapper.ts b/lib/platform/azure/azure-got-wrapper.ts index 28167172dd..1a2b50040d 100644 --- a/lib/platform/azure/azure-got-wrapper.ts +++ b/lib/platform/azure/azure-got-wrapper.ts @@ -3,8 +3,9 @@ import { IGitApi } from 'azure-devops-node-api/GitApi'; import { ICoreApi } from 'azure-devops-node-api/CoreApi'; import { IPolicyApi } from 'azure-devops-node-api/PolicyApi'; import * as hostRules from '../../util/host-rules'; +import { PLATFORM_TYPE_AZURE } from '../../constants/platforms'; -const hostType = 'azure'; +const hostType = PLATFORM_TYPE_AZURE; let endpoint: string; export function azureObj(): azure.WebApi { diff --git a/lib/platform/azure/index.ts b/lib/platform/azure/index.ts index bdbbb3c8a0..0dfa208723 100644 --- a/lib/platform/azure/index.ts +++ b/lib/platform/azure/index.ts @@ -23,6 +23,7 @@ import { import { sanitize } from '../../util/sanitize'; import { smartTruncate } from '../utils/pr-body'; import { REPOSITORY_DISABLED } from '../../constants/error-messages'; +import { PLATFORM_TYPE_AZURE } from '../../constants/platforms'; import { BRANCH_STATUS_FAILED, BRANCH_STATUS_PENDING, @@ -48,7 +49,7 @@ interface Config { let config: Config = {} as any; const defaults: any = { - hostType: 'azure', + hostType: PLATFORM_TYPE_AZURE, }; export function initPlatform({ diff --git a/lib/platform/bitbucket-server/bb-got-wrapper.ts b/lib/platform/bitbucket-server/bb-got-wrapper.ts index 7650abb807..d020bb04ea 100644 --- a/lib/platform/bitbucket-server/bb-got-wrapper.ts +++ b/lib/platform/bitbucket-server/bb-got-wrapper.ts @@ -2,6 +2,7 @@ import URL from 'url'; import { GotJSONOptions } from 'got'; import got from '../../util/got'; import { GotApi, GotApiOptions, GotResponse } from '../common'; +import { PLATFORM_TYPE_BITBUCKET_SERVER } from '../../constants/platforms'; let baseUrl: string; @@ -11,7 +12,7 @@ function get( ): Promise<GotResponse> { const url = URL.resolve(baseUrl, path); const opts: GotApiOptions & GotJSONOptions = { - hostType: 'bitbucket-server', + hostType: PLATFORM_TYPE_BITBUCKET_SERVER, json: true, ...options, }; diff --git a/lib/platform/bitbucket-server/index.ts b/lib/platform/bitbucket-server/index.ts index 8a493415d9..515cd44941 100644 --- a/lib/platform/bitbucket-server/index.ts +++ b/lib/platform/bitbucket-server/index.ts @@ -21,6 +21,7 @@ import { } from '../common'; import { sanitize } from '../../util/sanitize'; import { smartTruncate } from '../utils/pr-body'; +import { PLATFORM_TYPE_BITBUCKET_SERVER } from '../../constants/platforms'; import { REPOSITORY_CHANGED, REPOSITORY_DISABLED, @@ -63,7 +64,7 @@ interface BbsConfig { let config: BbsConfig = {} as any; const defaults: any = { - hostType: 'bitbucket-server', + hostType: PLATFORM_TYPE_BITBUCKET_SERVER, }; /* istanbul ignore next */ diff --git a/lib/platform/bitbucket/bb-got-wrapper.ts b/lib/platform/bitbucket/bb-got-wrapper.ts index 64070bd95a..e1072c3c68 100644 --- a/lib/platform/bitbucket/bb-got-wrapper.ts +++ b/lib/platform/bitbucket/bb-got-wrapper.ts @@ -1,6 +1,7 @@ import { GotJSONOptions } from 'got'; import got from '../../util/got'; import { GotApi, GotApiOptions, GotResponse } from '../common'; +import { PLATFORM_TYPE_BITBUCKET } from '../../constants/platforms'; async function get( path: string, @@ -8,7 +9,7 @@ async function get( ): Promise<GotResponse> { const opts: GotApiOptions & GotJSONOptions = { json: true, - hostType: 'bitbucket', + hostType: PLATFORM_TYPE_BITBUCKET, baseUrl: 'https://api.bitbucket.org/', ...options, }; diff --git a/lib/platform/bitbucket/index.ts b/lib/platform/bitbucket/index.ts index 8216c2e80e..4d2570031d 100644 --- a/lib/platform/bitbucket/index.ts +++ b/lib/platform/bitbucket/index.ts @@ -26,6 +26,7 @@ import { REPOSITORY_DISABLED, REPOSITORY_NOT_FOUND, } from '../../constants/error-messages'; +import { PLATFORM_TYPE_BITBUCKET } from '../../constants/platforms'; import { BRANCH_STATUS_FAILED, BRANCH_STATUS_PENDING, @@ -83,7 +84,7 @@ export async function initRepo({ }: RepoParams): Promise<RepoConfig> { logger.debug(`initRepo("${repository}")`); const opts = hostRules.find({ - hostType: 'bitbucket', + hostType: PLATFORM_TYPE_BITBUCKET, url: 'https://api.bitbucket.org/', }); config = { diff --git a/lib/platform/github/gh-got-wrapper.ts b/lib/platform/github/gh-got-wrapper.ts index 9cccae229f..7105ec8768 100644 --- a/lib/platform/github/gh-got-wrapper.ts +++ b/lib/platform/github/gh-got-wrapper.ts @@ -14,8 +14,9 @@ import { PLATFORM_RATE_LIMIT_EXCEEDED, REPOSITORY_CHANGED, } from '../../constants/error-messages'; +import { PLATFORM_TYPE_GITHUB } from '../../constants/platforms'; -const hostType = 'github'; +const hostType = PLATFORM_TYPE_GITHUB; export const getHostType = (): string => hostType; let baseUrl = 'https://api.github.com/'; diff --git a/lib/platform/github/index.ts b/lib/platform/github/index.ts index ea865ca9d6..7b2e2df3e6 100644 --- a/lib/platform/github/index.ts +++ b/lib/platform/github/index.ts @@ -38,6 +38,7 @@ import { REPOSITORY_NOT_FOUND, REPOSITORY_RENAMED, } from '../../constants/error-messages'; +import { PLATFORM_TYPE_GITHUB } from '../../constants/platforms'; import { BRANCH_STATUS_FAILED, BRANCH_STATUS_PENDING, @@ -100,7 +101,7 @@ type PrList = Record<number, Pr>; let config: LocalRepoConfig = {} as any; const defaults = { - hostType: 'github', + hostType: PLATFORM_TYPE_GITHUB, endpoint: 'https://api.github.com/', }; @@ -248,7 +249,7 @@ export async function initRepo({ api.setBaseUrl(endpoint); } const opts = hostRules.find({ - hostType: 'github', + hostType: PLATFORM_TYPE_GITHUB, url: defaults.endpoint, }); config.isGhe = !defaults.endpoint.startsWith('https://api.github.com'); diff --git a/lib/platform/gitlab/gl-got-wrapper.ts b/lib/platform/gitlab/gl-got-wrapper.ts index 3888b4eb0e..e98b1f9a60 100644 --- a/lib/platform/gitlab/gl-got-wrapper.ts +++ b/lib/platform/gitlab/gl-got-wrapper.ts @@ -4,8 +4,9 @@ import { GotApi, GotResponse } from '../common'; import got from '../../util/got'; import { logger } from '../../logger'; import { PLATFORM_FAILURE } from '../../constants/error-messages'; +import { PLATFORM_TYPE_GITLAB } from '../../constants/platforms'; -const hostType = 'gitlab'; +const hostType = PLATFORM_TYPE_GITLAB; let baseUrl = 'https://gitlab.com/api/v4/'; async function get(path: string, options: any): Promise<GotResponse> { diff --git a/lib/platform/gitlab/index.ts b/lib/platform/gitlab/index.ts index b22edbfc2f..8d59b1fb1e 100644 --- a/lib/platform/gitlab/index.ts +++ b/lib/platform/gitlab/index.ts @@ -33,6 +33,7 @@ import { REPOSITORY_MIRRORED, REPOSITORY_NOT_FOUND, } from '../../constants/error-messages'; +import { PLATFORM_TYPE_GITLAB } from '../../constants/platforms'; import { BRANCH_STATUS_FAILED, BRANCH_STATUS_FAILURE, @@ -55,7 +56,7 @@ let config: { } = {} as any; const defaults = { - hostType: 'gitlab', + hostType: PLATFORM_TYPE_GITLAB, endpoint: 'https://gitlab.com/api/v4/', }; diff --git a/lib/util/got/auth.ts b/lib/util/got/auth.ts index f4d947950c..dde19ac7c8 100644 --- a/lib/util/got/auth.ts +++ b/lib/util/got/auth.ts @@ -1,5 +1,9 @@ import { logger } from '../../logger'; import { create } from './util'; +import { + PLATFORM_TYPE_GITHUB, + PLATFORM_TYPE_GITLAB, +} from '../../constants/platforms'; // istanbul ignore next export default create({ @@ -13,9 +17,9 @@ export default create({ { hostname: options.hostname }, 'Converting token to Bearer auth' ); - if (options.hostType === 'github') { + if (options.hostType === PLATFORM_TYPE_GITHUB) { options.headers.authorization = `token ${options.token}`; // eslint-disable-line no-param-reassign - } else if (options.hostType === 'gitlab') { + } else if (options.hostType === PLATFORM_TYPE_GITLAB) { options.headers['Private-token'] = options.token; // eslint-disable-line no-param-reassign } else { options.headers.authorization = `Bearer ${options.token}`; // eslint-disable-line no-param-reassign diff --git a/lib/workers/pr/changelog/source-github.ts b/lib/workers/pr/changelog/source-github.ts index 188465d10e..5e289fe3ff 100644 --- a/lib/workers/pr/changelog/source-github.ts +++ b/lib/workers/pr/changelog/source-github.ts @@ -11,6 +11,7 @@ import { ChangeLogResult, } from './common'; import { Release } from '../../../datasource'; +import { PLATFORM_TYPE_GITHUB } from '../../../constants/platforms'; const { get: ghGot } = api; @@ -68,7 +69,7 @@ export async function getChangeLogJSON({ ? 'https://api.github.com/' : sourceUrl; const config = hostRules.find({ - hostType: 'github', + hostType: PLATFORM_TYPE_GITHUB, url, }); if (!config.token) { diff --git a/test/config/cli.spec.ts b/test/config/cli.spec.ts index 24b4423029..9b25b9c984 100644 --- a/test/config/cli.spec.ts +++ b/test/config/cli.spec.ts @@ -1,6 +1,7 @@ import * as cli from '../../lib/config/cli'; import getArgv from './config/_fixtures/argv'; import { RenovateOptions } from '../../lib/config/definitions'; +import { DATASOURCE_DOCKER } from '../../lib/constants/data-binary-source'; describe('config/cli', () => { let argv: string[]; @@ -77,13 +78,13 @@ describe('config/cli', () => { }); it('parses json lists correctly', () => { argv.push( - `--host-rules=[{"domainName":"docker.io","hostType":"docker","username":"user","password":"password"}]` + `--host-rules=[{"domainName":"docker.io","hostType":"${DATASOURCE_DOCKER}","username":"user","password":"password"}]` ); expect(cli.getConfig(argv)).toEqual({ hostRules: [ { domainName: 'docker.io', - hostType: 'docker', + hostType: DATASOURCE_DOCKER, username: 'user', password: 'password', }, diff --git a/test/datasource/maven.spec.ts b/test/datasource/maven.spec.ts index d5af7816b6..e26884d3be 100644 --- a/test/datasource/maven.spec.ts +++ b/test/datasource/maven.spec.ts @@ -35,7 +35,7 @@ const config = { describe('datasource/maven', () => { beforeEach(() => { hostRules.add({ - hostType: 'maven', + hostType: DATASOURCE_MAVEN, hostName: 'frontend_for_private_s3_repository', username: 'username', password: 'password', diff --git a/test/platform/azure/azure-got-wrapper.spec.ts b/test/platform/azure/azure-got-wrapper.spec.ts index 963ca80fbd..1228267eec 100644 --- a/test/platform/azure/azure-got-wrapper.spec.ts +++ b/test/platform/azure/azure-got-wrapper.spec.ts @@ -1,4 +1,5 @@ import * as _hostRules from '../../../lib/util/host-rules'; +import { PLATFORM_TYPE_AZURE } from '../../../lib/constants/platforms'; describe('platform/azure/azure-got-wrapper', () => { let azure: typeof import('../../../lib/platform/azure/azure-got-wrapper'); @@ -18,7 +19,7 @@ describe('platform/azure/azure-got-wrapper', () => { }); it('should set token and endpoint', () => { hostRules.add({ - hostType: 'azure', + hostType: PLATFORM_TYPE_AZURE, token: 'token', baseUrl: 'https://dev.azure.com/renovate12345', }); diff --git a/test/platform/bitbucket/bb-got-wrapper.spec.ts b/test/platform/bitbucket/bb-got-wrapper.spec.ts index 562b936d4b..aef1abc8a0 100644 --- a/test/platform/bitbucket/bb-got-wrapper.spec.ts +++ b/test/platform/bitbucket/bb-got-wrapper.spec.ts @@ -1,4 +1,5 @@ import { GotApi } from '../../../lib/platform/common'; +import { PLATFORM_TYPE_BITBUCKET } from '../../../lib/constants/platforms'; describe('platform/gl-got-wrapper', () => { let api: GotApi; @@ -15,7 +16,7 @@ describe('platform/gl-got-wrapper', () => { // clean up hostRules hostRules.clear(); hostRules.add({ - hostType: 'bitbucket', + hostType: PLATFORM_TYPE_BITBUCKET, baseUrl: 'https://api.bitbucket.org', token: 'token', }); diff --git a/test/platform/gitlab/gl-got-wrapper.spec.ts b/test/platform/gitlab/gl-got-wrapper.spec.ts index 64b4d91859..0432d2c880 100644 --- a/test/platform/gitlab/gl-got-wrapper.spec.ts +++ b/test/platform/gitlab/gl-got-wrapper.spec.ts @@ -1,13 +1,14 @@ import _got from '../../../lib/util/got'; import { api } from '../../../lib/platform/gitlab/gl-got-wrapper'; import * as hostRules from '../../../lib/util/host-rules'; +import { PLATFORM_TYPE_GITLAB } from '../../../lib/constants/platforms'; jest.mock('../../../lib/util/got'); const got: any = _got; hostRules.add({ - hostType: 'gitlab', + hostType: PLATFORM_TYPE_GITLAB, token: 'abc123', }); diff --git a/test/util/host-rules.spec.ts b/test/util/host-rules.spec.ts index c3c47ffbe1..00bf21eceb 100644 --- a/test/util/host-rules.spec.ts +++ b/test/util/host-rules.spec.ts @@ -1,4 +1,6 @@ import { add, find, clear, hosts } from '../../lib/util/host-rules'; +import { DATASOURCE_NUGET } from '../../lib/constants/data-binary-source'; +import { PLATFORM_TYPE_AZURE } from '../../lib/constants/platforms'; describe('util/host-rules', () => { beforeEach(() => { @@ -8,7 +10,7 @@ describe('util/host-rules', () => { it('throws if both domainName and hostName', () => { expect(() => add({ - hostType: 'azure', + hostType: PLATFORM_TYPE_AZURE, domainName: 'github.com', hostName: 'api.github.com', }) @@ -17,7 +19,7 @@ describe('util/host-rules', () => { it('throws if both domainName and baseUrl', () => { expect(() => add({ - hostType: 'azure', + hostType: PLATFORM_TYPE_AZURE, domainName: 'github.com', baseUrl: 'https://api.github.com', }) @@ -26,7 +28,7 @@ describe('util/host-rules', () => { it('throws if both hostName and baseUrl', () => { expect(() => add({ - hostType: 'azure', + hostType: PLATFORM_TYPE_AZURE, hostName: 'api.github.com', baseUrl: 'https://api.github.com', }) @@ -47,38 +49,38 @@ describe('util/host-rules', () => { }); it('needs exact host matches', () => { add({ - hostType: 'nuget', + hostType: DATASOURCE_NUGET, hostName: 'nuget.org', username: 'root', password: 'p4$$w0rd', token: undefined, }); - expect(find({ hostType: 'nuget' })).toMatchSnapshot(); - expect(find({ hostType: 'nuget', url: 'https://nuget.org' })).not.toEqual( - {} - ); - expect(find({ hostType: 'nuget', url: 'https://not.nuget.org' })).toEqual( - {} - ); - expect(find({ hostType: 'nuget', url: 'https://not-nuget.org' })).toEqual( - {} - ); + expect(find({ hostType: DATASOURCE_NUGET })).toMatchSnapshot(); + expect( + find({ hostType: DATASOURCE_NUGET, url: 'https://nuget.org' }) + ).not.toEqual({}); + expect( + find({ hostType: DATASOURCE_NUGET, url: 'https://not.nuget.org' }) + ).toEqual({}); + expect( + find({ hostType: DATASOURCE_NUGET, url: 'https://not-nuget.org' }) + ).toEqual({}); }); it('matches on empty rules', () => { add({ json: true, }); expect( - find({ hostType: 'nuget', url: 'https://api.github.com' }) + find({ hostType: DATASOURCE_NUGET, url: 'https://api.github.com' }) ).toEqual({ json: true }); }); it('matches on hostType', () => { add({ - hostType: 'nuget', + hostType: DATASOURCE_NUGET, token: 'abc', }); expect( - find({ hostType: 'nuget', url: 'https://nuget.local/api' }) + find({ hostType: DATASOURCE_NUGET, url: 'https://nuget.local/api' }) ).toMatchSnapshot(); }); it('matches on domainName', () => { @@ -87,7 +89,8 @@ describe('util/host-rules', () => { token: 'def', }); expect( - find({ hostType: 'nuget', url: 'https://api.github.com' }).token + find({ hostType: DATASOURCE_NUGET, url: 'https://api.github.com' }) + .token ).toEqual('def'); }); it('matches on hostName', () => { @@ -96,49 +99,50 @@ describe('util/host-rules', () => { token: 'abc', }); expect( - find({ hostType: 'nuget', url: 'https://nuget.local/api' }) + find({ hostType: DATASOURCE_NUGET, url: 'https://nuget.local/api' }) ).toMatchSnapshot(); }); it('matches on hostType and endpoint', () => { add({ - hostType: 'nuget', + hostType: DATASOURCE_NUGET, baseUrl: 'https://nuget.local/api', token: 'abc', }); expect( - find({ hostType: 'nuget', url: 'https://nuget.local/api' }).token + find({ hostType: DATASOURCE_NUGET, url: 'https://nuget.local/api' }) + .token ).toEqual('abc'); }); it('matches on endpoint subresource', () => { add({ - hostType: 'nuget', + hostType: DATASOURCE_NUGET, baseUrl: 'https://nuget.local/api', token: 'abc', }); expect( find({ - hostType: 'nuget', + hostType: DATASOURCE_NUGET, url: 'https://nuget.local/api/sub-resource', }) ).toMatchSnapshot(); }); it('returns hosts', () => { add({ - hostType: 'nuget', + hostType: DATASOURCE_NUGET, token: 'aaaaaa', }); add({ - hostType: 'nuget', + hostType: DATASOURCE_NUGET, baseUrl: 'https://nuget.local/api', token: 'abc', }); add({ - hostType: 'nuget', + hostType: DATASOURCE_NUGET, hostName: 'my.local.registry', token: 'def', }); const res = hosts({ - hostType: 'nuget', + hostType: DATASOURCE_NUGET, }); expect(res).toMatchSnapshot(); expect(res).toHaveLength(2); diff --git a/test/workers/global/index.spec.ts b/test/workers/global/index.spec.ts index b896e18dc9..2efb94d69d 100644 --- a/test/workers/global/index.spec.ts +++ b/test/workers/global/index.spec.ts @@ -7,6 +7,7 @@ import { PLATFORM_TYPE_GITHUB, PLATFORM_TYPE_GITLAB, } from '../../../lib/constants/platforms'; +import { DATASOURCE_DOCKER } from '../../../lib/constants/data-binary-source'; jest.mock('../../../lib/workers/repository'); @@ -45,7 +46,7 @@ describe('lib/workers/global', () => { repositories: ['a', 'b'], hostRules: [ { - hostType: 'docker', + hostType: DATASOURCE_DOCKER, host: 'docker.io', username: 'some-user', password: 'some-password', @@ -65,7 +66,7 @@ describe('lib/workers/global', () => { repositories: ['a', 'b'], hostRules: [ { - hostType: 'docker', + hostType: DATASOURCE_DOCKER, host: 'docker.io', username: 'some-user', password: 'some-password', diff --git a/test/workers/pr/changelog/index.spec.ts b/test/workers/pr/changelog/index.spec.ts index 17ce9cc637..1d462a4bce 100644 --- a/test/workers/pr/changelog/index.spec.ts +++ b/test/workers/pr/changelog/index.spec.ts @@ -6,6 +6,7 @@ import { ChangeLogError, } from '../../../../lib/workers/pr/changelog'; import { mocked } from '../../../util'; +import { PLATFORM_TYPE_GITHUB } from '../../../../lib/constants/platforms'; import { VERSION_SCHEME_SEMVER } from '../../../../lib/constants/version-schemes'; jest.mock('../../../../lib/platform/github/gh-got-wrapper'); @@ -40,7 +41,7 @@ describe('workers/pr/changelog', () => { ghGot.mockClear(); hostRules.clear(); hostRules.add({ - hostType: 'github', + hostType: PLATFORM_TYPE_GITHUB, baseUrl: 'https://api.github.com/', token: 'abc', }); @@ -167,7 +168,7 @@ describe('workers/pr/changelog', () => { }); it('supports github enterprise and github.com changelog', async () => { hostRules.add({ - hostType: 'github', + hostType: PLATFORM_TYPE_GITHUB, token: 'super_secret', baseUrl: 'https://github-enterprise.example.com/', }); @@ -180,7 +181,7 @@ describe('workers/pr/changelog', () => { }); it('supports github enterprise and github enterprise changelog', async () => { hostRules.add({ - hostType: 'github', + hostType: PLATFORM_TYPE_GITHUB, baseUrl: 'https://github-enterprise.example.com/', token: 'abc', }); -- GitLab