Get a download artifact link

Return a redirect to the contents of a download artifact. This endpoint returns the actual file contents and not the artifact's metadata. $ curl -s -L https://api.bitbucket.org/2.0/repositories/evzijst/git-tests/downloads/hello.txt Hello World

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 a download artifact link
7
 * Return a redirect to the contents of a download artifact.
8

9
This endpoint returns the actual file contents and not the artifact's
10
metadata.
11

12
    $ curl -s -L https://api.bitbucket.org/2.0/repositories/evzijst/git-tests/downloads/hello.txt
13
    Hello World
14
 */
15
export async function main(
16
  auth: Bitbucket,
17
  filename: 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}/downloads/${filename}`
23
  );
24

25
  const response = await fetch(url, {
26
    method: "GET",
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