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';