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
2 changes: 1 addition & 1 deletion plugins/linear/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ same connect.
| `linear_search` | Issues by free text, team, state, assignee. |
| `linear_create_issue` | A new issue in a team: title, description, state, assignee, labels, priority, parent. |
| `linear_comment` | A Markdown comment, signed with the thread id. |
| `linear_set_state` | Move an issue to a workflow state by name. |
| `linear_update_issue` | Change an issue's state, assignee, labels and priority together. |
| `linear_query` | Any read-only GraphQL query. Mutations are refused. |

Writes are deliberately named tools rather than a raw mutation tool, so the
Expand Down
5 changes: 4 additions & 1 deletion plugins/linear/linear.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ describe("parsing", () => {
it("reads mutation results and refuses failures", () => {
expect(parseCommentCreate({ data: { commentCreate: { success: true, comment: { url: "https://x" } } } })).toEqual({ url: "https://x" });
expect(() => parseCommentCreate({ data: { commentCreate: { success: false } } })).toThrow(/did not create/u);
expect(parseIssueUpdate({ data: { issueUpdate: { success: true, issue: { identifier: "ENG-1", state: { name: "Done" } } } } })).toEqual({ identifier: "ENG-1", state: "Done" });
expect(parseIssueUpdate({ data: { issueUpdate: { success: true, issue: { identifier: "ENG-1", state: { name: "Done" } } } } })).toEqual({ identifier: "ENG-1", state: "Done", assignee: null, labels: [], priority: null });
expect(
parseIssueUpdate({ data: { issueUpdate: { success: true, issue: { identifier: "ENG-1", state: { name: "Todo" }, assignee: { name: "Jane Doe", displayName: "jane" }, labels: { nodes: [{ name: "Bug" }] }, priority: 2 } } } }),
).toEqual({ identifier: "ENG-1", state: "Todo", assignee: "jane", labels: ["Bug"], priority: 2 });
expect(() => parseIssueUpdate({ data: { issueUpdate: { success: false, issue: null } } })).toThrow(/did not update/u);
});
});
Expand Down
68 changes: 55 additions & 13 deletions plugins/linear/linear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ const COMMENT_FIELDS = "comments(first: 50) { nodes { body createdAt url user {
export const ISSUE_QUERY = `query($id: String!) { issue(id: $id) { ${ISSUE_FIELDS} ${COMMENT_FIELDS} } }`;
export const ISSUES_QUERY = `query($filter: IssueFilter, $first: Int!) { issues(filter: $filter, first: $first, orderBy: updatedAt) { nodes { ${ISSUE_FIELDS} } } }`;
export const SEARCH_QUERY = `query($term: String!, $filter: IssueFilter, $first: Int!) { searchIssues(term: $term, filter: $filter, first: $first) { nodes { ${ISSUE_FIELDS} } } }`;
export const ISSUE_REF_QUERY = "query($id: String!) { issue(id: $id) { id identifier team { key states { nodes { id name type position } } } } }";
export const ISSUE_REF_QUERY =
"query($id: String!) { issue(id: $id) { id identifier labels { nodes { id name } } team { key states { nodes { id name type position } } labels(first: 250) { nodes { id name } } } } " +
"issueLabels(filter: { team: { null: true } }, first: 250) { nodes { id name } } }";
export const COMMENT_CREATE = "mutation($input: CommentCreateInput!) { commentCreate(input: $input) { success comment { url } } }";
export const ISSUE_UPDATE = "mutation($id: String!, $input: IssueUpdateInput!) { issueUpdate(id: $id, input: $input) { success issue { identifier state { name } } } }";
export const ISSUE_UPDATE = "mutation($id: String!, $input: IssueUpdateInput!) { issueUpdate(id: $id, input: $input) { success issue { identifier state { name } assignee { name displayName email } labels { nodes { name } } priority } } }";
export const ISSUE_CREATE = "mutation($input: IssueCreateInput!) { issueCreate(input: $input) { success issue { identifier url } } }";
/** A team by key, with the states and labels an issue in it can take. Workspace labels apply to every team. */
export const TEAM_QUERY =
Expand Down Expand Up @@ -170,19 +172,46 @@ export function parseIssueList(payload: unknown, field: "issues" | "searchIssues
return list.nodes;
}

export type IssueRef = { id: string; identifier: string; team: { key: string; states: Array<{ id: string; name: string; type: string; position: number }> } };
const labelNodes = z.object({ nodes: z.array(z.object({ id: z.string(), name: z.string() })) });

export type IssueRef = {
id: string;
identifier: string;
/** The labels on the issue now. */
labels: Array<{ id: string; name: string }>;
team: {
key: string;
states: Array<{ id: string; name: string; type: string; position: number }>;
/** The labels the issue may carry: the team's own and the workspace's. */
labels: Array<{ id: string; name: string }>;
};
};

