From 3d475a0ddb91d5ebd85718a86aa02d340bf23fbb Mon Sep 17 00:00:00 2001 From: Isabel Tomb Date: Fri, 13 Dec 2024 16:23:15 -0600 Subject: [PATCH 1/3] Update condition before calling normalizeCodeOwner Update the condition in the ternary operator in the resolveCodeOwner function to check that match.owners isn't empty before calling normalizeCodeOwner with match.owners[0] Before this change normalizeCodeOwner could potentially throw an error during processing if the `match` object exists but the match.owners array is empty because normalizeCodeOwner will be passed `undefined` instead of the string it expects. Signed-off-by: Isabel Tomb --- .changeset/twenty-laws-tie.md | 5 +++++ plugins/catalog-backend/src/processors/codeowners/resolve.ts | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 .changeset/twenty-laws-tie.md diff --git a/.changeset/twenty-laws-tie.md b/.changeset/twenty-laws-tie.md new file mode 100644 index 0000000000..a1f9e88e57 --- /dev/null +++ b/.changeset/twenty-laws-tie.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend': patch +--- + +Updated condition in resolveCodeOwner to fix a bug where normalizeCodeOwner could potentially be called with an invalid arg causing an error in CodeOwnersProcessor. diff --git a/plugins/catalog-backend/src/processors/codeowners/resolve.ts b/plugins/catalog-backend/src/processors/codeowners/resolve.ts index 675f6542aa..b0c44a8797 100644 --- a/plugins/catalog-backend/src/processors/codeowners/resolve.ts +++ b/plugins/catalog-backend/src/processors/codeowners/resolve.ts @@ -30,7 +30,9 @@ export function resolveCodeOwner( const { filepath } = parseGitUrl(catalogInfoFileUrl); const match = codeowners.matchFile(filepath, codeOwnerEntries); - return match ? normalizeCodeOwner(match.owners[0]) : undefined; + return match?.owners?.length + ? normalizeCodeOwner(match.owners[0]) + : undefined; } export function normalizeCodeOwner(owner: string) { From f59722dcfa706c39bf941a25192ad5671dc8af77 Mon Sep 17 00:00:00 2001 From: Isabel Tomb Date: Fri, 13 Dec 2024 16:30:45 -0600 Subject: [PATCH 2/3] Add test Add a test to test the scenario that the previous commit fixes. If a repo has a malformed CODEOWNERS file that contains just a pattern and no names the match object will have an empty `owners` array, in which case resolveCodeOwner should return undefined. Signed-off-by: Isabel Tomb --- .../src/processors/codeowners/resolve.test.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/plugins/catalog-backend/src/processors/codeowners/resolve.test.ts b/plugins/catalog-backend/src/processors/codeowners/resolve.test.ts index f9cdf79530..722d27fdb9 100644 --- a/plugins/catalog-backend/src/processors/codeowners/resolve.test.ts +++ b/plugins/catalog-backend/src/processors/codeowners/resolve.test.ts @@ -46,6 +46,14 @@ describe('resolveCodeOwner', () => { ), ).toBe('team-foo'); }); + it('should return undefined if the codeowners file contains no names', () => { + expect( + resolveCodeOwner( + `*`, + 'https://github.com/acme/repo/tree/docs/catalog-info.yaml', + ), + ).toBe(undefined); + }); }); describe('normalizeCodeOwner', () => { From 6c878823523adcca35fd8b43e7fecea5abbb4877 Mon Sep 17 00:00:00 2001 From: Isabel Tomb Date: Wed, 18 Dec 2024 15:32:23 -0600 Subject: [PATCH 3/3] Update .changeset/twenty-laws-tie.md Co-authored-by: Ben Lambert Signed-off-by: Isabel Tomb --- .changeset/twenty-laws-tie.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/twenty-laws-tie.md b/.changeset/twenty-laws-tie.md index a1f9e88e57..02e6fca70c 100644 --- a/.changeset/twenty-laws-tie.md +++ b/.changeset/twenty-laws-tie.md @@ -2,4 +2,4 @@ '@backstage/plugin-catalog-backend': patch --- -Updated condition in resolveCodeOwner to fix a bug where normalizeCodeOwner could potentially be called with an invalid arg causing an error in CodeOwnersProcessor. +Updated condition in `resolveCodeOwner` to fix a bug where `normalizeCodeOwner` could potentially be called with an invalid argument causing an error in `CodeOwnersProcessor`