From 661b354277d71a9a5005930b6b900c4d24601965 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Veith=20M=2E=20B=C3=BCrgerhoff?= Date: Mon, 24 Jun 2024 14:17:19 +0200 Subject: [PATCH] Fixed validation for RepoUrlPicker MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Veith M. Bürgerhoff --- .changeset/honest-pears-run.md | 7 ++++ .../src/actions/azure.examples.ts | 6 ++-- .../src/actions/azure.test.ts | 33 +++++++++++-------- .../src/actions/azure.ts | 9 +++-- plugins/scaffolder-node/src/actions/util.ts | 4 +++ .../fields/RepoUrlPicker/validation.ts | 9 ++++- 6 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 .changeset/honest-pears-run.md diff --git a/.changeset/honest-pears-run.md b/.changeset/honest-pears-run.md new file mode 100644 index 0000000000..acf9543997 --- /dev/null +++ b/.changeset/honest-pears-run.md @@ -0,0 +1,7 @@ +--- +'@backstage/plugin-scaffolder-backend-module-azure': patch +'@backstage/plugin-scaffolder-node': patch +'@backstage/plugin-scaffolder': patch +--- + +Fixed a bug where the RepoUrlPicker would still require the 'owner' field after the usage was changed to 'repo'. diff --git a/plugins/scaffolder-backend-module-azure/src/actions/azure.examples.ts b/plugins/scaffolder-backend-module-azure/src/actions/azure.examples.ts index d1459d1820..22c2299b30 100644 --- a/plugins/scaffolder-backend-module-azure/src/actions/azure.examples.ts +++ b/plugins/scaffolder-backend-module-azure/src/actions/azure.examples.ts @@ -29,7 +29,7 @@ export const examples: TemplateExample[] = [ name: 'Publish to Azure', input: { repoUrl: - 'dev.azure.com?organization=organization&owner=project&repo=repo', + 'dev.azure.com?organization=organization&project=project&repo=repo', }, }, ], @@ -45,7 +45,7 @@ export const examples: TemplateExample[] = [ name: 'Publish to Azure', input: { repoUrl: - 'dev.azure.com?organization=organization&owner=project&repo=repo', + 'dev.azure.com?organization=organization&project=project&repo=repo', description: 'Initialize a git repository', }, }, @@ -63,7 +63,7 @@ export const examples: TemplateExample[] = [ name: 'Publish to Azure', input: { repoUrl: - 'dev.azure.com?organization=organization&owner=project&repo=repo', + 'dev.azure.com?organization=organization&project=project&repo=repo', defaultBranch: 'main', }, }, diff --git a/plugins/scaffolder-backend-module-azure/src/actions/azure.test.ts b/plugins/scaffolder-backend-module-azure/src/actions/azure.test.ts index a21520f751..140c77fb52 100644 --- a/plugins/scaffolder-backend-module-azure/src/actions/azure.test.ts +++ b/plugins/scaffolder-backend-module-azure/src/actions/azure.test.ts @@ -55,7 +55,9 @@ describe('publish:azure', () => { const action = createPublishAzureAction({ integrations, config }); const mockContext = createMockActionContext({ - input: { repoUrl: 'dev.azure.com?repo=repo&owner=owner&organization=org' }, + input: { + repoUrl: 'dev.azure.com?repo=repo&project=project&organization=org', + }, }); const mockGitClient = { @@ -77,19 +79,19 @@ describe('publish:azure', () => { ...mockContext, input: { repoUrl: 'dev.azure.com?repo=bob' }, }), - ).rejects.toThrow(/missing owner/); + ).rejects.toThrow(/missing project/); await expect( action.handler({ ...mockContext, - input: { repoUrl: 'dev.azure.com?owner=owner' }, + input: { repoUrl: 'dev.azure.com?project=project' }, }), ).rejects.toThrow(/missing repo/); await expect( action.handler({ ...mockContext, - input: { repoUrl: 'dev.azure.com?owner=owner&repo=repo' }, + input: { repoUrl: 'dev.azure.com?project=project&repo=repo' }, }), ).rejects.toThrow(/missing organization/); }); @@ -98,7 +100,9 @@ describe('publish:azure', () => { await expect( action.handler({ ...mockContext, - input: { repoUrl: 'azure.com?repo=bob&owner=owner&organization=org' }, + input: { + repoUrl: 'azure.com?repo=bob&project=project&organization=org', + }, }), ).rejects.toThrow(/No matching integration configuration/); }); @@ -109,7 +113,7 @@ describe('publish:azure', () => { ...mockContext, input: { repoUrl: - 'myazurehostnotoken.com?repo=bob&owner=owner&organization=org', + 'myazurehostnotoken.com?repo=bob&project=project&organization=org', }, }), ).rejects.toThrow( @@ -122,7 +126,7 @@ describe('publish:azure', () => { action.handler({ ...mockContext, input: { - repoUrl: 'dev.azure.com?repo=bob&owner=owner&organization=org', + repoUrl: 'dev.azure.com?repo=bob&project=project&organization=org', }, }), ).rejects.toThrow(/Unable to create the repository/); @@ -138,7 +142,8 @@ describe('publish:azure', () => { await action.handler({ ...mockContext, input: { - repoUrl: 'myazurehostnotoken.com?repo=bob&owner=owner&organization=org', + repoUrl: + 'myazurehostnotoken.com?repo=bob&project=project&organization=org', token: 'lols', }, }); @@ -152,7 +157,7 @@ describe('publish:azure', () => { { name: 'bob', }, - 'owner', + 'project', ); }); @@ -166,7 +171,7 @@ describe('publish:azure', () => { action.handler({ ...mockContext, input: { - repoUrl: 'dev.azure.com?repo=bob&owner=owner&organization=org', + repoUrl: 'dev.azure.com?repo=bob&project=project&organization=org', }, }), ).rejects.toThrow(/No remote URL returned/); @@ -182,7 +187,7 @@ describe('publish:azure', () => { action.handler({ ...mockContext, input: { - repoUrl: 'dev.azure.com?repo=bob&owner=owner&organization=org', + repoUrl: 'dev.azure.com?repo=bob&project=project&organization=org', }, }), ).rejects.toThrow(/No Id returned/); @@ -198,7 +203,7 @@ describe('publish:azure', () => { action.handler({ ...mockContext, input: { - repoUrl: 'dev.azure.com?repo=bob&owner=owner&organization=org', + repoUrl: 'dev.azure.com?repo=bob&project=project&organization=org', }, }), ).rejects.toThrow(/No web URL returned/); @@ -214,7 +219,7 @@ describe('publish:azure', () => { await action.handler({ ...mockContext, input: { - repoUrl: 'dev.azure.com?repo=bob&owner=owner&organization=org', + repoUrl: 'dev.azure.com?repo=bob&project=project&organization=org', }, }); @@ -227,7 +232,7 @@ describe('publish:azure', () => { { name: 'bob', }, - 'owner', + 'project', ); }); diff --git a/plugins/scaffolder-backend-module-azure/src/actions/azure.ts b/plugins/scaffolder-backend-module-azure/src/actions/azure.ts index bd75cedc65..a284f463a6 100644 --- a/plugins/scaffolder-backend-module-azure/src/actions/azure.ts +++ b/plugins/scaffolder-backend-module-azure/src/actions/azure.ts @@ -136,7 +136,7 @@ export function createPublishAzureAction(options: { gitAuthorEmail, } = ctx.input; - const { owner, repo, host, organization } = parseRepoUrl( + const { project, repo, host, organization } = parseRepoUrl( repoUrl, integrations, ); @@ -166,11 +166,14 @@ export function createPublishAzureAction(options: { const webApi = new WebApi(url, authHandler); const client = await webApi.getGitApi(); const createOptions: GitRepositoryCreateOptions = { name: repo }; - const returnedRepo = await client.createRepository(createOptions, owner); + const returnedRepo = await client.createRepository( + createOptions, + project, + ); if (!returnedRepo) { throw new InputError( - `Unable to create the repository with Organization ${organization}, Project ${owner} and Repo ${repo}. + `Unable to create the repository with Organization ${organization}, Project ${project} and Repo ${repo}. Please make sure that both the Org and Project are typed corrected and exist.`, ); } diff --git a/plugins/scaffolder-node/src/actions/util.ts b/plugins/scaffolder-node/src/actions/util.ts index 2b3a1296bf..5e4ce60a1a 100644 --- a/plugins/scaffolder-node/src/actions/util.ts +++ b/plugins/scaffolder-node/src/actions/util.ts @@ -85,6 +85,10 @@ export const parseRepoUrl = ( checkRequiredParams(parsed, 'project', 'repo'); break; } + case 'azure': { + checkRequiredParams(parsed, 'project', 'repo'); + break; + } case 'gitlab': { // project is the projectID, and if defined, owner and repo won't be needed. if (!project) { diff --git a/plugins/scaffolder/src/components/fields/RepoUrlPicker/validation.ts b/plugins/scaffolder/src/components/fields/RepoUrlPicker/validation.ts index 55ba92abdc..c8dc66ab4c 100644 --- a/plugins/scaffolder/src/components/fields/RepoUrlPicker/validation.ts +++ b/plugins/scaffolder/src/components/fields/RepoUrlPicker/validation.ts @@ -54,7 +54,14 @@ export const repoPickerValidation = ( ); } } - // For anything other than bitbucket and gerrit + if (integrationApi?.byHost(host)?.type === 'azure') { + if (!searchParams.get('project')) { + validation.addError( + 'Incomplete repository location provided, project not provided', + ); + } + } + // For anything other than bitbucket, azure, and gerrit else if (integrationApi?.byHost(host)?.type !== 'gerrit') { if (!searchParams.get('owner')) { validation.addError(