0

Get Pull Request Files

by
Published today

List the files changed in a pull request, with per-file additions, deletions and patch. Paginate with page (max 3000 files).

Script github Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 1 hour ago
1
//native
2

3
/**
4
 * Get Pull Request Files
5
 * List the files changed in a pull request, with per-file additions, deletions and patch. Paginate with page (max 3000 files).
6
 */
7
export async function main(
8
  auth: RT.Github,
9
  owner: string,
10
  repo: string,
11
  pull_number: number,
12
  per_page: number = 30,
13
  page: number = 1
14
) {
15
  const url = new URL(
16
    `https://api.github.com/repos/${owner}/${repo}/pulls/${pull_number}/files`
17
  )
18
  url.searchParams.append("per_page", String(per_page))
19
  url.searchParams.append("page", String(page))
20

21
  const response = await fetch(url, {
22
    headers: {
23
      Authorization: `Bearer ${auth.token}`,
24
      Accept: "application/vnd.github+json",
25
      "X-GitHub-Api-Version": "2022-11-28",
26
    },
27
  })
28

29
  if (!response.ok) {
30
    throw new Error(`${response.status} ${await response.text()}`)
31
  }
32

33
  return await response.json()
34
}
35