主要是因為上一篇文章撰寫了離職心得,在寫的過程中想到,好像可以用 GitHub API 抓一下我在前公司這將近兩年的日子到底送了多少 commits。
廢話不多說,直接進程式碼。
pip install --pre github3.py
來安裝github3.py (1.0.0a4)
#!/usr/bin/env python3
from pprint import pprint
# Make sure you've installed github3.py via `pip install --pre github3.py`
from github3 import login
MY_GITHUB_USERNAME = ""
# Visit https://github.com/settings/tokens to create a token if you don't have.
# Check "repo" permission
MY_GITHUB_TOKEN = ""
TARGET_ORGNIZATION_NAME = ""
user = login(token=MY_GITHUB_TOKEN)
org = user.organization(TARGET_ORGNIZATION_NAME)
my_stats = []
for repo in org.repositories():
for contribution in repo.contributor_statistics():
if MY_GITHUB_USERNAME in repr(contribution.author):
print(repo.name, contribution.total)
my_stats.append((repo.name, contribution.total))
print("")
print('=' * 80)
print("")
print("GitHub username: {}".format(MY_GITHUB_USERNAME))
print("Target GitHub organization: {}".format(TARGET_ORGNIZATION_NAME))
print("Total contributed repos: {}".format(len(my_stats)))
print("Total commits: {}".format(sum(commits for repo, commits in my_stats)))
my_stats_desc_sorted = sorted(my_stats, key=lambda x: x[1], reverse=True)
print("Repo and commits:")
pprint(my_stats_desc_sorted)
一樣把程式碼開一個 gist 存:https://gist.github.com/M157q/94be5759d2f13bfdcdd485feff2be3e6
GitHub username: M157q
Target GitHub organization: Tagtoo
Total contributed repos: 36
Total commits: 1521
總共在 36 個 repos 貢獻了 1521 個 commits,由於大多是 private repo,所以就不一一秀出了。
這邊只有算在 master branch 上的 commits 而已,不包含 issue 跟 PR。
如果覺得我的文章不錯的話,
歡迎按讚、追蹤、訂閱、留言、分享,
也可以利用像是 Feedly 等 RSS Reader,
直接訂閱我的部落格:https://blog.m157q.tw。
因為 iThome 這邊未來我不保證持續更新,
雖然目前用起來沒太大問題,
但就是覺得要管兩個地方有點麻煩。