Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 46 additions & 15 deletions apps/server/src/issue/IssueService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function makeService(input: {
readonly projects: ReadonlyArray<OrchestrationProjectShell>;
readonly trackers: ReadonlyArray<IssueTrackerApi>;
readonly bindings?: Record<string, IssueTrackerProjectBinding>;
readonly bindingsFailure?: PersistenceSqlError;
readonly readBindings?: IssueTrackerBindings.IssueTrackerBindingsRepository["Service"]["list"];
}) {
return IssueService.IssueService.pipe(
Effect.provide(
Expand All @@ -165,16 +165,15 @@ function makeService(input: {
Layer.mergeAll(
Layer.mock(IssueTrackerBindings.IssueTrackerBindingsRepository)({
list:
input.bindingsFailure !== undefined
? Effect.fail(input.bindingsFailure)
: Effect.succeed(
new Map(
Object.entries(input.bindings ?? {}).map(([id, binding]) => [
id as ProjectId,
binding,
]),
),
),
input.readBindings ??
Effect.succeed(
new Map(
Object.entries(input.bindings ?? {}).map(([id, binding]) => [
id as ProjectId,
binding,
]),
),
),
}),
Layer.succeed(
IssueTrackerRegistry.IssueTrackerRegistry,
Expand Down Expand Up @@ -510,6 +509,36 @@ describe("IssueService", () => {
});

describe("IssueService with a standalone tracker", () => {
it.effect("reads bindings once per listing and sees changes on the next listing", () =>
Effect.gen(function* () {
const web = project({ id: "web", repository: "acme/web" });
const docs = project({ id: "docs", repository: "acme/docs" });
let reads = 0;
let scope = "ENG";
const service = yield* makeService({
projects: [web, docs],
trackers: [fakeTracker(), fakeStandaloneTracker()],
readBindings: Effect.sync(() => {
reads += 1;
return new Map([[web.id, { tracker: LINEAR, scope }]]);
}),
});

const first = yield* service.list({ state: "open" });
expect(first.scopes?.map((entry) => entry.scope)).toEqual(["ENG", "acme/web", "acme/docs"]);
expect(reads).toBe(1);

scope = "OPS";
const next = yield* service.list({ state: "open", refresh: true });
expect(next.scopes?.map((entry) => entry.scope)).toEqual(["OPS", "acme/web", "acme/docs"]);
expect(reads).toBe(2);

const missing = yield* service.list({ state: "open", projectId: "missing" as ProjectId });
expect(missing.scopes).toEqual([]);
expect(reads).toBe(2);
}),
);

it.effect("rejects a URL from another workspace with the same team and issue key", () =>
Effect.gen(function* () {
const service = yield* makeService({
Expand Down Expand Up @@ -643,10 +672,12 @@ describe("IssueService with a standalone tracker", () => {
const unreadable = yield* makeService({
projects,
trackers,
bindingsFailure: new PersistenceSqlError({
operation: "IssueTrackerBindingsRepository.list",
detail: "disk I/O error",
}),
readBindings: Effect.fail(
new PersistenceSqlError({
operation: "IssueTrackerBindingsRepository.list",
detail: "disk I/O error",
}),
),
});
expect(yield* Effect.flip(unreadable.list({ state: "open" }))).toMatchObject({
_tag: "IssueOperationError",
Expand Down
86 changes: 43 additions & 43 deletions apps/server/src/issue/IssueService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
type IssueSetStateInput,
type IssueTemplateList,
type IssueTrackerSummary,
type IssueTrackerProjectBinding,
type IssueUpdateInput,
type OrchestrationProjectShell,
type ProjectId,
Expand Down Expand Up @@ -189,50 +190,32 @@ const make = Effect.gen(function* () {
const repositoryIdentities = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
const bindings = yield* IssueTrackerBindings.IssueTrackerBindingsRepository;

// Read per resolution so a binding saved in Settings applies without a restart.
const resolveStandaloneBinding = (
project: OrchestrationProjectShell,
): Effect.Effect<ProjectTrackerBinding | null, IssueOperationError> =>
bindings.list.pipe(
Effect.map((bound) => {
const binding = bound.get(project.id);
const api = binding === undefined ? null : registry.get(binding.tracker);
if (binding === undefined || api === null || !isStandaloneIssueTracker(api)) return null;
return {
tracker: binding.tracker,
host: api.standalone.host,
scope: binding.scope,
};
}),
// A failed read is not an unbound project: that would list the git host's issues alone.
Effect.mapError(
(cause) =>
new IssueOperationError({
operation: "listProjects",
detail: "The project's issue tracker binding could not be read.",
cause,
}),
),
);
const resolveStandaloneBinding = (binding: IssueTrackerProjectBinding | undefined) => {
if (binding === undefined) return null;
const api = registry.get(binding.tracker);
if (api === null || !isStandaloneIssueTracker(api)) return null;
return { tracker: binding.tracker, host: api.standalone.host, scope: binding.scope };
};

/**
* Where a project's issues live: its bound standalone tracker, its git host's, or both. The
* tracker bound in Settings leads, as the one the user chose for the project.
*/
const resolveProjectTracker = (
project: OrchestrationProjectShell,
): Effect.Effect<ReadonlyArray<ProjectTrackerBinding>, IssueOperationError> =>
Effect.all([
resolveStandaloneBinding(project),
(project.repositoryIdentity != null
? Effect.succeed(project.repositoryIdentity)
: repositoryIdentities.resolve(project.workspaceRoot)
).pipe(
Effect.map((repositoryIdentity) =>
binding: IssueTrackerProjectBinding | undefined,
) =>
(project.repositoryIdentity != null
? Effect.succeed(project.repositoryIdentity)
: repositoryIdentities.resolve(project.workspaceRoot)
).pipe(
Effect.map((repositoryIdentity) =>
[
resolveStandaloneBinding(binding),
resolveGitHostBinding({ ...project, repositoryIdentity }),
),
].filter((candidate) => candidate !== null),
),
]).pipe(Effect.map((resolved) => resolved.filter((binding) => binding !== null)));
);

const listProjects = (filter: {
readonly projectId?: IssueListInput["projectId"];
Expand All @@ -252,15 +235,32 @@ const make = Effect.gen(function* () {
cause,
}),
),
Effect.flatMap((shells) =>
Effect.forEach(
shells,
(project) =>
resolveProjectTracker(project).pipe(
Effect.map((bindings) => bindings.map((binding) => ({ project, ...binding }))),
Effect.flatMap(
Effect.fnUntraced(function* (shells) {
if (shells.length === 0) return [];
// One read per listing, not per project, and never cached so Settings changes apply.
const bound = yield* bindings.list.pipe(
// A failed read is not an unbound project: that would list the git host's issues alone.
Effect.mapError(
(cause) =>
new IssueOperationError({
operation: "listProjects",
detail: "The project's issue tracker binding could not be read.",
cause,
}),
),
{ concurrency: SCOPE_CONCURRENCY },
),
);
return yield* Effect.forEach(
shells,
(project) =>
resolveProjectTracker(project, bound.get(project.id)).pipe(
Effect.map((candidates) =>
candidates.map((candidate) => ({ project, ...candidate })),
),
),
{ concurrency: SCOPE_CONCURRENCY },
);
}),
),
Effect.map((bound) => {
const host = filter.host?.trim().toLowerCase();
Expand Down
Loading