0

List Commits

by
Published today

List commits newest first, on a branch (sha) or touching a path, filtered by author and date range. Paginate with page.

Script github Verified

The script

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

3
/**
4
 * List Commits
5
 * List commits newest first, on a branch (sha) or touching a path, filtered by author and date range. Paginate with page.
6
 */
7
export async function main(
8
  auth: RT.Github,
9
  owner: string,
10
  repo: string,
11
  sha: string | undefined,
12
  path: string | undefined,
13
  author: string | undefined,
14
  since: string | undefined,
15
  until: string | undefined,
16
  per_page: number = 30,
17
  page: number = 1
18
) {
19
  const url = new URL(`https://api.github.com/repos/${owner}/${repo}/commits`)
20
  for (const [k, v] of Object.entries({
21
    sha,
22
    path,
23
    author,
24
    since,
25
    until,
26
    per_page,
27
    page,
28
  })) {
29
    if (v !== undefined && v !== "") url.searchParams.append(k, String(v))
30
  }
31

32
  const response = await fetch(url, {
33
    headers: {
34
      Authorization: `Bearer ${auth.token}`,
35
      Accept: "application/vnd.github+json",
36
      "X-GitHub-Api-Version": "2022-11-28",
37
    },
38
  })
39

40
  if (!response.ok) {
41
    throw new Error(`${response.status} ${await response.text()}`)
42
  }
43

44
  return await response.json()
45
}
46