export function parseIssueRef(payload: unknown): IssueRef | null {
const data = dataOf(payload);
if (data.issue === null || data.issue === undefined) return null;
const parsed = z
.object({
id: z.string(),
identifier: z.string(),
team: z.object({ key: z.string(), states: z.object({ nodes: z.array(z.object({ id: z.string(), name: z.string(), type: z.string(), position: z.number() })) }) }),
issue: z.object({
id: z.string(),
identifier: z.string(),
labels: labelNodes.nullish(),
team: z.object({
key: z.string(),
states: z.object({ nodes: z.array(z.object({ id: z.string(), name: z.string(), type: z.string(), position: z.number() })) }),
labels: labelNodes.nullish(),
}),
}),
issueLabels: labelNodes.nullish(),
})
.parse(data.issue);
return { id: parsed.id, identifier: parsed.identifier, team: { key: parsed.team.key, states: parsed.team.states.nodes } };
.parse(data);
const issue = parsed.issue;
return {
id: issue.id,
identifier: issue.identifier,
labels: issue.labels?.nodes ?? [],
team: { key: issue.team.key, states: issue.team.states.nodes, labels: [...(issue.team.labels?.nodes ?? []), ...(parsed.issueLabels?.nodes ?? [])] },
};
}

/** The state whose name matches, ignoring case; null when none does. */
Expand All @@ -198,13 +227,27 @@ export function parseCommentCreate(payload: unknown): { url: string | null } {
return { url: parsed.comment?.url ?? null };
}

