diff --git a/apps/server/src/issue/IssueService.test.ts b/apps/server/src/issue/IssueService.test.ts index 2e260451de2d7..e4f61ff49b82b 100644 --- a/apps/server/src/issue/IssueService.test.ts +++ b/apps/server/src/issue/IssueService.test.ts @@ -293,6 +293,39 @@ describe("IssueService", () => { }), ); + it.effect("resolves both legacy Azure work item URLs through the bound project", () => + Effect.gen(function* () { + const tracker = IssueTrackerKind.make("azure-devops"); + const host = "acme.visualstudio.com"; + const service = yield* makeService({ + projects: [ + project({ id: "api", repository: "Api/_git/api", provider: tracker, host }), + project({ + id: "web", + repository: "DefaultCollection/Web/_git/web", + provider: tracker, + host, + }), + ], + trackers: [fakeTracker({ kind: tracker })], + }); + for (const path of ["Web", "DefaultCollection/Web"]) { + const ref = yield* service.resolveUrl( + `https://${host}/${path}/_workitems/edit/9`, + "api" as ProjectId, + ); + expect(ref).toEqual({ + projectId: "web", + tracker, + host, + scope: "DefaultCollection/Web", + key: "9", + writable: true, + }); + } + }), + ); + it.effect("sends writes as interactive requests and reads as background ones", () => Effect.gen(function* () { const interactive: Array = []; diff --git a/apps/server/src/issue/IssueService.ts b/apps/server/src/issue/IssueService.ts index bf3d301f73708..fbf869e562001 100644 --- a/apps/server/src/issue/IssueService.ts +++ b/apps/server/src/issue/IssueService.ts @@ -704,9 +704,12 @@ const make = Effect.gen(function* () { const onTracker = onAuthority.length > 0 ? onAuthority : onHost; const preferFirst = (projects: ReadonlyArray) => projects.find((project) => project.project.id === preferredProjectId) ?? projects[0]; - const own = preferFirst( - onTracker.filter((project) => project.scope.toLowerCase() === link.scope.toLowerCase()), - ); + const scope = link.scope.toLowerCase(); + const own = + preferFirst(onTracker.filter((project) => project.scope.toLowerCase() === scope)) ?? + preferFirst( + onTracker.filter((project) => project.scope.toLowerCase() === gitHost?.link.scopeAlias), + ); const lender = own ?? preferFirst(onTracker); if (lender === undefined) { return yield* new IssueUnavailableError({ reason: "tracker-unsupported" }); diff --git a/packages/client-runtime/src/issueLinks.ts b/packages/client-runtime/src/issueLinks.ts index 3509bbbf33ed5..44da23d9d85bf 100644 --- a/packages/client-runtime/src/issueLinks.ts +++ b/packages/client-runtime/src/issueLinks.ts @@ -78,7 +78,9 @@ export function resolveIssueLinkTarget(input: { const preferFirst = (candidates: typeof bound) => candidates.find((candidate) => candidate.project.id === input.preferredProjectId) ?? candidates[0]; - const own = preferFirst(bound.filter((candidate) => candidate.binding.scope === link.scope)); + const own = + preferFirst(bound.filter((candidate) => candidate.binding.scope === link.scope)) ?? + preferFirst(bound.filter((candidate) => candidate.binding.scope === link.scopeAlias)); const lender = own ?? preferFirst(bound); if (lender === undefined) return null; return { diff --git a/packages/source-control-azure-devops/src/shared/definition.test.ts b/packages/source-control-azure-devops/src/shared/definition.test.ts index 8a10c457f66c3..f18b627a874be 100644 --- a/packages/source-control-azure-devops/src/shared/definition.test.ts +++ b/packages/source-control-azure-devops/src/shared/definition.test.ts @@ -260,14 +260,39 @@ describe("Azure DevOps issue scope", () => { definition.issueScope?.({ provider: kind, displayName: "acme/Web%20App/_git/web" }), ).toBe("acme/Web App"); expect(definition.issueScope?.({ provider: kind, displayName: "web" })).toBeNull(); - // One segment before `_git` is a different thing on each kind of host, so it is not placed. + // One segment before `_git` is a different thing on each kind of host, so it needs the host. expect(definition.issueScope?.({ provider: kind, displayName: "acme/_git/web" })).toBeNull(); + const scopeOn = (canonicalKey: string, displayName: string) => + definition.issueScope?.({ provider: kind, canonicalKey, displayName }); + expect(scopeOn("dev.azure.com/acme/_git/web", "acme/_git/Web")).toBe("acme/Web"); + expect(scopeOn("acme.visualstudio.com/web/_git/web", "Web%20App/_git/web")).toBe("Web App"); + expect(scopeOn("acme.visualstudio.com/_git/web", "_git/Web")).toBe("Web"); + // Its work items are at `acme.visualstudio.com/Web/_workitems/...`, without the collection. + expect( + scopeOn("acme.visualstudio.com/defaultcollection/_git/web", "DefaultCollection/_git/Web"), + ).toBe("Web"); + expect(scopeOn("dev.azure.com/_git/web", "_git/web")).toBeNull(); // A legacy host keeps the organization in its name, and so does its work item URL. expect( definition.parseIssueUrl?.( new URL("https://acme.visualstudio.com/Web%20App/_workitems/edit/9"), ), - ).toEqual({ host: "acme.visualstudio.com", scope: "web app", key: "9" }); + ).toEqual({ + host: "acme.visualstudio.com", + scope: "web app", + scopeAlias: "defaultcollection/web app", + key: "9", + }); + expect( + definition.parseIssueUrl?.( + new URL("https://acme.visualstudio.com/DefaultCollection/Web%20App/_workitems/edit/9"), + ), + ).toEqual({ + host: "acme.visualstudio.com", + scope: "defaultcollection/web app", + scopeAlias: "web app", + key: "9", + }); expect( definition.parseIssueUrl?.(new URL("https://dev.azure.com/acme/_workitems/edit/9")), ).toBeNull(); diff --git a/packages/source-control-azure-devops/src/shared/definition.ts b/packages/source-control-azure-devops/src/shared/definition.ts index 38490a062c76b..78f1a4df45a8d 100644 --- a/packages/source-control-azure-devops/src/shared/definition.ts +++ b/packages/source-control-azure-devops/src/shared/definition.ts @@ -124,14 +124,18 @@ export const definition = defineSourceControl({ }, // A project named with spaces arrives percent-encoded; the binding it is matched against is not. parseIssueUrl: (url) => { + const legacy = url.hostname.toLowerCase().endsWith(".visualstudio.com"); const link = parseIssuePath(url, WORK_ITEM_PATH) ?? - (url.hostname.toLowerCase().endsWith(".visualstudio.com") - ? parseIssuePath(url, LEGACY_WORK_ITEM_PATH) - : null); - return link === null - ? null - : { ...link, scope: link.scope.split("/").map(decodeUrlPathSegment).join("/").toLowerCase() }; + (legacy ? parseIssuePath(url, LEGACY_WORK_ITEM_PATH) : null); + if (link === null) return null; + const scope = link.scope.split("/").map(decodeUrlPathSegment).join("/").toLowerCase(); + if (!legacy) return { ...link, scope }; + // On legacy hosts the default collection is optional in a work item URL. + const scopeAlias = + /^defaultcollection\/([^/]+)$/u.exec(scope)?.[1] ?? + (scope.includes("/") ? undefined : `defaultcollection/${scope}`); + return { ...link, scope, ...(scopeAlias === undefined ? {} : { scopeAlias }) }; }, changeRequestUrlFromReferenceAutolink: () => null, changeRequestActions: new Set([ @@ -170,13 +174,25 @@ export const definition = defineSourceControl({ return name ? decodeUrlPathSegment(name) : null; }, // Work items belong to the project: `organization/project` out of the remote's - // `organization/project/_git/repository`. A shorter path is not placed: `acme/_git/web` is a - // project named after its repository on dev.azure.com and a bare project on a legacy host, and - // nothing here says which host the remote is on. + // `organization/project/_git/repository`. A shorter path depends on the host: `acme/_git/web` + // is a project named after its repository on dev.azure.com and a bare project on a legacy host. issueScope: (identity) => { - const segments = (identity.displayName ?? "").split("/").filter((part) => part !== ""); + const segments = (identity.displayName ?? "") + .split("/") + .filter((part) => part !== "") + .map(decodeUrlPathSegment); const at = segments.indexOf("_git"); - return at === 2 ? segments.slice(0, at).map(decodeUrlPathSegment).join("/") : null; + if (at === 2) return segments.slice(0, at).join("/"); + const repository = segments[at + 1]; + if ((at !== 0 && at !== 1) || repository === undefined) return null; + const host = identity.canonicalKey?.split("/", 1)[0]?.toLowerCase() ?? ""; + const legacy = host.endsWith(".visualstudio.com"); + const [first = ""] = segments; + if (at === 0) return legacy ? repository : null; + if (host === "dev.azure.com") return `${first}/${repository}`; + if (!legacy) return null; + // Work item URLs on a legacy host leave the collection out. + return first.toLowerCase() === "defaultcollection" ? repository : first; }, repositoryKeyFromRemote, canonicalRepositoryKey, diff --git a/packages/source-control-core/src/shared/definition.ts b/packages/source-control-core/src/shared/definition.ts index acd85f0c877a7..d9fe29114a96c 100644 --- a/packages/source-control-core/src/shared/definition.ts +++ b/packages/source-control-core/src/shared/definition.ts @@ -39,6 +39,8 @@ export interface ChangeRequestLink { export interface IssueLink { readonly host: string; readonly scope: string; + /** An equivalent binding scope when the provider allows another spelling in URLs. */ + readonly scopeAlias?: string; readonly key: string; } @@ -94,6 +96,9 @@ export type RepositorySelectorIdentity = Pick< "provider" | "displayName" | "owner" | "name" >; +export type IssueScopeIdentity = RepositorySelectorIdentity & + Partial>; + /** A git remote's host, as `recognizeRemote` reads it. */ export interface GitRemoteHost { /** Trimmed. */ @@ -362,7 +367,7 @@ export interface SourceControlDefinition { * The container the project's issues live in, where that is not its repository: Azure DevOps * keeps work items per project. Null when the repository cannot be placed. */ - readonly issueScope?: (identity: RepositorySelectorIdentity) => string | null; + readonly issueScope?: (identity: IssueScopeIdentity) => string | null; /** * The key of a remote's lower case `host/path`, `.git` suffix kept, for a host that spells it * apart from `normalizeGitRemoteUrl`: over SSH and the web, or with `.git` as part of the name. @@ -699,7 +704,7 @@ export interface SourceControlDefinitionRegistry { identity: RepositorySelectorIdentity | null | undefined, ) => string | null; /** Where the project's issues live: the host's `issueScope`, else its repository. */ - readonly issueScope: (identity: RepositorySelectorIdentity | null | undefined) => string | null; + readonly issueScope: (identity: IssueScopeIdentity | null | undefined) => string | null; readonly changeRequestHost: ( identity: ChangeRequestHostIdentity, kind: Kind,