Documentation

Class: Github

Description: This class provides a collection of functions designed to interact with the GitHub API. It leverages the Axios library for making HTTP requests and utilizes various functions to retrieve information about users, repositories, branches, files, and pull requests.

Methods


Method: getAvatar

Description: Retrieves the avatar URL for a given GitHub user.

Parameters:

  • login (string): The GitHub username.

Returns:

  • string: The URL of the user's avatar.

Example:

const avatarUrl = await Github.getAvatar("octocat");

Method: getRepo

Description: Retrieves detailed information about a GitHub repository. Optionally, it can also retrieve the list of branches for the repository.

Parameters:

  • repo (object): An object containing the repository owner and name.
    • owner (string): The repository owner's username.
    • name (string): The repository name.
  • branches (boolean): Optional parameter specifying whether to retrieve the list of branches. Defaults to false.

Returns:

  • object: An object containing the repository information.
    • Includes properties like name, description, URL, etc.
    • If branches is true, the object will also contain a branches property with an array of branch objects.

Throws:

  • Error: If there is an error fetching repository data.

Example:

const repoData = await Github.getRepo({ owner: "facebook", name: "react" });

Method: getBranches

Description: Retrieves the list of branches for a given GitHub repository.

Parameters:

  • repo (object): An object containing the repository owner and name.
    • owner (string): The repository owner's username.
    • name (string): The repository name.

Returns:

  • array: An array of branch objects, or undefined if there is an error fetching branch data.

Throws:

  • Error: If there is an error fetching branch data.

Example:

const branches = await Github.getBranches({ owner: "google", name: "angular" });

Method: getFiles

Description: Retrieves the list of files within a specified path of a GitHub repository. It supports recursive fetching of files within subdirectories.

Parameters:

  • repo (object): An object containing the repository owner, name, and default branch.
    • owner (string): The repository owner's username.
    • name (string): The repository name.
    • default_branch (string): The repository's default branch name.
  • path (string): The path within the repository to retrieve files from. Defaults to null, which retrieves files from the default branch.
  • recursive (boolean): Optional parameter specifying whether to recursively fetch files within subdirectories. Defaults to false.

Returns:

  • object: An object containing the file data, or undefined if there is an error fetching file data.

Throws:

  • Error: If there is an error fetching file data or if no file data is available.

Example:

const files = await Github.getFiles({ owner: "vuejs", name: "vue", default_branch: "main" }, "src/components");

Method: getFile

Description: Retrieves the content of a specific file within a GitHub repository.

Parameters:

  • repo (object): An object containing the repository owner and name.
    • owner (string): The repository owner's username.
    • name (string): The repository name.
  • file (string): The path to the file within the repository.
  • ref (string): The branch or commit SHA to retrieve the file from.

Returns:

  • object: An object containing the file data, including the file content decoded as UTF-8 string, or undefined if there is an error fetching file data.

Throws:

  • Error: If there is an error fetching file data.

Example:

const fileContent = await Github.getFile({ owner: "axios/axios", name: "axios" }, "index.js", "main");

Method: _request

Description: This is a private helper function responsible for making requests to the GitHub API using Axios. It includes authentication and basic error handling.

Parameters:

  • path (string): The API endpoint path.
  • args (object): Optional arguments to be passed to Axios (e.g., query parameters, headers). Defaults to null.
  • method (string): The HTTP method to use for the request. Defaults to "GET".

Returns:

  • object: The Axios response data, or undefined if there is an error during the request.

Throws:

  • Error: If there is an error during the request.

Example:

// Not meant to be called directly, but demonstrates its usage
const response = await Github._request("/repos/facebook/react/issues", { params: { state: "open" } }, "GET");

Method: URLs

Description: This object provides helper functions for generating URLs for various GitHub resources based on object properties. These functions make it easier to construct URLs for specific user profiles, repositories, branches, pull requests, and files.

Example:

const repoUrl = Github.URLs.repo({ owner: "facebook", name: "react" });

Important Considerations

  • Authentication: The code requires a valid GitHub access token to function. The getAccessToken() function should be implemented to retrieve the token from a secure location.
  • Rate Limiting: The GitHub API has rate limits in place. Be mindful of the number of requests you make to avoid exceeding the limits.
  • Error Handling: The code includes basic error handling using try...catch blocks, but it might be necessary to enhance the error handling based on specific requirements.
  • Data Parsing: The _request function returns the response data. It might be necessary to parse the data further depending on the API endpoint and the desired information.

This documentation provides a comprehensive overview of the Github class and its methods. For more detailed information, refer to the official GitHub API documentation: https://docs.github.com/en/rest