0

Search Issues with JQL

by
Published Nov 2, 2023

Search issues with a JQL query, e.g. `project = PROJ AND status = "In Progress" ORDER BY created DESC`. The query must be bounded (name a project, assignee, date range...). Returns `*navigable` fields unless `fields` says otherwise; when the response has a `nextPageToken`, pass it back to get the next page.

Script jira Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 4 hours ago
1
//native
2

3
/**
4
 * Search Issues with JQL
5
 * Search issues with a JQL query, e.g. `project = PROJ AND status = "In Progress" ORDER BY created DESC`. The query must be bounded (name a project, assignee, date range...). Returns `*navigable` fields unless `fields` says otherwise; when the response has a `nextPageToken`, pass it back to get the next page.
6
 */
7
export async function main(
8
  auth: RT.Jira,
9
  jql: string,
10
  fields: string[] | undefined,
11
  expand: string | undefined,
12
  max_results: number | undefined,
13
  next_page_token: string | undefined
14
) {
15
  const url = new URL(
16
    `https://${auth.domain}.atlassian.net/rest/api/3/search/jql`
17
  )
18

19
  const body: { [key: string]: any } = {
20
    jql,
21
    fields: fields?.length ? fields : ["*navigable"],
22
  }
23
  if (expand != null && expand !== "") {
24
    body.expand = expand
25
  }
26
  if (max_results != null) {
27
    body.maxResults = max_results
28
  }
29
  if (next_page_token != null && next_page_token !== "") {
30
    body.nextPageToken = next_page_token
31
  }
32

33
  const response = await fetch(url, {
34
    method: "POST",
35
    headers: {
36
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
37
      "Content-Type": "application/json",
38
      Accept: "application/json",
39
    },
40
    body: JSON.stringify(body),
41
  })
42

43
  if (!response.ok) {
44
    throw new Error(`${response.status} ${await response.text()}`)
45
  }
46

47
  return await response.json()
48
}
49

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 525 days ago
    1
    type Jira = {
    2
      username: string;
    3
      password: string;
    4
      domain: string;
    5
    };
    6
    /**
    7
     * Search for issues using JQL (POST)
    8
     * Searches for issues using [JQL](https://confluence.
    9
     */
    10
    export async function main(
    11
      auth: Jira,
    12
      body: {
    13
        expand?: string[];
    14
        fields?: string[];
    15
        fieldsByKeys?: boolean;
    16
        jql?: string;
    17
        maxResults?: number;
    18
        properties?: string[];
    19
        startAt?: number;
    20
        validateQuery?: "strict" | "warn" | "none" | "true" | "false";
    21
      }
    22
    ) {
    23
      const url = new URL(`https://${auth.domain}.atlassian.net/rest/api/2/search`);
    24
    
    
    25
      const response = await fetch(url, {
    26
        method: "POST",
    27
        headers: {
    28
          "Content-Type": "application/json",
    29
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    30
        },
    31
        body: JSON.stringify(body),
    32
      });
    33
      if (!response.ok) {
    34
        const text = await response.text();
    35
        throw new Error(`${response.status} ${text}`);
    36
      }
    37
      return await response.json();
    38
    }
    39