0
Update SSH key pair
One script reply has been approved by the moderators Verified

Create or update the repository SSH key pair. The private key will be set as a default SSH identity in your build container.

Created by hugo697 360 days ago Viewed 8996 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 360 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Update SSH key pair
7
 * Create or update the repository SSH key pair. The private key will be set as a default SSH identity in your build container.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  workspace: string,
12
  repo_slug: string,
13
  body: { type: string; [k: string]: unknown } & {
14
    private_key?: string;
15
    public_key?: string;
16
    [k: string]: unknown;
17
  }
18
) {
19
  const url = new URL(
20
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/pipelines_config/ssh/key_pair`
21
  );
22

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