From e621899ed1238a4ad8ea41ccdf3321c064e6c71e Mon Sep 17 00:00:00 2001 From: Huiwen Date: Fri, 27 Sep 2024 09:31:27 +0000 Subject: [PATCH] Add project context supports --- .../azure-devops/azure-context-parser.spec.ts | 19 +++++++++ .../src/azure-devops/azure-context-parser.ts | 42 ++++++++++--------- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/components/server/src/azure-devops/azure-context-parser.spec.ts b/components/server/src/azure-devops/azure-context-parser.spec.ts index d37e8ceccad4ae..65910a2bbf2a4d 100644 --- a/components/server/src/azure-devops/azure-context-parser.spec.ts +++ b/components/server/src/azure-devops/azure-context-parser.spec.ts @@ -49,6 +49,25 @@ class TestAzureDevOpsContextParser { }); } + @test public async testEmptyProjectWithoutGitSegment() { + const result = await this.parser.handle({}, this.user, "https://dev.azure.com/services-azure/empty-project"); + expect(result).to.deep.include({ + path: "", + isFile: false, + title: "empty-project/empty-project", + repository: { + host: "dev.azure.com", + owner: "services-azure/empty-project", + name: "empty-project", + cloneUrl: "https://services-azure@dev.azure.com/services-azure/empty-project/_git/empty-project", + description: "main", + webUrl: "https://dev.azure.com/services-azure/empty-project/_git/empty-project", + defaultBranch: "main", + }, + revision: "", + }); + } + @test public async testDefault() { const result = await this.parser.handle( {}, diff --git a/components/server/src/azure-devops/azure-context-parser.ts b/components/server/src/azure-devops/azure-context-parser.ts index c5b4590cc6430b..df33d85a7ab38f 100644 --- a/components/server/src/azure-devops/azure-context-parser.ts +++ b/components/server/src/azure-devops/azure-context-parser.ts @@ -113,31 +113,35 @@ export class AzureDevOpsContextParser extends AbstractContextParser implements I const pathname = url.pathname.replace(/^\//, "").replace(/\/$/, ""); const segments = pathname.split("/").filter((e) => e !== ""); const host = this.host; - // case when there is only one repository in the project - // https://dev.azure.com/services-azure/_git/project2 - if (segments.length === 3 && segments[1] === "_git") { - const azOrganization = segments[0]; - const azProject = segments[2]; - const repo = azProject; - return { - host, - owner: `${azOrganization}/${azProject}`, - repoName: repo, - moreSegments: segments.slice(3), - searchParams: url.searchParams, - }; - } - if (segments.length < 4 || segments[2] !== "_git") { + let azOrganization = ""; + let azProject = ""; + let repo = ""; + let moreSegments: string[] = []; + if (segments.length === 2) { + // https://dev.azure.com/services-azure/empty-project + azOrganization = segments[0]; + azProject = segments[1]; + repo = azProject; + } else if (segments.length === 3 && segments[1] === "_git") { + // https://dev.azure.com/services-azure/_git/project2 + azOrganization = segments[0]; + azProject = segments[2]; + repo = azProject; + moreSegments = segments.slice(3); + } else if (segments.length < 4 || segments[2] !== "_git") { + // https://dev.azure.com/services-azure/project2/_git/project2 throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Invalid Azure DevOps repository URL"); + } else { + moreSegments = segments.slice(4); + azOrganization = segments[0]; + azProject = segments[1]; + repo = segments[3]; } - const azOrganization = segments[0]; - const azProject = segments[1]; - const repo = segments[3]; return { host, owner: `${azOrganization}/${azProject}`, repoName: repo, - moreSegments: segments.slice(4), + moreSegments, searchParams: url.searchParams, }; }