Use CatalogService

Signed-off-by: Andre Wanlin <awanlin@spotify.com>
This commit is contained in:
Andre Wanlin
2025-04-16 12:04:40 -05:00
parent ff335e5456
commit 0b016aed9f
5 changed files with 15 additions and 27 deletions
+1 -1
View File
@@ -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.
@@ -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
@@ -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,
});
@@ -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 },
@@ -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,