0

Get avatar image by ID

by
Published Nov 2, 2023

Returns a project or issue type avatar image by ID. This operation can be accessed anonymously. **[Permissions](#permissions) required:** * For system avatars, none. * For custom project avatars, *Browse projects* [project permission](https://confluence.atlassian.com/x/yodKLg) for the project the avatar belongs to. * For custom issue type avatars, *Browse projects* [project permission](https://confluence.atlassian.com/x/yodKLg) for at least one project the issue type is used in.

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 avatar image by ID
    8
     * Returns a project or issue type avatar image by ID.
    9
    
    
    10
    This operation can be accessed anonymously.
    11
    
    
    12
    **[Permissions](#permissions) required:**
    13
    
    
    14
     *  For system avatars, none.
    15
     *  For custom project avatars, *Browse projects* [project permission](https://confluence.atlassian.com/x/yodKLg) for the project the avatar belongs to.
    16
     *  For custom issue type avatars, *Browse projects* [project permission](https://confluence.atlassian.com/x/yodKLg) for at least one project the issue type is used in.
    17
     */
    18
    export async function main(
    19
      auth: Jira,
    20
      type: "issuetype" | "project",
    21
      id: string,
    22
      size: "xsmall" | "small" | "medium" | "large" | "xlarge" | undefined,
    23
      format: "png" | "svg" | undefined
    24
    ) {
    25
      const url = new URL(
    26
        `https://${auth.domain}.atlassian.net/rest/api/2/universal_avatar/view/type/${type}/avatar/${id}`
    27
      );
    28
      for (const [k, v] of [
    29
        ["size", size],
    30
        ["format", format],
    31
      ]) {
    32
        if (v !== undefined && v !== "") {
    33
          url.searchParams.append(k, v);
    34
        }
    35
      }
    36
      const response = await fetch(url, {
    37
        method: "GET",
    38
        headers: {
    39
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    40
        },
    41
        body: undefined,
    42
      });
    43
      if (!response.ok) {
    44
        const text = await response.text();
    45
        throw new Error(`${response.status} ${text}`);
    46
      }
    47
      return await response.json();
    48
    }
    49