Create a variable for a workspace

Create a workspace level variable.

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
 * Create a variable for a workspace
7
 * Create a workspace level variable.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  workspace: string,
12
  body: { type: string; [k: string]: unknown } & {
13
    uuid?: string;
14
    key?: string;
15
    value?: string;
16
    secured?: boolean;
17
    [k: string]: unknown;
18
  }
19
) {
20
  const url = new URL(
21
    `https://api.bitbucket.org/2.0/workspaces/${workspace}/pipelines-config/variables`
22
  );
23

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