From 67d0530796c4e5faed42f48ee25dbbd9988c5b73 Mon Sep 17 00:00:00 2001 From: sblausten Date: Wed, 5 Jun 2024 19:02:52 +0200 Subject: [PATCH 1/4] Fix bug in root repo import where catalog-info.yaml.hcl file breaks import Signed-off-by: sblausten --- .changeset/orange-beans-float.md | 5 +++++ .../src/analyzers/GithubLocationAnalyzer.ts | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 .changeset/orange-beans-float.md diff --git a/.changeset/orange-beans-float.md b/.changeset/orange-beans-float.md new file mode 100644 index 0000000000..7e31277890 --- /dev/null +++ b/.changeset/orange-beans-float.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend-module-github': patch +--- + +Fix bug in root repo import where catalog-info.yaml.hcl file is found by search and breaks the import diff --git a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts index 2648b1bcdc..e3b367eb14 100644 --- a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts +++ b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts @@ -76,8 +76,13 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer { const { owner, name: repo } = parseGitUrl(url); const catalogFile = catalogFilename || 'catalog-info.yaml'; + const fileParts = catalogFile.split('.'); + const extension = + fileParts.length > 0 + ? `extension:${fileParts[fileParts.length - 1]}` + : ''; - const query = `filename:${catalogFile} repo:${owner}/${repo}`; + const query = `filename:${catalogFile} ${extension} repo:${owner}/${repo}`; const integration = this.integrations.github.byUrl(url); if (!integration) { From 9ad3ba6011dbd93ff915f20c8a02bbee75e3a5be Mon Sep 17 00:00:00 2001 From: sblausten Date: Wed, 5 Jun 2024 19:11:08 +0200 Subject: [PATCH 2/4] Update test mocks Signed-off-by: sblausten --- .../src/analyzers/GithubLocationAnalyzer.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts index b745dd37ba..8fcffd1a33 100644 --- a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts +++ b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts @@ -112,7 +112,7 @@ describe('GithubLocationAnalyzer', () => { it('should analyze', async () => { octokit.search.code.mockImplementation((opts: { q: string }) => { - if (opts.q === 'filename:catalog-info.yaml repo:foo/bar') { + if (opts.q === 'filename:catalog-info.yaml extension:yaml repo:foo/bar') { return Promise.resolve({ data: { items: [{ path: 'catalog-info.yaml' }], total_count: 1 }, }); @@ -139,7 +139,7 @@ describe('GithubLocationAnalyzer', () => { it('should use the provided entity filename for search', async () => { octokit.search.code.mockImplementation((opts: { q: string }) => { - if (opts.q === 'filename:anvil.yaml repo:foo/bar') { + if (opts.q === 'filename:anvil.yaml extension:yaml repo:foo/bar') { return Promise.resolve({ data: { items: [{ path: 'anvil.yaml' }], total_count: 1 }, }); From 1d495e65556b9539646e98ad60853a00086a2304 Mon Sep 17 00:00:00 2001 From: sblausten Date: Fri, 14 Jun 2024 14:36:20 +0200 Subject: [PATCH 3/4] Use extname Signed-off-by: sblausten --- .../src/analyzers/GithubLocationAnalyzer.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts index e3b367eb14..4d84d6b5d1 100644 --- a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts +++ b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts @@ -22,7 +22,7 @@ import { ScmIntegrations, } from '@backstage/integration'; import { Octokit } from '@octokit/rest'; -import { trimEnd } from 'lodash'; +import { isEmpty, trimEnd } from 'lodash'; import parseGitUrl from 'git-url-parse'; import { AnalyzeOptions, @@ -35,6 +35,7 @@ import { } from '@backstage/backend-common'; import { Config } from '@backstage/config'; import { AuthService } from '@backstage/backend-plugin-api'; +import { extname } from 'path'; /** @public */ export type GithubLocationAnalyzerOptions = { @@ -76,13 +77,10 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer { const { owner, name: repo } = parseGitUrl(url); const catalogFile = catalogFilename || 'catalog-info.yaml'; - const fileParts = catalogFile.split('.'); - const extension = - fileParts.length > 0 - ? `extension:${fileParts[fileParts.length - 1]}` - : ''; + const extension = extname(catalogFile); + const extensionQuery = !isEmpty(extension) ? `extension:${extension}` : ''; - const query = `filename:${catalogFile} ${extension} repo:${owner}/${repo}`; + const query = `filename:${catalogFile} ${extensionQuery} repo:${owner}/${repo}`; const integration = this.integrations.github.byUrl(url); if (!integration) { From bb3cdd685f4896dc4d78aa54b47b01081a15f3a2 Mon Sep 17 00:00:00 2001 From: sblausten Date: Fri, 14 Jun 2024 14:49:56 +0200 Subject: [PATCH 4/4] Use extname Signed-off-by: sblausten --- .../analyzers/GithubLocationAnalyzer.test.ts | 26 +++++++++++++++++++ .../src/analyzers/GithubLocationAnalyzer.ts | 4 ++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts index 8fcffd1a33..e5c7eb5896 100644 --- a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts +++ b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.test.ts @@ -162,4 +162,30 @@ describe('GithubLocationAnalyzer', () => { target: 'https://github.com/foo/bar/blob/my_default_branch/anvil.yaml', }); }); + + it('should use the provided entity file extension in search query only if present', async () => { + octokit.search.code.mockImplementation((opts: { q: string }) => { + if (opts.q === 'filename:.gitignore repo:foo/bar') { + return Promise.resolve({ + data: { items: [{ path: '.gitignore' }], total_count: 1 }, + }); + } + return Promise.reject(); + }); + + const analyzer = new GithubLocationAnalyzer({ + discovery: mockDiscoveryApi, + auth: mockAuthService, + config, + }); + const result = await analyzer.analyze({ + url: 'https://github.com/foo/bar', + catalogFilename: '.gitignore', + }); + + expect(result.existing[0].location).toEqual({ + type: 'url', + target: 'https://github.com/foo/bar/blob/my_default_branch/.gitignore', + }); + }); }); diff --git a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts index 4d84d6b5d1..7e6ac73054 100644 --- a/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts +++ b/plugins/catalog-backend-module-github/src/analyzers/GithubLocationAnalyzer.ts @@ -78,7 +78,9 @@ export class GithubLocationAnalyzer implements ScmLocationAnalyzer { const catalogFile = catalogFilename || 'catalog-info.yaml'; const extension = extname(catalogFile); - const extensionQuery = !isEmpty(extension) ? `extension:${extension}` : ''; + const extensionQuery = !isEmpty(extension) + ? `extension:${extension.replace('.', '')}` + : ''; const query = `filename:${catalogFile} ${extensionQuery} repo:${owner}/${repo}`;