iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 8
0
自我挑戰組

M157q 的待業程式生活日誌系列 第 8

[2018 iThome 鐵人賽] Day 8: 用 Python 抓出我在前公司貢獻了多少 GitHub commits


前言

主要是因為上一篇文章撰寫了離職心得,在寫的過程中想到,好像可以用 GitHub API 抓一下我在前公司這將近兩年的日子到底送了多少 commits。


正文

廢話不多說,直接進程式碼。

  • 使用 Python 3
  • 要用到 github3.py 這個 package
    • 請使用 pip install --pre github3.py 來安裝
    • 使用的版本為 github3.py (1.0.0a4)
  • 雖然可以直接用帳號密碼登入,但因為我有用 2FA,所以直接產生一組 Access Token 比較方便,也比較安全。
  • 這個 script 可以拿來算你在某個 organization 的總 commits 數,總共 35 行就搞定了。
#!/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 這邊未來我不保證持續更新,
雖然目前用起來沒太大問題,
但就是覺得要管兩個地方有點麻煩。


上一篇
[2018 iThome 鐵人賽] Day 7: 我的新鮮人離職紀錄
下一篇
[2018 iThome 鐵人賽] Day 9: 關於 Django 的 F() expressions, Q object, Aggregation 的一些筆記
系列文
M157q 的待業程式生活日誌31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言