0
List a commit's comments
One script reply has been approved by the moderators Verified

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.

Created by hugo697 456 days ago Viewed 12000 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 456 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