0

Get a tree

by
Published Oct 25, 2023

Returns a single tree using the SHA1 value for that tree. If `truncated` is `true` in the response then the number of items in the `tree` array exceeded our maximum limit. If you need to fetch more items, use the non-recursive method of fetching trees, and fetch one sub-tree at a time. **Note**: The limit for the `tree` array is 100,000 entries with a maximum size of 7 MB when using the `recursive` parameter.

Script github
  • Submitted by hugo697 Typescript (fetch-only)
    Created 542 days ago
    1
    type Github = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Get a tree
    6
     * Returns a single tree using the SHA1 value for that tree.
    7
    
    
    8
    If `truncated` is `true` in the response then the number of items in the `tree` array exceeded our maximum limit. If you need to fetch more items, use the non-recursive method of fetching trees, and fetch one sub-tree at a time.
    9
    
    
    10
    
    
    11
    **Note**: The limit for the `tree` array is 100,000 entries with a maximum size of 7 MB when using the `recursive` parameter.
    12
     */
    13
    export async function main(
    14
      auth: Github,
    15
      owner: string,
    16
      repo: string,
    17
      tree_sha: string,
    18
      recursive: string | undefined
    19
    ) {
    20
      const url = new URL(
    21
        `https://api.github.com/repos/${owner}/${repo}/git/trees/${tree_sha}`
    22
      );
    23
      for (const [k, v] of [["recursive", recursive]]) {
    24
        if (v !== undefined && v !== "") {
    25
          url.searchParams.append(k, v);
    26
        }
    27
      }
    28
      const response = await fetch(url, {
    29
        method: "GET",
    30
        headers: {
    31
          Authorization: "Bearer " + auth.token,
    32
        },
    33
        body: undefined,
    34
      });
    35
      if (!response.ok) {
    36
        const text = await response.text();
    37
        throw new Error(`${response.status} ${text}`);
    38
      }
    39
      return await response.json();
    40
    }
    41