Update a known host

Update a repository level known host.

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 known host
7
 * Update a repository level known host.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  workspace: string,
12
  repo_slug: string,
13
  known_host_uuid: string,
14
  body: { type: string; [k: string]: unknown } & {
15
    uuid?: string;
16
    hostname?: string;
17
    public_key?: { type: string; [k: string]: unknown } & {
18
      key_type?: string;
19
      key?: string;
20
      md5_fingerprint?: string;
21
      sha256_fingerprint?: string;
22
      [k: string]: unknown;
23
    };
24
    [k: string]: unknown;
25
  }
26
) {
27
  const url = new URL(
28
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/pipelines_config/ssh/known_hosts/${known_host_uuid}`
29
  );
30

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