-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitHubIssuesContextProvider.ts
100 lines (85 loc) · 2.31 KB
/
GitHubIssuesContextProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import {
ContextItem,
ContextProviderDescription,
ContextProviderExtras,
ContextSubmenuItem,
LoadSubmenuItemsArgs,
} from "../../index.js";
import { BaseContextProvider } from "../index.js";
class GitHubIssuesContextProvider extends BaseContextProvider {
static description: ContextProviderDescription = {
title: "issue",
displayTitle: "GitHub Issues",
description: "Reference GitHub issues",
type: "submenu",
};
async getContextItems(
query: string,
extras: ContextProviderExtras,
): Promise<ContextItem[]> {
const issueId = query;
const { Octokit } = await import("@octokit/rest");
const octokit = new Octokit({
auth: this.options?.githubToken,
request: {
fetch: extras.fetch,
},
});
const { owner, repo, issue_number } = JSON.parse(issueId);
const issue = await octokit.issues.get({
owner,
repo,
issue_number,
});
let content = `# GitHub Issue #${issue.data.number.toString()} in ${owner}/${repo}`;
const comments = await octokit.issues.listComments({
owner,
repo,
issue_number,
});
const parts = [
issue.data.body || "No description",
...comments.data.map((comment) => comment.body),
];
content += `\n\n${parts.join("\n\n---\n\n")}`;
return [
{
name: issue.data.title,
content,
description: `#${issue.data.number.toString()}`,
},
];
}
async loadSubmenuItems(
args: LoadSubmenuItemsArgs,
): Promise<ContextSubmenuItem[]> {
const { Octokit } = await import("@octokit/rest");
const octokit = new Octokit({
auth: this.options?.githubToken,
request: {
fetch: args.fetch,
},
});
const allIssues = [];
for (const repo of this.options?.repos) {
const issues = await octokit.issues.listForRepo({
owner: repo.owner,
repo: repo.repo,
state: repo.type || "open",
});
allIssues.push(
...issues.data.map((issue) => ({
title: issue.title,
description: `#${issue.number.toString()}`,
id: JSON.stringify({
owner: repo.owner,
repo: repo.repo,
issue_number: issue.number,
}),
})),
);
}
return allIssues;
}
}
export default GitHubIssuesContextProvider;