从commit中获取GitHub存储库URL

问题描述 投票:1回答:1

我想从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
python git github
1个回答
1
投票

该URL在_rawData中可用,但我怀疑它是否有用,前缀为下划线通常用于表示私有属性

from github import Github
g = Github()

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

print(url)
© www.soinside.com 2019 - 2024. All rights reserved.