Thursday, September 12, 2019

GitHub API Library for Python

Software:
  • MacOS 10.14.6
  • Python 3.7.4
Goals:
  • Use the GitHub Python API to interact with Repositories, Issues, and Pull Requests.
Install:
  • Using the Terminal:
    • pip3 install github3
  • Generate the Token:
    • Go to your GitHub Settings page
    • Uses the sidebar to access Personal access tokens
    • Click on the Generate new token button in the top right of the view
    • Give the token a name, then uncheck all scopes except for User
    • Click Generate token and GitHub will take you back to the list of tokens from before
    • Copy the code into your clipboard
  • Put the code in your environment:
    • Linux: Edit the `/etc/environment` file and add the following:
      • GITHUB_TOKEN=5c2...5ed
Tests:
  • Login:
    • Access using username and password:
      • from github3 import login
      • gh = login('username', 'password')
    • Access using Personal Access Tokens:
      • from github3 import login
      • gh = login(token='eb89...04e2')
  • List of Repositories:
    • from github3 import login
    • gh = login(token='token')
    • repos = gh.repositories()
    •  for repo in repos:
      • print(repo, repo.description)
  • List of Issues:
    • from github3 import login
    • gh = login(token='token')
    • issues = gh.issues()
    • for issue in issues:
      • print(issue, issue.body, issue.as_json())
  • List or Pull Requests:
    • from github3 import login
    • gh = login(token='token')
    • pr = gh.pull_requests()
    • ToDo: Find an alternative function to get PRs, cause this one raises an exception.

References: