Skip to content

Commit

Permalink
refactor: constants (#11690)
Browse files Browse the repository at this point in the history
  • Loading branch information
pret-a-porter committed Oct 6, 2021
1 parent d7e1f7c commit 1f86b71
Show file tree
Hide file tree
Showing 44 changed files with 161 additions and 205 deletions.
4 changes: 2 additions & 2 deletions lib/config/migration.spec.ts
@@ -1,4 +1,4 @@
import { PLATFORM_TYPE_GITHUB } from '../constants/platforms';
import { PlatformId } from '../constants';
import { getConfig } from './defaults';
import { setGlobalConfig } from './global';
import * as configMigration from './migration';
Expand All @@ -20,7 +20,7 @@ describe('config/migration', () => {
const config: TestRenovateConfig = {
endpoints: [{}] as never,
enabled: true,
platform: PLATFORM_TYPE_GITHUB,
platform: PlatformId.Github,
hostRules: [
{
platform: 'docker',
Expand Down
4 changes: 2 additions & 2 deletions lib/config/options/index.ts
@@ -1,4 +1,4 @@
import { PLATFORM_TYPE_GITHUB } from '../../constants/platforms';
import { PlatformId } from '../../constants';
import { getManagers } from '../../manager';
import { getPlatformList } from '../../platform';
import { getVersioningList } from '../../versioning';
Expand Down Expand Up @@ -559,7 +559,7 @@ const options: RenovateOptions[] = [
description: 'Platform type of repository.',
type: 'string',
allowedValues: getPlatformList(),
default: PLATFORM_TYPE_GITHUB,
default: PlatformId.Github,
globalOnly: true,
},
{
Expand Down
21 changes: 7 additions & 14 deletions lib/config/presets/local/index.ts
@@ -1,11 +1,4 @@
import {
PLATFORM_TYPE_AZURE,
PLATFORM_TYPE_BITBUCKET,
PLATFORM_TYPE_BITBUCKET_SERVER,
PLATFORM_TYPE_GITEA,
PLATFORM_TYPE_GITHUB,
PLATFORM_TYPE_GITLAB,
} from '../../../constants/platforms';
import { PlatformId } from '../../../constants';
import * as azure from '../azure';
import * as bitbucket from '../bitbucket';
import * as bitbucketServer from '../bitbucket-server';
Expand All @@ -15,12 +8,12 @@ import * as gitlab from '../gitlab';
import type { Preset, PresetConfig } from '../types';

const resolvers = {
[PLATFORM_TYPE_AZURE]: azure,
[PLATFORM_TYPE_BITBUCKET]: bitbucket,
[PLATFORM_TYPE_BITBUCKET_SERVER]: bitbucketServer,
[PLATFORM_TYPE_GITEA]: gitea,
[PLATFORM_TYPE_GITHUB]: github,
[PLATFORM_TYPE_GITLAB]: gitlab,
[PlatformId.Azure]: azure,
[PlatformId.Bitbucket]: bitbucket,
[PlatformId.BitbucketServer]: bitbucketServer,
[PlatformId.Gitea]: gitea,
[PlatformId.Github]: github,
[PlatformId.Gitlab]: gitlab,
};

export function getPreset({
Expand Down
1 change: 1 addition & 0 deletions lib/constants/index.ts
@@ -0,0 +1 @@
export * from './platforms';
19 changes: 5 additions & 14 deletions lib/constants/platform.spec.ts
Expand Up @@ -7,8 +7,7 @@ import { id as POD_DS } from '../datasource/pod';
import {
GITHUB_API_USING_HOST_TYPES,
GITLAB_API_USING_HOST_TYPES,
PLATFORM_TYPE_GITHUB,
PLATFORM_TYPE_GITLAB,
PlatformId,
} from './platforms';

describe('constants/platform', () => {
Expand All @@ -20,29 +19,21 @@ describe('constants/platform', () => {
expect(
GITLAB_API_USING_HOST_TYPES.includes(GitlabPackagesDatasource.id)
).toBeTrue();
expect(
GITLAB_API_USING_HOST_TYPES.includes(PLATFORM_TYPE_GITLAB)
).toBeTrue();
expect(GITLAB_API_USING_HOST_TYPES.includes(PlatformId.Gitlab)).toBeTrue();
});

it('should be not part of the GITLAB_API_USING_HOST_TYPES ', () => {
expect(
GITLAB_API_USING_HOST_TYPES.includes(PLATFORM_TYPE_GITHUB)
).toBeFalse();
expect(GITLAB_API_USING_HOST_TYPES.includes(PlatformId.Github)).toBeFalse();
});

it('should be part of the GITHUB_API_USING_HOST_TYPES ', () => {
expect(GITHUB_API_USING_HOST_TYPES.includes(GH_TAGS_DS)).toBeTrue();
expect(GITHUB_API_USING_HOST_TYPES.includes(GH_RELEASES_DS)).toBeTrue();
expect(GITHUB_API_USING_HOST_TYPES.includes(POD_DS)).toBeTrue();
expect(
GITHUB_API_USING_HOST_TYPES.includes(PLATFORM_TYPE_GITHUB)
).toBeTrue();
expect(GITHUB_API_USING_HOST_TYPES.includes(PlatformId.Github)).toBeTrue();
});

it('should be not part of the GITHUB_API_USING_HOST_TYPES ', () => {
expect(
GITHUB_API_USING_HOST_TYPES.includes(PLATFORM_TYPE_GITLAB)
).toBeFalse();
expect(GITHUB_API_USING_HOST_TYPES.includes(PlatformId.Gitlab)).toBeFalse();
});
});
18 changes: 10 additions & 8 deletions lib/constants/platforms.ts
@@ -1,19 +1,21 @@
export const PLATFORM_TYPE_AZURE = 'azure';
export const PLATFORM_TYPE_BITBUCKET = 'bitbucket';
export const PLATFORM_TYPE_BITBUCKET_SERVER = 'bitbucket-server';
export const PLATFORM_TYPE_GITEA = 'gitea';
export const PLATFORM_TYPE_GITHUB = 'github';
export const PLATFORM_TYPE_GITLAB = 'gitlab';
export const enum PlatformId {
Azure = 'azure',
Bitbucket = 'bitbucket',
BitbucketServer = 'bitbucket-server',
Gitea = 'gitea',
Github = 'github',
Gitlab = 'gitlab',
}

export const GITHUB_API_USING_HOST_TYPES = [
PLATFORM_TYPE_GITHUB,
PlatformId.Github,
'github-releases',
'github-tags',
'pod',
];

export const GITLAB_API_USING_HOST_TYPES = [
PLATFORM_TYPE_GITLAB,
PlatformId.Gitlab,
'gitlab-releases',
'gitlab-tags',
'gitlab-packages',
Expand Down
4 changes: 2 additions & 2 deletions lib/datasource/go/index.ts
@@ -1,5 +1,5 @@
import URL from 'url';
import { PLATFORM_TYPE_GITLAB } from '../../constants/platforms';
import { PlatformId } from '../../constants';
import { logger } from '../../logger';
import * as hostRules from '../../util/host-rules';
import { regEx } from '../../util/regex';
Expand Down Expand Up @@ -92,7 +92,7 @@ async function getDatasource(goModule: string): Promise<DataSource | null> {
}

const opts = hostRules.find({
hostType: PLATFORM_TYPE_GITLAB,
hostType: PlatformId.Gitlab,
url: goSourceUrl,
});
if (opts.token) {
Expand Down
9 changes: 3 additions & 6 deletions lib/manager/composer/artifacts.spec.ts
Expand Up @@ -3,10 +3,7 @@ import { envMock, exec, mockExecAll } from '../../../test/exec-util';
import { env, fs, git, mocked, partial } from '../../../test/util';
import { setGlobalConfig } from '../../config/global';
import type { RepoGlobalConfig } from '../../config/types';
import {
PLATFORM_TYPE_GITHUB,
PLATFORM_TYPE_GITLAB,
} from '../../constants/platforms';
import { PlatformId } from '../../constants';
import * as _datasource from '../../datasource';
import * as datasourcePackagist from '../../datasource/packagist';
import * as docker from '../../util/exec/docker';
Expand Down Expand Up @@ -97,12 +94,12 @@ describe('manager/composer/artifacts', () => {

it('uses hostRules to set COMPOSER_AUTH', async () => {
hostRules.add({
hostType: PLATFORM_TYPE_GITHUB,
hostType: PlatformId.Github,
matchHost: 'api.github.com',
token: 'github-token',
});
hostRules.add({
hostType: PLATFORM_TYPE_GITLAB,
hostType: PlatformId.Gitlab,
matchHost: 'gitlab.com',
token: 'gitlab-token',
});
Expand Down
9 changes: 3 additions & 6 deletions lib/manager/composer/artifacts.ts
@@ -1,14 +1,11 @@
import is from '@sindresorhus/is';
import { quote } from 'shlex';
import { getGlobalConfig } from '../../config/global';
import { PlatformId } from '../../constants';
import {
SYSTEM_INSUFFICIENT_DISK_SPACE,
TEMPORARY_ERROR,
} from '../../constants/error-messages';
import {
PLATFORM_TYPE_GITHUB,
PLATFORM_TYPE_GITLAB,
} from '../../constants/platforms';
import * as datasourcePackagist from '../../datasource/packagist';
import { logger } from '../../logger';
import { ExecOptions, exec } from '../../util/exec';
Expand Down Expand Up @@ -36,7 +33,7 @@ function getAuthJson(): string | null {
const authJson: AuthJson = {};

const githubCredentials = hostRules.find({
hostType: PLATFORM_TYPE_GITHUB,
hostType: PlatformId.Github,
url: 'https://api.github.com/',
});
if (githubCredentials?.token) {
Expand All @@ -46,7 +43,7 @@ function getAuthJson(): string | null {
}

hostRules
.findAll({ hostType: PLATFORM_TYPE_GITLAB })
.findAll({ hostType: PlatformId.Gitlab })
?.forEach((gitlabHostRule) => {
if (gitlabHostRule?.token) {
const host = gitlabHostRule.resolvedHost || 'gitlab.com';
Expand Down
4 changes: 2 additions & 2 deletions lib/manager/gomod/artifacts.ts
Expand Up @@ -2,8 +2,8 @@ import is from '@sindresorhus/is';
import { quote } from 'shlex';
import { dirname, join } from 'upath';
import { getGlobalConfig } from '../../config/global';
import { PlatformId } from '../../constants';
import { TEMPORARY_ERROR } from '../../constants/error-messages';
import { PLATFORM_TYPE_GITHUB } from '../../constants/platforms';
import { logger } from '../../logger';
import { ExecOptions, exec } from '../../util/exec';
import { ensureCacheDir, readLocalFile, writeLocalFile } from '../../util/fs';
Expand All @@ -19,7 +19,7 @@ import type {

function getPreCommands(): string[] | null {
const credentials = find({
hostType: PLATFORM_TYPE_GITHUB,
hostType: PlatformId.Github,
url: 'https://api.github.com/',
});
let preCommands = null;
Expand Down
12 changes: 4 additions & 8 deletions lib/manager/pre-commit/extract.ts
@@ -1,10 +1,6 @@
import is from '@sindresorhus/is';
import { load } from 'js-yaml';
import {
PLATFORM_TYPE_GITEA,
PLATFORM_TYPE_GITHUB,
PLATFORM_TYPE_GITLAB,
} from '../../constants/platforms';
import { PlatformId } from '../../constants';
import { id as githubTagsId } from '../../datasource/github-tags';
import { id as gitlabTagsId } from '../../datasource/gitlab-tags';
import { logger } from '../../logger';
Expand Down Expand Up @@ -55,9 +51,9 @@ function determineDatasource(
return { skipReason: SkipReason.UnknownRegistry, registryUrls: [hostname] };
}
for (const [hostType, sourceId] of [
[PLATFORM_TYPE_GITEA, gitlabTagsId],
[PLATFORM_TYPE_GITHUB, githubTagsId],
[PLATFORM_TYPE_GITLAB, gitlabTagsId],
[PlatformId.Gitea, gitlabTagsId],
[PlatformId.Github, githubTagsId],
[PlatformId.Gitlab, gitlabTagsId],
]) {
if (!isEmptyObject(find({ hostType, url: hostUrl }))) {
logger.debug(
Expand Down
8 changes: 4 additions & 4 deletions lib/platform/azure/azure-got-wrapper.spec.ts
@@ -1,4 +1,4 @@
import { PLATFORM_TYPE_AZURE } from '../../constants/platforms';
import { PlatformId } from '../../constants';
import * as _hostRules from '../../util/host-rules';

describe('platform/azure/azure-got-wrapper', () => {
Expand All @@ -19,7 +19,7 @@ describe('platform/azure/azure-got-wrapper', () => {
});
it('should set personal access token and endpoint', () => {
hostRules.add({
hostType: PLATFORM_TYPE_AZURE,
hostType: PlatformId.Azure,
token: '123test',
matchHost: 'https://dev.azure.com/renovate1',
});
Expand All @@ -35,7 +35,7 @@ describe('platform/azure/azure-got-wrapper', () => {
});
it('should set bearer token and endpoint', () => {
hostRules.add({
hostType: PLATFORM_TYPE_AZURE,
hostType: PlatformId.Azure,
token: 'testtoken',
matchHost: 'https://dev.azure.com/renovate2',
});
Expand All @@ -52,7 +52,7 @@ describe('platform/azure/azure-got-wrapper', () => {

it('should set password and endpoint', () => {
hostRules.add({
hostType: PLATFORM_TYPE_AZURE,
hostType: PlatformId.Azure,
username: 'user',
password: 'pass',
matchHost: 'https://dev.azure.com/renovate3',
Expand Down
4 changes: 2 additions & 2 deletions lib/platform/azure/azure-got-wrapper.ts
Expand Up @@ -4,11 +4,11 @@ import { ICoreApi } from 'azure-devops-node-api/CoreApi';
import { IGitApi } from 'azure-devops-node-api/GitApi';
import { IPolicyApi } from 'azure-devops-node-api/PolicyApi';
import { IRequestHandler } from 'azure-devops-node-api/interfaces/common/VsoBaseInterfaces';
import { PLATFORM_TYPE_AZURE } from '../../constants/platforms';
import { PlatformId } from '../../constants';
import { HostRule } from '../../types';
import * as hostRules from '../../util/host-rules';

const hostType = PLATFORM_TYPE_AZURE;
const hostType = PlatformId.Azure;
let endpoint: string;

function getAuthenticationHandler(config: HostRule): IRequestHandler {
Expand Down
4 changes: 2 additions & 2 deletions lib/platform/azure/index.ts
Expand Up @@ -8,8 +8,8 @@ import {
PullRequestStatus,
} from 'azure-devops-node-api/interfaces/GitInterfaces';
import delay from 'delay';
import { PlatformId } from '../../constants';
import { REPOSITORY_EMPTY } from '../../constants/error-messages';
import { PLATFORM_TYPE_AZURE } from '../../constants/platforms';
import { logger } from '../../logger';
import { BranchStatus, PrState, VulnerabilityAlert } from '../../types';
import * as git from '../../util/git';
Expand Down Expand Up @@ -73,7 +73,7 @@ const defaults: {
endpoint?: string;
hostType: string;
} = {
hostType: PLATFORM_TYPE_AZURE,
hostType: PlatformId.Azure,
};

export function initPlatform({
Expand Down
4 changes: 2 additions & 2 deletions lib/platform/bitbucket-server/index.ts
Expand Up @@ -2,12 +2,12 @@ import url from 'url';
import is from '@sindresorhus/is';
import delay from 'delay';
import type { PartialDeep } from 'type-fest';
import { PlatformId } from '../../constants';
import {
REPOSITORY_CHANGED,
REPOSITORY_EMPTY,
REPOSITORY_NOT_FOUND,
} from '../../constants/error-messages';
import { PLATFORM_TYPE_BITBUCKET_SERVER } from '../../constants/platforms';
import { logger } from '../../logger';
import { BranchStatus, PrState, VulnerabilityAlert } from '../../types';
import { GitProtocol } from '../../types/git';
Expand Down Expand Up @@ -68,7 +68,7 @@ const defaults: {
endpoint?: string;
hostType: string;
} = {
hostType: PLATFORM_TYPE_BITBUCKET_SERVER,
hostType: PlatformId.BitbucketServer,
};

/* istanbul ignore next */
Expand Down
4 changes: 2 additions & 2 deletions lib/platform/bitbucket/index.ts
@@ -1,8 +1,8 @@
import URL from 'url';
import is from '@sindresorhus/is';
import parseDiff from 'parse-diff';
import { PlatformId } from '../../constants';
import { REPOSITORY_NOT_FOUND } from '../../constants/error-messages';
import { PLATFORM_TYPE_BITBUCKET } from '../../constants/platforms';
import { logger } from '../../logger';
import { BranchStatus, PrState, VulnerabilityAlert } from '../../types';
import * as git from '../../util/git';
Expand Down Expand Up @@ -127,7 +127,7 @@ export async function initRepo({
}: RepoParams): Promise<RepoResult> {
logger.debug(`initRepo("${repository}")`);
const opts = hostRules.find({
hostType: PLATFORM_TYPE_BITBUCKET,
hostType: PlatformId.Bitbucket,
url: defaults.endpoint,
});
config = {
Expand Down

0 comments on commit 1f86b71

Please sign in to comment.