0
Create a variable for a repository
One script reply has been approved by the moderators Verified

Create a repository level variable.

Created by hugo697 563 days ago Viewed 21690 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 563 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Create a variable for a repository
7
 * Create a repository level variable.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  workspace: string,
12
  repo_slug: string,
13
  body: { type: string; [k: string]: unknown } & {
14
    uuid?: string;
15
    key?: string;
16
    value?: string;
17
    secured?: boolean;
18
    [k: string]: unknown;
19
  }
20
) {
21
  const url = new URL(
22
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/pipelines_config/variables`
23
  );
24

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