chore: more work to including the integrations config everywhere

This commit is contained in:
blam
2021-01-11 15:52:14 +01:00
parent 4330a51457
commit d5fa852aa2
4 changed files with 59 additions and 11 deletions
@@ -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",
);
}
}
@@ -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;
}
@@ -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<PublisherResult> {
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<string, JsonValue>,
) {
@@ -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 };
@@ -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 = () => {