Utilities

Authentication

class github.Auth.Login(login: str, password: str)

This class is used to authenticate with login and password.

property username

The username.

property password

The password

class github.Auth.Token(token: str)

This class is used to authenticate with a single constant token.

property token_type

The type of the auth token as used in the HTTP Authorization header, e.g. Bearer or Basic. :return: token type

property token

The auth token as used in the HTTP Authorization header. :return: token

class github.Auth.JWT

This class is the base class to authenticate with a JSON Web Token (JWT). https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-json-web-token-jwt-for-a-github-app

property token_type

The type of the auth token as used in the HTTP Authorization header, e.g. Bearer or Basic. :return: token type

class github.Auth.AppAuth(app_id: Union[int, str], private_key: str, jwt_expiry: int = 300, jwt_issued_at: int = -60, jwt_algorithm: str = 'RS256')

This class is used to authenticate as a GitHub App. https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app

property token

The auth token as used in the HTTP Authorization header. :return: token

get_installation_auth(installation_id: int, token_permissions: Optional[Dict[str, str]] = None, requester: Optional[github.Requester.Requester] = None) → github.Auth.AppInstallationAuth

Creates a github.Auth.AppInstallationAuth instance for an installation. :param installation_id: installation id :param token_permissions: optional permissions :param requester: optional requester with app authentication :return:

create_jwt(expiration: Optional[int] = None) → str

Create a signed JWT https://docs.github.com/en/developers/apps/building-github-apps/authenticating-with-github-apps#authenticating-as-a-github-app

Return string

jwt

class github.Auth.AppAuthToken(token: str)

This class is used to authenticate as a GitHub App with a single constant JWT. https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app

property token

The auth token as used in the HTTP Authorization header. :return: token

class github.Auth.AppInstallationAuth(app_auth: github.Auth.AppAuth, installation_id: int, token_permissions: Optional[Dict[str, str]] = None, requester: Optional[github.Requester.Requester] = None)

This class is used to authenticate as a GitHub App Installation. https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation

property token_type

The type of the auth token as used in the HTTP Authorization header, e.g. Bearer or Basic. :return: token type

property token

The auth token as used in the HTTP Authorization header. :return: token

class github.Auth.AppUserAuth(client_id: str, client_secret: str, token: str, token_type: Optional[str] = None, expires_at: Optional[datetime.datetime] = None, refresh_token: Optional[str] = None, refresh_expires_at: Optional[datetime.datetime] = None, requester: Optional[github.Requester.Requester] = None)

This class is used to authenticate as a GitHub App on behalf of a user. https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-with-a-github-app-on-behalf-of-a-user

class ApplicationOAuth(requester: Requester, headers: dict[str, Any], attributes: Any, completed: bool)

This class is used for identifying and authorizing users for Github Apps. The reference can be found at https://docs.github.com/en/developers/apps/building-github-apps/identifying-and-authorizing-users-for-github-apps

get_access_token(code: str, state: str | None = None) → AccessToken
Calls

POST /login/oauth/access_token

get_login_url(redirect_uri: str | None = None, state: str | None = None, login: str | None = None) → str

Return the URL you need to redirect a user to in order to authorize your App.

refresh_access_token(refresh_token: str) → AccessToken
Calls

POST /login/oauth/access_token

Parameters

refresh_token – string

property token_type

The type of the auth token as used in the HTTP Authorization header, e.g. Bearer or Basic. :return: token type

property token

The auth token as used in the HTTP Authorization header. :return: token

class github.Auth.NetrcAuth

This class is used to authenticate via .netrc.

property username

The username.

property password

The password

Logging

github.enable_console_debug_logging() → None

This function sets up a very simple logging configuration (log everything on standard output) that is useful for troubleshooting.

Error Handling

exception github.GithubException.GithubException(status: int, data: Any = None, headers: Optional[Dict[str, str]] = None, message: Optional[str] = None)

Error handling in PyGithub is done with exceptions. This class is the base of all exceptions raised by PyGithub (but github.GithubException.BadAttributeException).

Some other types of exceptions might be raised by underlying libraries, for example for network-related issues.

property status

The status returned by the Github API

property data

The (decoded) data returned by the Github API

property headers

The headers returned by the Github API

exception github.GithubException.BadCredentialsException(status: int, data: Any = None, headers: Optional[Dict[str, str]] = None, message: Optional[str] = None)

Exception raised in case of bad credentials (when Github API replies with a 401 or 403 HTML status)

exception github.GithubException.UnknownObjectException(status: int, data: Any = None, headers: Optional[Dict[str, str]] = None, message: Optional[str] = None)

Exception raised when a non-existing object is requested (when Github API replies with a 404 HTML status)

exception github.GithubException.BadUserAgentException(status: int, data: Any = None, headers: Optional[Dict[str, str]] = None, message: Optional[str] = None)

Exception raised when request is sent with a bad user agent header (when Github API replies with a 403 bad user agent HTML status)

exception github.GithubException.RateLimitExceededException(status: int, data: Any = None, headers: Optional[Dict[str, str]] = None, message: Optional[str] = None)

Exception raised when the rate limit is exceeded (when Github API replies with a 403 rate limit exceeded HTML status)

exception github.GithubException.BadAttributeException(actualValue: Any, expectedType: Union[Dict[Tuple[Type[str], Type[str]], Type[dict]], Tuple[Type[str], Type[str]], List[Type[dict]], List[Tuple[Type[str], Type[str]]]], transformationException: Optional[Exception])

Exception raised when Github returns an attribute with the wrong type.

property actual_value

The value returned by Github

property expected_type

The type PyGithub expected

property transformation_exception

The exception raised when PyGithub tried to parse the value

exception github.GithubException.TwoFactorException(status: int, data: Any = None, headers: Optional[Dict[str, str]] = None, message: Optional[str] = None)

Exception raised when Github requires a onetime password for two-factor authentication

exception github.GithubException.IncompletableObject(status: int, data: Any = None, headers: Optional[Dict[str, str]] = None, message: Optional[str] = None)

Exception raised when we can not request an object from Github because the data returned did not include a URL

Default argument

github.NotSet is a special value for arguments you don’t want to provide. You should not have to manipulate it directly, because it’s the default value of all parameters accepting it. Just note that it is different from None, which is an allowed value for some parameters.

Pagination

class github.PaginatedList.PaginatedList

This class abstracts the pagination of the API.

You can simply enumerate through instances of this class:

for repo in user.get_repos():
    print(repo.name)

If you want to know the total number of items in the list:

print(user.get_repos().totalCount)

You can also index them or take slices:

second_repo = user.get_repos()[1]
first_repos = user.get_repos()[:10]

If you want to iterate in reversed order, just do:

for repo in user.get_repos().reversed:
    print(repo.name)

And if you really need it, you can explicitly access a specific page:

some_repos = user.get_repos().get_page(0)
some_other_repos = user.get_repos().get_page(3)

Input classes

class github.InputFileContent.InputFileContent(content: str, new_name: Union[str, github.GithubObject._NotSetType] = NotSet)

This class represents InputFileContents

class github.InputGitAuthor.InputGitAuthor(name: str, email: str, date: Union[str, github.GithubObject._NotSetType] = NotSet)

This class represents InputGitAuthors

class github.InputGitTreeElement.InputGitTreeElement(path: str, mode: str, type: str, content: Opt[str] = NotSet, sha: Opt[str | None] = NotSet)

This class represents InputGitTreeElements