Elffuss BraidHub

kikocis/continue public

19cfb6ec829a · 2.1 KiBRawPermalink
1import {
2 ContextItem,
3 ContextProviderDescription,
4 ContextProviderExtras,
5 ContextSubmenuItem,
6 LoadSubmenuItemsArgs,
7} from "../../../index.js";
8import { BaseContextProvider } from "../../index.js";
9
10import { JiraClient } from "./JiraClient.js";
11
12class JiraIssuesContextProvider extends BaseContextProvider {
13 static description: ContextProviderDescription = {
14 title: "jira",
15 displayTitle: "Jira Issues",
16 description: "Reference Jira issues",
17 type: "submenu",
18 };
19
20 private getApi() {
21 return new JiraClient({
22 domain: this.options.domain,
23 username: this.options.email,
24 password: this.options.token,
25 issueQuery: this.options.issueQuery,
26 apiVersion: this.options.apiVersion,
27 requestOptions: this.options.requestOptions,
28 maxResults: this.options.maxResults,
29 });
30 }
31
32 async getContextItems(
33 query: string,
34 extras: ContextProviderExtras,
35 ): Promise<ContextItem[]> {
36 const api = this.getApi();
37 const issue = await api.issue(query, extras.fetch);
38
39 const parts = [
40 `# Jira Issue ${issue.key}: ${issue.summary}`,
41 "## Description",
42 issue.description ?? "No description",
43 ];
44
45 if (issue.comments.length > 0) {
46 parts.push("## Comments");
47
48 parts.push(
49 ...issue.comments.map((comment) => {
50 return `### ${comment.author.displayName} on ${comment.created}\n\n${comment.body}`;
51 }),
52 );
53 }
54
55 const content = parts.join("\n\n");
56
57 return [
58 {
59 name: `${issue.key}: ${issue.summary}`,
60 content,
61 description: issue.key,
62 },
63 ];
64 }
65
66 async loadSubmenuItems(
67 args: LoadSubmenuItemsArgs,
68 ): Promise<ContextSubmenuItem[]> {
69 const api = await this.getApi();
70
71 try {
72 const issues = await api.listIssues(args.fetch);
73
74 return issues.map((issue) => ({
75 id: issue.id,
76 title: `${issue.key}: ${issue.summary}`,
77 description: "",
78 }));
79 } catch (ex) {
80 console.error(`Unable to get jira tickets: ${ex}`);
81 return [];
82 }
83 }
84}
85
86export default JiraIssuesContextProvider;