Update a variable for a repository

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

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