From 75cb2580d3d7acc2ed0f0bd39292d4b8bf9182d4 Mon Sep 17 00:00:00 2001 From: Renato Vicente Date: Tue, 25 Jun 2024 11:10:26 +0100 Subject: [PATCH 1/4] fix: avoid repo path not available in container --- src/domain/getLabelConfigs.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/domain/getLabelConfigs.ts b/src/domain/getLabelConfigs.ts index 751e884..39f61f3 100644 --- a/src/domain/getLabelConfigs.ts +++ b/src/domain/getLabelConfigs.ts @@ -11,7 +11,15 @@ const getFilePath = (configurationPath: string): string | undefined => { if (configurationPath.includes('.json') && fs.existsSync(repoPath)) return repoPath if (!configurationPath.includes('.json')) { - const allFiles = fs.readdirSync(repoPath) + let allFiles; + try { + allFiles = fs.readdirSync(repoPath) + } catch (error: any) { + core.warning( + `Could not read configuration file at ${repoPath}: ${error.message}. Skipping.` + ) + return + } const files = allFiles.filter((elem) => jsonTypes.map((elem) => `auto-label.${elem}`).includes(elem) ) From 346f98953d9bacd18a37d018a00049f22ef7d6b9 Mon Sep 17 00:00:00 2001 From: Renato Vicente Date: Tue, 25 Jun 2024 11:25:51 +0100 Subject: [PATCH 2/4] perf: expected file names mapped --- src/domain/getLabelConfigs.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/domain/getLabelConfigs.ts b/src/domain/getLabelConfigs.ts index 39f61f3..5142e69 100644 --- a/src/domain/getLabelConfigs.ts +++ b/src/domain/getLabelConfigs.ts @@ -20,9 +20,8 @@ const getFilePath = (configurationPath: string): string | undefined => { ) return } - const files = allFiles.filter((elem) => - jsonTypes.map((elem) => `auto-label.${elem}`).includes(elem) - ) + const expectedFilenames = jsonTypes.map((type) => `auto-label.${type}`); + const files = allFiles.filter((filename) => expectedFilenames.includes(filename)); if (!files.length) return return `${repoPath}/${files[0]}`.replace('//', '/') } From 73d67e56cd877f2f99032007c97b30716c013506 Mon Sep 17 00:00:00 2001 From: Renato Vicente Date: Tue, 25 Jun 2024 11:29:10 +0100 Subject: [PATCH 3/4] chore: lint --- src/domain/getLabelConfigs.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/domain/getLabelConfigs.ts b/src/domain/getLabelConfigs.ts index 5142e69..2cd793f 100644 --- a/src/domain/getLabelConfigs.ts +++ b/src/domain/getLabelConfigs.ts @@ -11,7 +11,7 @@ const getFilePath = (configurationPath: string): string | undefined => { if (configurationPath.includes('.json') && fs.existsSync(repoPath)) return repoPath if (!configurationPath.includes('.json')) { - let allFiles; + let allFiles try { allFiles = fs.readdirSync(repoPath) } catch (error: any) { @@ -20,8 +20,10 @@ const getFilePath = (configurationPath: string): string | undefined => { ) return } - const expectedFilenames = jsonTypes.map((type) => `auto-label.${type}`); - const files = allFiles.filter((filename) => expectedFilenames.includes(filename)); + const expectedFilenames = jsonTypes.map((type) => `auto-label.${type}`) + const files = allFiles.filter((filename) => + expectedFilenames.includes(filename) + ) if (!files.length) return return `${repoPath}/${files[0]}`.replace('//', '/') } From 5abd9ac2124e45efb9ba47b9ea04a473466e7208 Mon Sep 17 00:00:00 2001 From: Renato Vicente Date: Tue, 25 Jun 2024 12:39:30 +0100 Subject: [PATCH 4/4] refactor: try catch for the whole function --- src/domain/getLabelConfigs.ts | 38 ++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/domain/getLabelConfigs.ts b/src/domain/getLabelConfigs.ts index 2cd793f..071bb1b 100644 --- a/src/domain/getLabelConfigs.ts +++ b/src/domain/getLabelConfigs.ts @@ -5,27 +5,29 @@ import JSON5 from 'json5' const jsonTypes = ['json', 'jsonc', 'json5'] const getFilePath = (configurationPath: string): string | undefined => { - const repoPath = `./${configurationPath}` - .replace('//', '/') - .replace('././', './') - if (configurationPath.includes('.json') && fs.existsSync(repoPath)) - return repoPath - if (!configurationPath.includes('.json')) { - let allFiles - try { - allFiles = fs.readdirSync(repoPath) - } catch (error: any) { - core.warning( - `Could not read configuration file at ${repoPath}: ${error.message}. Skipping.` + try { + const repoPath = `./${configurationPath}` + .replace('//', '/') + .replace('././', './') + if (configurationPath.includes('.json') && fs.existsSync(repoPath)) + return repoPath + if (!configurationPath.includes('.json')) { + const allFiles = fs.readdirSync(repoPath) + + const expectedFilenames = jsonTypes.map((type) => `auto-label.${type}`) + const files = allFiles.filter((filename) => + expectedFilenames.includes(filename) ) - return + if (!files.length) { + throw new Error('No default files located.') + } + return `${repoPath}/${files[0]}`.replace('//', '/') } - const expectedFilenames = jsonTypes.map((type) => `auto-label.${type}`) - const files = allFiles.filter((filename) => - expectedFilenames.includes(filename) + } catch (error: any) { + core.warning( + `Could not read configuration file, configurationPath: "${configurationPath}", error: "${error.message}". Skipping.` ) - if (!files.length) return - return `${repoPath}/${files[0]}`.replace('//', '/') + return } }