Update worklog

Updates a worklog.

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
 * Update worklog
8
 * Updates a worklog.
9
 */
10
export async function main(
11
  auth: Jira,
12
  issueIdOrKey: string,
13
  id: string,
14
  notifyUsers: string | undefined,
15
  adjustEstimate: "new" | "leave" | "manual" | "auto" | undefined,
16
  newEstimate: 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/${id}`
75
  );
76
  for (const [k, v] of [
77
    ["notifyUsers", notifyUsers],
78
    ["adjustEstimate", adjustEstimate],
79
    ["newEstimate", newEstimate],
80
    ["expand", expand],
81
    ["overrideEditableFlag", overrideEditableFlag],
82
  ]) {
83
    if (v !== undefined && v !== "") {
84
      url.searchParams.append(k, v);
85
    }
86
  }
87
  const response = await fetch(url, {
88
    method: "PUT",
89
    headers: {
90
      "Content-Type": "application/json",
91
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
92
    },
93
    body: JSON.stringify(body),
94
  });
95
  if (!response.ok) {
96
    const text = await response.text();
97
    throw new Error(`${response.status} ${text}`);
98
  }
99
  return await response.json();
100
}
101