diff --git a/.changeset/swift-waves-laugh.md b/.changeset/swift-waves-laugh.md new file mode 100644 index 0000000000..0db8ae6d1c --- /dev/null +++ b/.changeset/swift-waves-laugh.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend-module-github': patch +--- + +fix catalog Path matcher to use glob on receive github events diff --git a/plugins/catalog-backend-module-github/package.json b/plugins/catalog-backend-module-github/package.json index 7d51bcdd20..120dcbb964 100644 --- a/plugins/catalog-backend-module-github/package.json +++ b/plugins/catalog-backend-module-github/package.json @@ -50,6 +50,7 @@ "@octokit/rest": "^19.0.3", "git-url-parse": "^13.0.0", "lodash": "^4.17.21", + "minimatch": "^5.1.2", "node-fetch": "^2.6.7", "uuid": "^8.0.0", "winston": "^3.2.1" 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 a41404ad9a..9916825cdc 100644 --- a/plugins/catalog-backend-module-github/src/providers/GithubEntityProvider.test.ts +++ b/plugins/catalog-backend-module-github/src/providers/GithubEntityProvider.test.ts @@ -718,6 +718,97 @@ describe('GithubEntityProvider', () => { expect(providers[0].getProviderName()).toEqual('github-provider:default'); }); + it('apply delta update on added files from push event with glob catalog path', 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: ['folder1/folder2/folder3/catalog-info.yaml'], + removed: [], + modified: [], + }, + ], + }, + }; + const url = + 'https://github.com/test-org/test-repo/blob/main/folder1/folder2/folder3/catalog-info.yaml'; + const expectedEntities = [ + { + entity: { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Location', + metadata: { + annotations: { + 'backstage.io/managed-by-location': `url:${url}`, + 'backstage.io/managed-by-origin-location': `url:${url}`, + }, + name: 'generated-c499bfb5e3f159d2bfefe26cac86a8a0b95b47f0', + }, + spec: { + presence: 'optional', + target: `${url}`, + type: 'url', + }, + }, + locationKey: 'github-provider:default', + }, + ]; + + await provider.onEvent(event); + + expect(entityProviderConnection.applyMutation).toHaveBeenCalledTimes(1); + expect(entityProviderConnection.applyMutation).toHaveBeenCalledWith({ + type: 'delta', + added: expectedEntities, + removed: [], + }); + }); + it('apply delta update on added files 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 8816d8ee80..2a6a84c8af 100644 --- a/plugins/catalog-backend-module-github/src/providers/GithubEntityProvider.ts +++ b/plugins/catalog-backend-module-github/src/providers/GithubEntityProvider.ts @@ -44,6 +44,7 @@ import { satisfiesTopicFilter } from '../lib/util'; import { EventParams, EventSubscriber } from '@backstage/plugin-events-node'; import { PushEvent, Commit } from '@octokit/webhooks-types'; +import { Minimatch } from 'minimatch'; const TOPIC_REPO_PUSH = 'github.push'; @@ -387,10 +388,11 @@ export class GithubEntityProvider implements EntityProvider, EventSubscriber { ? this.config.catalogPath.substring(1) : this.config.catalogPath; + const matcher = new Minimatch(catalogFile); return commits .map(transformOperation) .flat() - .filter(file => catalogFile.includes(file)); + .filter(file => matcher.match(file)); } private toDeferredEntities(targets: string[]): DeferredEntity[] { diff --git a/yarn.lock b/yarn.lock index f4f2e9a774..67c7314254 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5086,6 +5086,7 @@ __metadata: git-url-parse: ^13.0.0 lodash: ^4.17.21 luxon: ^3.0.0 + minimatch: ^5.1.2 msw: ^0.49.0 node-fetch: ^2.6.7 uuid: ^8.0.0 @@ -29453,12 +29454,12 @@ __metadata: languageName: node linkType: hard -"minimatch@npm:^5.0.0, minimatch@npm:^5.0.1, minimatch@npm:^5.1.0, minimatch@npm:^5.1.1": - version: 5.1.1 - resolution: "minimatch@npm:5.1.1" +"minimatch@npm:^5.0.0, minimatch@npm:^5.0.1, minimatch@npm:^5.1.0, minimatch@npm:^5.1.1, minimatch@npm:^5.1.2": + version: 5.1.2 + resolution: "minimatch@npm:5.1.2" dependencies: brace-expansion: ^2.0.1 - checksum: 215edd0978320a3354188f84a537d45841f2449af4df4379f79b9b777e71aa4f5722cc9d1717eabd2a70d38ef76ab7b708d24d83ea6a6c909dfd8833de98b437 + checksum: 32ffda25b9fb8270a1c1beafdb7489dc0e411af553495136509a945691f63c9b6b000eeeaaf8bffe3efa609c1d6d3bc0f5a106f6c3443b5c05da649100ded964 languageName: node linkType: hard