0
Create a commit comment
One script reply has been approved by the moderators Verified

Create a comment for a commit using its :commit_sha.

Created by hugo697 200 days ago Viewed 6165 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 200 days ago
1
type Github = {
2
  token: string;
3
};
4
/**
5
 * Create a commit comment
6
 * Create a comment for a commit using its `:commit_sha`.
7
 */
8
export async function main(
9
  auth: Github,
10
  owner: string,
11
  repo: string,
12
  commit_sha: string,
13
  body: {
14
    body: string;
15
    line?: number;
16
    path?: string;
17
    position?: number;
18
    [k: string]: unknown;
19
  }
20
) {
21
  const url = new URL(
22
    `https://api.github.com/repos/${owner}/${repo}/commits/${commit_sha}/comments`
23
  );
24

25
  const response = await fetch(url, {
26
    method: "POST",
27
    headers: {
28
      "Content-Type": "application/json",
29
      Authorization: "Bearer " + auth.token,
30
    },
31
    body: JSON.stringify(body),
32
  });
33
  if (!response.ok) {
34
    const text = await response.text();
35
    throw new Error(`${response.status} ${text}`);
36
  }
37
  return await response.json();
38
}
39