catalog-backend-module-github: migrated to support new auth services

Co-authored-by: Fredrik Adelöw <freben@gmail.com>
Co-authored-by: Carl-Erik Bergström <cbergstrom@spotify.com>
Co-authored-by: blam <ben@blam.sh>
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-02-15 19:01:13 +01:00
parent 72572b2fe1
commit a936a8f8c8
4 changed files with 34 additions and 16 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend-module-github': patch
---
Migrated the `GithubLocationAnalyzer` to support new auth services.
@@ -4,6 +4,7 @@
```ts
import { AnalyzeOptions } from '@backstage/plugin-catalog-node';
import { AuthService } from '@backstage/backend-plugin-api';
import { CatalogProcessor } from '@backstage/plugin-catalog-node';
import { CatalogProcessorEmit } from '@backstage/plugin-catalog-node';
import { Config } from '@backstage/config';
@@ -124,7 +125,8 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer {
export type GithubLocationAnalyzerOptions = {
config: Config;
discovery: PluginEndpointDiscovery;
tokenManager: TokenManager;
tokenManager?: TokenManager;
auth?: AuthService;
githubCredentialsProvider?: GithubCredentialsProvider;
};
@@ -32,12 +32,12 @@ jest.mock('@octokit/rest', () => {
return { Octokit };
});
import {
PluginEndpointDiscovery,
TokenManager,
} from '@backstage/backend-common';
import { PluginEndpointDiscovery } from '@backstage/backend-common';
import { GithubLocationAnalyzer } from './GithubLocationAnalyzer';
import { setupRequestMockHandlers } from '@backstage/backend-test-utils';
import {
setupRequestMockHandlers,
mockServices,
} from '@backstage/backend-test-utils';
import { setupServer } from 'msw/node';
import { rest } from 'msw';
import { ConfigReader } from '@backstage/config';
@@ -49,10 +49,9 @@ describe('GithubLocationAnalyzer', () => {
getBaseUrl: jest.fn().mockResolvedValue('http://localhost:7007'),
getExternalBaseUrl: jest.fn(),
};
const mockTokenManager: jest.Mocked<TokenManager> = {
authenticate: jest.fn(),
getToken: jest.fn().mockResolvedValue('abc123'),
};
const mockAuthService = mockServices.auth.mock({
getPluginRequestToken: async () => ({ token: 'abc123' }),
});
const config = new ConfigReader({
integrations: {
github: [
@@ -123,8 +122,8 @@ describe('GithubLocationAnalyzer', () => {
const analyzer = new GithubLocationAnalyzer({
discovery: mockDiscoveryApi,
auth: mockAuthService,
config,
tokenManager: mockTokenManager,
});
const result = await analyzer.analyze({
url: 'https://github.com/foo/bar',
@@ -137,6 +136,7 @@ describe('GithubLocationAnalyzer', () => {
'https://github.com/foo/bar/blob/my_default_branch/catalog-info.yaml',
});
});
it('should use the provided entity filename for search', async () => {
octokit.search.code.mockImplementation((opts: { q: string }) => {
if (opts.q === 'filename:anvil.yaml repo:foo/bar') {
@@ -149,8 +149,8 @@ describe('GithubLocationAnalyzer', () => {
const analyzer = new GithubLocationAnalyzer({
discovery: mockDiscoveryApi,
auth: mockAuthService,
config,
tokenManager: mockTokenManager,
});
const result = await analyzer.analyze({
url: 'https://github.com/foo/bar',
@@ -31,14 +31,17 @@ import {
import {
PluginEndpointDiscovery,
TokenManager,
createLegacyAuthAdapters,
} from '@backstage/backend-common';
import { Config } from '@backstage/config';
import { AuthService } from '@backstage/backend-plugin-api';
/** @public */
export type GithubLocationAnalyzerOptions = {
config: Config;
discovery: PluginEndpointDiscovery;
tokenManager: TokenManager;
tokenManager?: TokenManager;
auth?: AuthService;
githubCredentialsProvider?: GithubCredentialsProvider;
};
@@ -47,7 +50,7 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer {
private readonly catalogClient: CatalogApi;
private readonly githubCredentialsProvider: GithubCredentialsProvider;
private readonly integrations: ScmIntegrationRegistry;
private readonly tokenManager: TokenManager;
private readonly auth: AuthService;
constructor(options: GithubLocationAnalyzerOptions) {
this.catalogClient = new CatalogClient({ discoveryApi: options.discovery });
@@ -55,7 +58,12 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer {
this.githubCredentialsProvider =
options.githubCredentialsProvider ||
DefaultGithubCredentialsProvider.fromIntegrations(this.integrations);
this.tokenManager = options.tokenManager;
this.auth = createLegacyAuthAdapters({
auth: options.auth,
discovery: options.discovery,
tokenManager: options.tokenManager,
}).auth;
}
supports(url: string) {
@@ -101,7 +109,10 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer {
});
const defaultBranch = repoInformation.data.default_branch;
const { token: serviceToken } = await this.tokenManager.getToken();
const { token: serviceToken } = await this.auth.getPluginRequestToken({
onBehalfOf: await this.auth.getOwnServiceCredentials(),
targetPluginId: 'catalog',
});
const result = await Promise.all(
searchResult.data.items