diff --git a/apps/server/src/issue/IssueService.test.ts b/apps/server/src/issue/IssueService.test.ts index fd6f7b2f0a0c1..00360c076256a 100644 --- a/apps/server/src/issue/IssueService.test.ts +++ b/apps/server/src/issue/IssueService.test.ts @@ -37,7 +37,12 @@ function fakeStandaloneTracker( ...fakeTracker({ getViewer: () => Effect.succeed("usr_julius"), getIssue: (input) => - Effect.succeed({ ...issue(input.key), displayKey: input.key, viewer: "usr_julius" }), + Effect.succeed({ + ...issue(input.key), + url: `https://linear.app/acme/issue/${input.key}/canonical-title`, + displayKey: input.key, + viewer: "usr_julius", + }), ...overrides, }), kind: LINEAR, @@ -48,6 +53,18 @@ function fakeStandaloneTracker( scopeNoun: "team", credential: { label: "Personal API key", helpUrl: null }, }, + parseIssueUrl: (url) => { + const links = new Map([ + ["https://linear.app/acme/issue/ENG-1/old-title", "acme"], + ["https://linear.app/acme/issue/ENG-1/issue-title", "acme"], + ["https://linear.app/acme/issue/ENG-1/canonical-title", "acme"], + ["https://linear.app/other-workspace/issue/ENG-1/old-title", "other-workspace"], + ]); + const workspace = links.get(url.href); + return workspace === undefined + ? null + : { host: "linear.app", workspace, scope: "ENG", key: "ENG-1" }; + }, probeCredential: () => Effect.succeed({ account: "julius", scopes: [] }), }; } @@ -437,6 +454,31 @@ describe("IssueService", () => { }); describe("IssueService with a standalone tracker", () => { + it.effect("rejects a URL from another workspace with the same team and issue key", () => + Effect.gen(function* () { + const service = yield* makeService({ + projects: [project({ id: "web" })], + trackers: [fakeStandaloneTracker()], + bindings: { web: { tracker: LINEAR, scope: "ENG" } }, + }); + expect( + yield* service.resolveUrl("https://linear.app/acme/issue/ENG-1/old-title"), + ).toMatchObject({ key: "ENG-1", writable: true }); + const refused = yield* service + .resolveUrl("https://linear.app/other-workspace/issue/ENG-1/old-title") + .pipe(Effect.result); + expect(refused).toMatchObject({ + _tag: "Failure", + failure: { + _tag: "IssueOperationError", + operation: "resolveUrl", + reason: "not-found", + detail: "This issue URL does not belong to the connected workspace.", + }, + }); + }), + ); + it.effect("lists a bound project's standalone scope beside its git host and serves both", () => Effect.gen(function* () { const listed: string[] = []; @@ -484,6 +526,21 @@ describe("IssueService with a standalone tracker", () => { }), ).toMatchObject({ host: "github.com", writable: true }); + const resolved = yield* bound + .resolveUrl("https://linear.app/acme/issue/ENG-1/issue-title", "web" as ProjectId) + .pipe(Effect.result); + expect(resolved).toMatchObject({ + _tag: "Success", + success: { + projectId: "web", + tracker: LINEAR, + host: "linear.app", + scope: "ENG", + key: "ENG-1", + writable: true, + }, + }); + // Unbound, the standalone scope is gone and its references are unsupported. listed.length = 0; const unbound = yield* makeService({ projects, trackers }); diff --git a/apps/server/src/issue/IssueService.ts b/apps/server/src/issue/IssueService.ts index a53aeb698ce89..389745dbf4b06 100644 --- a/apps/server/src/issue/IssueService.ts +++ b/apps/server/src/issue/IssueService.ts @@ -36,7 +36,9 @@ import { IssueTrackerError, isStandaloneIssueTracker, type IssueTrackerApi, + type StandaloneIssueTrackerApi, } from "@t3tools/source-control-core/server/IssueTracker"; +import { parseUrl } from "@t3tools/shared/url"; import * as IssueTrackerBindings from "../persistence/IssueTrackerBindings.ts"; import * as ProjectService from "../project/ProjectService.ts"; @@ -91,6 +93,16 @@ function resolveGitHostBinding(project: OrchestrationProjectShell): ProjectTrack }; } +function findStandaloneIssueUrl(trackers: ReadonlyArray, value: string) { + const url = parseUrl(value); + if (url === null) return null; + for (const api of trackers) { + const link = api.parseIssueUrl(url); + if (link !== null) return { api, tracker: api.kind, link }; + } + return null; +} + /** Lower case, so a scope keeps its cursor whichever project's spelling of it is read. */ function listCursorKey(host: string, scope: string): string { return `${host} ${scope.toLowerCase()}`; @@ -145,15 +157,14 @@ export class IssueService extends Context.Service< readonly update: (input: IssueUpdateInput) => Effect.Effect; readonly setState: (input: IssueSetStateInput) => Effect.Effect; readonly react: (input: IssueReactionInput) => Effect.Effect; - /** - * The reference behind an issue URL: served by the project whose binding is that host and - * scope, else read-only through any project on the same tracker and host. `preferredProjectId` - * wins among equals, so a thread's own project serves its own issues. - */ /** The tracker and container a project's own issues live in. */ readonly projectScope: ( projectId: ProjectId, ) => Effect.Effect; + /** + * Resolve through a matching project, preferring `preferredProjectId`; other scopes are read-only. + * Standalone links must match the workspace in the issue's canonical URL from the connected tracker. + */ readonly resolveUrl: ( url: string, preferredProjectId?: ProjectId, @@ -621,7 +632,12 @@ const make = Effect.gen(function* () { const resolveUrl: IssueService["Service"]["resolveUrl"] = Effect.fn("IssueService.resolveUrl")( function* (url, preferredProjectId) { - const parsed = sourceControlDefinitions.parseIssueUrl(url); + const gitHost = sourceControlDefinitions.parseIssueUrl(url); + const standalone = gitHost === null ? findStandaloneIssueUrl(registry.standalone, url) : null; + const parsed = + gitHost === null + ? standalone + : { tracker: IssueTrackerKind.make(gitHost.definition.kind), link: gitHost.link }; if (parsed === null) { return yield* new IssueOperationError({ operation: "resolveUrl", @@ -629,24 +645,42 @@ const make = Effect.gen(function* () { detail: "This is not a recognised issue URL.", }); } - const { definition, link } = parsed; + const { tracker, link } = parsed; // A link names its server's port; a binding does only when its remote or web URL did. const authority = new URL(url).host.toLowerCase(); const { supported } = yield* listProjects({ host: link.host, authority }); - const onHost = supported.filter( - (project) => project.tracker === IssueTrackerKind.make(definition.kind), - ); + const onHost = supported.filter((project) => project.tracker === tracker); const onAuthority = onHost.filter((project) => project.host === authority); 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), + onTracker.filter((project) => project.scope.toLowerCase() === link.scope.toLowerCase()), ); const lender = own ?? preferFirst(onTracker); if (lender === undefined) { return yield* new IssueUnavailableError({ reason: "tracker-unsupported" }); } + if (standalone !== null) { + // Team keys repeat across workspaces; only the credential's canonical issue URL identifies one. + const issue = yield* lender.api + .getIssue({ + cwd: lender.project.workspaceRoot, + host: lender.host, + scope: link.scope, + key: link.key, + }) + .pipe(Effect.mapError(toIssueError("resolveUrl"))); + const canonicalUrl = parseUrl(issue.url); + const canonical = canonicalUrl === null ? null : standalone.api.parseIssueUrl(canonicalUrl); + if (canonical === null || canonical.workspace !== standalone.link.workspace) { + return yield* new IssueOperationError({ + operation: "resolveUrl", + reason: "not-found", + detail: "This issue URL does not belong to the connected workspace.", + }); + } + } return { projectId: lender.project.id, tracker: lender.tracker, diff --git a/packages/issue-tracker-linear/src/server/LinearIssueTracker.test.ts b/packages/issue-tracker-linear/src/server/LinearIssueTracker.test.ts index 9490b449cce4d..ccaaa62d14888 100644 --- a/packages/issue-tracker-linear/src/server/LinearIssueTracker.test.ts +++ b/packages/issue-tracker-linear/src/server/LinearIssueTracker.test.ts @@ -127,6 +127,40 @@ function fakeLinear( const target = { cwd: "/w/web", host: "linear.app", scope: "ENG" }; describe("LinearIssueTracker", () => { + it.effect("parses Linear issue links with their workspace and team", () => + Effect.gen(function* () { + const api = yield* fakeLinear({}).tracker; + for (const suffix of ["", "/issue-title", "/old-title?view=all#comment"]) { + expect(api.parseIssueUrl(new URL(`https://linear.app/acme/issue/ENG-1${suffix}`))).toEqual({ + host: "linear.app", + workspace: "acme", + scope: "ENG", + key: "ENG-1", + }); + } + expect( + api.parseIssueUrl(new URL("https://linear.app/other-workspace/issue/ENG-1")), + ).toMatchObject({ + workspace: "other-workspace", + key: "ENG-1", + }); + for (const url of [ + "http://linear.app/acme/issue/ENG-1", + "https://example.com/acme/issue/ENG-1", + "https://linear.app.example.com/acme/issue/ENG-1", + "https://linear.app:444/acme/issue/ENG-1", + "https://linear.app/acme/team/ENG-1", + "https://linear.app//issue/ENG-1", + "https://linear.app/acme/issue/", + "https://linear.app/acme/issue/ENG-0", + "https://linear.app/acme/issue/eng-1", + "https://linear.app/acme/issue/ENG-1extra", + ]) { + expect(api.parseIssueUrl(new URL(url)), url).toBeNull(); + } + }), + ); + it.effect("lists a team's issues as summaries keyed by identifier", () => Effect.gen(function* () { const { tracker, requests } = fakeLinear({ diff --git a/packages/issue-tracker-linear/src/server/LinearIssueTracker.ts b/packages/issue-tracker-linear/src/server/LinearIssueTracker.ts index daa4954bdc0d4..9721a47471fdc 100644 --- a/packages/issue-tracker-linear/src/server/LinearIssueTracker.ts +++ b/packages/issue-tracker-linear/src/server/LinearIssueTracker.ts @@ -418,6 +418,13 @@ export const make: Effect.Effect { + if (url.protocol !== "https:" || url.host.toLowerCase() !== DESCRIPTOR.host) return null; + const [, workspace, resource, key] = url.pathname.split("/"); + if (!workspace || resource !== "issue" || key === undefined) return null; + const scope = /^([A-Z][A-Z0-9_]*)-[1-9][0-9]*$/.exec(key)?.[1]; + return scope === undefined ? null : { host: DESCRIPTOR.host, workspace, scope, key }; + }, probeCredential: (token) => linear.viewer(token).pipe( diff --git a/packages/source-control-core/src/server/IssueTracker.ts b/packages/source-control-core/src/server/IssueTracker.ts index e4fd95c287d4b..34b0c2e257d66 100644 --- a/packages/source-control-core/src/server/IssueTracker.ts +++ b/packages/source-control-core/src/server/IssueTracker.ts @@ -26,6 +26,7 @@ import type { StandaloneIssueTrackerDescriptor, } from "@t3tools/contracts"; import { IssueTrackerKind as IssueTrackerKindSchema } from "@t3tools/contracts"; +import type { IssueLink } from "../shared/definition.ts"; /** * The one failure shape every tracker reports, so the service can decide what a failure means @@ -196,6 +197,8 @@ export interface IssueTrackerApi { */ export interface StandaloneIssueTrackerApi extends IssueTrackerApi { readonly standalone: StandaloneIssueTrackerDescriptor; + /** Retains the workspace identity so canonical URLs can disambiguate repeated scope keys. */ + readonly parseIssueUrl: (url: URL) => (IssueLink & { readonly workspace: string }) | null; /** * Who a credential signs in as and which scopes it reaches, asked of the tracker itself. Run on * a credential before it is stored, and on the stored one to report connection status.