From c75d80fdf0ae02a06a38d0a87b77954ebe5c7027 Mon Sep 17 00:00:00 2001 From: Justin De Burgo Date: Fri, 11 Nov 2022 11:02:33 -0600 Subject: [PATCH] adding filters to catalog import Signed-off-by: Lucas De Souza --- .../src/components/ImportStepper/ImportStepper.tsx | 5 ++++- .../src/components/ImportStepper/defaults.tsx | 4 +++- .../StepInitAnalyzeUrl/StepInitAnalyzeUrl.tsx | 11 +++++++---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/plugins/catalog-import/src/components/ImportStepper/ImportStepper.tsx b/plugins/catalog-import/src/components/ImportStepper/ImportStepper.tsx index b2638e582e..7724a3c7d1 100644 --- a/plugins/catalog-import/src/components/ImportStepper/ImportStepper.tsx +++ b/plugins/catalog-import/src/components/ImportStepper/ImportStepper.tsx @@ -46,6 +46,7 @@ export interface ImportStepperProps { defaults: StepperProvider, ) => StepperProvider; variant?: InfoCardVariants; + filters?: Array; } /** @@ -58,6 +59,7 @@ export const ImportStepper = (props: ImportStepperProps) => { initialUrl, generateStepper = defaultGenerateStepper, variant, + filters = [], } = props; const catalogImportApi = useApi(catalogImportApiRef); @@ -88,7 +90,8 @@ export const ImportStepper = (props: ImportStepperProps) => { {render( states.analyze( state as Extract, - { apis: { catalogImportApi } }, + { apis: { catalogImportApi } }, + filters, ), )} {render( diff --git a/plugins/catalog-import/src/components/ImportStepper/defaults.tsx b/plugins/catalog-import/src/components/ImportStepper/defaults.tsx index 94dff39d0f..6d39a38e4e 100644 --- a/plugins/catalog-import/src/components/ImportStepper/defaults.tsx +++ b/plugins/catalog-import/src/components/ImportStepper/defaults.tsx @@ -51,6 +51,7 @@ export interface StepperProvider { analyze: ( s: Extract, opts: { apis: StepperApis }, + filters?: String[], ) => StepConfiguration; prepare: ( s: Extract, @@ -263,7 +264,7 @@ export function defaultGenerateStepper( } export const defaultStepper: StepperProvider = { - analyze: (state, { apis }) => ({ + analyze: (state, { apis }, filters=[]) => ({ stepLabel: Select URL, content: ( ), }), diff --git a/plugins/catalog-import/src/components/StepInitAnalyzeUrl/StepInitAnalyzeUrl.tsx b/plugins/catalog-import/src/components/StepInitAnalyzeUrl/StepInitAnalyzeUrl.tsx index 3c655ec11b..51c5a4d3fd 100644 --- a/plugins/catalog-import/src/components/StepInitAnalyzeUrl/StepInitAnalyzeUrl.tsx +++ b/plugins/catalog-import/src/components/StepInitAnalyzeUrl/StepInitAnalyzeUrl.tsx @@ -42,6 +42,7 @@ export interface StepInitAnalyzeUrlProps { disablePullRequest?: boolean; analysisUrl?: string; exampleLocationUrl?: string; + filters?: Array, } /** @@ -58,6 +59,7 @@ export const StepInitAnalyzeUrl = (props: StepInitAnalyzeUrlProps) => { analysisUrl = '', disablePullRequest = false, exampleLocationUrl = 'https://github.com/backstage/backstage/blob/master/catalog-info.yaml', + filters = [], } = props; const errorApi = useApi(errorApiRef); @@ -78,6 +80,8 @@ export const StepInitAnalyzeUrl = (props: StepInitAnalyzeUrlProps) => { const [submitted, setSubmitted] = useState(false); const [error, setError] = useState(undefined); + const filteredRegex = new RegExp(`^http[s]?://${filters ? `[${filters.map((filter, i) => i === 0 ? filter : `|${filter}`)}]` : ''}`) + const handleResult = useCallback( async ({ url }: FormData) => { setSubmitted(true); @@ -113,9 +117,8 @@ export const StepInitAnalyzeUrl = (props: StepInitAnalyzeUrlProps) => { } default: { - const err = `Received unknown analysis result of type ${ - (analysisResult as any).type - }. Please contact the support team.`; + const err = `Received unknown analysis result of type ${(analysisResult as any).type + }. Please contact the support team.`; setError(err); setSubmitted(false); @@ -140,7 +143,7 @@ export const StepInitAnalyzeUrl = (props: StepInitAnalyzeUrlProps) => { validate: { httpsValidator: (value: any) => (typeof value === 'string' && - value.match(/^http[s]?:\/\//) !== null) || + value.match(filteredRegex) !== null) || 'Must start with http:// or https://.', }, }),