UNPKG

@backstage/integration

Version:

Helpers for managing integrations towards external systems

74 lines (71 loc) 2.72 kB
import { CachedAzureDevOpsCredentialsProvider } from './CachedAzureDevOpsCredentialsProvider.esm.js'; import { DefaultAzureCredential } from '@azure/identity'; class DefaultAzureDevOpsCredentialsProvider { constructor(providers) { this.providers = providers; } static fromIntegrations(integrations) { const providers = integrations.azure.list().reduce((acc, integration) => { integration.config.credentials?.forEach((credential) => { if (credential.organizations === void 0 || credential.organizations.length === 0) { if (acc.get(integration.config.host) === void 0) { acc.set( integration.config.host, CachedAzureDevOpsCredentialsProvider.fromAzureDevOpsCredential( credential ) ); } } else { const provider = CachedAzureDevOpsCredentialsProvider.fromAzureDevOpsCredential( credential ); credential.organizations?.forEach((organization) => { acc.set(`${integration.config.host}/${organization}`, provider); }); } }); if (integration.config.host === "dev.azure.com" && acc.get(integration.config.host) === void 0) { acc.set( integration.config.host, CachedAzureDevOpsCredentialsProvider.fromTokenCredential( new DefaultAzureCredential() ) ); } return acc; }, /* @__PURE__ */ new Map()); return new DefaultAzureDevOpsCredentialsProvider(providers); } forAzureDevOpsServerOrganization(url) { const parts = url.pathname.split("/").filter((part) => part !== ""); if (url.host !== "dev.azure.com" && parts.length > 0) { if (parts[0] !== "tfs") { return this.providers.get(`${url.host}/${parts[0]}`); } else if (parts[0] === "tfs" && parts.length > 1) { return this.providers.get(`${url.host}/${parts[1]}`); } } return void 0; } forAzureDevOpsOrganization(url) { const parts = url.pathname.split("/").filter((part) => part !== ""); if (url.host === "dev.azure.com" && parts.length > 0) { return this.providers.get(`${url.host}/${parts[0]}`); } return void 0; } forHost(url) { return this.providers.get(url.host); } async getCredentials(opts) { const url = new URL(opts.url); const provider = this.forAzureDevOpsOrganization(url) ?? this.forAzureDevOpsServerOrganization(url) ?? this.forHost(url); if (provider === void 0) { return void 0; } return provider.getCredentials(opts); } } export { DefaultAzureDevOpsCredentialsProvider }; //# sourceMappingURL=DefaultAzureDevOpsCredentialsProvider.esm.js.map