Add worklog

Adds a worklog to an issue.

Script jira Verified

by hugo697 ยท 11/2/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 396 days ago
1
type Jira = {
2
  username: string;
3
  password: string;
4
  domain: string;
5
};
6
/**
7
 * Add worklog
8
 * Adds a worklog to an issue.
9
 */
10
export async function main(
11
  auth: Jira,
12
  issueIdOrKey: string,
13
  notifyUsers: string | undefined,
14
  adjustEstimate: "new" | "leave" | "manual" | "auto" | undefined,
15
  newEstimate: string | undefined,
16
  reduceBy: string | undefined,
17
  expand: string | undefined,
18
  overrideEditableFlag: string | undefined,
19
  body: {
20
    author?: {
21
      accountId?: string;
22
      accountType?: string;
23
      active?: boolean;
24
      avatarUrls?: {
25
        "16x16"?: string;
26
        "24x24"?: string;
27
        "32x32"?: string;
28
        "48x48"?: string;
29
      };
30
      displayName?: string;
31
      emailAddress?: string;
32
      key?: string;
33
      name?: string;
34
      self?: string;
35
      timeZone?: string;
36
    };
37
    comment?: string;
38
    created?: string;
39
    id?: string;
40
    issueId?: string;
41
    properties?: { key?: string; value?: { [k: string]: unknown } }[];
42
    self?: string;
43
    started?: string;
44
    timeSpent?: string;
45
    timeSpentSeconds?: number;
46
    updateAuthor?: {
47
      accountId?: string;
48
      accountType?: string;
49
      active?: boolean;
50
      avatarUrls?: {
51
        "16x16"?: string;
52
        "24x24"?: string;
53
        "32x32"?: string;
54
        "48x48"?: string;
55
      };
56
      displayName?: string;
57
      emailAddress?: string;
58
      key?: string;
59
      name?: string;
60
      self?: string;
61
      timeZone?: string;
62
    };
63
    updated?: string;
64
    visibility?: {
65
      identifier?: string;
66
      type?: "group" | "role";
67
      value?: string;
68
      [k: string]: unknown;
69
    };
70
    [k: string]: unknown;
71
  }
72
) {
73
  const url = new URL(
74
    `https://${auth.domain}.atlassian.net/rest/api/2/issue/${issueIdOrKey}/worklog`
75
  );
76
  for (const [k, v] of [
77
    ["notifyUsers", notifyUsers],
78
    ["adjustEstimate", adjustEstimate],
79
    ["newEstimate", newEstimate],
80
    ["reduceBy", reduceBy],
81
    ["expand", expand],
82
    ["overrideEditableFlag", overrideEditableFlag],
83
  ]) {
84
    if (v !== undefined && v !== "") {
85
      url.searchParams.append(k, v);
86
    }
87
  }
88
  const response = await fetch(url, {
89
    method: "POST",
90
    headers: {
91
      "Content-Type": "application/json",
92
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
93
    },
94
    body: JSON.stringify(body),
95
  });
96
  if (!response.ok) {
97
    const text = await response.text();
98
    throw new Error(`${response.status} ${text}`);
99
  }
100
  return await response.json();
101
}
102