List a commit's comments

Returns the commit's comments. This includes both global and inline comments. The default sorting is oldest to newest and can be overridden with the `sort` query parameter.

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
 * List a commit's comments
7
 * Returns the commit's comments.
8

9
This includes both global and inline comments.
10

11
The default sorting is oldest to newest and can be overridden with
12
the `sort` query parameter.
13
 */
14
export async function main(
15
  auth: Bitbucket,
16
  commit: string,
17
  repo_slug: string,
18
  workspace: string,
19
  q: string | undefined,
20
  sort: string | undefined
21
) {
22
  const url = new URL(
23
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/commit/${commit}/comments`
24
  );
25
  for (const [k, v] of [
26
    ["q", q],
27
    ["sort", sort],
28
  ]) {
29
    if (v !== undefined && v !== "") {
30
      url.searchParams.append(k, v);
31
    }
32
  }
33
  const response = await fetch(url, {
34
    method: "GET",
35
    headers: {
36
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
37
    },
38
    body: undefined,
39
  });
40
  if (!response.ok) {
41
    const text = await response.text();
42
    throw new Error(`${response.status} ${text}`);
43
  }
44
  return await response.json();
45
}
46