repocribro.github

GitHubAPI

class repocribro.github.GitHubAPI(client_id, client_secret, webhooks_secret, session=None, token=None)

Bases: object

Simple GitHub API communication wrapper

It provides simple way for getting the basic GitHub API resources and special methods for working with webhooks.

Todo

handle if GitHub is out of service, custom errors, better abstraction, work with extensions

API_URL = 'https://api.github.com'

URL to GitHub API

AUTH_URL = 'https://github.com/login/oauth/authorize?scope={}&client_id={}'

URL for OAuth request at GitHub

CONNECTIONS_URL = 'https://github.com/settings/connections/applications/{}'

URL for checking connections within GitHub

SCOPES = ['user', 'repo', 'admin:repo_hook']

Scopes for OAuth request

TOKEN_URL = 'https://github.com/login/oauth/access_token'

URL for OAuth token at GitHub

WEBHOOKS = ['push', 'release', 'repository']

Required webhooks to be registered

WEBHOOK_CONTROLLER = 'webhooks.gh_webhook'

Controller for incoming webhook events

__init__(client_id, client_secret, webhooks_secret, session=None, token=None)

Initialize self. See help(type(self)) for accurate signature.

_get_headers()

Prepare auth header fields (empty if no token provided)

Returns:Headers for the request
Return type:dict
get(what, page=0)

Perform GET request on GitHub API

Parameters:
  • what (str) – URI of requested resource
  • page (int) – Number of requested page
Returns:

Response from the GitHub

Return type:

repocribro.github.GitHubResponse

get_auth_url()

Create OAuth request URL

Returns:OAuth request URL
Return type:str
login(session_code)

Authorize via OAuth with given session code

Parameters:session_code (str) – The session code for OAuth
Returns:If the auth procedure was successful
Return type:bool

Todo

check granted scope vs GH_SCOPES

webhook_create(full_name, hook_url, events=None)

Create new webhook for specified repository

Parameters:
  • full_name (str) – Full name of the repository
  • hook_url (str) – URL where the webhook data will be sent
  • events (list of str) – List of requested events for that webhook
Returns:

The created webhook data

Return type:

dict or None

webhook_delete(full_name, hook_id)

Perform DELETE request for repo’s webhook

Parameters:
  • full_name (str) – Full name of repository that contains the hook
  • hook_id (int) – GitHub ID of hook to be deleted
Returns:

If request was successful

Return type:

bool

webhook_get(full_name, hook_id)

Perform GET request for repo’s webhook

Parameters:
  • full_name (str) – Full name of repository that contains the hook
  • hook_id (int) – GitHub ID of hook to be get
Returns:

Data of the webhook

Return type:

repocribro.github.GitHubResponse

webhook_tests(full_name, hook_id)

Perform test request for repo’s webhook

Parameters:
  • full_name (str) – Full name of repository that contains the hook
  • hook_id (int) – GitHub ID of hook to be tested
Returns:

If request was successful

Return type:

bool

webhook_verify_signature(data, signature)

Verify the content with signature

Parameters:
  • data – Request data to be verified
  • signature (str) – The signature of data
Returns:

If the content is verified

Return type:

bool

webhooks_get(full_name)

GET all webhooks of the repository

Parameters:full_name (str) – Full name of repository
Returns:List of returned webhooks
Return type:repocribro.github.GitHubResponse

GitHubResponse

class repocribro.github.GitHubResponse(response)

Bases: object

Wrapper for GET request response from GitHub

__init__(response)

Initialize self. See help(type(self)) for accurate signature.

actual_page

Actual page number

Returns:actual page number
Return type:int
data

Response data as dict/list

Returns:data of response
Return type:dict|list
is_first_page

Check if this is the first page of data

Returns:if it is the first page of data
Return type:bool
is_last_page

Check if this is the last page of data

Returns:if it is the last page of data
Return type:bool
is_ok

Check if request has been successful

Returns:if it was OK
Return type:bool
is_only_page

Check if this is the only page of data

Returns:if it is the only page page of data
Return type:bool

Response header links

Returns:URL origin
Return type:dict
static parse_page_number(url)

Parse page number from GitHub GET URL

Parameters:url (str) – URL used for GET request
Returns:page number
Return type:int
total_pages

Number of pages

Returns:number of pages
Return type:int
url

URL of the request leading to this response

Returns:URL origin
Return type:str