Файли
openidea/src/features/github/issues/api.ts
T
l0ng-ai 71bfc70996 feat(github): browse and work on repository issues (#8077)
* feat(github): browse and work on repository issues

* fix(github): fix issue browse assignee filter, list keyboard navigation, and edge cases
2026-12-05 00:54:51 +08:00

168 рядки
4.9 KiB
TypeScript

import { graphql, repoVariables } from "../api/client";
import type { GHActor, GHConnection, GHRepositoryCoordinates } from "../api/types";
import { nameWithOwner } from "../data/repository";
export interface IssueRow {
number: number;
title: string;
state: "OPEN" | "CLOSED";
}
export interface IssueComment {
id: string;
author: GHActor | null;
body: string;
createdAt: string;
}
interface Issue extends IssueRow, IssueComment {
url: string;
locked: boolean;
labels: { nodes: { name: string; color: string }[] };
assignees: { nodes: { login: string }[] };
comments: GHConnection<IssueComment>;
}
const rowFields = "number title state";
const pageInfo = "pageInfo { hasNextPage endCursor }";
const numbered = `query($repoOwner:String!, $repoName:String!, $number:Int!) {
repository(owner:$repoOwner,name:$repoName) {
issueOrPullRequest(number:$number) { __typename ... on Issue { ${rowFields} } }
}
}`;
const browse = `query($repoOwner:String!, $repoName:String!, $states:[IssueState!], $filter:IssueFilters, $cursor:String) {
repository(owner:$repoOwner,name:$repoName) {
issues(first:50, after:$cursor, states:$states, filterBy:$filter, orderBy:{field:UPDATED_AT,direction:DESC}) {
nodes { ${rowFields} }
${pageInfo}
}
}
}`;
const search = `query($query:String!, $cursor:String) {
search(first:50, after:$cursor, type:ISSUE, query:$query) {
nodes { ... on Issue { ${rowFields} } }
${pageInfo}
}
}`;
export type IssueStateFilter = "OPEN" | "CLOSED" | "ALL";
export async function issueList(
repo: GHRepositoryCoordinates,
text: string,
state: IssueStateFilter,
assigned: string | null,
cursor: string | null,
) {
const term = text.trim();
const vars = repoVariables(repo);
if (/^#\d+$/.test(term)) {
const number = Number(term.slice(1));
if (!number || number > 2147483647) return { items: [], next: null };
const data = await graphql<{
repository: { issueOrPullRequest: (IssueRow & { __typename: string }) | null } | null;
}>(numbered, { ...vars, number });
const row = data.repository?.issueOrPullRequest;
return { items: row?.__typename === "Issue" ? [row] : [], next: null };
}
let page: GHConnection<IssueRow | null> | null;
if (term) {
const query = [
`repo:${nameWithOwner(repo)}`,
"is:issue",
"in:title",
"sort:updated-desc",
state !== "ALL" && `is:${state.toLowerCase()}`,
assigned && `assignee:${assigned}`,
JSON.stringify(term),
]
.filter(Boolean)
.join(" ");
page = (await graphql<{ search: GHConnection<IssueRow | null> }>(search, { query, cursor })).search;
} else {
page =
(
await graphql<{ repository: { issues: GHConnection<IssueRow | null> } | null }>(browse, {
...vars,
states: state === "ALL" ? null : [state],
filter: assigned ? { assignee: assigned } : null,
cursor,
})
).repository?.issues ?? null;
}
return {
items: (page?.nodes ?? []).filter((row): row is IssueRow => Boolean(row?.number)),
next: page?.pageInfo.hasNextPage && page.pageInfo.endCursor ? page.pageInfo.endCursor : null,
};
}
const details = `query($repoOwner:String!, $repoName:String!, $number:Int!, $cursor:String) {
repository(owner:$repoOwner,name:$repoName) {
issue(number:$number) {
${rowFields} id url body createdAt locked
author {
...actorInfo
}
labels(first:100) { nodes { name color } }
assignees(first:100) { nodes { login } }
comments(first:50, after:$cursor) {
nodes {
id body createdAt
author {
...actorInfo
}
}
${pageInfo}
}
}
}
}`;
export async function issueDetails(repo: GHRepositoryCoordinates, number: number, cursor: string | null) {
const data = await graphql<{ repository: { issue: Issue | null } | null }>(details, {
...repoVariables(repo),
number,
cursor,
});
const raw = data.repository?.issue;
const issue = raw
? {
...raw,
comments: {
...raw.comments,
nodes: raw.comments.nodes.filter((comment): comment is IssueComment => Boolean(comment?.id)),
},
}
: null;
return {
items: issue ? [issue] : [],
next:
issue?.comments.pageInfo.hasNextPage && issue.comments.pageInfo.endCursor
? issue.comments.pageInfo.endCursor
: null,
};
}
export async function postIssueComment(id: string, body: string) {
const result = await graphql<{ addComment: { commentEdge: { node: IssueComment } } }>(
`
mutation ($id: ID!, $body: String!) {
addComment(input: { subjectId: $id, body: $body }) {
commentEdge {
node {
id
body
createdAt
author {
...actorInfo
}
}
}
}
}
`,
{ id, body },
);
return result.addComment.commentEdge.node;
}