Set user property

Sets the value of a user's property. Use this resource to store custom data against a user. Note: This operation does not access the [user properties](https://confluence.atlassian.com/x/8YxjL) created and maintained in Jira. **[Permissions](#permissions) required:** * *Administer Jira* [global permission](https://confluence.atlassian.com/x/x4dKLg), to set a property on any user. * Access to Jira, to set a property on the calling user's record.

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
 * Set user property
8
 * Sets the value of a user's property. Use this resource to store custom data against a user.
9

10
Note: This operation does not access the [user properties](https://confluence.atlassian.com/x/8YxjL) created and maintained in Jira.
11

12
**[Permissions](#permissions) required:**
13

14
 *  *Administer Jira* [global permission](https://confluence.atlassian.com/x/x4dKLg), to set a property on any user.
15
 *  Access to Jira, to set a property on the calling user's record.
16
 */
17
export async function main(
18
  auth: Jira,
19
  propertyKey: string,
20
  accountId: string | undefined,
21
  userKey: string | undefined,
22
  username: string | undefined,
23
  body: { [k: string]: unknown }
24
) {
25
  const url = new URL(
26
    `https://${auth.domain}.atlassian.net/rest/api/2/user/properties/${propertyKey}`
27
  );
28
  for (const [k, v] of [
29
    ["accountId", accountId],
30
    ["userKey", userKey],
31
    ["username", username],
32
  ]) {
33
    if (v !== undefined && v !== "") {
34
      url.searchParams.append(k, v);
35
    }
36
  }
37
  const response = await fetch(url, {
38
    method: "PUT",
39
    headers: {
40
      "Content-Type": "application/json",
41
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
42
    },
43
    body: JSON.stringify(body),
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51