diff --git a/.changeset/hungry-cups-jog.md b/.changeset/hungry-cups-jog.md new file mode 100644 index 0000000000..bd15fa92d3 --- /dev/null +++ b/.changeset/hungry-cups-jog.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder': patch +--- + +Removed waiting for the workspace and repository fields to be filled in before requesting user credentials diff --git a/plugins/scaffolder/src/components/fields/RepoUrlPicker/RepoUrlPicker.test.tsx b/plugins/scaffolder/src/components/fields/RepoUrlPicker/RepoUrlPicker.test.tsx index 1fa723c8c6..59c09c106c 100644 --- a/plugins/scaffolder/src/components/fields/RepoUrlPicker/RepoUrlPicker.test.tsx +++ b/plugins/scaffolder/src/components/fields/RepoUrlPicker/RepoUrlPicker.test.tsx @@ -213,7 +213,7 @@ describe('RepoUrlPicker', () => {
{JSON.stringify({ secrets })}
); }; - const { getAllByRole, getByTestId } = await renderInTestApp( + const { getByTestId } = await renderInTestApp( { , ); - const [ownerInput, repoInput] = getAllByRole('textbox'); - await act(async () => { - fireEvent.change(ownerInput, { target: { value: 'backstage' } }); - fireEvent.change(repoInput, { target: { value: 'repo123' } }); - // need to wait for the debounce to finish await new Promise(resolve => setTimeout(resolve, 600)); }); expect(mockScmAuthApi.getCredentials).toHaveBeenCalledWith({ - url: 'https://github.com/backstage/repo123', + url: 'https://github.com', additionalScope: { repoWrite: true, customScopes: { @@ -278,7 +273,7 @@ describe('RepoUrlPicker', () => {
{JSON.stringify({ secrets })}
); }; - const { getAllByRole } = await renderInTestApp( + await renderInTestApp( { , ); - const [projectInput, repoInput] = getAllByRole('textbox'); - await act(async () => { - fireEvent.change(projectInput, { - target: { value: 'backstage/mysubgroup' }, - }); - fireEvent.change(repoInput, { target: { value: 'repo123' } }); - // need to wait for the debounce to finish await new Promise(resolve => setTimeout(resolve, 600)); }); expect(mockScmAuthApi.getCredentials).toHaveBeenCalledWith({ - url: 'https://gitlab.example.com/backstage/mysubgroup/repo123', + url: 'https://gitlab.example.com', additionalScope: { repoWrite: true, }, }); }); - it('should call the scmAuthApi with the correct params if only a project is set', async () => { - const SecretsComponent = () => { - const { secrets } = useTemplateSecrets(); - return ( -
{JSON.stringify({ secrets })}
- ); - }; - const { getAllByRole, getByTestId } = await renderInTestApp( - - -
, - }} - /> - - - , - ); - - const [projectInput, repoInput] = getAllByRole('textbox'); - - await act(async () => { - fireEvent.change(projectInput, { target: { value: 'backstage' } }); - fireEvent.change(repoInput, { target: { value: 'repo123' } }); - - // need to wait for the debounce to finish - await new Promise(resolve => setTimeout(resolve, 600)); - }); - - expect(mockScmAuthApi.getCredentials).toHaveBeenCalledWith({ - url: 'https://server.bitbucket.org/backstage/repo123', - additionalScope: { - repoWrite: true, - }, - }); - - const currentSecrets = JSON.parse( - getByTestId('current-secrets').textContent!, - ); - - expect(currentSecrets).toEqual({ - secrets: { testKey: 'abc123' }, - }); - }); it('should not call the scmAuthApi if secret is available in the state', async () => { const SecretsComponent = () => { @@ -397,7 +323,7 @@ describe('RepoUrlPicker', () => {
{JSON.stringify({ secrets })}
); }; - const { getAllByRole, getByTestId } = await renderInTestApp( + const { getByTestId } = await renderInTestApp( { , ); - const [ownerInput, repoInput] = getAllByRole('textbox'); - await act(async () => { - fireEvent.change(ownerInput, { target: { value: 'backstage' } }); - fireEvent.change(repoInput, { target: { value: 'repo123' } }); - // need to wait for the debounce to finish await new Promise(resolve => setTimeout(resolve, 600)); }); diff --git a/plugins/scaffolder/src/components/fields/RepoUrlPicker/RepoUrlPicker.tsx b/plugins/scaffolder/src/components/fields/RepoUrlPicker/RepoUrlPicker.tsx index 5a40480889..85245639b0 100644 --- a/plugins/scaffolder/src/components/fields/RepoUrlPicker/RepoUrlPicker.tsx +++ b/plugins/scaffolder/src/components/fields/RepoUrlPicker/RepoUrlPicker.tsx @@ -124,11 +124,7 @@ export const RepoUrlPicker = (props: RepoUrlPickerProps) => { async () => { const { requestUserCredentials } = uiSchema?.['ui:options'] ?? {}; - const workspace = state.owner ? state.owner : state.project; - if ( - !requestUserCredentials || - !(state.host && workspace && state.repoName) - ) { + if (!requestUserCredentials || !state.host) { return; } @@ -137,19 +133,11 @@ export const RepoUrlPicker = (props: RepoUrlPickerProps) => { return; } - // previously, we were encodeURI for state.host, workspace and state.repoName separately. - // That created an issue where GitLab workspace can be nested like groupA/subgroupB - // when we encodeURi separately and then join, the URL will be malformed and - // resulting in 400 request error from GitLab API - const [encodedHost, encodedRepoName] = [state.host, state.repoName].map( - encodeURIComponent, - ); - // user has requested that we use the users credentials // so lets grab them using the scmAuthApi and pass through // any additional scopes from the ui:options const { token } = await scmAuthApi.getCredentials({ - url: `https://${encodedHost}/${workspace}/${encodedRepoName}`, + url: `https://${state.host}`, additionalScope: { repoWrite: true, customScopes: requestUserCredentials.additionalScopes,