Add some more casing changes.
Modify DefaultTechDocsCollator to have a static constructor method. Signed-off-by: Jussi Hallila <jussi@hallila.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
---
|
||||
'@backstage/plugin-techdocs-backend': patch
|
||||
'@backstage/plugin-techdocs': patch
|
||||
---
|
||||
|
||||
Modify TechDocsCollator to be aware of new TechDocs URL pattern
|
||||
Modify TechDocsCollator to be aware of new TechDocs URL pattern. Modify tech docs in context search to use correct casing when creating initial filter.
|
||||
|
||||
@@ -73,7 +73,7 @@ export default async function createPlugin({
|
||||
|
||||
indexBuilder.addCollator({
|
||||
defaultRefreshIntervalSeconds: 600,
|
||||
collator: new DefaultTechDocsCollator({
|
||||
collator: DefaultTechDocsCollator.fromConfig(config, {
|
||||
discovery,
|
||||
logger,
|
||||
legacyPathCasing:
|
||||
|
||||
@@ -21,6 +21,7 @@ import fetch from 'cross-fetch';
|
||||
import unescape from 'lodash/unescape';
|
||||
import { Logger } from 'winston';
|
||||
import pLimit from 'p-limit';
|
||||
import { Config } from '@backstage/config';
|
||||
import { CatalogApi, CatalogClient } from '@backstage/catalog-client';
|
||||
import { TechDocsDocument } from '@backstage/techdocs-common';
|
||||
|
||||
@@ -30,6 +31,21 @@ interface MkSearchIndexDoc {
|
||||
location: string;
|
||||
}
|
||||
|
||||
type TechDocsCollatorOptions = {
|
||||
discovery: PluginEndpointDiscovery;
|
||||
logger: Logger;
|
||||
locationTemplate?: string;
|
||||
catalogClient?: CatalogApi;
|
||||
parallelismLimit?: number;
|
||||
legacyPathCasing?: boolean;
|
||||
};
|
||||
|
||||
type EntityInfo = {
|
||||
name: string;
|
||||
namespace: string;
|
||||
kind: string;
|
||||
};
|
||||
|
||||
export class DefaultTechDocsCollator implements DocumentCollator {
|
||||
protected discovery: PluginEndpointDiscovery;
|
||||
protected locationTemplate: string;
|
||||
@@ -39,6 +55,9 @@ export class DefaultTechDocsCollator implements DocumentCollator {
|
||||
private readonly legacyPathCasing: boolean;
|
||||
public readonly type: string = 'techdocs';
|
||||
|
||||
/**
|
||||
* @deprecated use static fromConfig method instead.
|
||||
*/
|
||||
constructor({
|
||||
discovery,
|
||||
locationTemplate,
|
||||
@@ -46,14 +65,7 @@ export class DefaultTechDocsCollator implements DocumentCollator {
|
||||
catalogClient,
|
||||
parallelismLimit = 10,
|
||||
legacyPathCasing = false,
|
||||
}: {
|
||||
discovery: PluginEndpointDiscovery;
|
||||
logger: Logger;
|
||||
locationTemplate?: string;
|
||||
catalogClient?: CatalogApi;
|
||||
parallelismLimit?: number;
|
||||
legacyPathCasing?: boolean;
|
||||
}) {
|
||||
}: TechDocsCollatorOptions) {
|
||||
this.discovery = discovery;
|
||||
this.locationTemplate =
|
||||
locationTemplate || '/docs/:namespace/:kind/:name/:path';
|
||||
@@ -64,6 +76,10 @@ export class DefaultTechDocsCollator implements DocumentCollator {
|
||||
this.legacyPathCasing = legacyPathCasing;
|
||||
}
|
||||
|
||||
static fromConfig(_config: Config, options: TechDocsCollatorOptions) {
|
||||
return new DefaultTechDocsCollator(options);
|
||||
}
|
||||
|
||||
async execute() {
|
||||
const limit = pLimit(this.parallelismLimit);
|
||||
const techDocsBaseUrl = await this.discovery.getBaseUrl('techdocs');
|
||||
@@ -83,13 +99,14 @@ export class DefaultTechDocsCollator implements DocumentCollator {
|
||||
.filter(it => it.metadata?.annotations?.['backstage.io/techdocs-ref'])
|
||||
.map((entity: Entity) =>
|
||||
limit(async (): Promise<TechDocsDocument[]> => {
|
||||
const entityInfo = {
|
||||
kind: this.legacyPathCasing
|
||||
? entity.kind
|
||||
: entity.kind.toLowerCase(),
|
||||
namespace: entity.metadata.namespace || 'default',
|
||||
name: entity.metadata.name,
|
||||
};
|
||||
const entityInfo = DefaultTechDocsCollator.handleEntityInfoCasing(
|
||||
this.legacyPathCasing,
|
||||
{
|
||||
kind: entity.kind,
|
||||
namespace: entity.metadata.namespace || 'default',
|
||||
name: entity.metadata.name,
|
||||
},
|
||||
);
|
||||
|
||||
try {
|
||||
const searchIndexResponse = await fetch(
|
||||
@@ -144,4 +161,15 @@ export class DefaultTechDocsCollator implements DocumentCollator {
|
||||
) {
|
||||
return `${techDocsBaseUrl}/static/docs/${entityInfo.namespace}/${entityInfo.kind}/${entityInfo.name}/search/search_index.json`;
|
||||
}
|
||||
|
||||
private static handleEntityInfoCasing(
|
||||
legacyPaths: boolean,
|
||||
entityInfo: EntityInfo,
|
||||
): EntityInfo {
|
||||
return legacyPaths
|
||||
? entityInfo
|
||||
: Object.entries(entityInfo).reduce((acc, [key, value]) => {
|
||||
return { ...acc, [key]: value.toLowerCase() };
|
||||
}, {} as EntityInfo);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user