Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: convert PlatformmId to union #18458

Merged
merged 17 commits into from
Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/config/migration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { PlatformId } from '../constants';
import { GlobalConfig } from './global';
import * as configMigration from './migration';
import type {
Expand All @@ -16,7 +15,7 @@ describe('config/migration', () => {
const config: TestRenovateConfig = {
endpoints: [{}] as never,
enabled: true,
platform: PlatformId.Github,
platform: 'github',
hostRules: [
{
platform: 'docker',
Expand Down
3 changes: 1 addition & 2 deletions lib/config/options/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { PlatformId } from '../../constants';
import { getManagers } from '../../modules/manager';
import { getPlatformList } from '../../modules/platform';
import { getVersioningList } from '../../modules/versioning';
Expand Down Expand Up @@ -644,7 +643,7 @@ const options: RenovateOptions[] = [
description: 'Platform type of repository.',
type: 'string',
allowedValues: getPlatformList(),
default: PlatformId.Github,
default: 'github',
globalOnly: true,
},
{
Expand Down
14 changes: 7 additions & 7 deletions lib/config/presets/local/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PlatformId } from '../../../constants';
import type { PlatformId } from '../../../constants';
import { GlobalConfig } from '../../global';
import * as azure from '../azure';
import * as bitbucket from '../bitbucket';
Expand All @@ -9,12 +9,12 @@ import * as gitlab from '../gitlab';
import type { Preset, PresetConfig } from '../types';

const resolvers = {
[PlatformId.Azure]: azure,
[PlatformId.Bitbucket]: bitbucket,
[PlatformId.BitbucketServer]: bitbucketServer,
[PlatformId.Gitea]: gitea,
[PlatformId.Github]: github,
[PlatformId.Gitlab]: gitlab,
azure,
bitbucket,
'bitbucket-server': bitbucketServer,
gitea,
github,
gitlab,
} as const;

export function getPreset({
Expand Down
5 changes: 3 additions & 2 deletions lib/config/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { LogLevel } from 'bunyan';
import type { Range } from 'semver';
import type { PlatformId } from '../constants';
import type { HostRule } from '../types';
import type { GitNoVerifyOption } from '../util/git/types';

Expand Down Expand Up @@ -100,7 +101,7 @@ export interface GlobalOnlyConfig {
privateKeyPathOld?: string;
redisUrl?: string;
repositories?: RenovateRepository[];
platform?: string;
platform?: PlatformId;
endpoint?: string;
}

Expand Down Expand Up @@ -128,7 +129,7 @@ export interface RepoGlobalConfig {
localDir?: string;
cacheDir?: string;
containerbaseDir?: string;
platform?: string;
platform?: PlatformId;
endpoint?: string;
}

Expand Down
13 changes: 5 additions & 8 deletions lib/constants/platform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
BITBUCKET_API_USING_HOST_TYPES,
GITHUB_API_USING_HOST_TYPES,
GITLAB_API_USING_HOST_TYPES,
PlatformId,
} from './platforms';

describe('constants/platform', () => {
Expand All @@ -29,11 +28,11 @@ describe('constants/platform', () => {
expect(
GITLAB_API_USING_HOST_TYPES.includes(GITLAB_CHANGELOG_ID)
).toBeTrue();
expect(GITLAB_API_USING_HOST_TYPES.includes(PlatformId.Gitlab)).toBeTrue();
expect(GITLAB_API_USING_HOST_TYPES.includes('gitlab')).toBeTrue();
});

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

it('should be part of the GITHUB_API_USING_HOST_TYPES ', () => {
Expand All @@ -50,19 +49,17 @@ describe('constants/platform', () => {
expect(
GITHUB_API_USING_HOST_TYPES.includes(GITHUB_CHANGELOG_ID)
).toBeTrue();
expect(GITHUB_API_USING_HOST_TYPES.includes(PlatformId.Github)).toBeTrue();
expect(GITHUB_API_USING_HOST_TYPES.includes('github')).toBeTrue();
});

it('should be not part of the GITHUB_API_USING_HOST_TYPES ', () => {
expect(GITHUB_API_USING_HOST_TYPES.includes(PlatformId.Gitlab)).toBeFalse();
expect(GITHUB_API_USING_HOST_TYPES.includes('gitlab')).toBeFalse();
});

it('should be part of the BITBUCKET_API_USING_HOST_TYPES ', () => {
expect(
BITBUCKET_API_USING_HOST_TYPES.includes(BitBucketTagsDatasource.id)
).toBeTrue();
expect(
BITBUCKET_API_USING_HOST_TYPES.includes(PlatformId.Bitbucket)
).toBeTrue();
expect(BITBUCKET_API_USING_HOST_TYPES.includes('bitbucket')).toBeTrue();
});
});
25 changes: 10 additions & 15 deletions lib/constants/platforms.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// eslint-disable-next-line typescript-enum/no-enum, typescript-enum/no-const-enum
export const enum PlatformId {
Azure = 'azure',
Bitbucket = 'bitbucket',
BitbucketServer = 'bitbucket-server',
Gitea = 'gitea',
Github = 'github',
Gitlab = 'gitlab',
}
export type PlatformId =
| 'azure'
| 'bitbucket'
| 'bitbucket-server'
| 'gitea'
| 'github'
| 'gitlab';

export const GITHUB_API_USING_HOST_TYPES = [
PlatformId.Github,
'github',
'github-releases',
'github-tags',
'pod',
Expand All @@ -18,14 +16,11 @@ export const GITHUB_API_USING_HOST_TYPES = [
];

export const GITLAB_API_USING_HOST_TYPES = [
PlatformId.Gitlab,
'gitlab',
'gitlab-releases',
'gitlab-tags',
'gitlab-packages',
'gitlab-changelog',
];

export const BITBUCKET_API_USING_HOST_TYPES = [
PlatformId.Bitbucket,
'bitbucket-tags',
];
export const BITBUCKET_API_USING_HOST_TYPES = ['bitbucket', 'bitbucket-tags'];
11 changes: 5 additions & 6 deletions lib/modules/datasource/go/base.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Fixtures } from '../../../../test/fixtures';
import * as httpMock from '../../../../test/http-mock';
import { mocked } from '../../../../test/util';
import { PlatformId } from '../../../constants';
import * as _hostRules from '../../../util/host-rules';
import { GithubTagsDatasource } from '../github-tags';
import { GitlabTagsDatasource } from '../gitlab-tags';
Expand Down Expand Up @@ -203,7 +202,7 @@ describe('modules/datasource/go/base', () => {
});

it('supports GitLab EE deps', async () => {
hostRules.hostType.mockReturnValue(PlatformId.Gitlab);
hostRules.hostType.mockReturnValue('gitlab');
httpMock
.scope('https://my.custom.domain')
.get('/golang/myrepo?go-get=1')
Expand All @@ -221,7 +220,7 @@ describe('modules/datasource/go/base', () => {
});

it('supports GitLab EE deps in subgroup', async () => {
hostRules.hostType.mockReturnValue(PlatformId.Gitlab);
hostRules.hostType.mockReturnValue('gitlab');
httpMock
.scope('https://my.custom.domain')
.get('/golang/subgroup/myrepo?go-get=1')
Expand All @@ -239,7 +238,7 @@ describe('modules/datasource/go/base', () => {
});

it('supports GitLab EE deps in subgroup with version', async () => {
hostRules.hostType.mockReturnValue(PlatformId.Gitlab);
hostRules.hostType.mockReturnValue('gitlab');
httpMock
.scope('https://my.custom.domain')
.get('/golang/subgroup/myrepo/v2?go-get=1')
Expand All @@ -257,7 +256,7 @@ describe('modules/datasource/go/base', () => {
});

it('supports GitLab EE deps in private subgroup with vcs indicator', async () => {
hostRules.hostType.mockReturnValue(PlatformId.Gitlab);
hostRules.hostType.mockReturnValue('gitlab');
httpMock
.scope('https://my.custom.domain')
.get('/golang/subgroup/myrepo.git/v2?go-get=1')
Expand All @@ -275,7 +274,7 @@ describe('modules/datasource/go/base', () => {
});

it('supports GitLab EE monorepo deps in subgroup', async () => {
hostRules.hostType.mockReturnValue(PlatformId.Gitlab);
hostRules.hostType.mockReturnValue('gitlab');
httpMock
.scope('https://my.custom.domain')
.get('/golang/subgroup/myrepo/monorepo?go-get=1')
Expand Down
3 changes: 1 addition & 2 deletions lib/modules/datasource/go/base.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// TODO: types (#7154)
/* eslint-disable @typescript-eslint/restrict-template-expressions */
import URL from 'url';
import { PlatformId } from '../../../constants';
import { logger } from '../../../logger';
import * as hostRules from '../../../util/host-rules';
import { Http } from '../../../util/http';
Expand Down Expand Up @@ -137,7 +136,7 @@ export class BaseGoDatasource {
};
}

if (hostRules.hostType({ url: goSourceUrl }) === PlatformId.Gitlab) {
if (hostRules.hostType({ url: goSourceUrl }) === 'gitlab') {
// get server base url from import url
const parsedUrl = URL.parse(goSourceUrl);

Expand Down
5 changes: 2 additions & 3 deletions lib/modules/manager/composer/artifacts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { envMock, mockExecAll } from '../../../../test/exec-util';
import { env, fs, git, mocked, partial } from '../../../../test/util';
import { GlobalConfig } from '../../../config/global';
import type { RepoGlobalConfig } from '../../../config/types';
import { PlatformId } from '../../../constants';
import * as docker from '../../../util/exec/docker';
import type { StatusResult } from '../../../util/git/types';
import * as hostRules from '../../../util/host-rules';
Expand Down Expand Up @@ -111,12 +110,12 @@ describe('modules/manager/composer/artifacts', () => {

it('uses hostRules to set COMPOSER_AUTH', async () => {
hostRules.add({
hostType: PlatformId.Github,
hostType: 'github',
matchHost: 'api.github.com',
token: 'github-token',
});
hostRules.add({
hostType: PlatformId.Gitlab,
hostType: 'gitlab',
matchHost: 'gitlab.com',
token: 'gitlab-token',
});
Expand Down
29 changes: 13 additions & 16 deletions lib/modules/manager/composer/artifacts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import is from '@sindresorhus/is';
import { quote } from 'shlex';
import { PlatformId } from '../../../constants';
import {
SYSTEM_INSUFFICIENT_DISK_SPACE,
TEMPORARY_ERROR,
Expand Down Expand Up @@ -34,7 +33,7 @@ function getAuthJson(): string | null {
const authJson: AuthJson = {};

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

hostRules
.findAll({ hostType: PlatformId.Gitlab })
?.forEach((gitlabHostRule) => {
if (gitlabHostRule?.token) {
const host = gitlabHostRule.resolvedHost ?? 'gitlab.com';
authJson['gitlab-token'] = authJson['gitlab-token'] ?? {};
authJson['gitlab-token'][host] = gitlabHostRule.token;
// https://getcomposer.org/doc/articles/authentication-for-private-packages.md#gitlab-token
authJson['gitlab-domains'] = [
host,
...(authJson['gitlab-domains'] ?? []),
];
}
});
hostRules.findAll({ hostType: 'gitlab' })?.forEach((gitlabHostRule) => {
if (gitlabHostRule?.token) {
const host = gitlabHostRule.resolvedHost ?? 'gitlab.com';
authJson['gitlab-token'] = authJson['gitlab-token'] ?? {};
authJson['gitlab-token'][host] = gitlabHostRule.token;
// https://getcomposer.org/doc/articles/authentication-for-private-packages.md#gitlab-token
authJson['gitlab-domains'] = [
host,
...(authJson['gitlab-domains'] ?? []),
];
}
});

hostRules
.findAll({ hostType: PackagistDatasource.id })
Expand Down
23 changes: 11 additions & 12 deletions lib/modules/manager/gomod/artifacts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { envMock, mockExecAll } from '../../../../test/exec-util';
import { env, fs, git, mocked } from '../../../../test/util';
import { GlobalConfig } from '../../../config/global';
import type { RepoGlobalConfig } from '../../../config/types';
import { PlatformId } from '../../../constants/platforms';
import * as docker from '../../../util/exec/docker';
import type { StatusResult } from '../../../util/git/types';
import * as _hostRules from '../../../util/host-rules';
Expand Down Expand Up @@ -399,7 +398,7 @@ describe('modules/manager/gomod/artifacts', () => {
hostRules.getAll.mockReturnValueOnce([
{
token: 'some-token',
hostType: PlatformId.Github,
hostType: 'github',
matchHost: 'api.github.com',
},
{ token: 'some-other-token', matchHost: 'https://gitea.com' },
Expand Down Expand Up @@ -499,13 +498,13 @@ describe('modules/manager/gomod/artifacts', () => {
hostRules.getAll.mockReturnValueOnce([
{
token: 'some-token',
hostType: PlatformId.Github,
hostType: 'github',
matchHost: 'api.github.com',
},
{
token: 'some-enterprise-token',
matchHost: 'github.enterprise.com',
hostType: PlatformId.Github,
hostType: 'github',
},
]);
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
Expand Down Expand Up @@ -568,7 +567,7 @@ describe('modules/manager/gomod/artifacts', () => {
{
token: 'some-enterprise-token',
matchHost: 'gitlab.enterprise.com',
hostType: PlatformId.Gitlab,
hostType: 'gitlab',
},
]);
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
Expand Down Expand Up @@ -623,12 +622,12 @@ describe('modules/manager/gomod/artifacts', () => {
{
token: 'some-enterprise-token-repo1',
matchHost: 'https://gitlab.enterprise.com/repo1',
hostType: PlatformId.Gitlab,
hostType: 'gitlab',
},
{
token: 'some-enterprise-token-repo2',
matchHost: 'https://gitlab.enterprise.com/repo2',
hostType: PlatformId.Gitlab,
hostType: 'gitlab',
},
]);
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
Expand Down Expand Up @@ -692,12 +691,12 @@ describe('modules/manager/gomod/artifacts', () => {
{
token: 'some-token',
matchHost: 'ssh://github.enterprise.com',
hostType: PlatformId.Github,
hostType: 'github',
},
{
token: 'some-gitlab-token',
matchHost: 'gitlab.enterprise.com',
hostType: PlatformId.Gitlab,
hostType: 'gitlab',
},
]);
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
Expand Down Expand Up @@ -755,17 +754,17 @@ describe('modules/manager/gomod/artifacts', () => {
{
token: 'some-token',
matchHost: 'api.github.com',
hostType: PlatformId.Github,
hostType: 'github',
},
{
token: 'some-enterprise-token',
matchHost: 'github.enterprise.com',
hostType: PlatformId.Github,
hostType: 'github',
},
{
token: 'some-gitlab-token',
matchHost: 'gitlab.enterprise.com',
hostType: PlatformId.Gitlab,
hostType: 'gitlab',
},
]);
fs.readLocalFile.mockResolvedValueOnce('Current go.sum');
Expand Down
Loading