diff --git a/packages/backend/src/plugins/search.ts b/packages/backend/src/plugins/search.ts index ad5111aaf3..671f4ea55e 100644 --- a/packages/backend/src/plugins/search.ts +++ b/packages/backend/src/plugins/search.ts @@ -13,22 +13,29 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { useHotCleanup } from '@backstage/backend-common'; import { createRouter } from '@backstage/plugin-search-backend'; import { IndexBuilder } from '@backstage/plugin-search-backend-node'; import { PluginEnvironment } from '../types'; -// import { DefaultCatalogCollator } from '@backstage/plugin-catalog-backend'; +import { DefaultCatalogCollator } from '@backstage/plugin-catalog-backend'; -export default async function createPlugin({ logger }: PluginEnvironment) { +export default async function createPlugin({ + logger, + discovery, +}: PluginEnvironment) { const indexBuilder = new IndexBuilder(); - // TODO: Within this PR, update to use REST API instead of Catalog Builder. - /* indexRegistry.addCollator({ + + indexBuilder.addCollator({ type: 'software-catalog', defaultRefreshIntervalSeconds: 600, - collator: new DefaultCatalogCollator(entitiesCatalog), - });*/ + collator: new DefaultCatalogCollator(discovery), + }); - // TODO: Make this a more proper refresh loop. - indexBuilder.build(); + // TODO: Move refresh loop logic into the builder. + const timerId = setInterval(() => { + indexBuilder.build(); + }, 60000); + useHotCleanup(module, () => clearInterval(timerId)); return await createRouter({ logger, diff --git a/packages/search-common/src/types.ts b/packages/search-common/src/types.ts index ad8dcf9fc6..ad784c3b36 100644 --- a/packages/search-common/src/types.ts +++ b/packages/search-common/src/types.ts @@ -49,16 +49,6 @@ export interface IndexableDocument { * is clicked). */ location: string; - - /** - * (Optional) The owner of the document (e.g. spec.owner on a catalog entity). - */ - owner?: string; - - /** - * (Optional) The lifecycle of the document (e.g. spec.lifecycle on a catalog entity). - */ - lifecycle?: string; } /** diff --git a/plugins/catalog-backend/src/search/Collator.ts b/plugins/catalog-backend/src/search/DefaultCatalogCollator.ts similarity index 66% rename from plugins/catalog-backend/src/search/Collator.ts rename to plugins/catalog-backend/src/search/DefaultCatalogCollator.ts index 625cc09a6b..72bf43f8f9 100644 --- a/plugins/catalog-backend/src/search/Collator.ts +++ b/plugins/catalog-backend/src/search/DefaultCatalogCollator.ts @@ -14,35 +14,40 @@ * limitations under the License. */ +import { PluginEndpointDiscovery } from '@backstage/backend-common'; +import { Entity } from '@backstage/catalog-model'; import { IndexableDocument, DocumentCollator } from '@backstage/search-common'; -import { EntitiesCatalog } from '../catalog'; +import fetch from 'cross-fetch'; export interface CatalogEntityDocument extends IndexableDocument { componentType: string; + namespace: string; + kind: string; } export class DefaultCatalogCollator implements DocumentCollator { - protected entitiesCatalog: EntitiesCatalog; + protected discovery: PluginEndpointDiscovery; - constructor(entitiesCatalog: EntitiesCatalog) { - this.entitiesCatalog = entitiesCatalog; + constructor(discovery: PluginEndpointDiscovery) { + this.discovery = discovery; } async execute() { - const entities = await this.entitiesCatalog.entities(); + const baseUrl = await this.discovery.getBaseUrl('catalog'); + const res = await fetch(`${baseUrl}/entities`); + const entities: Entity[] = await res.json(); return entities.map( (entity): CatalogEntityDocument => { return { title: entity.metadata.name, + // TODO: Use a config-based template approach for entity location. location: `/catalog/${ entity.metadata.namespace || 'default' }/component/${entity.metadata.name}`, text: entity.metadata.description || '', componentType: entity.spec?.type?.toString() || 'other', - ...(entity.spec?.owner && { owner: entity.spec.owner.toString() }), - ...(entity.metadata.lifecycle && { - lifecycle: entity.metadata.lifecycle.toString(), - }), + namespace: entity.metadata.namespace || 'default', + kind: entity.kind, }; }, ); diff --git a/plugins/catalog-backend/src/search/index.ts b/plugins/catalog-backend/src/search/index.ts index 568fafaed5..aed16aad76 100644 --- a/plugins/catalog-backend/src/search/index.ts +++ b/plugins/catalog-backend/src/search/index.ts @@ -14,4 +14,4 @@ * limitations under the License. */ -export * from './Collator'; +export * from './DefaultCatalogCollator';