PyGitHub Quickstart Examples

DZone 's Guide to

PyGitHub Quickstart Examples

Helpful examples can make up for a lack of documentation. In this article, Chase Seibert shares some of his example code.

· Integration Zone ·
Free Resource

PyGitHub is the most popular GitHub API SDK for Python. Their documentation is very light on examples. They seem to think that this is fine. This makes for a prime candidate for the new Stack Overflow Documentation site!

In the meantime, I thought I would share my example code. These took me an hour of playing with the REPL to figure out.

Working With Pull Requests

The main challenge here was realizing that I needed to scope everything to my private org, not my user.

git = PyGithub('GITHUB_TOKEN')
org = git.get_organization('OrgName')
repo = org.get_repo('repo-name')
pr = repo.get_pull(1)
print 'PR author: %s' % pr.user.login
comments = pr.get_issue_comments()
for comment in comments:
  print 'Comment: ', comment.created_at, comment.user.login, comment.body
pr.create_issue_comment('Comment from GITHUB_TOKEN user') # aka git.get_user()

Getting the Contents of a File

From the default branch, not a particular pull request.

git = PyGithub('GITHUB_TOKEN')
org = git.get_organization('OrgName')
repo = org.get_repo('repo-name')
file_contents = repo.get_file_contents('path/to/file')

Listing the Members of a GitHub Team

There is no method to get a team by name, so you need to get them all and then pick out the one you want.

git = PyGithub('GITHUB_TOKEN')
org = git.get_organization('OrgName')
teams = org.get_teams()
team = [t for t in teams if t.name == 'TeamName'][0]  # assumes a match
print [m.login for m in team.get_members()]
github ,api ,pygithub ,integration

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}