From 0b501431e0f528c4c6928b46b0d1372d6a919ea6 Mon Sep 17 00:00:00 2001 From: Thomas Cardonne Date: Thu, 28 Mar 2024 23:36:15 +0100 Subject: [PATCH 1/2] fix(plugins/catalog-backend-module-github): support push events with catalogPath containing glob patterns Signed-off-by: Thomas Cardonne --- .changeset/pink-years-peel.md | 6 ++ .../providers/GithubEntityProvider.test.ts | 71 +++++++++++++++++++ .../src/providers/GithubEntityProvider.ts | 23 +++--- 3 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 .changeset/pink-years-peel.md diff --git a/.changeset/pink-years-peel.md b/.changeset/pink-years-peel.md new file mode 100644 index 0000000000..b2d44e2113 --- /dev/null +++ b/.changeset/pink-years-peel.md @@ -0,0 +1,6 @@ +--- +'@backstage/plugin-catalog-backend-module-github': patch +--- + +GitHub push events now schedule a refresh on entities that have a refresh_key matching the `catalogPath` config itself. +This allows to support a `catalogPath` configuration that uses glob patterns. diff --git a/plugins/catalog-backend-module-github/src/providers/GithubEntityProvider.test.ts b/plugins/catalog-backend-module-github/src/providers/GithubEntityProvider.test.ts index d0fc54245f..a201177198 100644 --- a/plugins/catalog-backend-module-github/src/providers/GithubEntityProvider.test.ts +++ b/plugins/catalog-backend-module-github/src/providers/GithubEntityProvider.test.ts @@ -1074,6 +1074,77 @@ describe('GithubEntityProvider', () => { expect(entityProviderConnection.applyMutation).toHaveBeenCalledTimes(0); }); + it('apply refresh call on modified files from push event when catalogPath contains a glob pattern', async () => { + const schedule = new PersistingTaskRunner(); + const config = new ConfigReader({ + catalog: { + providers: { + github: { + organization: 'test-org', + catalogPath: '**/catalog-info.yaml', + }, + }, + }, + }); + + const provider = GithubEntityProvider.fromConfig(config, { + logger, + schedule, + })[0]; + + const entityProviderConnection: EntityProviderConnection = { + applyMutation: jest.fn(), + refresh: jest.fn(), + }; + await provider.connect(entityProviderConnection); + + const event: EventParams = { + topic: 'github.push', + metadata: { + 'x-github-event': 'push', + }, + eventPayload: { + ref: 'refs/heads/main', + repository: { + name: 'teste-1', + url: 'https://github.com/test-org/test-repo', + default_branch: 'main', + stargazers: 0, + master_branch: 'main', + organization: 'test-org', + topics: [], + }, + created: true, + deleted: false, + forced: false, + commits: [ + { + added: ['new-file.yaml'], + removed: [], + modified: [], + }, + { + added: [], + removed: [], + modified: ['catalog-info.yaml'], + }, + ], + }, + }; + + await provider.onEvent(event); + + expect(entityProviderConnection.refresh).toHaveBeenCalledTimes(1); + expect(entityProviderConnection.refresh).toHaveBeenCalledWith({ + keys: [ + 'url:https://github.com/test-org/test-repo/tree/main/catalog-info.yaml', + 'url:https://github.com/test-org/test-repo/blob/main/catalog-info.yaml', + 'url:https://github.com/test-org/test-repo/tree/main/**/catalog-info.yaml', + ], + }); + expect(entityProviderConnection.applyMutation).toHaveBeenCalledTimes(0); + }); + it('should process repository when match filters from push event', async () => { const schedule = new PersistingTaskRunner(); const config = new ConfigReader({ diff --git a/plugins/catalog-backend-module-github/src/providers/GithubEntityProvider.ts b/plugins/catalog-backend-module-github/src/providers/GithubEntityProvider.ts index 3c1e6c390b..b099a597b0 100644 --- a/plugins/catalog-backend-module-github/src/providers/GithubEntityProvider.ts +++ b/plugins/catalog-backend-module-github/src/providers/GithubEntityProvider.ts @@ -374,16 +374,23 @@ export class GithubEntityProvider implements EntityProvider, EventSubscriber { ); if (modified.length > 0) { + const catalogPath = this.config.catalogPath.startsWith('/') + ? this.config.catalogPath.substring(1) + : this.config.catalogPath; + await this.connection.refresh({ keys: [ - ...modified.map( - filePath => - `url:${event.repository.url}/tree/${branch}/${filePath}`, - ), - ...modified.map( - filePath => - `url:${event.repository.url}/blob/${branch}/${filePath}`, - ), + ...new Set([ + ...modified.map( + filePath => + `url:${event.repository.url}/tree/${branch}/${filePath}`, + ), + ...modified.map( + filePath => + `url:${event.repository.url}/blob/${branch}/${filePath}`, + ), + `url:${event.repository.url}/tree/${branch}/${catalogPath}`, + ]), ], }); } From 6c317a74d3d720fd28923928a70a1d8d8c14787a Mon Sep 17 00:00:00 2001 From: Thomas Cardonne Date: Fri, 5 Apr 2024 18:09:09 +0200 Subject: [PATCH 2/2] Update pink-years-peel.md Signed-off-by: Thomas Cardonne --- .changeset/pink-years-peel.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/pink-years-peel.md b/.changeset/pink-years-peel.md index b2d44e2113..339889f27e 100644 --- a/.changeset/pink-years-peel.md +++ b/.changeset/pink-years-peel.md @@ -2,5 +2,5 @@ '@backstage/plugin-catalog-backend-module-github': patch --- -GitHub push events now schedule a refresh on entities that have a refresh_key matching the `catalogPath` config itself. +GitHub push events now schedule a refresh on entities that have a `refresh_key` matching the `catalogPath` config itself. This allows to support a `catalogPath` configuration that uses glob patterns.