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