UNPKG

@backstage/plugin-catalog-backend

Version:

The Backstage backend plugin that provides the Backstage catalog

76 lines (70 loc) 2.17 kB
'use strict'; var parseGitUrl = require('git-url-parse'); function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; } var parseGitUrl__default = /*#__PURE__*/_interopDefaultCompat(parseGitUrl); class RepoLocationAnalyzer { logger; scmIntegrations; analyzers; constructor(logger, scmIntegrations, analyzers) { this.logger = logger; this.scmIntegrations = scmIntegrations; this.analyzers = analyzers; } async analyzeLocation(request) { const integration = this.scmIntegrations.byUrl(request.location.target); const { owner, name } = parseGitUrl__default.default(request.location.target); let annotationPrefix; switch (integration?.type) { case "azure": annotationPrefix = "dev.azure.com"; break; case "bitbucket": annotationPrefix = "bitbucket.org"; break; case "github": annotationPrefix = "github.com"; break; case "gitlab": annotationPrefix = "gitlab.com"; break; } const analyzer = this.analyzers.find( (a) => a.supports(request.location.target) ); if (analyzer) { const analyzerResult = await analyzer.analyze({ url: request.location.target }); if (analyzerResult.existing.length > 0) { this.logger.debug( `entity for ${request.location.target} already exists.` ); return { existingEntityFiles: analyzerResult.existing, generateEntities: [] }; } } const entity = { apiVersion: "backstage.io/v1alpha1", kind: "Component", metadata: { name }, spec: { type: "other", lifecycle: "unknown" } }; if (annotationPrefix) { entity.metadata.annotations = { [`${annotationPrefix}/project-slug`]: `${owner}/${name}` }; } this.logger.debug(`entity created for ${request.location.target}`); return { existingEntityFiles: [], generateEntities: [{ entity, fields: [] }] }; } } exports.RepoLocationAnalyzer = RepoLocationAnalyzer; //# sourceMappingURL=LocationAnalyzer.cjs.js.map