diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.test.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.test.ts index 0251b3d983..e4410e9838 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.test.ts @@ -17,7 +17,7 @@ jest.mock('./helpers'); jest.mock('azure-devops-node-api', () => ({ WebApi: jest.fn(), - getPersonalAccessTokenHandler: jest.fn(), + getPersonalAccessTokenHandler: jest.fn().mockReturnValue(() => {}), })); import { AzurePublisher } from './azure'; @@ -50,13 +50,18 @@ describe('Azure Publisher', () => { const result = await publisher!.publish({ values: { - storePath: 'project/repo', + storePath: 'https://dev.azure.com/organisation/project/_git/repo', owner: 'bob', }, directory: '/tmp/test', logger, }); + expect(WebApi).toHaveBeenCalledWith( + 'https://dev.azure.com/organisation', + expect.any(Function), + ); + expect(result).toEqual({ remoteUrl: 'https://dev.azure.com/organization/project/_git/repo', catalogInfoUrl: diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.ts index a27eba8a14..b2ac486d77 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/azure.ts @@ -27,24 +27,30 @@ export class AzurePublisher implements PublisherBase { if (!config.token) { return undefined; } - const authHandler = getPersonalAccessTokenHandler(config.token); - const webApi = new WebApi(config.host, authHandler); - const azureClient = await webApi.getGitApi(); - return new AzurePublisher({ token: config.token, client: azureClient }); + return new AzurePublisher({ token: config.token }); } - constructor(private readonly config: { token: string; client: IGitApi }) {} + constructor(private readonly config: { token: string }) {} async publish({ values, directory, logger, }: PublisherOptions): Promise { - const { owner, name } = parseGitUrl(values.storePath); + const { owner, name, organization, resource } = parseGitUrl( + values.storePath, + ); + const authHandler = getPersonalAccessTokenHandler(this.config.token); + const webApi = new WebApi( + `https://${resource}/${organization}`, + authHandler, + ); + const client = await webApi.getGitApi(); const remoteUrl = await this.createRemote({ project: owner, name, + client, }); const catalogInfoUrl = `${remoteUrl}?path=%2Fcatalog-info.yaml`; @@ -62,13 +68,14 @@ export class AzurePublisher implements PublisherBase { return { remoteUrl, catalogInfoUrl }; } - private async createRemote(opts: { name: string; project: string }) { - const { name, project } = opts; + private async createRemote(opts: { + name: string; + project: string; + client: IGitApi; + }) { + const { name, project, client } = opts; const createOptions: GitRepositoryCreateOptions = { name }; - const repo = await this.config.client.createRepository( - createOptions, - project, - ); + const repo = await client.createRepository(createOptions, project); return repo.remoteUrl || ''; }