@backstage/integration
Version:
Helpers for managing integrations towards external systems
55 lines (51 loc) • 1.81 kB
JavaScript
;
var SingleInstanceGithubCredentialsProvider = require('./SingleInstanceGithubCredentialsProvider.cjs.js');
class DefaultGithubCredentialsProvider {
constructor(providers) {
this.providers = providers;
}
static fromIntegrations(integrations) {
const credentialsProviders = /* @__PURE__ */ new Map();
integrations.github.list().forEach((integration) => {
const credentialsProvider = SingleInstanceGithubCredentialsProvider.SingleInstanceGithubCredentialsProvider.create(integration.config);
credentialsProviders.set(integration.config.host, credentialsProvider);
});
return new DefaultGithubCredentialsProvider(credentialsProviders);
}
/**
* Returns {@link GithubCredentials} for a given URL.
*
* @remarks
*
* Consecutive calls to this method with the same URL will return cached
* credentials.
*
* The shortest lifetime for a token returned is 10 minutes.
*
* @example
* ```ts
* const { token, headers } = await getCredentials({
* url: 'https://github.com/backstage/foobar'
* })
*
* const { token, headers } = await getCredentials({
* url: 'https://github.com/backstage'
* })
* ```
*
* @param opts - The organization or repository URL
* @returns A promise of {@link GithubCredentials}.
*/
async getCredentials(opts) {
const parsed = new URL(opts.url);
const provider = this.providers.get(parsed.host);
if (!provider) {
throw new Error(
`There is no GitHub integration that matches ${opts.url}. Please add a configuration for an integration.`
);
}
return provider.getCredentials(opts);
}
}
exports.DefaultGithubCredentialsProvider = DefaultGithubCredentialsProvider;
//# sourceMappingURL=DefaultGithubCredentialsProvider.cjs.js.map