Skip to content

Instantly share code, notes, and snippets.

@nekoya
Last active December 14, 2021 09:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nekoya/21a634619e80f1b3cbd313d25cb171e0 to your computer and use it in GitHub Desktop.
Save nekoya/21a634619e80f1b3cbd313d25cb171e0 to your computer and use it in GitHub Desktop.
PyGithub cheat sheet
from github import Github
GITHUB_TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
g = Github(GITHUB_TOKEN)
def get_user():
"""
Returns:
<AuthenticatedUser>
"""
user = g.get_user()
return user
def get_organization(organization_name):
"""
Returns:
<Organization>
"""
organization = g.get_organization(organization_name)
return organization
def get_repo(owner, repo_name):
"""
Args:
<AuthenticatedUser|Organization> repository owner
<string> repository name, not fullname "owner/XXXX"
"""
repo = owner.get_repo(repo_name)
return repo
def auto_merge(repo, branch_name):
"""
Args:
<Repository>
<string> target branch name
"""
c = repo.get_commit('HEAD')
ref = repo.create_git_ref('refs/heads/auto', c.sha) # create branch
repo.merge('auto', branch_name, 'auto merge')
# repo.get_git_ref('heads/auto').delete() # without "refs" prefix
ref.delete()
def show_branches(repo):
[print(x.name, x.commit.sha) for x in repo.get_branches()]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment