feat(catalog/bitbucketCloud,events): improve refresh for updated catalog files

Refresh (potentially) updated catalog files on `repo:push` more efficiently.

Signed-off-by: Patrick Jungermann <Patrick.Jungermann@gmail.com>
This commit is contained in:
Patrick Jungermann
2022-11-24 21:41:09 +01:00
parent cbe78bf116
commit ce08e1798e
5 changed files with 24 additions and 34 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend-module-bitbucket-cloud': patch
---
Refresh (potentially) updated catalog files on `repo:push` more efficiently.
@@ -45,7 +45,6 @@
"@backstage/plugin-catalog-common": "workspace:^",
"@backstage/plugin-catalog-node": "workspace:^",
"@backstage/plugin-events-node": "workspace:^",
"p-limit": "^3.1.0",
"uuid": "^8.0.0",
"winston": "^3.2.1"
},
@@ -22,11 +22,7 @@ import {
} from '@backstage/backend-tasks';
import { setupRequestMockHandlers } from '@backstage/backend-test-utils';
import { CatalogApi } from '@backstage/catalog-client';
import {
Entity,
LocationEntity,
stringifyEntityRef,
} from '@backstage/catalog-model';
import { Entity, LocationEntity } from '@backstage/catalog-model';
import { ConfigReader } from '@backstage/config';
import {
EntityProviderConnection,
@@ -458,7 +454,6 @@ describe('BitbucketCloudEntityProvider', () => {
items: [keptModule, removedModule],
};
},
refreshEntity: jest.fn(),
};
const provider = BitbucketCloudEntityProvider.fromConfig(defaultConfig, {
catalogApi: catalogApi as any as CatalogApi,
@@ -557,11 +552,12 @@ describe('BitbucketCloudEntityProvider', () => {
},
];
expect(catalogApi.refreshEntity).toHaveBeenCalledTimes(1);
expect(catalogApi.refreshEntity).toHaveBeenCalledWith(
stringifyEntityRef(keptModule),
{ token: 'fake-token' },
);
expect(entityProviderConnection.refresh).toHaveBeenCalledTimes(1);
expect(entityProviderConnection.refresh).toHaveBeenCalledWith({
keys: [
'url:https://bitbucket.org/test-ws/test-repo/src/main/kept-module/catalog-custom.yaml',
],
});
expect(entityProviderConnection.applyMutation).toHaveBeenCalledTimes(1);
expect(entityProviderConnection.applyMutation).toHaveBeenCalledWith({
type: 'delta',
@@ -17,11 +17,7 @@
import { TokenManager } from '@backstage/backend-common';
import { PluginTaskScheduler, TaskRunner } from '@backstage/backend-tasks';
import { CatalogApi } from '@backstage/catalog-client';
import {
Entity,
LocationEntity,
stringifyEntityRef,
} from '@backstage/catalog-model';
import { LocationEntity } from '@backstage/catalog-model';
import { Config } from '@backstage/config';
import {
BitbucketCloudIntegration,
@@ -44,7 +40,6 @@ import {
BitbucketCloudEntityProviderConfig,
readProviderConfigs,
} from './BitbucketCloudEntityProviderConfig';
import limiterFactory from 'p-limit';
import * as uuid from 'uuid';
import { Logger } from 'winston';
@@ -273,9 +268,7 @@ export class BitbucketCloudEntityProvider
),
);
const limiter = limiterFactory(10);
const stillExisting: Entity[] = [];
const stillExisting: LocationEntity[] = [];
const removed: DeferredEntity[] = [];
existing.forEach(item => {
if (targets.find(value => value.fileUrl === item.spec.target)) {
@@ -288,22 +281,20 @@ export class BitbucketCloudEntityProvider
}
});
const promises: Promise<void>[] = stillExisting.map(entity =>
limiter(async () =>
this.catalogApi!.refreshEntity(stringifyEntityRef(entity), { token }),
),
);
const promises: Promise<void>[] = [
this.connection.refresh({
keys: stillExisting.map(entity => `url:${entity.spec.target}`),
}),
];
if (added.length > 0 || removed.length > 0) {
const connection = this.connection;
promises.push(
limiter(async () =>
connection.applyMutation({
type: 'delta',
added: added,
removed: removed,
}),
),
connection.applyMutation({
type: 'delta',
added: added,
removed: removed,
}),
);
}
-1
View File
@@ -4485,7 +4485,6 @@ __metadata:
"@backstage/plugin-events-node": "workspace:^"
luxon: ^3.0.0
msw: ^0.49.0
p-limit: ^3.1.0
uuid: ^8.0.0
winston: ^3.2.1
languageName: unknown