使用 PyGitHub 库获取成员最后提交日期

Get member last commit date using the PyGitHub library

我正在开发一个 python 脚本,它要求我获取所有 GitHub 组织成员的列表,这些成员长时间未对任何组织存储库执行提交。我们希望找到不活跃的成员并将他们从组织中移除

关于如何使用 PyGitHub 库获取成员最后提交日期的任何想法?

存储库对象有一个 API get_commits。它有一个参数author。您可以使用作者的用户 ID 或电子邮件地址,并获取该特定作者对特定存储库的所有提交。

之后你必须比较收到的时间戳并找出最后一次提交的日期和时间。

这是API返回的输出格式。 https://developer.github.com/v3/repos/commits/

API 文档:https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository.get_commits

使用 Search commit API 并使用 org 参数过滤您组织的回购:

GET https://api.github.com/search/commits?q=[SEARCH REQUEST]

您需要使用 header Accept: application/vnd.github.cloak-preview 才能使用此 API。以下给出了在特定组织拥有的存储库中为特定用户所做的所有提交,并按最近的排在第一位:

curl -s -H "Accept: application/vnd.github.cloak-preview" \
    https://api.github.com/search/commits?q=author:fgette%20org:BboxLab%20sort:author-date-desc

然后您可以过滤该组织中最近提交的日期:

curl -s -H "Accept: application/vnd.github.cloak-preview" \
    https://api.github.com/search/commits?q=author:fgette%20org:BboxLab%20sort:author-date-desc | \
    jq -r '.items[0].commit.author.date'

使用 您将使用以下方法检查最近的提交是否低于截止日期(此处 < 12 个月后):

from github import Github
from datetime import date
from datetime import datetime
from dateutil.relativedelta import relativedelta

deadline = datetime.combine(
    date.today() + relativedelta(months=-12), 
    datetime.min.time()
)

g = Github("YOUR_TOKEN", per_page = 1)

commits = g.search_commits(
    query = 'author:fgette org:BboxLab sort:author-date-desc'
)

data = commits.get_page(0)

if (len(data) > 0):
    last_commit = data[0].commit.author.date
    print(f'last commit : {last_commit}')
    if (last_commit < deadline):
        print("too old :(")
    else:
        print("ok :)")

输出:

last commit : 2019-03-06 15:29:26

too old :(