Get attachment for an issue

Returns the contents of the specified file attachment. Note that this endpoint does not return a JSON response, but instead returns a redirect pointing to the actual file that in turn will return the raw contents. The redirect URL contains a one-time token that has a limited lifetime. As a result, the link should not be persisted, stored, or shared.

Script bitbucket Verified

by hugo697 ยท 10/24/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 375 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Get attachment for an issue
7
 * Returns the contents of the specified file attachment.
8

9
Note that this endpoint does not return a JSON response, but instead
10
returns a redirect pointing to the actual file that in turn will return
11
the raw contents.
12

13
The redirect URL contains a one-time token that has a limited lifetime.
14
As a result, the link should not be persisted, stored, or shared.
15
 */
16
export async function main(
17
  auth: Bitbucket,
18
  issue_id: string,
19
  path: string,
20
  repo_slug: string,
21
  workspace: string
22
) {
23
  const url = new URL(
24
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/issues/${issue_id}/attachments/${path}`
25
  );
26

27
  const response = await fetch(url, {
28
    method: "GET",
29
    headers: {
30
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
31
    },
32
    body: undefined,
33
  });
34
  if (!response.ok) {
35
    const text = await response.text();
36
    throw new Error(`${response.status} ${text}`);
37
  }
38
  return await response.text();
39
}
40