github3.py能否用于查找分叉回购的父级/上游?

2024-04-27 18:36:38 发布

您现在位置:Python中文网/ 问答频道 /正文

给定一个分叉回购,如何使用github3.py查找分叉回购的父回购或上游回购?这对于请求来说相当容易,但是我不知道如何在github3.py中实现它。你知道吗

请求:

for repo in gh.repositories_by(username):  # github3.py provides a user's repos
  if repo.fork:                            # we only care about forked repos
    assert 'parent' not in repo.as_dict()  # can't find parent using github3.py
    repo_info = requests.get(repo.url).json()  # try with requests instead
    assert 'parent' in repo_info, repo_info    # can find parent using requests
    print(f'{repo_info["url"]} was forked from {repo_info["parent"]["url"]}')
    # https://github.com/username/repo was forked from
    # https://github.com/parent/repo

这个用例类似于How can I find all public repos in github that a user contributes to?,但是我们还需要检查从中派生出用户repo的父/上游repo。你知道吗


Tags: inpyinfogithuburlusernamerepofind
1条回答
网友
1楼 · 发布于 2024-04-27 18:36:38

文档显示它存储为repo.parent,但是,它仅在Repository对象上可用。repositories_by返回ShortRepository对象。你知道吗

这看起来像:

for short_repo in gh.repositories_by(username):
    repo = short_repo.refresh()
    if repo.fork:
        parent = repo.parent

相关问题 更多 >