Create a review comment for a pull request

Creates a review comment in the pull request diff.

Script github Verified

by hugo697 ยท 10/25/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 367 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Create a review comment for a pull request
6
 * 
7
Creates a review comment in the pull request diff.
8
 */
9
export async function main(
10
  auth: Github,
11
  owner: string,
12
  repo: string,
13
  pull_number: string,
14
  body: {
15
    body: string;
16
    commit_id: string;
17
    in_reply_to?: number;
18
    line: number;
19
    path: string;
20
    position?: number;
21
    side?: "LEFT" | "RIGHT";
22
    start_line?: number;
23
    start_side?: "LEFT" | "RIGHT" | "side";
24
    [k: string]: unknown;
25
  }
26
) {
27
  const url = new URL(
28
    `https://api.github.com/repos/${owner}/${repo}/pulls/${pull_number}/comments`
29
  );
30

31
  const response = await fetch(url, {
32
    method: "POST",
33
    headers: {
34
      "Content-Type": "application/json",
35
      Authorization: "Bearer " + auth.token,
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