Hi, this is @ yshr10ic.
Recently, as an output goal, I've been trying to make more than one commit per day on GitHub. (Of course, I know that committing shouldn't be the goal.)
Since I commit every day, I wanted to visualize how much I committed. So, when I was investigating how to get the commit log of GitHub, I found out that PyGithub exists.
In this article, I would like to briefly summarize how to use PyGithub.
PyGitHub is a Python library to access the GitHub API v3 and Github Enterprise API v3. This library enables you to manage GitHub resources such as repositories, user profiles, and organizations in your Python applications.
A library for accessing GitHub API v3
and Github Enterprise API v3
. You will be able to manage GitHub resources such as repositories, user profiles, and organizations in your Python application.
$ pip install PyGithub
In my environment, the version is 1.47
.
Partial excerpt from PyGithub --Examples.
create_instance.py
from github import Github
#Instantiation by user name and password
g = GitHub('username', 'password')
#Instance generation by access token
g = Github('access_token')
#Instantiate a GitHub enterprise with a custom host
g = Github(base_url='https://{hostname}/api/v3', login_or_token='access_token')
get_repos.py
for repo in g.get_user().get_repos():
print(repo)
output
Repository(full_name="yshr10ic/deep-learning-from-scratch")
Repository(full_name="yshr10ic/deep-learning-from-scratch-2")
...
get_count_of_stars.py
repo = g.get_repo('yshr10ic/deep-learning-from-scratch-2')
print(repo.stargazers_count)
output
1
get_branches.py
repo = g.get_repo('yshr10ic/deep-learning-from-scratch-2')
for branch in repo.get_branches():
print(branch)
output
Branch(name="images")
Branch(name="master")
get_committed_files.py
repo = g.get_repo('yshr10ic/sample')
for commit in repo.get_commits():
print(commit.files)
output
[File(sha="xxx", filename="django/djangorestframework/tutorial/tutorial/urls.py")]
[File(sha="yyy", filename="django/djangorestframework/tutorial/tutorial/quickstart/views.py")]
[File(sha="zzz", filename="django/djangorestframework/tutorial/tutorial/quickstart/serializers.py")]
...
This time, I only got the information of the existing GitHub repository, but in reality, I can also create files, pull requests, issues, etc.
I tried everything from creating a GitHub instance to getting various information, and I'm glad I was able to get the information very easily. However, in order to achieve the original purpose of "I want to visualize how many commits I have made", I have to be able to search the commit log by time. With PyGithub, it is unlikely that you can search by time, so I will consider other means.
Recommended Posts