diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/azure.test.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/azure.test.ts index 896d91f4ed..d0860ea96c 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/azure.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/azure.test.ts @@ -92,7 +92,6 @@ describe('AzurePreparer', () => { }, }, }), - { logger }, ); await preparer.prepare(mockEntity, { logger }); @@ -113,7 +112,6 @@ describe('AzurePreparer', () => { ], }, }), - { logger }, ); await preparer.prepare(mockEntity, { logger }); @@ -126,7 +124,7 @@ describe('AzurePreparer', () => { }); it('calls the clone command with the correct arguments for a repository', async () => { - const preparer = new AzurePreparer(new ConfigReader({}), { logger }); + const preparer = new AzurePreparer(new ConfigReader({})); await preparer.prepare(mockEntity, { logger: getVoidLogger() }); @@ -138,7 +136,7 @@ describe('AzurePreparer', () => { }); it('calls the clone command with the correct arguments for a repository when no path is provided', async () => { - const preparer = new AzurePreparer(new ConfigReader({}), { logger }); + const preparer = new AzurePreparer(new ConfigReader({})); delete mockEntity.spec.path; await preparer.prepare(mockEntity, { logger: getVoidLogger() }); @@ -151,7 +149,7 @@ describe('AzurePreparer', () => { }); it('return the temp directory with the path to the folder if it is specified', async () => { - const preparer = new AzurePreparer(new ConfigReader({}), { logger }); + const preparer = new AzurePreparer(new ConfigReader({})); mockEntity.spec.path = './template/test/1/2/3'; const response = await preparer.prepare(mockEntity, { @@ -164,7 +162,7 @@ describe('AzurePreparer', () => { }); it('return the working directory with the path to the folder if it is specified', async () => { - const preparer = new AzurePreparer(new ConfigReader({}), { logger }); + const preparer = new AzurePreparer(new ConfigReader({})); mockEntity.spec.path = './template/test/1/2/3'; const response = await preparer.prepare(mockEntity, { diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/azure.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/azure.ts index d39b1f9e2d..001626f417 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/azure.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/azure.ts @@ -32,26 +32,31 @@ export class AzurePreparer implements PreparerBase { private readonly integrations: AzureIntegrationConfig[]; private readonly scaffolderToken: string | undefined; - constructor(config: Config, { logger }: { logger: Logger }) { + static fromConfig(config: Config, { logger }: { logger: Logger }) { + const integrations = readAzureIntegrationConfigs( + config.getOptionalConfigArray('integrations.azure') ?? [], + ); + + if ( + config.getOptionalString('scaffolder.azure.api.token') && + !integrations.length + ) { + logger.warn( + "DEPRECATION: Using the token format under 'scaffolder.azure.api.token' will not be respected in future releases. Please consider using integrations config instead", + 'Please migrate to using integrations config and specifying tokens under hostnames', + ); + } + return new AzurePreparer(config); + } + + constructor(config: Config) { this.integrations = readAzureIntegrationConfigs( config.getOptionalConfigArray('integrations.azure') ?? [], ); - if (!this.integrations.length) { - logger.warn( - 'Integrations for Azure in Scaffolder are not set. This will cause errors in a future release. Please migrate to using integrations config and specifying tokens under hostnames', - ); - } - this.scaffolderToken = config.getOptionalString( 'scaffolder.azure.api.token', ); - - if (this.scaffolderToken) { - logger.warn( - "DEPRECATION: Using the token format under 'scaffolder.azure.api.token' will not be respected in future releases. Please consider using integrations config instead", - ); - } } async prepare( diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.test.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.test.ts index d55ca7a01d..05b64ca777 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.test.ts @@ -80,7 +80,7 @@ describe('BitbucketPreparer', () => { }); it('calls the clone command with the correct arguments for a repository', async () => { - const preparer = new BitbucketPreparer(new ConfigReader({}), { logger }); + const preparer = new BitbucketPreparer(new ConfigReader({})); await preparer.prepare(mockEntity, { logger: getVoidLogger() }); expect(mockGitClient.clone).toHaveBeenCalledWith({ url: 'https://bitbucket.org/backstage-project/backstage-repo', @@ -101,7 +101,6 @@ describe('BitbucketPreparer', () => { ], }, }), - { logger }, ); await preparer.prepare(mockEntity, { logger }); @@ -114,7 +113,7 @@ describe('BitbucketPreparer', () => { }); it('calls the clone command with the correct arguments for a repository when no path is provided', async () => { - const preparer = new BitbucketPreparer(new ConfigReader({}), { logger }); + const preparer = new BitbucketPreparer(new ConfigReader({})); delete mockEntity.spec.path; await preparer.prepare(mockEntity, { logger: getVoidLogger() }); expect(mockGitClient.clone).toHaveBeenCalledWith({ @@ -124,7 +123,7 @@ describe('BitbucketPreparer', () => { }); it('return the temp directory with the path to the folder if it is specified', async () => { - const preparer = new BitbucketPreparer(new ConfigReader({}), { logger }); + const preparer = new BitbucketPreparer(new ConfigReader({})); mockEntity.spec.path = './template/test/1/2/3'; const response = await preparer.prepare(mockEntity, { logger: getVoidLogger(), @@ -147,7 +146,6 @@ describe('BitbucketPreparer', () => { }, }, }), - { logger }, ); await preparer.prepare(mockEntity, { logger }); @@ -172,7 +170,6 @@ describe('BitbucketPreparer', () => { ], }, }), - { logger }, ); await preparer.prepare(mockEntity, { logger }); @@ -197,7 +194,6 @@ describe('BitbucketPreparer', () => { ], }, }), - { logger }, ); await preparer.prepare(mockEntity, { logger }); @@ -210,7 +206,7 @@ describe('BitbucketPreparer', () => { }); it('return the working directory with the path to the folder if it is specified', async () => { - const preparer = new BitbucketPreparer(new ConfigReader({}), { logger }); + const preparer = new BitbucketPreparer(new ConfigReader({})); mockEntity.spec.path = './template/test/1/2/3'; const response = await preparer.prepare(mockEntity, { workingDirectory: '/workDir', diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.ts index 26d9b476ee..7cd724cbc1 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.ts @@ -32,27 +32,35 @@ export class BitbucketPreparer implements PreparerBase { private readonly privateToken: string; private readonly username: string; private readonly integrations: BitbucketIntegrationConfig[]; - constructor(config: Config, { logger }: { logger: Logger }) { - this.integrations = readBitbucketIntegrationConfigs( + + static fromConfig(config: Config, { logger }: { logger: Logger }) { + const integrations = readBitbucketIntegrationConfigs( config.getOptionalConfigArray('integrations.bitbucket') ?? [], ); - if (!this.integrations.length) { + const user = config.getOptionalString('scaffolder.bitbucket.api.username'); + const token = config.getOptionalString('scaffolder.bitbucket.api.token'); + const password = config.getOptionalString( + 'scaffolder.bitbucket.api.appPassword', + ); + + if (!integrations && (user || token || password)) { logger.warn( - 'Integrations for BitBucket in Scaffolder are not set. This will cause errors in a future release. Please migrate to using integrations config and specifying tokens under hostnames', + "DEPRECATION: Setting credentials under 'scaffolder.bitbucket.api' will not be respected in future releases. Please consider using integrations config instead", + 'Please migrate to using integrations config and specifying tokens under hostnames', ); } + return new BitbucketPreparer(config); + } + constructor(config: Config) { + this.integrations = readBitbucketIntegrationConfigs( + config.getOptionalConfigArray('integrations.bitbucket') ?? [], + ); this.username = config.getOptionalString('scaffolder.bitbucket.api.username') ?? ''; this.privateToken = config.getOptionalString('scaffolder.bitbucket.api.token') ?? ''; - - if (this.username || this.privateToken) { - logger.warn( - "DEPRECATION: Using the token format under 'scaffolder.bitbucket.token' will not be respected in future releases. Please consider using integrations config instead", - ); - } } async prepare( diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.test.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.test.ts index 122a1abb4c..68178610b5 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.test.ts @@ -87,7 +87,6 @@ describe('GitHubPreparer', () => { }, }, }), - { logger }, ); await preparer.prepare(mockEntity, { logger: getVoidLogger() }); @@ -98,7 +97,7 @@ describe('GitHubPreparer', () => { }); }); it('calls the clone command with the correct arguments for a repository when no path is provided', async () => { - const preparer = new GithubPreparer(new ConfigReader({}), { logger }); + const preparer = new GithubPreparer(new ConfigReader({})); delete mockEntity.spec.path; await preparer.prepare(mockEntity, { logger: getVoidLogger() }); @@ -110,7 +109,7 @@ describe('GitHubPreparer', () => { }); it('return the temp directory with the path to the folder if it is specified', async () => { - const preparer = new GithubPreparer(new ConfigReader({}), { logger }); + const preparer = new GithubPreparer(new ConfigReader({})); mockEntity.spec.path = './template/test/1/2/3'; const response = await preparer.prepare(mockEntity, { logger: getVoidLogger(), @@ -121,7 +120,7 @@ describe('GitHubPreparer', () => { }); it('return the working directory with the path to the folder if it is specified', async () => { - const preparer = new GithubPreparer(new ConfigReader({}), { logger }); + const preparer = new GithubPreparer(new ConfigReader({})); mockEntity.spec.path = './template/test/1/2/3'; const response = await preparer.prepare(mockEntity, { workingDirectory: '/workDir', @@ -142,7 +141,6 @@ describe('GitHubPreparer', () => { }, }, }), - { logger }, ); await preparer.prepare(mockEntity, { logger }); @@ -161,7 +159,6 @@ describe('GitHubPreparer', () => { github: [{ host: 'github.com', token: 'fake-me' }], }, }), - { logger }, ); await preparer.prepare(mockEntity, { logger }); diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts index 81b212baaf..6ba341ad2c 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts @@ -32,24 +32,29 @@ export class GithubPreparer implements PreparerBase { private readonly integrations: GitHubIntegrationConfig[]; private readonly scaffolderToken: string | undefined; - constructor(config: Config, { logger }: { logger: Logger }) { - this.integrations = readGitHubIntegrationConfigs( + static fromConfig(config: Config, { logger }: { logger: Logger }) { + const integrations = readGitHubIntegrationConfigs( config.getOptionalConfigArray('integrations.github') ?? [], ); - if (!this.integrations.length) { - logger.warn( - 'Integrations for Github in Scaffolder are not set. This will cause errors in a future release. Please migrate to using integrations config and specifying tokens under hostnames', - ); - } - - this.scaffolderToken = config.getOptionalString('scaffolder.github.token'); - - if (this.scaffolderToken) { + if ( + config.getOptionalString('scaffolder.github.token') && + !integrations.length + ) { logger.warn( "DEPRECATION: Using the token format under 'scaffolder.github.token' will not be respected in future releases. Please consider using integrations config instead", + 'Please migrate to using integrations config and specifying tokens under hostnames', ); } + + return new GithubPreparer(config); + } + + constructor(config: Config) { + this.integrations = readGitHubIntegrationConfigs( + config.getOptionalConfigArray('integrations.github') ?? [], + ); + this.scaffolderToken = config.getOptionalString('scaffolder.github.token'); } async prepare( diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/gitlab.test.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/gitlab.test.ts index c1e42920ae..f9e79e6c71 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/gitlab.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/gitlab.test.ts @@ -80,7 +80,7 @@ describe('GitLabPreparer', () => { ['gitlab'].forEach(protocol => { it(`calls the clone command with the correct arguments for a repository using the ${protocol} protocol`, async () => { - const preparer = new GitlabPreparer(new ConfigReader({}), { logger }); + const preparer = new GitlabPreparer(new ConfigReader({})); mockEntity = mockEntityWithProtocol(protocol); await preparer.prepare(mockEntity, { logger: getVoidLogger() }); @@ -103,7 +103,6 @@ describe('GitLabPreparer', () => { ], }, }), - { logger }, ); mockEntity = mockEntityWithProtocol(protocol); @@ -123,7 +122,6 @@ describe('GitLabPreparer', () => { gitlab: { api: { token: 'fake-token' } }, }, }), - { logger }, ); mockEntity = mockEntityWithProtocol(protocol); @@ -137,7 +135,7 @@ describe('GitLabPreparer', () => { }); it(`calls the clone command with the correct arguments for a repository when no path is provided using the ${protocol} protocol`, async () => { - const preparer = new GitlabPreparer(new ConfigReader({}), { logger }); + const preparer = new GitlabPreparer(new ConfigReader({})); mockEntity = mockEntityWithProtocol(protocol); delete mockEntity.spec.path; @@ -150,7 +148,7 @@ describe('GitLabPreparer', () => { }); it(`return the temp directory with the path to the folder if it is specified using the ${protocol} protocol`, async () => { - const preparer = new GitlabPreparer(new ConfigReader({}), { logger }); + const preparer = new GitlabPreparer(new ConfigReader({})); mockEntity = mockEntityWithProtocol(protocol); mockEntity.spec.path = './template/test/1/2/3'; const response = await preparer.prepare(mockEntity, { @@ -162,7 +160,7 @@ describe('GitLabPreparer', () => { }); it('return the working directory with the path to the folder if it is specified', async () => { - const preparer = new GitlabPreparer(new ConfigReader({}), { logger }); + const preparer = new GitlabPreparer(new ConfigReader({})); mockEntity.spec.path = './template/test/1/2/3'; const response = await preparer.prepare(mockEntity, { workingDirectory: '/workDir', diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/gitlab.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/gitlab.ts index 7f9d9c7d3a..4e5cdb9cc7 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/gitlab.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/gitlab.ts @@ -32,26 +32,32 @@ export class GitlabPreparer implements PreparerBase { private readonly integrations: GitLabIntegrationConfig[]; private readonly scaffolderToken: string | undefined; - constructor(config: Config, { logger }: { logger: Logger }) { + static fromConfig(config: Config, { logger }: { logger: Logger }) { + const integrations = readGitLabIntegrationConfigs( + config.getOptionalConfigArray('integrations.gitlab') ?? [], + ); + + if ( + config.getOptionalString('scaffolder.gitlab.api.token') && + !integrations.length + ) { + logger.warn( + "DEPRECATION: Using the token format under 'scaffolder.gitlab.token' will not be respected in future releases. Please consider using integrations config instead", + 'Please migrate to using integrations config and specifying tokens under hostnames', + ); + } + + return new GitlabPreparer(config); + } + + constructor(config: Config) { this.integrations = readGitLabIntegrationConfigs( config.getOptionalConfigArray('integrations.gitlab') ?? [], ); - if (!this.integrations.length) { - logger.warn( - 'Integrations for GitLab in Scaffolder are not set. This will cause errors in a future release. Please migrate to using integrations config and specifying tokens under hostnames', - ); - } - this.scaffolderToken = config.getOptionalString( 'scaffolder.gitlab.api.token', ); - - if (this.scaffolderToken) { - logger.warn( - "DEPRECATION: Using the token format under 'scaffolder.gitlab.api.token' will not be respected in future releases. Please consider using integrations config instead", - ); - } } async prepare( diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/preparers.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/preparers.ts index 251e8b0ae3..94d2706cd8 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/preparers.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/preparers.ts @@ -78,10 +78,10 @@ export class Preparers implements PreparerBuilder { const preparers = new Preparers(typeDetector); const filePreparer = new FilePreparer(); - const gitlabPreparer = new GitlabPreparer(config, { logger }); - const azurePreparer = new AzurePreparer(config, { logger }); - const githubPreparer = new GithubPreparer(config, { logger }); - const bitbucketPreparer = new BitbucketPreparer(config, { logger }); + const gitlabPreparer = GitlabPreparer.fromConfig(config, { logger }); + const azurePreparer = AzurePreparer.fromConfig(config, { logger }); + const githubPreparer = GithubPreparer.fromConfig(config, { logger }); + const bitbucketPreparer = BitbucketPreparer.fromConfig(config, { logger }); preparers.register('file', filePreparer); preparers.register('gitlab', gitlabPreparer);