我想从Commit HASH获取GitHub存储库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?
一个可能的kludge将是:
import re
repo = g.get_repo(
re.search('repos/(.*?)/commits', c.raw_data['url']).group(1)
)
clone_url = repo.clone_url
该URL在_rawData
中可用,但我怀疑它是否有用,前缀为下划线通常用于表示私有属性
from github import Github
g = Github()
commit = g.search_commits('e83c5163316f89bfbde7d9ab23ca2e25604af290')[0]
url = commit._rawData['repository']['url']
print(url)