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:
@@ -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"
|
||||
},
|
||||
|
||||
+7
-11
@@ -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',
|
||||
|
||||
+12
-21
@@ -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,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user