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

Ant*_*nov 14 github github-api

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

我目前正在尝试使用PyGithub来修改它,但我完全可以手动执行请求,只要它有效.

到目前为止我的算法是:

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

Chr*_*ris 22

这必须与特定提交相关,因为某些提交中可能存在某些文件而其他提交中不存在,因此在查看文件之前,您需要在存储库中使用类似List提交的内容:

GET /repos/:owner/:repo/commits
Run Code Online (Sandbox Code Playgroud)

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

sha string SHA或分支开始列出提交.

一旦有了提交哈希,就可以检查该提交

GET /repos/:owner/:repo/git/commits/:sha
Run Code Online (Sandbox Code Playgroud)

应该返回这样的东西(从GitHub的文档中截断):

{
  "sha": "...",
  "...",
  "tree": {
    "url": "https://api.github.com/repos/octocat/Hello-World/git/trees/691272480426f78a0138979dd3ce63b77f706feb",
    "sha": "691272480426f78a0138979dd3ce63b77f706feb"
  },
  "...": "..."
}
Run Code Online (Sandbox Code Playgroud)

查看其的哈希值,它本质上是其目录内容.在这种情况下,691272480426f78a0138979dd3ce63b77f706feb.现在我们终于可以请求该树的内容:

GET /repos/:owner/:repo/git/trees/:sha
Run Code Online (Sandbox Code Playgroud)

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"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我们有一些blobs,它们对应于文件,还有一些额外的树,它们对应于子目录.您可能希望以递归方式执行此操作.

  • [Repository Contents API](https://developer.github.com/v3/repos/contents/#get-contents)也值得一看.它提供了熟悉的目录式导航结构,公开了目录和文件树. (8认同)
  • `:sha`可以是标签,如`master` (3认同)

Dan*_*ann 20

你可以使用Github git 树

https://api.github.com/repos/[USER]/[REPO]/git/trees/[BRANCH]?recursive=1

回购

https://github.com/deeja/bing-maps-loader

接口调用

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
}
Run Code Online (Sandbox Code Playgroud)


小智 11

现在使用 graphql api 更容易,您可以在单个查询中获得所有内容

首先你得到你的回购:

query {
  repository(name: "MyRepo" owner: "mylogin"){

  }
}
Run Code Online (Sandbox Code Playgroud)

然后你得到它的 defaultBranchRef 使生活更轻松

    defaultBranchRef{

    }
Run Code Online (Sandbox Code Playgroud)

现在所有的分支 ref 真的是,只是一个指向提交的指针,并且由于 graphql 是强类型的(并且 refs 可以是不同的东西)我们需要让它知道它是一个提交,

   target{
      ...on Commit {

      }
   }
Run Code Online (Sandbox Code Playgroud)

所以 target 是我们的 ref 所指的,我们说“如果它是一个提交,就这样做”

它应该怎么做?它应该获得最近的提交(因为它将在 repo 中包含最新的文件)

为此,我们查询历史记录

        history(first: 1 until: "2019-10-08T00:00:00"){
            nodes{

            }
        }
Run Code Online (Sandbox Code Playgroud)

现在nodes我们在我们的提交里面,现在我们可以看到文件,提交指针中的文件实际上只是一个指向树的指针,树只有条目,可以是树类型的对象,也可以是类型斑点

代表文件的条目被称为 blob,但由于我们不会对它们做任何事情,而是列出它们的名字,你甚至不需要知道

但重要的是要知道树也是条目,所以如果你找到一棵树,你需要更深入地挖掘,但你只能深入预先定义的层次。

       tree{
           entries {
             name
             object {
               ...on Tree{
                 entries{
                   name
                   object {
                      ...on Tree{
                        entries{
                          name
                        }
                      }
                   }
                 }
               }
             }
           } 
       }
Run Code Online (Sandbox Code Playgroud)

现在把它们放在一起:

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
                                  }                                  
                                }
                              }
                            }   
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


小智 10

正如 Dan 提到的:github 树

\n\n

请参阅下面的工作示例

\n\n
import requests\n\nuser = "grumbach"\nrepo = "ft_ping"\n\nurl = "https://api.github.com/repos/{}/{}/git/trees/master?recursive=1".format(user, repo)\nr = requests.get(url)\nres = r.json()\n\nfor file in res["tree"]:\n    print(file["path"])\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

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

\n
\n