Update a comment on a snippet

Updates a comment. The only required field in the body is `content.raw`. Comments can only be updated by their author.

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 comment on a snippet
7
 * Updates a comment.
8

9
The only required field in the body is `content.raw`.
10

11
Comments can only be updated by their author.
12
 */
13
export async function main(
14
  auth: Bitbucket,
15
  comment_id: string,
16
  encoded_id: string,
17
  workspace: string,
18
  body: { type: string; [k: string]: unknown } & {
19
    links?: {
20
      self?: { href?: string; name?: string };
21
      html?: { href?: string; name?: string };
22
    };
23
    snippet?: { type: string; [k: string]: unknown } & {
24
      id?: number;
25
      title?: string;
26
      scm?: "git";
27
      created_on?: string;
28
      updated_on?: string;
29
      owner?: { type: string; [k: string]: unknown } & {
30
        links?: {
31
          avatar?: { href?: string; name?: string };
32
          [k: string]: unknown;
33
        };
34
        created_on?: string;
35
        display_name?: string;
36
        username?: string;
37
        uuid?: string;
38
        [k: string]: unknown;
39
      };
40
      creator?: { type: string; [k: string]: unknown } & {
41
        links?: {
42
          avatar?: { href?: string; name?: string };
43
          [k: string]: unknown;
44
        };
45
        created_on?: string;
46
        display_name?: string;
47
        username?: string;
48
        uuid?: string;
49
        [k: string]: unknown;
50
      };
51
      is_private?: boolean;
52
      [k: string]: unknown;
53
    };
54
    [k: string]: unknown;
55
  }
56
) {
57
  const url = new URL(
58
    `https://api.bitbucket.org/2.0/snippets/${workspace}/${encoded_id}/comments/${comment_id}`
59
  );
60

61
  const response = await fetch(url, {
62
    method: "PUT",
63
    headers: {
64
      "Content-Type": "application/json",
65
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
66
    },
67
    body: JSON.stringify(body),
68
  });
69
  if (!response.ok) {
70
    const text = await response.text();
71
    throw new Error(`${response.status} ${text}`);
72
  }
73
  return await response.json();
74
}
75