From bb3cdd685f4896dc4d78aa54b47b01081a15f3a2 Mon Sep 17 00:00:00 2001 From: sblausten Date: Fri, 14 Jun 2024 14:49:56 +0200 Subject: [PATCH] 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}`;