Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2025-04-22 12:29:52 +02:00
parent 970292ff67
commit bdc20a5902
2 changed files with 46 additions and 59 deletions
@@ -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',
@@ -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 },