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
19 changes: 19 additions & 0 deletions apps/server/src/issue/IssueService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
type IssueTrackerApi,
type StandaloneIssueTrackerApi,
type TrackerIssueDetail,
type TrackerIssueLink,
} from "@t3tools/source-control-core/server/IssueTracker";
import { parseUrl } from "@t3tools/shared/url";

Expand Down Expand Up @@ -148,6 +149,7 @@ export class IssueService extends Context.Service<
{
readonly list: (input: IssueListInput) => Effect.Effect<IssueListResult, IssueError>;
readonly detail: (input: IssueRef) => Effect.Effect<IssueDetail, IssueError>;
readonly linkMetadata: (input: IssueRef) => Effect.Effect<TrackerIssueLink, IssueError>;
readonly comments: (
input: IssueCommentsPageInput,
) => Effect.Effect<IssueCommentsPage, IssueError>;
Expand Down Expand Up @@ -524,6 +526,22 @@ const make = Effect.gen(function* () {
),
);

const linkMetadata: IssueService["Service"]["linkMetadata"] = (input) =>
requireProject(input).pipe(
Effect.flatMap((project) => {
const read = project.api.getIssueLink ?? project.api.getIssue;
return read({
cwd: project.project.workspaceRoot,
host: project.host,
scope: project.scope,
key: input.key,
}).pipe(
Effect.map(({ title, url, displayKey }) => ({ title, url, displayKey })),
Effect.mapError(toIssueError("detail")),
);
}),
);

const comments: IssueService["Service"]["comments"] = (input) =>
requireProject(input).pipe(
Effect.flatMap((project) =>
Expand Down Expand Up @@ -796,6 +814,7 @@ const make = Effect.gen(function* () {
return IssueService.of({
list,
detail: traced("detail", detail),
linkMetadata: traced("linkMetadata", linkMetadata),
comments: traced("comments", comments),
templates: traced("templates", templates),
candidates: interactive(traced("candidates", candidates)),
Expand Down
1 change: 1 addition & 0 deletions apps/server/src/mcp/toolkits/issues/handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ const makeHarness = Effect.fn("makeIssuesToolkitHarness")(function* (
issueLayer ??
Layer.mock(IssueService.IssueService)({
detail: () => Effect.succeed(detail),
linkMetadata: () => Effect.succeed(detail),
projectScope: (projectId) =>
Effect.succeed({
projectId,
Expand Down
10 changes: 5 additions & 5 deletions apps/server/src/mcp/toolkits/issues/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,20 +269,20 @@ const make = Effect.gen(function* () {
link_issue: writesThread((input) =>
Effect.gen(function* () {
const thread = yield* requireThread(input.threadId);
const target = yield* resolveTarget({
const target: ResolvedTarget = yield* resolveTarget({
...input,
projectId: input.projectId ?? thread.projectId,
});
// Reading the issue proves it exists and names it for the thread's row.
const detail = yield* readIssue(target);
const url = target.url ?? detail.url;
const issue = target.issue ?? (yield* issues.linkMetadata(refOf(target)));
const url = target.url ?? issue.url;
const { alreadyLinked } = yield* threadIssues
.link({
threadId: thread.id,
ref: refOf(target),
url,
displayKey: detail.displayKey,
title: detail.title,
displayKey: issue.displayKey,
title: issue.title,
source: "agent",
})
.pipe(Effect.catchCause(linkFailure));
Expand Down
6 changes: 6 additions & 0 deletions packages/source-control-core/src/server/IssueTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export interface IssueTarget extends IssueScopeTarget {

/** An issue as the tracker sees it, before the service attaches project context. */
export type TrackerIssueSummary = Omit<IssueSummary, "tracker" | "host" | "scope">;
export type TrackerIssueLink = Pick<TrackerIssueSummary, "title" | "url" | "displayKey">;

export interface TrackerIssuePage {
readonly items: ReadonlyArray<TrackerIssueSummary>;
Expand Down Expand Up @@ -134,6 +135,11 @@ export interface IssueTrackerApi {
/** The issue with its first page of conversation, in one read where the tracker allows. */
readonly getIssue: (input: IssueTarget) => Effect.Effect<TrackerIssueDetail, IssueTrackerError>;

/** Confirms an issue exists and names it for a thread link. */
readonly getIssueLink?: (
input: IssueTarget,
) => Effect.Effect<TrackerIssueLink, IssueTrackerError>;

/** One page of older comments past a cursor `getIssue` or a previous page handed over. */
readonly getComments: (
input: IssueTarget & { readonly cursor: string },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,31 @@ const decodeBody = Schema.decodeUnknownSync(
);

describe("GitHubIssueTracker", () => {
it.effect("reads only the fields needed to link a GitHub issue and rejects missing issues", () =>
Effect.gen(function* () {
const requests: unknown[] = [];
const target = { cwd: "/w", host: "github.com", scope: "acme/web", key: "42" };
const url = "https://github.com/acme/web/issues/42";
const found = yield* tracker((input) => requests.push(input), {
issue: { number: 42, title: "Fixture issue", url },
});
expect(yield* found.getIssueLink(target)).toEqual({
displayKey: "#42",
title: "Fixture issue",
url,
});

const missing = yield* tracker((input) => requests.push(input), { issue: null });
expect(yield* Effect.flip(missing.getIssueLink(target))).toMatchObject({
reason: "not-found",
});
expect(requests).toEqual([
{ owner: "acme", name: "web", number: 42 },
{ owner: "acme", name: "web", number: 42 },
]);
}),
);

it.effect("writes an issue lifecycle without mutating pull requests or foreign comments", () =>
Effect.gen(function* () {
const writes: unknown[] = [];
Expand Down
10 changes: 10 additions & 0 deletions packages/source-control-github/src/server/GitHubIssueTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ export const make = Effect.gen(function* () {
};
}),

getIssueLink: Effect.fn("GitHubIssueTracker.getIssueLink")(function* (input) {
const { owner, name, number } = yield* issue(input, "getIssue");
const result = yield* client
.IssueLinkMetadata({ owner, name, number })
.pipe(graphql(input.host, "getIssue"));
const found = result.repository?.issue;
if (found == null) return yield* notFound("getIssue");
return { displayKey: `#${found.number}`, title: found.title, url: found.url };
}),

getComments: (input) =>
issue(input, "getComments").pipe(
Effect.flatMap(({ owner, name, number }) =>
Expand Down
10 changes: 10 additions & 0 deletions packages/source-control-github/src/server/issues.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,16 @@ query IssueComments($owner: String!, $name: String!, $number: Int!, $cursor: Str
}
}

query IssueLinkMetadata($owner: String!, $name: String!, $number: Int!) {
repository(owner: $owner, name: $name) {
issue(number: $number) {
number
title
url
}
}
}

query IssueNodeId($owner: String!, $name: String!, $number: Int!) {
repository(owner: $owner, name: $name) {
issue(number: $number) {
Expand Down
48 changes: 47 additions & 1 deletion packages/source-control-github/src/server/issues.graphql.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading