diff --git a/.changeset/cold-dolphins-raise.md b/.changeset/cold-dolphins-raise.md new file mode 100644 index 0000000000..1581edcc14 --- /dev/null +++ b/.changeset/cold-dolphins-raise.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend-module-github': patch +--- + +Migrated the `GithubLocationAnalyzer` to support new auth services. diff --git a/plugins/catalog-backend-module-github/api-report.md b/plugins/catalog-backend-module-github/api-report.md index eb43748aa1..abae10035f 100644 --- a/plugins/catalog-backend-module-github/api-report.md +++ b/plugins/catalog-backend-module-github/api-report.md @@ -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; }; diff --git a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts index 549ca385e8..b745dd37ba 100644 --- a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts +++ b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts @@ -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 = { - 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', diff --git a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts index 3353254400..2648b1bcdc 100644 --- a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts +++ b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts @@ -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