From 32ad816421dedb97f9767cb12c74ee0c522844be Mon Sep 17 00:00:00 2001 From: Patrick Jungermann Date: Sat, 17 Jun 2023 22:37:25 +0200 Subject: [PATCH] fix(bitbucketCloud,events): handle missing slug in event payload The current handling for `repo:push` events assumed that the repo slug is provided as part of the event. However, this is not the case which led to a few issues: - repo slug filter was always positive as it was checked against `undefined` - the `repo:` filter at the code search query was never applied which led to the same result as at the full refresh - all additionally discovered catalog files were categorized as "added" - this may not have caused additional API requests (not verified) This change will enhance the event by adding the missing slug, extracted from the `full_name` (format: `{workspace-slug}/{repo-slug}`). Signed-off-by: Patrick Jungermann --- .changeset/sixty-icons-brush.md | 5 +++++ .../BitbucketCloudEntityProvider.test.ts | 4 ++-- .../src/providers/BitbucketCloudEntityProvider.ts | 15 +++++++++++---- 3 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 .changeset/sixty-icons-brush.md diff --git a/.changeset/sixty-icons-brush.md b/.changeset/sixty-icons-brush.md new file mode 100644 index 0000000000..2a7db2b81f --- /dev/null +++ b/.changeset/sixty-icons-brush.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend-module-bitbucket-cloud': patch +--- + +Fix missing repo slug in `repo:push` events. diff --git a/plugins/catalog-backend-module-bitbucket-cloud/src/providers/BitbucketCloudEntityProvider.test.ts b/plugins/catalog-backend-module-bitbucket-cloud/src/providers/BitbucketCloudEntityProvider.test.ts index 727b7fd40c..573f21ce60 100644 --- a/plugins/catalog-backend-module-bitbucket-cloud/src/providers/BitbucketCloudEntityProvider.test.ts +++ b/plugins/catalog-backend-module-bitbucket-cloud/src/providers/BitbucketCloudEntityProvider.test.ts @@ -101,7 +101,7 @@ describe('BitbucketCloudEntityProvider', () => { }, repository: { type: 'repository', - slug: 'test-repo', + full_name: 'test-ws/test-repo', links: { html: { href: 'https://bitbucket.org/test-ws/test-repo', @@ -627,7 +627,7 @@ describe('BitbucketCloudEntityProvider', () => { ...repoPushEventParams.eventPayload, repository: { ...repoPushEventParams.eventPayload.repository, - slug: 'not-matching', + full_name: `${repoPushEventParams.eventPayload.repository.workspace.slug}/not-matching`, }, }, }); diff --git a/plugins/catalog-backend-module-bitbucket-cloud/src/providers/BitbucketCloudEntityProvider.ts b/plugins/catalog-backend-module-bitbucket-cloud/src/providers/BitbucketCloudEntityProvider.ts index 11c872cf7b..96c6be5b80 100644 --- a/plugins/catalog-backend-module-bitbucket-cloud/src/providers/BitbucketCloudEntityProvider.ts +++ b/plugins/catalog-backend-module-bitbucket-cloud/src/providers/BitbucketCloudEntityProvider.ts @@ -225,6 +225,11 @@ export class BitbucketCloudEntityProvider return false; } + private enhanceEvent(event: Events.RepoPushEvent): void { + // add missing slug + event.repository.slug = event.repository.full_name!.split('/', 2)[1]; + } + async onRepoPush(event: Events.RepoPushEvent): Promise { if (!this.canHandleEvents()) { return; @@ -234,6 +239,8 @@ export class BitbucketCloudEntityProvider throw new Error('Not initialized'); } + this.enhanceEvent(event); + if (event.repository.workspace.slug !== this.config.workspace) { return; } @@ -242,7 +249,7 @@ export class BitbucketCloudEntityProvider return; } - const repoName = event.repository.slug; + const repoSlug = event.repository.slug!; const repoUrl = event.repository.links!.html!.href!; this.logger.info(`handle repo:push event for ${repoUrl}`); @@ -253,7 +260,7 @@ export class BitbucketCloudEntityProvider // Hence, we will just trigger a refresh for catalog file(s) within the repository // if we get notified about changes there. - const targets = await this.findCatalogFiles(repoName); + const targets = await this.findCatalogFiles(repoSlug); const { token } = await this.tokenManager!.getToken(); const existing = await this.findExistingLocations(repoUrl, token); @@ -316,7 +323,7 @@ export class BitbucketCloudEntityProvider } private async findCatalogFiles( - repoName?: string, + repoSlug?: string, ): Promise { const workspace = this.config.workspace; const catalogPath = this.config.catalogPath; @@ -340,7 +347,7 @@ export class BitbucketCloudEntityProvider // ...except the one we need '+values.file.commit.repository.links.html.href', ].join(','); - const optRepoFilter = repoName ? ` repo:${repoName}` : ''; + const optRepoFilter = repoSlug ? ` repo:${repoSlug}` : ''; const query = `"${catalogFilename}" path:${catalogPath}${optRepoFilter}`; const searchResults = this.client .searchCode(workspace, query, { fields })