From 968b588f79a4d0d51d4c52060650c61991115f97 Mon Sep 17 00:00:00 2001 From: Andrew Thauer <6507159+andrewthauer@users.noreply.github.com> Date: Sat, 20 Feb 2021 13:48:57 -0500 Subject: [PATCH 1/2] feat: support codeowners in catalog-import --- .changeset/silly-lemons-dream.md | 5 + .../src/components/ImportStepper/defaults.tsx | 144 ++++++++++-------- .../CheckboxField.tsx | 53 +++++++ .../PreparePullRequestForm.tsx | 6 +- .../StepPrepareCreatePullRequest.tsx | 15 +- .../StepPrepareCreatePullRequest/index.ts | 1 + 6 files changed, 157 insertions(+), 67 deletions(-) create mode 100644 .changeset/silly-lemons-dream.md create mode 100644 plugins/catalog-import/src/components/StepPrepareCreatePullRequest/CheckboxField.tsx diff --git a/.changeset/silly-lemons-dream.md b/.changeset/silly-lemons-dream.md new file mode 100644 index 0000000000..a93be80597 --- /dev/null +++ b/.changeset/silly-lemons-dream.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-import': patch +--- + +Allows the CodeOwnersProcessor to set the owner automatically within the catalog-import plugin. This adds an additional checkbox that overrides the group selector and will omit the owner option in the generated catalog file yaml. diff --git a/plugins/catalog-import/src/components/ImportStepper/defaults.tsx b/plugins/catalog-import/src/components/ImportStepper/defaults.tsx index 5252b0aeae..f55cf5e911 100644 --- a/plugins/catalog-import/src/components/ImportStepper/defaults.tsx +++ b/plugins/catalog-import/src/components/ImportStepper/defaults.tsx @@ -22,6 +22,7 @@ import { StepFinishImportLocation } from '../StepFinishImportLocation'; import { StepInitAnalyzeUrl } from '../StepInitAnalyzeUrl'; import { AutocompleteTextField, + CheckboxField, StepPrepareCreatePullRequest, } from '../StepPrepareCreatePullRequest'; import { StepPrepareSelectLocations } from '../StepPrepareSelectLocations'; @@ -169,74 +170,97 @@ export function defaultGenerateStepper( renderFormFields={({ control, errors, + watch, groupsLoading, groups, register, - }) => ( - <> - - Pull Request Details - + }) => { + const watchUseCodeowners = watch('useCodeowners', false); - + return ( + <> + + + Pull Request Details + + - + - - Entity Configuration - + - + + + Entity Configuration + + - - - )} + + + {!watchUseCodeowners && ( + + )} + + { + if (value) { + control.setValue('owner', ''); + } + }} + /> + + ); + }} /> ), }; diff --git a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/CheckboxField.tsx b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/CheckboxField.tsx new file mode 100644 index 0000000000..dbce1b5229 --- /dev/null +++ b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/CheckboxField.tsx @@ -0,0 +1,53 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Checkbox, FormControlLabel, FormHelperText } from '@material-ui/core'; +import React from 'react'; + +type Props = { + name: TFieldValue; + label: React.ReactNode; + inputRef: + | ((instance: HTMLInputElement | null) => void) + | React.RefObject + | null + | undefined; + onChange?: ( + event: React.ChangeEvent, + checked: boolean, + ) => void; + helperText?: React.ReactNode | string; +}; + +export const CheckboxField = ({ + name, + label, + inputRef, + onChange, + helperText, +}: Props) => { + return ( + <> + + } + label={label} + /> + {helperText && {helperText}} + + ); +}; diff --git a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/PreparePullRequestForm.tsx b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/PreparePullRequestForm.tsx index 9d59f4e10e..aed87a3b34 100644 --- a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/PreparePullRequestForm.tsx +++ b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/PreparePullRequestForm.tsx @@ -32,7 +32,7 @@ type Props> = Pick< render: ( props: Pick< UseFormMethods, - 'errors' | 'register' | 'control' + 'errors' | 'register' | 'control' | 'formState' | 'watch' > & { values: UnpackNestedValue; }, @@ -55,13 +55,13 @@ export const PreparePullRequestForm = < onSubmit, render, }: Props) => { - const { handleSubmit, watch, control, register, errors } = useForm< + const { handleSubmit, control, register, errors, formState, watch } = useForm< TFieldValues >({ mode: 'onTouched', defaultValues }); return (
- {render({ values: watch(), errors, register, control })} + {render({ values: watch(), errors, register, control, formState, watch })}
); }; diff --git a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx index 333df76026..1966b8457f 100644 --- a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx +++ b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx @@ -48,6 +48,7 @@ type FormData = { body: string; componentName: string; owner: string; + useCodeowners: boolean; }; type Props = { @@ -62,7 +63,10 @@ type Props = { defaultBody: string; renderFormFields: ( - props: Pick, 'errors' | 'register' | 'control'> & { + props: Pick< + UseFormMethods, + 'errors' | 'register' | 'control' | 'formState' | 'watch' + > & { groups: string[]; groupsLoading: boolean; }, @@ -72,7 +76,7 @@ type Props = { export function generateEntities( entities: PartialEntity[], componentName: string, - owner: string, + owner?: string, ): Entity[] { return entities.map(e => ({ ...e, @@ -84,7 +88,7 @@ export function generateEntities( }, spec: { ...e.spec, - owner, + ...(owner ? { owner } : {}), }, })); } @@ -189,13 +193,16 @@ export const StepPrepareCreatePullRequest = ({ (analyzeResult.generatedEntities[0]?.spec?.owner as string) || '', componentName: analyzeResult.generatedEntities[0]?.metadata?.name || '', + useCodeowners: false, }} - render={({ values, errors, control, register }) => ( + render={({ values, errors, control, register, formState, watch }) => ( <> {renderFormFields({ errors, register, control, + formState, + watch, groups: groups ?? [], groupsLoading, })} diff --git a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/index.ts b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/index.ts index 119feee4a1..a0fc6cbc95 100644 --- a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/index.ts +++ b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/index.ts @@ -15,6 +15,7 @@ */ export { AutocompleteTextField } from './AutocompleteTextField'; +export { CheckboxField } from './CheckboxField'; export { PreparePullRequestForm } from './PreparePullRequestForm'; export { PreviewCatalogInfoComponent } from './PreviewCatalogInfoComponent'; export { PreviewPullRequestComponent } from './PreviewPullRequestComponent'; From 436b76fbdddd675022ff41e7a2eeffc195bcc1c1 Mon Sep 17 00:00:00 2001 From: Andrew Thauer <6507159+andrewthauer@users.noreply.github.com> Date: Sat, 20 Feb 2021 14:49:25 -0500 Subject: [PATCH 2/2] remove unecessary watch & formState --- .../src/components/ImportStepper/defaults.tsx | 176 +++++++++--------- .../CheckboxField.tsx | 53 ------ .../PreparePullRequestForm.tsx | 6 +- .../StepPrepareCreatePullRequest.tsx | 13 +- .../StepPrepareCreatePullRequest/index.ts | 1 - 5 files changed, 101 insertions(+), 148 deletions(-) delete mode 100644 plugins/catalog-import/src/components/StepPrepareCreatePullRequest/CheckboxField.tsx diff --git a/plugins/catalog-import/src/components/ImportStepper/defaults.tsx b/plugins/catalog-import/src/components/ImportStepper/defaults.tsx index f55cf5e911..dff2136aaa 100644 --- a/plugins/catalog-import/src/components/ImportStepper/defaults.tsx +++ b/plugins/catalog-import/src/components/ImportStepper/defaults.tsx @@ -15,14 +15,21 @@ */ import { ConfigApi } from '@backstage/core'; -import { Box, StepLabel, TextField, Typography } from '@material-ui/core'; +import { + Box, + Checkbox, + FormControlLabel, + FormHelperText, + StepLabel, + TextField, + Typography, +} from '@material-ui/core'; import React from 'react'; import { BackButton } from '../Buttons'; import { StepFinishImportLocation } from '../StepFinishImportLocation'; import { StepInitAnalyzeUrl } from '../StepInitAnalyzeUrl'; import { AutocompleteTextField, - CheckboxField, StepPrepareCreatePullRequest, } from '../StepPrepareCreatePullRequest'; import { StepPrepareSelectLocations } from '../StepPrepareSelectLocations'; @@ -168,99 +175,102 @@ export function defaultGenerateStepper( defaultTitle={title} defaultBody={body} renderFormFields={({ + values, control, errors, - watch, groupsLoading, groups, register, - }) => { - const watchUseCodeowners = watch('useCodeowners', false); + }) => ( + <> + + Pull Request Details + - return ( - <> - - - Pull Request Details - - + - + - + + Entity Configuration + - - - Entity Configuration - - + - - - {!watchUseCodeowners && ( - - )} - - { - if (value) { - control.setValue('owner', ''); - } + {!values.useCodeowners && ( + - - ); - }} + )} + + { + if (value) { + control.setValue('owner', ''); + } + }} + /> + } + label={ + <> + Use CODEOWNERS file as Entity Owner + + } + /> + + WARNING: This may fail is no CODEOWNERS file is found at + the target location. + + + )} /> ), }; diff --git a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/CheckboxField.tsx b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/CheckboxField.tsx deleted file mode 100644 index dbce1b5229..0000000000 --- a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/CheckboxField.tsx +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2021 Spotify AB - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { Checkbox, FormControlLabel, FormHelperText } from '@material-ui/core'; -import React from 'react'; - -type Props = { - name: TFieldValue; - label: React.ReactNode; - inputRef: - | ((instance: HTMLInputElement | null) => void) - | React.RefObject - | null - | undefined; - onChange?: ( - event: React.ChangeEvent, - checked: boolean, - ) => void; - helperText?: React.ReactNode | string; -}; - -export const CheckboxField = ({ - name, - label, - inputRef, - onChange, - helperText, -}: Props) => { - return ( - <> - - } - label={label} - /> - {helperText && {helperText}} - - ); -}; diff --git a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/PreparePullRequestForm.tsx b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/PreparePullRequestForm.tsx index aed87a3b34..9d59f4e10e 100644 --- a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/PreparePullRequestForm.tsx +++ b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/PreparePullRequestForm.tsx @@ -32,7 +32,7 @@ type Props> = Pick< render: ( props: Pick< UseFormMethods, - 'errors' | 'register' | 'control' | 'formState' | 'watch' + 'errors' | 'register' | 'control' > & { values: UnpackNestedValue; }, @@ -55,13 +55,13 @@ export const PreparePullRequestForm = < onSubmit, render, }: Props) => { - const { handleSubmit, control, register, errors, formState, watch } = useForm< + const { handleSubmit, watch, control, register, errors } = useForm< TFieldValues >({ mode: 'onTouched', defaultValues }); return (
- {render({ values: watch(), errors, register, control, formState, watch })} + {render({ values: watch(), errors, register, control })}
); }; diff --git a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx index 1966b8457f..c34d5f374b 100644 --- a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx +++ b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/StepPrepareCreatePullRequest.tsx @@ -23,7 +23,7 @@ import { import { Box, FormHelperText, Grid, Typography } from '@material-ui/core'; import { makeStyles } from '@material-ui/core/styles'; import React, { useCallback, useState } from 'react'; -import { UseFormMethods } from 'react-hook-form'; +import { UnpackNestedValue, UseFormMethods } from 'react-hook-form'; import { useAsync } from 'react-use'; import YAML from 'yaml'; import { AnalyzeResult, catalogImportApiRef } from '../../api'; @@ -63,10 +63,8 @@ type Props = { defaultBody: string; renderFormFields: ( - props: Pick< - UseFormMethods, - 'errors' | 'register' | 'control' | 'formState' | 'watch' - > & { + props: Pick, 'errors' | 'register' | 'control'> & { + values: UnpackNestedValue; groups: string[]; groupsLoading: boolean; }, @@ -195,14 +193,13 @@ export const StepPrepareCreatePullRequest = ({ analyzeResult.generatedEntities[0]?.metadata?.name || '', useCodeowners: false, }} - render={({ values, errors, control, register, formState, watch }) => ( + render={({ values, errors, control, register }) => ( <> {renderFormFields({ + values, errors, register, control, - formState, - watch, groups: groups ?? [], groupsLoading, })} diff --git a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/index.ts b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/index.ts index a0fc6cbc95..119feee4a1 100644 --- a/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/index.ts +++ b/plugins/catalog-import/src/components/StepPrepareCreatePullRequest/index.ts @@ -15,7 +15,6 @@ */ export { AutocompleteTextField } from './AutocompleteTextField'; -export { CheckboxField } from './CheckboxField'; export { PreparePullRequestForm } from './PreparePullRequestForm'; export { PreviewCatalogInfoComponent } from './PreviewCatalogInfoComponent'; export { PreviewPullRequestComponent } from './PreviewPullRequestComponent';