From d5fa852aa2f8dac3f6464c41ca849978f9198963 Mon Sep 17 00:00:00 2001 From: blam Date: Mon, 11 Jan 2021 15:52:14 +0100 Subject: [PATCH] chore: more work to including the integrations config everywhere --- .../scaffolder/stages/prepare/bitbucket.ts | 2 +- .../src/scaffolder/stages/publish/azure.ts | 7 ++- .../src/scaffolder/stages/publish/gitlab.ts | 57 +++++++++++++++++-- .../components/TemplatePage/TemplatePage.tsx | 4 +- 4 files changed, 59 insertions(+), 11 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.ts b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.ts index cc647982d8..8a9e4f452d 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/prepare/bitbucket.ts @@ -52,7 +52,7 @@ export class BitbucketPreparer implements PreparerBase { if (this.username || this.privateToken) { this.logger.warn( - "DEPRECATION: Using the token format under 'scaffolder.github.token' will not be respected in future releases. Please consider using integrations config instead", + "DEPRECATION: Using the token format under 'scaffolder.bitbucket.token' will not be respected in future releases. Please consider using integrations config instead", ); } } diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.ts index 8947c09e60..fe4637b971 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.ts @@ -20,12 +20,15 @@ import { GitRepositoryCreateOptions } from 'azure-devops-node-api/interfaces/Git import { JsonValue, Config } from '@backstage/config'; import { RequiredTemplateValues } from '../templater'; import { initRepoAndPush } from './helpers'; - +import { Config } from '@backstage/config'; +import { Logger } from 'winston'; export class AzurePublisher implements PublisherBase { private readonly client: GitApi; private readonly token: string; + private readonly logger: Logger; - constructor(config: Config) { + constructor(config: Config, { logger }: { logger: Logger }) { + this.logger = logger; this.client = client; this.token = token; } diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/gitlab.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/gitlab.ts index 4fb47813b2..cda9a83019 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/gitlab.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/gitlab.ts @@ -28,6 +28,7 @@ import { export class GitlabPublisher implements PublisherBase { private readonly integrations: GitLabIntegrationConfig[]; private readonly scaffolderToken: string | undefined; + private readonly apiBaseUrl: string | undefined; private readonly logger: Logger; constructor(config: Config, { logger }: { logger: Logger }) { @@ -46,11 +47,19 @@ export class GitlabPublisher implements PublisherBase { 'scaffolder.gitlab.api.token', ); + this.apiBaseUrl = config.getOptionalString('scaffolder.gitlab.api.baseUrl'); + if (this.scaffolderToken) { this.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", ); } + + if (this.apiBaseUrl) { + this.logger.warn( + "DEPRECATION: Using the apiBaseUrl format under 'scaffolder.gitlab.api.baseUrl' will not be respected in future releases. Please consider using integrations config instead", + ); + } } async publish({ @@ -58,20 +67,47 @@ export class GitlabPublisher implements PublisherBase { directory, }: PublisherOptions): Promise { const remoteUrl = await this.createRemote(values); + const { host } = new URL(remoteUrl); + const token = this.getToken(host); + + if (!token) { + throw new Error('No token provided to create the remote repository'); + } await initRepoAndPush({ dir: directory, remoteUrl, auth: { username: 'oauth2', - password: this.token, + password: token, }, - logger, + logger: this.logger, }); return { remoteUrl }; } + private getToken(host: string): string | undefined { + return ( + this.scaffolderToken || + this.integrations.find(c => c.host === host)?.token + ); + } + + private getBaseUrl(host: string): string | undefined { + return ( + this.apiBaseUrl || + this.integrations.find(c => c.host === host)?.apiBaseUrl + ); + } + + private getConfig(host: string): { baseUrl?: string; token?: string } { + return { + baseUrl: this.getBaseUrl(host), + token: this.getToken(host), + }; + } + private async createRemote( values: RequiredTemplateValues & Record, ) { @@ -80,15 +116,24 @@ export class GitlabPublisher implements PublisherBase { pathElements.pop(); const owner = pathElements.join('/'); - let targetNamespace = ((await this.client.Namespaces.show(owner)) as { + const config = this.getConfig(); + + if (!config.token) { + throw new Error( + 'No authentication set for Gitlab publisher. Creating the remote repository is not possible without a token', + ); + } + + const client = new Gitlab({ host: config.baseUrl, token: config.token }); + + let targetNamespace = ((await client.Namespaces.show(owner)) as { id: number; }).id; if (!targetNamespace) { - targetNamespace = ((await this.client.Users.current()) as { id: number }) - .id; + targetNamespace = ((await client.Users.current()) as { id: number }).id; } - const project = (await this.client.Projects.create({ + const project = (await client.Projects.create({ namespace_id: targetNamespace, name: name, })) as { http_url_to_repo: string }; diff --git a/plugins/scaffolder/src/components/TemplatePage/TemplatePage.tsx b/plugins/scaffolder/src/components/TemplatePage/TemplatePage.tsx index fd4e4c3ded..aceaf673e2 100644 --- a/plugins/scaffolder/src/components/TemplatePage/TemplatePage.tsx +++ b/plugins/scaffolder/src/components/TemplatePage/TemplatePage.tsx @@ -63,7 +63,7 @@ const OWNER_REPO_SCHEMA = { description: 'Who is going to own this component', }, storePath: { - format: 'GitHub user or org / Repo name', + format: 'storeLocation', type: 'string' as const, title: 'Store path', description: 'GitHub store path in org/repo format', @@ -77,7 +77,7 @@ const OWNER_REPO_SCHEMA = { }; const REPO_FORMAT = { - 'GitHub user or org / Repo name': /[^\/]*\/[^\/]*/, + storeLocation: /[^\/]*\/[^\/]*/, }; export const TemplatePage = () => {