是否可以使用 GitHub API 从存储库获取所有文件名?
我目前正在尝试使用 PyGithub 修补这个问题,但只要它有效,我完全可以手动执行请求。
到目前为止我的算法是:
您可以使用Github git trees
https://api.github.com/repos/[USER]/[REPO]/git/trees/[BRANCH]?recursive=1
回购
https://github.com/deeja/bing-maps-loader
API调用
https://api.github.com/repos/deeja/bing-maps-loader/git/trees/master?recursive=1
返回
{
sha: "55382e87889ccb4c173bc99a42cc738358fc253a",
url: "https://api.github.com/repos/deeja/bing-maps-loader/git/trees/55382e87889ccb4c173bc99a42cc738358fc253a",
tree: [
{
path: "README.md",
mode: "100644",
type: "blob",
sha: "41ceefc1262bb80a25529342ee3ec2ec7add7063",
size: 3196,
url: "https://api.github.com/repos/deeja/bing-maps-loader/git/blobs/41ceefc1262bb80a25529342ee3ec2ec7add7063"
},
{
path: "index.js",
mode: "100644",
type: "blob",
sha: "a81c94f70d1ca2a0df02bae36eb2aa920c7fb20e",
size: 1581,
url: "https://api.github.com/repos/deeja/bing-maps-loader/git/blobs/a81c94f70d1ca2a0df02bae36eb2aa920c7fb20e"
},
{
path: "package.json",
mode: "100644",
type: "blob",
sha: "45f24dcb7a457b14fede4cb907e957600882b340",
size: 595,
url: "https://api.github.com/repos/deeja/bing-maps-loader/git/blobs/45f24dcb7a457b14fede4cb907e957600882b340"
}
],
truncated: false
}
这必须与特定提交相关,因为某些文件可能存在于某些提交中而在其他提交中不存在,因此在查看文件之前,您需要使用类似 List commits on a repository:
GET /repos/:owner/:repo/commits
如果您只对分支上的最新提交感兴趣,您可以将
sha
参数设置为分支名称:
sha
SHA 或分支开始列出提交。string
一旦有了提交哈希,您就可以检查该提交
GET /repos/:owner/:repo/git/commits/:sha
它应该返回类似这样的内容(从 GitHub 的文档中截断):
{
"sha": "...",
"...",
"tree": {
"url": "https://api.github.com/repos/octocat/Hello-World/git/trees/691272480426f78a0138979dd3ce63b77f706feb",
"sha": "691272480426f78a0138979dd3ce63b77f706feb"
},
"...": "..."
}
查看其tree的哈希值,它本质上是其目录内容。在这种情况下,
691272480426f78a0138979dd3ce63b77f706feb
。现在我们终于可以请求该树的内容了:
GET /repos/:owner/:repo/git/trees/:sha
GitHub 示例的输出是
{
"sha": "9fb037999f264ba9a7fc6274d15fa3ae2ab98312",
"url": "https://api.github.com/repos/octocat/Hello-World/trees/9fb037999f264ba9a7fc6274d15fa3ae2ab98312",
"tree": [
{
"path": "file.rb",
"mode": "100644",
"type": "blob",
"size": 30,
"sha": "44b4fc6d56897b048c772eb4087f854f46256132",
"url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/44b4fc6d56897b048c772eb4087f854f46256132"
},
{
"path": "subdir",
"mode": "040000",
"type": "tree",
"sha": "f484d249c660418515fb01c2b9662073663c242e",
"url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/f484d249c660418515fb01c2b9662073663c242e"
},
{
"path": "exec_file",
"mode": "100755",
"type": "blob",
"size": 75,
"sha": "45b983be36b73c0788dc9cbcb76cbb80fc7bb057",
"url": "https://api.github.com/repos/octocat/Hello-World/git/blobs/45b983be36b73c0788dc9cbcb76cbb80fc7bb057"
}
]
}
如您所见,我们有一些blob,对应于文件,以及一些附加的树,对应于子目录。您可能想递归地执行此操作。
现在使用 graphql api 更容易了,您可以在单个查询中获取所有内容
首先你得到你的仓库:
query {
repository(name: "MyRepo" owner: "mylogin"){
}
}
然后你就可以得到它的defaultBranchRef,让生活变得轻松
defaultBranchRef{
}
现在分支引用实际上只是一个指向提交的指针,并且由于 graphql 是强类型的(引用可以是不同的东西),我们需要让它知道这是一个提交,
target{
...on Commit {
}
}
所以目标就是我们的引用所指向的,我们说“如果它是一个提交,就执行此操作”
它应该做什么? 它应该获得最新的提交(因为存储库中将包含最新的文件)
为此我们查询历史记录
history(first: 1 until: "2019-10-08T00:00:00"){
nodes{
}
}
现在在
nodes
内部,我们处于提交内部,现在我们可以看到文件,
提交指针中的文件实际上只是指向树的指针,而树只有条目,可以是 Tree 类型或 blob 类型的对象
代表文件的条目称为 blob,但由于我们除了列出它们的名称之外不会对它们执行任何操作,因此您甚至不需要知道这一点
但重要的是要知道树也是条目,所以如果你找到一棵树,你需要深入挖掘,但你只能深入预先定义的深度。
tree{
entries {
name
object {
...on Tree{
entries{
name
object {
...on Tree{
entries{
name
}
}
}
}
}
}
}
}
现在把它们放在一起:
query{
repository(owner: "MyLogin", name: "MyRepo") {
defaultBranchRef {
target {
... on Commit {
history(first: 1 until: "2019-10-08T00:00:00") {
nodes {
tree {
entries {
name
object {
... on Tree {
entries {
name
object{
...on Tree{
entries{
name
object{
...on Tree{
entries{
name
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
正如 Dan 提到的:github trees
请参阅下面的工作示例
import requests
user = "grumbach"
repo = "ft_ping"
url = "https://api.github.com/repos/{}/{}/git/trees/master?recursive=1".format(user, repo)
r = requests.get(url)
res = r.json()
for file in res["tree"]:
print(file["path"])
为了简单起见,我省略了错误管理,无论如何,迅猛龙已经灭绝了……
gh api
对 GitHub API 进行经过身份验证的 HTTP 请求gh api -X GET /repos/octocat/Hello-World/commits | grep -E -o ".{0,0}\[{\"sha\":\".{0,40}" | sed 's/\[{\"sha\":\"//' | xargs -I {} gh api -X GET /repos/octocat/Hello-World/commits/{} | grep -E -o "\"filename\":\".*?\""
gh api -X GET /repos/octocat/Hello-World/commits | grep -E -o ".{0,0}\[{\"sha\":\".{0,40}" | sed 's/\[{\"sha\":\"//' >> ~/commits
xargs < ~/commits -I {} gh api -X GET /repos/octocat/Hello-World/commits/{} | grep -E -o "\"filename\":\".*?\""
使用 JavaScript 获取存储库中所有文件列表的两个 REST API 示例:
const { Octokit, App } = await import("https://esm.sh/octokit");
const octokit = new Octokit({ auth: `...` });
1
var res = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner: 'aaa',
repo: 'bbb',
path: '',
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})
2
var res2 = await octokit.request('GET /repos/{owner}/{repo}/git/trees/{tree_sha}?recursive=1', {
owner: 'aaa',
repo: 'bbb',
tree_sha: 'main',
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})