Python Package PyGithub: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 54: Line 54:
</syntaxhighlight>
</syntaxhighlight>
==Pull Requests (PRs)==
==Pull Requests (PRs)==
===Get PRs from a Repository===
===Get Multiple PRs from a Repository===
Calls <code>GET /repos/<owner>/<repo>/pulls</code>. See: https://docs.github.com/en/rest/reference/pulls for reference.
Returns a paginated list of <code>PullRequest</code> instances.


Calls <code>GET /repos/<owner>/<repo>/pulls</code>.  
<syntaxhighlight lang='py'>
repository = ...
repository.get_pulls()
</syntaxhighlight>
===Get One PR from a Repository===
 
Calls <code>GET /repos/<owner>/<repo>/pulls/<pr-number></code>.  


See: https://docs.github.com/en/rest/reference/pulls
See: https://docs.github.com/en/rest/reference/pulls

Revision as of 22:45, 9 January 2023

External

Internal

Overview

A package used to interact with GitHub APIs.

Example

import os
from github import Github
from urllib3 import Retry


def access_github():
    # github = Github('user', 'password')
    github_pat = os.environ.get('GITHUB_PAT')
    if not github_pat:
        raise ValueError("'GITHUB_PAT' not setup")
    base_url = 'https://github.com'
    api_endpoint = 'api/v3'
    status_forcelist = [403, 500, 502, 504]  # retry 403s, 5XX from GitHub
    retry = Retry(total=10, backoff_factor=0.2, raise_on_status=True, status_forcelist=status_forcelist)
    github = Github(base_url=f'{base_url}/{api_endpoint}', login_or_token=github_pat, retry=retry, per_page=100)

    for repo in github.get_user().get_repos():
        print(repo.name)

Programming Model

Repository

https://pygithub.readthedocs.io/en/latest/examples/Repository.html

Attributes:

  • full_name

Get a Repository

github = Github(...)
repo = github.get_repo('some-owner/some-repo')

Issues

https://pygithub.readthedocs.io/en/latest/examples/Issue.html
repo = ...
issues = repo.get_issues(state=state, milestone=milestone)

Milestones

repo = ...
milestones = repo.get_milestones()

Pull Requests (PRs)

Get Multiple PRs from a Repository

Calls GET /repos/<owner>/<repo>/pulls. See: https://docs.github.com/en/rest/reference/pulls for reference. Returns a paginated list of PullRequest instances.

repository = ...
repository.get_pulls()

Get One PR from a Repository

Calls GET /repos/<owner>/<repo>/pulls/<pr-number>.

See: https://docs.github.com/en/rest/reference/pulls