feat(techdocs): Allow to pass options to GCS publisher (#26836)

* feat(techdocs): Allow to pass options to GCS publisher

Signed-off-by: Adrian Kosinski <adrian.kosinski@allegro.com>

---------

Signed-off-by: Adrian Kosinski <adrian.kosinski@allegro.com>
This commit is contained in:
Adrian Kosiński
2024-10-01 19:40:47 +02:00
committed by GitHub
parent 1b05958bb1
commit fbdc63116c
10 changed files with 115 additions and 9 deletions
+5
View File
@@ -23,6 +23,7 @@ import {
TechdocsGenerator,
} from './stages';
import * as winston from 'winston';
import { PublisherSettings } from './stages/publish/types';
/**
* Extension point type for configuring TechDocs builds.
@@ -89,6 +90,10 @@ export const techdocsPreparerExtensionPoint =
*/
export interface TechdocsPublisherExtensionPoint {
registerPublisher(type: PublisherType, publisher: PublisherBase): void;
registerPublisherSettings<T extends keyof PublisherSettings>(
publisher: T,
settings: PublisherSettings[T],
): void;
}
/**
@@ -26,9 +26,12 @@ import {
createMockDirectory,
mockServices,
} from '@backstage/backend-test-utils';
import { StorageOptions } from '@google-cloud/storage';
const mockDir = createMockDirectory();
let createdStorageOptions: Array<StorageOptions | undefined> = [];
jest.mock('@google-cloud/storage', () => {
class GCSFile {
constructor(private readonly filePath: string) {}
@@ -118,6 +121,10 @@ jest.mock('@google-cloud/storage', () => {
}
class Storage {
constructor(readonly options?: StorageOptions) {
createdStorageOptions.push(options);
}
bucket(bucketName: string) {
return new Bucket(bucketName);
}
@@ -144,10 +151,12 @@ const createPublisherFromConfig = ({
bucketName = 'bucketName',
bucketRootPath = '/',
legacyUseCaseSensitiveTripletPaths = false,
storageOptions = {},
}: {
bucketName?: string;
bucketRootPath?: string;
legacyUseCaseSensitiveTripletPaths?: boolean;
storageOptions?: StorageOptions;
} = {}) => {
const config = new ConfigReader({
techdocs: {
@@ -162,7 +171,7 @@ const createPublisherFromConfig = ({
legacyUseCaseSensitiveTripletPaths,
},
});
return GoogleGCSPublish.fromConfig(config, logger);
return GoogleGCSPublish.fromConfig(config, logger, storageOptions);
};
describe('GoogleGCSPublish', () => {
@@ -211,11 +220,24 @@ describe('GoogleGCSPublish', () => {
};
beforeEach(() => {
createdStorageOptions = [];
mockDir.setContent({
[directory]: files,
});
});
it('should pass options to storage', () => {
createPublisherFromConfig({
storageOptions: {
userAgent: 'Test-UA',
},
});
expect(createdStorageOptions.map(opt => opt?.userAgent)).toContain(
'Test-UA',
);
});
describe('getReadiness', () => {
it('should validate correct config', async () => {
const publisher = createPublisherFromConfig();
@@ -68,7 +68,11 @@ export class GoogleGCSPublish implements PublisherBase {
this.bucketRootPath = options.bucketRootPath;
}
static fromConfig(config: Config, logger: LoggerService): PublisherBase {
static fromConfig(
config: Config,
logger: LoggerService,
options?: StorageOptions,
): PublisherBase {
let bucketName = '';
try {
bucketName = config.getString('techdocs.publisher.googleGcs.bucketName');
@@ -103,7 +107,7 @@ export class GoogleGCSPublish implements PublisherBase {
}
}
const clientOpts: StorageOptions = {};
const clientOpts: StorageOptions = options ?? {};
if (projectId) {
clientOpts.projectId = projectId;
}
@@ -24,4 +24,5 @@ export type {
MigrateRequest,
ReadinessResponse,
TechDocsMetadata,
PublisherSettings,
} from './types';
@@ -86,7 +86,11 @@ export class Publisher implements PublisherBuilder {
logger.info('Creating Google Storage Bucket publisher for TechDocs');
publishers.register(
publisherType,
GoogleGCSPublish.fromConfig(config, logger),
GoogleGCSPublish.fromConfig(
config,
logger,
options.publisherSettings?.googleGcs,
),
);
break;
case 'awsS3':
@@ -17,6 +17,7 @@ import express from 'express';
import { Config } from '@backstage/config';
import { DiscoveryService, LoggerService } from '@backstage/backend-plugin-api';
import { Entity, CompoundEntityRef } from '@backstage/catalog-model';
import { StorageOptions } from '@google-cloud/storage';
/**
* Options for building publishers
@@ -26,8 +27,17 @@ export type PublisherFactory = {
logger: LoggerService;
discovery: DiscoveryService;
customPublisher?: PublisherBase | undefined;
publisherSettings?: PublisherSettings;
};
/**
* Additional configurations for publishers.
* @public
*/
export interface PublisherSettings {
googleGcs?: StorageOptions;
}
/**
* Key for all the different types of TechDocs publishers that are supported.
* @public