0
Search for issues using JQL (GET)
One script reply has been approved by the moderators Verified

Searches for issues using [JQL](https://confluence.

Created by hugo697 589 days ago Viewed 116513 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 589 days ago
1
type Jira = {
2
  username: string;
3
  password: string;
4
  domain: string;
5
};
6
/**
7
 * Search for issues using JQL (GET)
8
 * Searches for issues using [JQL](https://confluence.
9
 */
10
export async function main(
11
  auth: Jira,
12
  jql: string | undefined,
13
  startAt: string | undefined,
14
  maxResults: string | undefined,
15
  validateQuery: "strict" | "warn" | "none" | "true" | "false" | undefined,
16
  fields: string | undefined,
17
  expand: string | undefined,
18
  properties: string | undefined,
19
  fieldsByKeys: string | undefined
20
) {
21
  const url = new URL(`https://${auth.domain}.atlassian.net/rest/api/2/search`);
22
  for (const [k, v] of [
23
    ["jql", jql],
24
    ["startAt", startAt],
25
    ["maxResults", maxResults],
26
    ["validateQuery", validateQuery],
27
    ["fields", fields],
28
    ["expand", expand],
29
    ["properties", properties],
30
    ["fieldsByKeys", fieldsByKeys],
31
  ]) {
32
    if (v !== undefined && v !== "") {
33
      url.searchParams.append(k, v);
34
    }
35
  }
36
  const response = await fetch(url, {
37
    method: "GET",
38
    headers: {
39
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
40
    },
41
    body: undefined,
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49