我无法找到如何获得github上拉请求中存在的文件内容。是否有任何方法来做到这一点使用pygithub?
对于repository,我们可以使用
contents = repo.get_contents(filename)
然而,我没有找到这样的api拉请求对象。有什么办法可以做到吗?
我找到了一个很好的解决方法。我们不能从File对象中获取内容,但是,可以从任何版本的存储库中获取它们。我是这样做的:
无论PR是打开/关闭/合并,都可以工作。
(1)从PR中抓取提交
(2)从提交中抓取文件。
Repository
对象获取对应于PR中提交的sha的引用文件的内容。的例子:
github = Github(login_or_token=my_github_token)
repo = github.get_repo(repo_name, lazy=False)
# Grab PR
pull_number = 20
pr = repo.get_pull(pull_number)
commits = pr.get_commits()
for commit in commits:
files = commit.files
for file in files:
filename = file.filename
contents = repo.get_contents(filename, ref=commit.sha).decoded_content
# Now do whatever you want to do with contents :)
看看这个:https://docs.github.com/en/rest/reference/pulls#list-pull-requests-files
没有尝试过,但pygithub
确实有一个方法,称为get_files
使用这个API调用:https://pygithub.readthedocs.io/en/latest/github_objects/PullRequest.html#github.PullRequest.PullRequest.get_files
编辑:Usingrequests
:
import requests
username = 'astrochun'
repo = 'Zcalbase_gal'
pr = 84
response = requests.get(f"https://api.github.com/repos/{username}/{repo}/pulls/{pr}/files")
dict0 = response.json()
pr_files = [elem['filename'] for elem in dict0]