Skip to content
33 changes: 33 additions & 0 deletions apps/server/src/issue/IssueService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<readonly [string, boolean]> = [];
Expand Down
9 changes: 6 additions & 3 deletions apps/server/src/issue/IssueService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,9 +704,12 @@ const make = Effect.gen(function* () {
const onTracker = onAuthority.length > 0 ? onAuthority : onHost;
const preferFirst = (projects: ReadonlyArray<SupportedProject>) =>
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" });
Expand Down
4 changes: 3 additions & 1 deletion packages/client-runtime/src/issueLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ export function resolveIssueLinkTarget<Project extends LinkProject>(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 {
Expand Down
29 changes: 27 additions & 2 deletions packages/source-control-azure-devops/src/shared/definition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
38 changes: 27 additions & 11 deletions packages/source-control-azure-devops/src/shared/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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("/");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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,
Expand Down
9 changes: 7 additions & 2 deletions packages/source-control-core/src/shared/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -94,6 +96,9 @@ export type RepositorySelectorIdentity = Pick<
"provider" | "displayName" | "owner" | "name"
>;

export type IssueScopeIdentity = RepositorySelectorIdentity &
Partial<Pick<RepositoryIdentity, "canonicalKey">>;

/** A git remote's host, as `recognizeRemote` reads it. */
export interface GitRemoteHost {
/** Trimmed. */
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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: <const Kind extends string | undefined>(
identity: ChangeRequestHostIdentity,
kind: Kind,
Expand Down
Loading