Upload an attachment to an issue

Upload new issue attachments. To upload files, perform a `multipart/form-data` POST containing one or more file fields. When a file is uploaded with the same name as an existing attachment, then the existing file will be replaced.

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
 * Upload an attachment to an issue
7
 * Upload new issue attachments.
8

9
To upload files, perform a `multipart/form-data` POST containing one
10
or more file fields.
11

12
When a file is uploaded with the same name as an existing attachment,
13
then the existing file will be replaced.
14
 */
15
export async function main(
16
  auth: Bitbucket,
17
  issue_id: string,
18
  repo_slug: string,
19
  workspace: string
20
) {
21
  const url = new URL(
22
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/issues/${issue_id}/attachments`
23
  );
24

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