export function parseIssueUpdate(payload: unknown): { identifier: string; state: string | null } {
export type IssueUpdated = { identifier: string; state: string | null; assignee: string | null; labels: string[]; priority: number | null };

export function parseIssueUpdate(payload: unknown): IssueUpdated {
const data = dataOf(payload);
const parsed = z
.object({ success: z.boolean(), issue: z.object({ identifier: z.string(), state: z.object({ name: z.string() }).nullish() }).nullish() })
.object({
success: z.boolean(),
issue: z
.object({
identifier: z.string(),
state: z.object({ name: z.string() }).nullish(),
assignee: person,
labels: z.object({ nodes: z.array(z.object({ name: z.string() })) }).nullish(),
priority: z.number().nullish(),
})
.nullish(),
})
.parse(data.issueUpdate);
if (!parsed.success || parsed.issue === null || parsed.issue === undefined) throw new Error("Linear did not update the issue");
return { identifier: parsed.issue.identifier, state: parsed.issue.state?.name ?? null };
const issue = parsed.issue;
return { identifier: issue.identifier, state: issue.state?.name ?? null, assignee: personName(issue.assignee), labels: (issue.labels?.nodes ?? []).map((l) => l.name), priority: issue.priority ?? null };
}

export function parseIssueCreate(payload: unknown): { identifier: string; url: string | null } {
Expand All @@ -225,8 +268,6 @@ export type Team = {
labels: Array<{ id: string; name: string }>;
};

const labelNodes = z.object({ nodes: z.array(z.object({ id: z.string(), name: z.string() })) });

export function parseTeam(payload: unknown): Team | null {
const data = dataOf(payload);
const parsed = z
Expand Down Expand Up @@ -281,6 +322,7 @@ export function parseUsers(payload: unknown): User[] {

/** Linear's priority numbers: 0 is none, 1 urgent, 4 low. */
export const PRIORITIES = { none: 0, urgent: 1, high: 2, medium: 3, low: 4 } as const;
export const PRIORITY_NAMES = Object.fromEntries(Object.entries(PRIORITIES).map(([name, n]) => [n, name])) as Record<number, keyof typeof PRIORITIES>;
export type PriorityName = keyof typeof PRIORITIES;

// ---- search filters --------------------------------------------------------
Expand Down
58 changes: 47 additions & 11 deletions plugins/linear/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ describe("tools", () => {
it("are registered with the names the skill documents", async () => {
const { harness } = await setup();
const names = harness.registrations.agentTools.map((t: { name: string }) => t.name).sort();
expect(names).toEqual(["linear_comment", "linear_create_issue", "linear_issue", "linear_query", "linear_search", "linear_set_state"]);
expect(names).toEqual(["linear_comment", "linear_create_issue", "linear_issue", "linear_query", "linear_search", "linear_update_issue"]);
});

it("say how to connect when there is no token", async () => {
Expand Down Expand Up @@ -233,22 +233,58 @@ describe("tools", () => {
expect(input.body).toMatch(/^PR opened: https:\/\/github\.com\/x\/y\/pull\/1\n\n_— from bb thread .+_$/u);
});

it("linear_set_state matches a state by name and lists the team's states otherwise", async () => {
it("linear_update_issue applies state, assignee, labels and priority together", async () => {
let lastInput: Record<string, unknown> = {};
const ref = issue({
labels: { nodes: [{ id: "l-bug", name: "Bug" }, { id: "l-ready", name: "build-ready" }] },
team: { ...(issue().team as object), labels: { nodes: [{ id: "l-bug", name: "Bug" }, { id: "l-ready", name: "build-ready" }, { id: "l-agent", name: "agent" }] } },
});
const { tool, text, linear } = await setup({
settings: { accessToken: "at-0" },
linear: {
answer: (body) =>
body.query.startsWith("mutation($id: String!, $input: IssueUpdateInput!)")
? { data: { issueUpdate: { success: true, issue: { identifier: "ENG-123", state: { name: "Done" } } } } }
: { data: { issue: issue() } },
answer: (body) => {
if (body.query.startsWith("mutation($id: String!, $input: IssueUpdateInput!)")) {
lastInput = body.variables!.input as Record<string, unknown>;
const who = lastInput.assigneeId === "u1" ? { displayName: "jane" } : lastInput.assigneeId === "u-bob" ? { name: "Bob" } : null;
const labels = Array.isArray(lastInput.labelIds) ? (lastInput.labelIds as string[]).map((id) => ({ name: id.replace(/^l-/u, "") })) : [{ name: "Bug" }, { name: "build-ready" }];
return { data: { issueUpdate: { success: true, issue: { identifier: "ENG-123", state: { name: lastInput.stateId === "s-done" ? "Done" : "Todo" }, assignee: who, labels: { nodes: labels }, priority: lastInput.priority ?? 0 } } } };
}
if (body.query.startsWith("query($who: String!) { users")) {
const who = String(body.variables!.who);
const all = [{ id: "u-bob", name: "Bob", displayName: "Bob", email: "bob@acme.test", active: true }, { id: "u-bobby", name: "Bobby", displayName: "Bobby", email: "bobby@acme.test", active: true }];
return { data: { users: { nodes: all.filter((u) => u.email === who || u.name.toLowerCase().includes(who.toLowerCase())) } } };
}
return { data: { issue: body.variables!.id === "ENG-999" ? null : ref, issueLabels: { nodes: [{ id: "l-ws", name: "Workspace" }] } } };
},
},
});
expect(text(await tool("linear_set_state", { key: "ENG-123", state: "done" }))).toBe("ENG-123 is now Done.");
expect(linear.calls.at(-1)!.body!.variables).toEqual({ id: "uuid-1", input: { stateId: "s-done" } });

const miss = await tool("linear_set_state", { key: "ENG-123", state: "Shipped" });
expect(miss).toMatchObject({ isError: true });
expect(text(miss)).toContain("Todo (unstarted), Done (completed)");
// Claiming a ticket: state and assignee in one mutation.
expect(text(await tool("linear_update_issue", { key: "eng-123", state: "done", assignee: "me" }))).toBe("ENG-123: now Done; assigned to jane.");
expect(linear.calls.at(-1)!.body!.variables).toEqual({ id: "uuid-1", input: { stateId: "s-done", assigneeId: "u1" } });

// Handing it back: unassign, relabel, reprioritize. Labels are the issue's current set, minus removed, plus added.
expect(text(await tool("linear_update_issue", { key: "ENG-123", assignee: "Nobody", removeLabels: ["BUILD-READY"], addLabels: ["agent", "workspace"], priority: "high" }))).toBe(
"ENG-123: unassigned; labels bug, agent, ws; priority high.",
);
expect(lastInput).toEqual({ assigneeId: null, labelIds: ["l-bug", "l-agent", "l-ws"], priority: 2 });

// A person by email.
expect(text(await tool("linear_update_issue", { key: "ENG-123", assignee: "bob@acme.test" }))).toBe("ENG-123: assigned to Bob.");
expect(lastInput).toEqual({ assigneeId: "u-bob" });

// Misses list the choices and change nothing.
const state = await tool("linear_update_issue", { key: "ENG-123", state: "Shipped" });
expect(state).toMatchObject({ isError: true });
expect(text(state)).toContain("Todo (unstarted), Done (completed)");
const label = await tool("linear_update_issue", { key: "ENG-123", addLabels: ["perf"] });
expect(label).toMatchObject({ isError: true });
expect(text(label)).toContain("Bug, build-ready, agent, Workspace");
const ambiguous = await tool("linear_update_issue", { key: "ENG-123", assignee: "bo" });
expect(ambiguous).toMatchObject({ isError: true });
expect(text(ambiguous)).toContain("Give an email");
expect(await tool("linear_update_issue", { key: "ENG-999", state: "Done" })).toMatchObject({ isError: true });
await expect(tool("linear_update_issue", { key: "ENG-123" })).rejects.toThrow(/at least one of/u);
});

it("linear_create_issue resolves the team, state, labels, assignee and parent, and signs the description", async () => {
Expand Down
Loading
Loading