0
Get Repo Content
One script reply has been approved by the moderators Verified
Created by adam186 614 days ago Viewed 9663 times
0
Submitted by adam186 Deno
Verified 614 days ago
1
import { Octokit } from "https://cdn.skypack.dev/@octokit/rest";
2

3
/**
4
 * @param owner The account owner of the repository. The name is not case sensitive.
5
 *
6
 * @param repo The name of the repository. The name is not case sensitive.
7
 *
8
 * @param path The path to the file or directory.
9
 * If omitted, the contents of the repository's root directory will be returned.
10
 *
11
 * @param ref The name of the commit/branch/tag. Defaults to the default branch of the repository.
12
 *
13
 * @param result_format The kind of data to be returned. This controls how the result is structured.
14
 * Learn more at https://docs.github.com/en/rest/repos/contents#get-repository-content
15
 */
16
type Github = {
17
  token: string;
18
};
19
export async function main(
20
  gh_auth: Github,
21
  owner: string,
22
  repo: string,
23
  path?: string,
24
  ref?: string,
25
  result_format: "github_object" | "json" = "github_object"
26
) {
27
  const octokit = new Octokit({ auth: gh_auth.token });
28

29
  return await octokit.request(
30
    `GET /repos/{owner}/{repo}/contents/{path}${ref ? "?{ref}" : ""}`,
31
    {
32
      owner,
33
      repo,
34
      path,
35
      headers: {
36
        "X-GitHub-Api-Version": "2022-11-28",
37
        Accept: `application/${
38
          result_format === "json" ? "vnd.github+json" : "vnd.github.object"
39
        }`,
40
      },
41
    }
42
  );
43
}
44