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}`, + ]), ], }); }