diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/azure.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/azure.ts index df127d22e7..8b204fa799 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/azure.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/azure.ts @@ -25,10 +25,10 @@ import { AzureIntegrationConfig } from '@backstage/integration'; export class AzurePreparer implements PreparerBase { static fromConfig(config: AzureIntegrationConfig) { - return new AzurePreparer(config.token); + return new AzurePreparer({ token: config.token }); } - constructor(private readonly token?: string) {} + constructor(private readonly config: { token?: string }) {} async prepare( template: TemplateEntityV1alpha1, @@ -53,9 +53,9 @@ export class AzurePreparer implements PreparerBase { // Username can be anything but the empty string according to: // https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page#use-a-pat - const git = this.token + const git = this.config.token ? Git.fromAuth({ - password: this.token, + password: this.config.token, username: 'notempty', logger, }) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.ts index 8a66176b9c..5020a5f658 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.ts @@ -25,17 +25,19 @@ import parseGitUrl from 'git-url-parse'; export class BitbucketPreparer implements PreparerBase { static fromConfig(config: BitbucketIntegrationConfig) { - return new BitbucketPreparer( - config.username, - config.token, - config.appPassword, - ); + return new BitbucketPreparer({ + username: config.username, + token: config.token, + appPassword: config.appPassword, + }); } constructor( - private readonly username?: string, - private readonly token?: string, - private readonly appPassword?: string, + private readonly config: { + username?: string; + token?: string; + appPassword?: string; + }, ) {} async prepare( @@ -78,14 +80,16 @@ export class BitbucketPreparer implements PreparerBase { } private getAuth(): { username: string; password: string } | undefined { - if (this.username && this.appPassword) { - return { username: this.username, password: this.appPassword }; + const { username, token, appPassword } = this.config; + + if (username && appPassword) { + return { username: username, password: appPassword }; } - if (this.token) { + if (token) { return { username: 'x-token-auth', - password: this.token! || this.appPassword!, + password: token! || appPassword!, }; } diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts index 38eb9d408d..cfeff454e7 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/github.ts @@ -25,10 +25,10 @@ import { GitHubIntegrationConfig } from '@backstage/integration'; export class GithubPreparer implements PreparerBase { static fromConfig(config: GitHubIntegrationConfig) { - return new GithubPreparer(config.token); + return new GithubPreparer({ token: config.token }); } - constructor(private readonly token?: string) {} + constructor(private readonly config: { token?: string }) {} async prepare( template: TemplateEntityV1alpha1, @@ -53,9 +53,9 @@ export class GithubPreparer implements PreparerBase { const checkoutLocation = path.resolve(tempDir, templateDirectory); - const git = this.token + const git = this.config.token ? Git.fromAuth({ - username: this.token, + username: this.config.token, password: 'x-oauth-basic', logger, }) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/gitlab.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/gitlab.ts index 9945a741c6..1fca2ff0f9 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/gitlab.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/gitlab.ts @@ -25,10 +25,10 @@ import { PreparerBase, PreparerOptions } from './types'; export class GitlabPreparer implements PreparerBase { static fromConfig(config: GitLabIntegrationConfig) { - return new GitlabPreparer(config.token); + return new GitlabPreparer({ token: config.token }); } - constructor(private readonly token?: string) {} + constructor(private readonly config: { token?: string }) {} async prepare( template: TemplateEntityV1alpha1, @@ -51,9 +51,9 @@ export class GitlabPreparer implements PreparerBase { template.spec.path ?? '.', ); - const git = this.token + const git = this.config.token ? Git.fromAuth({ - password: this.token, + password: this.config.token, username: 'oauth2', logger, }) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.ts index 858809556f..a27eba8a14 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.ts @@ -30,13 +30,10 @@ export class AzurePublisher implements PublisherBase { const authHandler = getPersonalAccessTokenHandler(config.token); const webApi = new WebApi(config.host, authHandler); const azureClient = await webApi.getGitApi(); - return new AzurePublisher(config.token, azureClient); + return new AzurePublisher({ token: config.token, client: azureClient }); } - constructor( - private readonly token: string, - private readonly azureClient: IGitApi, - ) {} + constructor(private readonly config: { token: string; client: IGitApi }) {} async publish({ values, @@ -57,7 +54,7 @@ export class AzurePublisher implements PublisherBase { remoteUrl, auth: { username: 'notempty', - password: this.token, + password: this.config.token, }, logger, }); @@ -68,7 +65,7 @@ export class AzurePublisher implements PublisherBase { private async createRemote(opts: { name: string; project: string }) { const { name, project } = opts; const createOptions: GitRepositoryCreateOptions = { name }; - const repo = await this.azureClient.createRepository( + const repo = await this.config.client.createRepository( createOptions, project, ); diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/bitbucket.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/bitbucket.ts index ddfc59a6f7..a0b867507d 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/bitbucket.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/bitbucket.ts @@ -26,19 +26,21 @@ import parseGitUrl from 'git-url-parse'; // a supported bitbucket client if one exists. export class BitbucketPublisher implements PublisherBase { static async fromConfig(config: BitbucketIntegrationConfig) { - return new BitbucketPublisher( - config.host, - config.token, - config.appPassword, - config.username, - ); + return new BitbucketPublisher({ + host: config.host, + token: config.token, + appPassword: config.appPassword, + username: config.username, + }); } constructor( - private readonly host: string, - private readonly token?: string, - private readonly appPassword?: string, - private readonly username?: string, + private readonly config: { + host: string; + token?: string; + appPassword?: string; + username?: string; + }, ) {} async publish({ @@ -59,8 +61,10 @@ export class BitbucketPublisher implements PublisherBase { dir: directory, remoteUrl: result.remoteUrl, auth: { - username: this.username ? this.username : 'x-token-auth', - password: this.appPassword ? this.appPassword : this.token ?? '', + username: this.config.username ? this.config.username : 'x-token-auth', + password: this.config.appPassword + ? this.config.appPassword + : this.config.token ?? '', }, logger, }); @@ -72,7 +76,7 @@ export class BitbucketPublisher implements PublisherBase { name: string; description: string; }): Promise { - if (this.host === 'bitbucket.org') { + if (this.config.host === 'bitbucket.org') { return this.createBitbucketCloudRepository(opts); } return this.createBitbucketServerRepository(opts); @@ -86,7 +90,10 @@ export class BitbucketPublisher implements PublisherBase { const { project, name, description } = opts; let response: Response; - const buffer = Buffer.from(`${this.username}:${this.appPassword}`, 'utf8'); + const buffer = Buffer.from( + `${this.config.username}:${this.config.appPassword}`, + 'utf8', + ); const options: RequestInit = { method: 'POST', @@ -138,13 +145,13 @@ export class BitbucketPublisher implements PublisherBase { description: description, }), headers: { - Authorization: `Bearer ${this.token}`, + Authorization: `Bearer ${this.config.token}`, 'Content-Type': 'application/json', }, }; try { response = await fetch( - `https://${this.host}/rest/api/1.0/projects/${project}/repos`, + `https://${this.config.host}/rest/api/1.0/projects/${project}/repos`, options, ); } catch (e) { diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts index 01ec7b630a..31d94d5f53 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/github.ts @@ -35,12 +35,18 @@ export class GithubPublisher implements PublisherBase { baseUrl: config.apiBaseUrl, }); - return new GithubPublisher(config.token, githubClient, repoVisibility); + return new GithubPublisher({ + token: config.token, + client: githubClient, + repoVisibility, + }); } constructor( - private readonly token: string, - private readonly client: Octokit, - private readonly repoVisibility: RepoVisibilityOptions, + private readonly config: { + token: string; + client: Octokit; + repoVisibility: RepoVisibilityOptions; + }, ) {} async publish({ @@ -63,7 +69,7 @@ export class GithubPublisher implements PublisherBase { dir: directory, remoteUrl, auth: { - username: this.token, + username: this.config.token, password: 'x-oauth-basic', }, logger, @@ -85,22 +91,22 @@ export class GithubPublisher implements PublisherBase { }) { const { access, description, owner, name } = opts; - const user = await this.client.users.getByUsername({ + const user = await this.config.client.users.getByUsername({ username: owner, }); const repoCreationPromise = user.data.type === 'Organization' - ? this.client.repos.createInOrg({ + ? this.config.client.repos.createInOrg({ name, org: owner, - private: this.repoVisibility !== 'public', - visibility: this.repoVisibility, + private: this.config.repoVisibility !== 'public', + visibility: this.config.repoVisibility, description, }) - : this.client.repos.createForAuthenticatedUser({ + : this.config.client.repos.createForAuthenticatedUser({ name, - private: this.repoVisibility === 'private', + private: this.config.repoVisibility === 'private', description, }); @@ -108,7 +114,7 @@ export class GithubPublisher implements PublisherBase { if (access?.startsWith(`${owner}/`)) { const [, team] = access.split('/'); - await this.client.teams.addOrUpdateRepoPermissionsInOrg({ + await this.config.client.teams.addOrUpdateRepoPermissionsInOrg({ org: owner, team_slug: team, owner, @@ -117,7 +123,7 @@ export class GithubPublisher implements PublisherBase { }); // no need to add access if it's the person who own's the personal account } else if (access && access !== owner) { - await this.client.repos.addCollaborator({ + await this.config.client.repos.addCollaborator({ owner, repo: name, username: access, diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/gitlab.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/gitlab.ts index 120ca751cd..24149ab2f3 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/gitlab.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/gitlab.ts @@ -29,12 +29,11 @@ export class GitlabPublisher implements PublisherBase { } const client = new Gitlab({ host: config.baseUrl, token: config.token }); - return new GitlabPublisher(config.token, client); + return new GitlabPublisher({ token: config.token, client }); } constructor( - private readonly token: string, - private readonly client: GitlabClient, + private readonly config: { token: string; client: GitlabClient }, ) {} async publish({ @@ -54,7 +53,7 @@ export class GitlabPublisher implements PublisherBase { remoteUrl, auth: { username: 'oauth2', - password: this.token, + password: this.config.token, }, logger, }); @@ -71,16 +70,19 @@ export class GitlabPublisher implements PublisherBase { // TODO(blam): this needs cleaning up to be nicer. The amount of brackets is too damn high! // Shouldn't have to cast things now - let targetNamespace = ((await this.client.Namespaces.show(owner)) as { + let targetNamespace = ((await this.config.client.Namespaces.show( + owner, + )) as { id: number; }).id; if (!targetNamespace) { - targetNamespace = ((await this.client.Users.current()) as { id: number }) - .id; + targetNamespace = ((await this.config.client.Users.current()) as { + id: number; + }).id; } - const project = (await this.client.Projects.create({ + const project = (await this.config.client.Projects.create({ namespace_id: targetNamespace, name: name, })) as { http_url_to_repo: string };