0

Get issue worklogs

by
Published Nov 2, 2023

Returns worklogs for an issue, starting from the oldest worklog or from the worklog started on or after a date and time.

Script jira
  • Submitted by hugo697 Typescript (fetch-only)
    Created 527 days ago
    1
    type Jira = {
    2
      username: string;
    3
      password: string;
    4
      domain: string;
    5
    };
    6
    /**
    7
     * Get issue worklogs
    8
     * Returns worklogs for an issue, starting from the oldest worklog or from the worklog started on or after a date and time.
    9
     */
    10
    export async function main(
    11
      auth: Jira,
    12
      issueIdOrKey: string,
    13
      startAt: string | undefined,
    14
      maxResults: string | undefined,
    15
      startedAfter: string | undefined,
    16
      startedBefore: string | undefined,
    17
      expand: string | undefined
    18
    ) {
    19
      const url = new URL(
    20
        `https://${auth.domain}.atlassian.net/rest/api/2/issue/${issueIdOrKey}/worklog`
    21
      );
    22
      for (const [k, v] of [
    23
        ["startAt", startAt],
    24
        ["maxResults", maxResults],
    25
        ["startedAfter", startedAfter],
    26
        ["startedBefore", startedBefore],
    27
        ["expand", expand],
    28
      ]) {
    29
        if (v !== undefined && v !== "") {
    30
          url.searchParams.append(k, v);
    31
        }
    32
      }
    33
      const response = await fetch(url, {
    34
        method: "GET",
    35
        headers: {
    36
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    37
        },
    38
        body: undefined,
    39
      });
    40
      if (!response.ok) {
    41
        const text = await response.text();
    42
        throw new Error(`${response.status} ${text}`);
    43
      }
    44
      return await response.json();
    45
    }
    46