diff --git a/.changeset/heavy-baths-rule.md b/.changeset/heavy-baths-rule.md new file mode 100644 index 0000000000..09f26e8b57 --- /dev/null +++ b/.changeset/heavy-baths-rule.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend-module-github': minor +--- + +**BREAKING** The `GithubLocationAnalyzer` now requires the `AuthService` and the `CatalogService` when being constructed and the `TokenManger` has been removed. diff --git a/plugins/catalog-backend-module-github/package.json b/plugins/catalog-backend-module-github/package.json index 42c56abf36..9028a33ab6 100644 --- a/plugins/catalog-backend-module-github/package.json +++ b/plugins/catalog-backend-module-github/package.json @@ -51,7 +51,6 @@ "test": "backstage-cli package test" }, "dependencies": { - "@backstage/backend-common": "^0.25.0", "@backstage/backend-plugin-api": "workspace:^", "@backstage/catalog-client": "workspace:^", "@backstage/catalog-model": "workspace:^", diff --git a/plugins/catalog-backend-module-github/report.api.md b/plugins/catalog-backend-module-github/report.api.md index 8cd87f646a..691cf271b4 100644 --- a/plugins/catalog-backend-module-github/report.api.md +++ b/plugins/catalog-backend-module-github/report.api.md @@ -6,11 +6,10 @@ import { AnalyzeOptions } from '@backstage/plugin-catalog-node'; import { AuthService } from '@backstage/backend-plugin-api'; import { BackendFeature } from '@backstage/backend-plugin-api'; -import { CatalogApi } from '@backstage/catalog-client'; import { CatalogProcessor } from '@backstage/plugin-catalog-node'; import { CatalogProcessorEmit } from '@backstage/plugin-catalog-node'; +import { CatalogService } from '@backstage/plugin-catalog-node'; import { Config } from '@backstage/config'; -import { DiscoveryService } from '@backstage/backend-plugin-api'; import { Entity } from '@backstage/catalog-model'; import { EntityProvider } from '@backstage/plugin-catalog-node'; import { EntityProviderConnection } from '@backstage/plugin-catalog-node'; @@ -26,7 +25,6 @@ import { SchedulerService } from '@backstage/backend-plugin-api'; import { SchedulerServiceTaskRunner } from '@backstage/backend-plugin-api'; import { ScmIntegrationRegistry } from '@backstage/integration'; import { ScmLocationAnalyzer } from '@backstage/plugin-catalog-node'; -import { TokenManager } from '@backstage/backend-common'; import { UserEntity } from '@backstage/catalog-model'; // @public @@ -127,11 +125,9 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer { // @public (undocumented) export type GithubLocationAnalyzerOptions = { config: Config; - discovery: DiscoveryService; - tokenManager?: TokenManager; - auth?: AuthService; + auth: AuthService; githubCredentialsProvider?: GithubCredentialsProvider; - catalog?: CatalogApi; + catalog: CatalogService; }; // @public 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 25a19ba1ff..9fccfd57eb 100644 --- a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts +++ b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts @@ -38,15 +38,12 @@ import { mockServices, } from '@backstage/backend-test-utils'; import { setupServer } from 'msw/node'; -import { http, HttpResponse } from 'msw'; +import { catalogServiceMock } from '@backstage/plugin-catalog-node/testUtils'; const server = setupServer(); describe('GithubLocationAnalyzer', () => { - const mockDiscovery = mockServices.discovery.mock({ - getBaseUrl: async () => 'http://localhost:7007', - }); - const mockAuthService = mockServices.auth.mock({ + const auth = mockServices.auth.mock({ getPluginRequestToken: async () => ({ token: 'abc123' }), }); const config = mockServices.rootConfig({ @@ -56,49 +53,48 @@ describe('GithubLocationAnalyzer', () => { }, }, }); + const catalog = catalogServiceMock.mock({ + addLocation: jest.fn(async location => ({ + location: { + id: 'test', + target: location.target, + type: location.type ?? 'url', + }, + exists: false, + entities: [ + { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Location', + metadata: { + name: 'test-entity', + }, + spec: { + type: 'url', + target: 'whatever', + }, + }, + { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + title: 'Test Entity', + name: 'test-entity-2', + description: 'The expected description 2', + }, + spec: { + type: 'some-type', + lifecycle: 'experimental', + owner: 'someone', + }, + }, + ], + })), + }); registerMswTestHooks(server); beforeEach(() => { - server.use( - http.post('http://localhost:7007/locations', () => - HttpResponse.json( - { - location: 'test', - exists: false, - entities: [ - { - apiVersion: 'backstage.io/v1alpha1', - kind: 'Location', - metadata: { - name: 'test-entity', - }, - spec: { - type: 'url', - target: 'whatever', - }, - }, - { - apiVersion: 'backstage.io/v1alpha1', - kind: 'Component', - metadata: { - title: 'Test Entity', - name: 'test-entity-2', - description: 'The expected description 2', - }, - spec: { - type: 'some-type', - lifecycle: 'experimental', - owner: 'someone', - }, - }, - ], - }, - { status: 201 }, - ), - ), - ); - + jest.clearAllMocks(); octokit.repos.get.mockResolvedValue({ data: { default_branch: 'my_default_branch' }, }); @@ -114,11 +110,8 @@ describe('GithubLocationAnalyzer', () => { return Promise.reject(); }); - const analyzer = new GithubLocationAnalyzer({ - discovery: mockDiscovery, - auth: mockAuthService, - config, - }); + const analyzer = new GithubLocationAnalyzer({ catalog, auth, config }); + const result = await analyzer.analyze({ url: 'https://github.com/foo/bar', }); @@ -141,11 +134,8 @@ describe('GithubLocationAnalyzer', () => { return Promise.reject(); }); - const analyzer = new GithubLocationAnalyzer({ - discovery: mockDiscovery, - auth: mockAuthService, - config, - }); + const analyzer = new GithubLocationAnalyzer({ catalog, auth, config }); + const result = await analyzer.analyze({ url: 'https://github.com/foo/bar', catalogFilename: 'anvil.yaml', @@ -167,11 +157,8 @@ describe('GithubLocationAnalyzer', () => { return Promise.reject(); }); - const analyzer = new GithubLocationAnalyzer({ - discovery: mockDiscovery, - auth: mockAuthService, - config, - }); + const analyzer = new GithubLocationAnalyzer({ catalog, auth, config }); + const result = await analyzer.analyze({ url: 'https://github.com/foo/bar', catalogFilename: '.gitignore', diff --git a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts index cbf43287a3..6a073394ad 100644 --- a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts +++ b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import { CatalogApi, CatalogClient } from '@backstage/catalog-client'; import { DefaultGithubCredentialsProvider, GithubCredentialsProvider, @@ -26,46 +25,36 @@ import { isEmpty, trimEnd } from 'lodash'; import parseGitUrl from 'git-url-parse'; import { AnalyzeOptions, + CatalogService, ScmLocationAnalyzer, } from '@backstage/plugin-catalog-node'; -import { - TokenManager, - createLegacyAuthAdapters, -} from '@backstage/backend-common'; import { Config } from '@backstage/config'; -import { AuthService, DiscoveryService } from '@backstage/backend-plugin-api'; +import { AuthService } from '@backstage/backend-plugin-api'; import { extname } from 'path'; /** @public */ export type GithubLocationAnalyzerOptions = { config: Config; - discovery: DiscoveryService; - tokenManager?: TokenManager; - auth?: AuthService; + auth: AuthService; githubCredentialsProvider?: GithubCredentialsProvider; - catalog?: CatalogApi; + catalog: CatalogService; }; /** @public */ export class GithubLocationAnalyzer implements ScmLocationAnalyzer { - private readonly catalogClient: CatalogApi; + private readonly catalogClient: CatalogService; private readonly githubCredentialsProvider: GithubCredentialsProvider; private readonly integrations: ScmIntegrationRegistry; private readonly auth: AuthService; constructor(options: GithubLocationAnalyzerOptions) { - this.catalogClient = - options.catalog ?? new CatalogClient({ discoveryApi: options.discovery }); + this.catalogClient = options.catalog; this.integrations = ScmIntegrations.fromConfig(options.config); this.githubCredentialsProvider = options.githubCredentialsProvider || DefaultGithubCredentialsProvider.fromIntegrations(this.integrations); - this.auth = createLegacyAuthAdapters({ - auth: options.auth, - discovery: options.discovery, - tokenManager: options.tokenManager, - }).auth; + this.auth = options.auth; } supports(url: string) { @@ -115,11 +104,6 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer { }); const defaultBranch = repoInformation.data.default_branch; - const { token: serviceToken } = await this.auth.getPluginRequestToken({ - onBehalfOf: await this.auth.getOwnServiceCredentials(), - targetPluginId: 'catalog', - }); - const result = await Promise.all( searchResult.data.items .map(i => `${trimEnd(url, '/')}/blob/${defaultBranch}/${i.path}`) @@ -130,7 +114,7 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer { target, dryRun: true, }, - { token: serviceToken }, + { credentials: await this.auth.getOwnServiceCredentials() }, ); return addLocationResult.entities.map(e => ({ location: { type: 'url', target }, diff --git a/plugins/catalog-backend-module-github/src/module/githubCatalogModule.ts b/plugins/catalog-backend-module-github/src/module/githubCatalogModule.ts index bc10b04802..4ae5adbb31 100644 --- a/plugins/catalog-backend-module-github/src/module/githubCatalogModule.ts +++ b/plugins/catalog-backend-module-github/src/module/githubCatalogModule.ts @@ -21,8 +21,8 @@ import { import { catalogAnalysisExtensionPoint, catalogProcessingExtensionPoint, - catalogServiceRef, } from '@backstage/plugin-catalog-node/alpha'; +import { catalogServiceRef } from '@backstage/plugin-catalog-node'; import { eventsServiceRef } from '@backstage/plugin-events-node'; import { GithubEntityProvider } from '../providers/GithubEntityProvider'; import { GithubLocationAnalyzer } from '../analyzers/GithubLocationAnalyzer'; @@ -42,7 +42,6 @@ export const githubCatalogModule = createBackendModule({ auth: coreServices.auth, catalogProcessing: catalogProcessingExtensionPoint, config: coreServices.rootConfig, - discovery: coreServices.discovery, events: eventsServiceRef, logger: coreServices.logger, scheduler: coreServices.scheduler, @@ -55,13 +54,11 @@ export const githubCatalogModule = createBackendModule({ logger, scheduler, catalogAnalyzers, - discovery, auth, catalog, }) { catalogAnalyzers.addScmLocationAnalyzer( new GithubLocationAnalyzer({ - discovery, config, auth, catalog, diff --git a/yarn.lock b/yarn.lock index 2e87dbc7c1..e68af75817 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5579,7 +5579,6 @@ __metadata: version: 0.0.0-use.local resolution: "@backstage/plugin-catalog-backend-module-github@workspace:plugins/catalog-backend-module-github" dependencies: - "@backstage/backend-common": "npm:^0.25.0" "@backstage/backend-plugin-api": "workspace:^" "@backstage/backend-test-utils": "workspace:^" "@backstage/catalog-client": "workspace:^"