List review comments on a pull request

Lists all review comments for a pull request. By default, review comments are in ascending order by ID.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 366 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * List review comments on a pull request
6
 * Lists all review comments for a pull request. By default, review comments are in ascending order by ID.
7
 */
8
export async function main(
9
  auth: Github,
10
  owner: string,
11
  repo: string,
12
  pull_number: string,
13
  sort: "created" | "updated" | undefined,
14
  direction: "asc" | "desc" | undefined,
15
  since: string | undefined,
16
  per_page: string | undefined,
17
  page: string | undefined
18
) {
19
  const url = new URL(
20
    `https://api.github.com/repos/${owner}/${repo}/pulls/${pull_number}/comments`
21
  );
22
  for (const [k, v] of [
23
    ["sort", sort],
24
    ["direction", direction],
25
    ["since", since],
26
    ["per_page", per_page],
27
    ["page", page],
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: "Bearer " + auth.token,
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