| 1 | // @ts-ignore |
| 2 | import adf2md from "adf-to-md"; |
| 3 | |
| 4 | import { RequestOptions } from "../../../"; |
| 5 | |
| 6 | interface JiraClientOptions { |
| 7 | domain: string; |
| 8 | username: string; |
| 9 | password: string; |
| 10 | issueQuery?: string; |
| 11 | apiVersion?: string; |
| 12 | maxResults?: string; |
| 13 | requestOptions?: RequestOptions; |
| 14 | } |
| 15 | |
| 16 | interface JiraComment { |
| 17 | id: string; |
| 18 | created: string; |
| 19 | updated: string; |
| 20 | author: { |
| 21 | emailAddress: string; |
| 22 | displayName: string; |
| 23 | }; |
| 24 | body: object | string; |
| 25 | } |
| 26 | |
| 27 | interface JiraIssue { |
| 28 | id: string; |
| 29 | key: string; |
| 30 | fields: { |
| 31 | summary: string; |
| 32 | description: object | string; |
| 33 | comment: { |
| 34 | total: number; |
| 35 | comments: Array<JiraComment>; |
| 36 | }; |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | interface QueryResult { |
| 41 | id: string; |
| 42 | key: string; |
| 43 | summary: string; |
| 44 | } |
| 45 | |
| 46 | interface QueryResults { |
| 47 | issues: JiraIssue[]; |
| 48 | } |
| 49 | |
| 50 | export interface Comment { |
| 51 | created: string; |
| 52 | updated: string; |
| 53 | author: { |
| 54 | emailAddress: string; |
| 55 | displayName: string; |
| 56 | }; |
| 57 | body: string; |
| 58 | } |
| 59 | |
| 60 | export interface Issue { |
| 61 | key: string; |
| 62 | summary: string; |
| 63 | description?: string; |
| 64 | comments: Array<Comment>; |
| 65 | } |
| 66 | |
| 67 | export class JiraClient { |
| 68 | private readonly options: Required<JiraClientOptions>; |
| 69 | private baseUrl: string; |
| 70 | private authHeader; |
| 71 | constructor(options: JiraClientOptions) { |
| 72 | this.options = { |
| 73 | issueQuery: |
| 74 | "assignee = currentUser() AND resolution = Unresolved order by updated DESC", |
| 75 | apiVersion: "3", |
| 76 | requestOptions: {}, |
| 77 | maxResults: "50", |
| 78 | ...options, |
| 79 | }; |
| 80 | this.baseUrl = `https://${this.options.domain}/rest/api/${this.options.apiVersion}`; |
| 81 | this.authHeader = this.options.username |
| 82 | ? { |
| 83 | Authorization: `Basic ${btoa( |
| 84 | `${this.options.username}:${this.options.password}`, |
| 85 | )}`, |
| 86 | } |
| 87 | : { |
| 88 | Authorization: `Bearer ${this.options.password}`, |
| 89 | }; |
| 90 | } |
| 91 | |
| 92 | async issue( |
| 93 | issueId: string, |
| 94 | customFetch: (url: string | URL, init: any) => Promise<any>, |
| 95 | ): Promise<Issue> { |
| 96 | const result = {} as Issue; |
| 97 | |
| 98 | const response = await customFetch( |
| 99 | new URL( |
| 100 | this.baseUrl + `/issue/${issueId}?fields=description,comment,summary`, |
| 101 | ), |
| 102 | { |
| 103 | method: "GET", |
| 104 | headers: { |
| 105 | "Content-Type": "application/json", |
| 106 | ...this.authHeader, |
| 107 | }, |
| 108 | }, |
| 109 | ); |
| 110 | |
| 111 | const issue = (await response.json()) as any; |
| 112 | |
| 113 | result.key = issue.key; |
| 114 | result.summary = issue.fields.summary; |
| 115 | |
| 116 | if (typeof issue.fields.description === "string") { |
| 117 | result.description = issue.fields.description; |
| 118 | } else if (issue.fields.description) { |
| 119 | result.description = adf2md.validate(issue.fields.description).result; |
| 120 | } else { |
| 121 | result.description = ""; |
| 122 | } |
| 123 | |
| 124 | result.comments = |
| 125 | issue.fields.comment?.comments?.map((comment: any) => { |
| 126 | const body = |
| 127 | typeof comment.body === "string" |
| 128 | ? comment.body |
| 129 | : adf2md.validate(comment.body).result; |
| 130 | |
| 131 | return { |
| 132 | body, |
| 133 | author: comment.author, |
| 134 | created: comment.created, |
| 135 | updated: comment.updated, |
| 136 | }; |
| 137 | }) ?? []; |
| 138 | |
| 139 | return result; |
| 140 | } |
| 141 | |
| 142 | async listIssues( |
| 143 | customFetch: (url: string | URL, init: any) => Promise<any>, |
| 144 | ): Promise<Array<QueryResult>> { |
| 145 | const response = await customFetch( |
| 146 | new URL( |
| 147 | this.baseUrl + |
| 148 | `/search?fields=summary&jql=${ |
| 149 | this.options.issueQuery ?? |
| 150 | "assignee = currentUser() AND resolution = Unresolved order by updated DESC" |
| 151 | }&maxResults=${this.options.maxResults ?? "50"}`, |
| 152 | ), |
| 153 | { |
| 154 | method: "GET", |
| 155 | headers: { |
| 156 | "Content-Type": "application/json", |
| 157 | ...this.authHeader, |
| 158 | }, |
| 159 | }, |
| 160 | ); |
| 161 | |
| 162 | if (response.status === 500) { |
| 163 | const text = await response.text(); |
| 164 | console.warn( |
| 165 | "Unable to get Jira tickets. You may need to set 'apiVersion': 2 in your config.json. See full documentation here: https://docs.continue.dev/customize/context-providers#jira-datacenter-support\n\n", |
| 166 | text, |
| 167 | ); |
| 168 | return Promise.resolve([]); |
| 169 | } else if (response.status !== 200) { |
| 170 | const text = await response.text(); |
| 171 | console.warn("Unable to get Jira tickets: ", text); |
| 172 | return Promise.resolve([]); |
| 173 | } |
| 174 | |
| 175 | const data = (await response.json()) as any; |
| 176 | |
| 177 | return ( |
| 178 | data.issues?.map((issue: any) => ({ |
| 179 | id: issue.id, |
| 180 | key: issue.key, |
| 181 | summary: issue.fields.summary, |
| 182 | })) ?? [] |
| 183 | ); |
| 184 | } |
| 185 | } |