Edits history of script submission #22396 for ' Get Repo Content (github)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { Octokit } from "@octokit/rest";
    
    /**
     * @param owner The account owner of the repository. The name is not case sensitive.
     *
     * @param repo The name of the repository. The name is not case sensitive.
     *
     * @param path The path to the file or directory.
     * If omitted, the contents of the repository's root directory will be returned.
     *
     * @param ref The name of the commit/branch/tag. Defaults to the default branch of the repository.
     *
     * @param result_format The kind of data to be returned. This controls how the result is structured.
     * Learn more at https://docs.github.com/en/rest/repos/contents#get-repository-content
     */
    type Github = {
      token: string;
    };
    export async function main(
      gh_auth: Github,
      owner: string,
      repo: string,
      path?: string,
      ref?: string,
      result_format: "github_object" | "json" = "github_object"
    ) {
      const octokit = new Octokit({ auth: gh_auth.token });
    
      return await octokit.request(
        `GET /repos/{owner}/{repo}/contents/{path}${ref ? "?{ref}" : ""}`,
        {
          owner,
          repo,
          path,
          headers: {
            "X-GitHub-Api-Version": "2022-11-28",
            Accept: `application/${
              result_format === "json" ? "vnd.github+json" : "vnd.github.object"
            }`,
          },
        }
      );
    }
    

    Submitted by hugo989 13 days ago