通过Github API从Github仓库获取所有文件名

时间:2014-07-29 18:02:35

标签: github github-api

是否可以使用GitHub API从存储库中获取所有文件名?

我目前正在尝试使用PyGithub修补此问题,但只要有效,我就可以手动执行请求。

到目前为止我的算法是:

  1. 获取用户回购名称
  2. 获取与特定描述相匹配的用户仓库
  3. ???获取repo文件名?

4 个答案:

答案 0 :(得分:18)

这必须与特定提交相关,因为某些提交中可能存在某些文件而其他提交中不存在,因此在查看文件之前,您需要使用List commits on a repository之类的内容:< / p>

GET /repos/:owner/:repo/commits

如果您只对分支上的最新提交感兴趣,可以将sha参数设置为分支名称:

  

sha string SHA或分支以开始列出来自。

的提交

提交哈希后,您可以inspect that commit

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。现在我们终于可以request the contents of that tree

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"
    }
  ]
}

如您所见,我们有一些 blobs ,它们对应于文件,还有一些额外的树,它们对应于子目录。您可能需要do this recursively

答案 1 :(得分:5)

您可以使用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
}

答案 2 :(得分:2)

如丹所言: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"])

为简单起见,我省略了错误管理,无论如何迅猛龙都灭绝了……

答案 3 :(得分:1)

现在使用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内部,我们已经在提交中,现在我们可以看到文件了, commits指针中的文件实际上只是指向树的指针,而树仅具有条目,这些条目可以是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
                                  }                                  
                                }
                              }
                            }   
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
相关问题