从提交中获取GitHub存储库URL

时间:2019-04-12 12:21:59

标签: python git github

我想从Commit HASH获取GitHub Repository URL。

我很快找到了这个解决方案:

import json
import requests

def search_commits(sha):
   headers = {'Accept': 'application/vnd.github.cloak-preview'}
   req = requests.get('http://api.github.com/search/commits',
       {'q': sha},
       headers=headers)
   return json.loads(req.text)


commit = search_commits('e83c5163316f89bfbde7d9ab23ca2e25604af290')['items'][0]
clone_url = commit['repository']['url']

我用PyGitHub尝试过相同的操作:

from github import Github
g = Github()

commit = g.search_commits('e83c5163316f89bfbde7d9ab23ca2e25604af290')[0]

不幸的是,此结果(即使在commit.raw_data中也没有键repository

PyGitHub似乎使用search/commits,如此处所述:https://developer.github.com/v3/search/#search-commits

如何获取存储库的URL?

一种可能的麻烦是:

import re

repo = g.get_repo(
    re.search('repos/(.*?)/commits', c.raw_data['url']).group(1)
)
clone_url = repo.clone_url

1 个答案:

答案 0 :(得分:1)

该网址在_rawData中可用,但我怀疑它是否有预定用途,通常在下划线前面加上前缀以表示私有属性

from github import Github
g = Github()

commit = g.search_commits('e83c5163316f89bfbde7d9ab23ca2e25604af290')[0]
url = commit._rawData['repository']['url']

print(url)
相关问题