From ff335e5456d2e9ced5e4cc07d319babd2fd3406a Mon Sep 17 00:00:00 2001 From: Andre Wanlin Date: Wed, 16 Apr 2025 10:23:45 -0500 Subject: [PATCH 1/4] github catalog - remove backend-common Signed-off-by: Andre Wanlin --- .changeset/heavy-baths-rule.md | 5 +++++ plugins/catalog-backend-module-github/package.json | 1 - plugins/catalog-backend-module-github/report.api.md | 4 +--- .../src/analyzers/GithubLocationAnalyzer.ts | 13 ++----------- yarn.lock | 1 - 5 files changed, 8 insertions(+), 16 deletions(-) create mode 100644 .changeset/heavy-baths-rule.md diff --git a/.changeset/heavy-baths-rule.md b/.changeset/heavy-baths-rule.md new file mode 100644 index 0000000000..4034035227 --- /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` 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..f6d69adbd0 100644 --- a/plugins/catalog-backend-module-github/report.api.md +++ b/plugins/catalog-backend-module-github/report.api.md @@ -26,7 +26,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 @@ -128,8 +127,7 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer { export type GithubLocationAnalyzerOptions = { config: Config; discovery: DiscoveryService; - tokenManager?: TokenManager; - auth?: AuthService; + auth: AuthService; githubCredentialsProvider?: GithubCredentialsProvider; catalog?: CatalogApi; }; diff --git a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts index cbf43287a3..21d8e5b8ea 100644 --- a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts +++ b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts @@ -28,10 +28,6 @@ import { AnalyzeOptions, 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 { extname } from 'path'; @@ -40,8 +36,7 @@ import { extname } from 'path'; export type GithubLocationAnalyzerOptions = { config: Config; discovery: DiscoveryService; - tokenManager?: TokenManager; - auth?: AuthService; + auth: AuthService; githubCredentialsProvider?: GithubCredentialsProvider; catalog?: CatalogApi; }; @@ -61,11 +56,7 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer { 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) { diff --git a/yarn.lock b/yarn.lock index 20037a1652..47568260b1 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:^" From 0b016aed9f35e1c2925fcbb651d8daf2f7af2af1 Mon Sep 17 00:00:00 2001 From: Andre Wanlin Date: Wed, 16 Apr 2025 12:04:40 -0500 Subject: [PATCH 2/4] Use CatalogService Signed-off-by: Andre Wanlin --- .changeset/heavy-baths-rule.md | 2 +- .../report.api.md | 6 ++---- .../analyzers/GithubLocationAnalyzer.test.ts | 10 ++++------ .../src/analyzers/GithubLocationAnalyzer.ts | 19 +++++++------------ .../src/module/githubCatalogModule.ts | 5 +---- 5 files changed, 15 insertions(+), 27 deletions(-) diff --git a/.changeset/heavy-baths-rule.md b/.changeset/heavy-baths-rule.md index 4034035227..09f26e8b57 100644 --- a/.changeset/heavy-baths-rule.md +++ b/.changeset/heavy-baths-rule.md @@ -2,4 +2,4 @@ '@backstage/plugin-catalog-backend-module-github': minor --- -**BREAKING** The `GithubLocationAnalyzer` now requires the `AuthService` when being constructed and the `TokenManger` has been removed. +**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/report.api.md b/plugins/catalog-backend-module-github/report.api.md index f6d69adbd0..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'; @@ -126,10 +125,9 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer { // @public (undocumented) export type GithubLocationAnalyzerOptions = { config: Config; - discovery: DiscoveryService; 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..c435366931 100644 --- a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts +++ b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts @@ -39,13 +39,11 @@ import { } 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({ getPluginRequestToken: async () => ({ token: 'abc123' }), }); @@ -115,7 +113,7 @@ describe('GithubLocationAnalyzer', () => { }); const analyzer = new GithubLocationAnalyzer({ - discovery: mockDiscovery, + catalog: catalogServiceMock(), auth: mockAuthService, config, }); @@ -142,7 +140,7 @@ describe('GithubLocationAnalyzer', () => { }); const analyzer = new GithubLocationAnalyzer({ - discovery: mockDiscovery, + catalog: catalogServiceMock(), auth: mockAuthService, config, }); @@ -168,7 +166,7 @@ describe('GithubLocationAnalyzer', () => { }); const analyzer = new GithubLocationAnalyzer({ - discovery: mockDiscovery, + catalog: catalogServiceMock(), auth: mockAuthService, config, }); diff --git a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts index 21d8e5b8ea..6719d60cd2 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,31 +25,30 @@ import { isEmpty, trimEnd } from 'lodash'; import parseGitUrl from 'git-url-parse'; import { AnalyzeOptions, + CatalogService, ScmLocationAnalyzer, } from '@backstage/plugin-catalog-node'; 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; 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 || @@ -106,10 +104,7 @@ 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 credentials = await this.auth.getOwnServiceCredentials(); const result = await Promise.all( searchResult.data.items @@ -121,7 +116,7 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer { target, dryRun: true, }, - { token: serviceToken }, + { credentials }, ); 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, From 970292ff67267dfbfa313241dc5f21e3e5d0753e Mon Sep 17 00:00:00 2001 From: Andre Wanlin Date: Mon, 21 Apr 2025 14:08:09 -0500 Subject: [PATCH 3/4] Trying to fix tests Signed-off-by: Andre Wanlin --- .../src/analyzers/GithubLocationAnalyzer.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 c435366931..951e67284b 100644 --- a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts +++ b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts @@ -113,7 +113,7 @@ describe('GithubLocationAnalyzer', () => { }); const analyzer = new GithubLocationAnalyzer({ - catalog: catalogServiceMock(), + catalog: catalogServiceMock.mock(), auth: mockAuthService, config, }); @@ -140,7 +140,7 @@ describe('GithubLocationAnalyzer', () => { }); const analyzer = new GithubLocationAnalyzer({ - catalog: catalogServiceMock(), + catalog: catalogServiceMock.mock(), auth: mockAuthService, config, }); @@ -166,7 +166,7 @@ describe('GithubLocationAnalyzer', () => { }); const analyzer = new GithubLocationAnalyzer({ - catalog: catalogServiceMock(), + catalog: catalogServiceMock.mock(), auth: mockAuthService, config, }); From bdc20a59026942b3b055b84a3106fd335dcbb927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Tue, 22 Apr 2025 12:29:52 +0200 Subject: [PATCH 4/4] fix test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .../analyzers/GithubLocationAnalyzer.test.ts | 101 ++++++++---------- .../src/analyzers/GithubLocationAnalyzer.ts | 4 +- 2 files changed, 46 insertions(+), 59 deletions(-) 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 951e67284b..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,13 +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 mockAuthService = mockServices.auth.mock({ + const auth = mockServices.auth.mock({ getPluginRequestToken: async () => ({ token: 'abc123' }), }); const config = mockServices.rootConfig({ @@ -54,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' }, }); @@ -112,11 +110,8 @@ describe('GithubLocationAnalyzer', () => { return Promise.reject(); }); - const analyzer = new GithubLocationAnalyzer({ - catalog: catalogServiceMock.mock(), - auth: mockAuthService, - config, - }); + const analyzer = new GithubLocationAnalyzer({ catalog, auth, config }); + const result = await analyzer.analyze({ url: 'https://github.com/foo/bar', }); @@ -139,11 +134,8 @@ describe('GithubLocationAnalyzer', () => { return Promise.reject(); }); - const analyzer = new GithubLocationAnalyzer({ - catalog: catalogServiceMock.mock(), - auth: mockAuthService, - config, - }); + const analyzer = new GithubLocationAnalyzer({ catalog, auth, config }); + const result = await analyzer.analyze({ url: 'https://github.com/foo/bar', catalogFilename: 'anvil.yaml', @@ -165,11 +157,8 @@ describe('GithubLocationAnalyzer', () => { return Promise.reject(); }); - const analyzer = new GithubLocationAnalyzer({ - catalog: catalogServiceMock.mock(), - 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 6719d60cd2..6a073394ad 100644 --- a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts +++ b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts @@ -104,8 +104,6 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer { }); const defaultBranch = repoInformation.data.default_branch; - const credentials = await this.auth.getOwnServiceCredentials(); - const result = await Promise.all( searchResult.data.items .map(i => `${trimEnd(url, '/')}/blob/${defaultBranch}/${i.path}`) @@ -116,7 +114,7 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer { target, dryRun: true, }, - { credentials }, + { credentials: await this.auth.getOwnServiceCredentials() }, ); return addLocationResult.entities.map(e => ({ location: { type: 'url', target },