Github API 检查文件是否存在,如果存在则获取它?

问题描述 投票:0回答:2

有没有办法通过github api来做到这一点?我已经尝试过 api.github.com/repos/name/repo/contents/path

但是我对内容和路径中的内容有点困惑。如果它对我试图获取的文件有帮助,则该文件存在于存储库的根目录中。所以这是我正在寻找的 ./fileName 。我将如何构建 GET url(除非我做对了,还有其他一些问题)

rest github-api
2个回答
7
投票

嗨,抱歉,我本想更新此内容,但如果有人犯了与我相同的错误,那就是字面上的错误

api.github.com/repos/name/repo/contents/fileNameOrPath

如果有人想知道,这会引导我找到该文件的原始 url,我对其进行了 GET 操作并能够获取内容:)。


0
投票

这是一个完整的工作示例,以防某些用户可以从中受益:

import github
from typing import Final
from github import Github, GithubException

GITHUB_ACCESS_TOKEN: Final[str] = "..."
github = Github(GITHUB_ACCESS_TOKEN)
repo = github.get_repo("star7th/showdoc")
composer_json_file = repo.get_contents("composer.json")
if composer_json_file:
    content = composer_json_file.decoded_content.decode("utf-8")
print(content)
© www.soinside.com 2019 - 2024. All rights reserved.